2018-02-19 04:31:29 -05:00
|
|
|
// Package ssh provides the connection helper for ssh:// URL.
|
|
|
|
// Requires Docker 18.09 or later on the remote host.
|
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns cmd and its args
|
|
|
|
func New(daemonURL string) (string, []string, error) {
|
|
|
|
sp, err := parseSSHURL(daemonURL)
|
|
|
|
if err != nil {
|
2018-11-09 02:35:34 -05:00
|
|
|
return "", nil, errors.Wrap(err, "SSH host connection is not valid")
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
return "ssh", append(sp.Args(), []string{"--", "docker", "system", "dial-stdio"}...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSSHURL(daemonURL string) (*sshSpec, error) {
|
|
|
|
u, err := url.Parse(daemonURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if u.Scheme != "ssh" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("expected scheme ssh, got %q", u.Scheme)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var sp sshSpec
|
|
|
|
|
|
|
|
if u.User != nil {
|
|
|
|
sp.user = u.User.Username()
|
|
|
|
if _, ok := u.User.Password(); ok {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.New("plain-text password is not supported")
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sp.host = u.Hostname()
|
|
|
|
if sp.host == "" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("no host specified")
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
sp.port = u.Port()
|
|
|
|
if u.Path != "" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("extra path after the host: %q", u.Path)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
if u.RawQuery != "" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("extra query after the host: %q", u.RawQuery)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
if u.Fragment != "" {
|
2018-11-09 02:35:34 -05:00
|
|
|
return nil, errors.Errorf("extra fragment after the host: %q", u.Fragment)
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
return &sp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshSpec struct {
|
|
|
|
user string
|
|
|
|
host string
|
|
|
|
port string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *sshSpec) Args() []string {
|
|
|
|
var args []string
|
|
|
|
if sp.user != "" {
|
|
|
|
args = append(args, "-l", sp.user)
|
|
|
|
}
|
|
|
|
if sp.port != "" {
|
|
|
|
args = append(args, "-p", sp.port)
|
|
|
|
}
|
|
|
|
args = append(args, sp.host)
|
|
|
|
return args
|
|
|
|
}
|