From aa3910f8df329dc46e7169bbf47caf16a6a20a16 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 20 Jun 2024 09:19:09 +0200 Subject: [PATCH] refactor!: drop all SSH opts / config handling See https://git.coopcloud.tech/coop-cloud/organising/issues/601 See https://git.coopcloud.tech/coop-cloud/organising/issues/482 --- cli/server/add.go | 13 ++---- pkg/client/context.go | 15 +++--- pkg/ssh/ssh.go | 65 ++------------------------ pkg/upstream/commandconn/connection.go | 10 ++-- 4 files changed, 19 insertions(+), 84 deletions(-) diff --git a/cli/server/add.go b/cli/server/add.go index c4bf6f131..b10d24bcf 100644 --- a/cli/server/add.go +++ b/cli/server/add.go @@ -53,7 +53,7 @@ func cleanUp(domainName string) { // Docker manages SSH connection details. These are stored to disk in // ~/.docker. Abra can manage this completely for the user, so it's an // implementation detail. -func newContext(c *cli.Context, domainName, username, port string) error { +func newContext(c *cli.Context, domainName string) error { store := contextPkg.NewDefaultDockerContextStore() contexts, err := store.Store.List() if err != nil { @@ -67,9 +67,9 @@ func newContext(c *cli.Context, domainName, username, port string) error { } } - logrus.Debugf("creating context with domain %s, username %s and port %s", domainName, username, port) + logrus.Debugf("creating context with domain %s", domainName) - if err := client.CreateContext(domainName, username, port); err != nil { + if err := client.CreateContext(domainName); err != nil { return err } @@ -158,12 +158,7 @@ developer machine. logrus.Fatal(err) } - hostConfig, err := sshPkg.GetHostConfig(domainName) - if err != nil { - logrus.Fatal(err) - } - - if err := newContext(c, domainName, hostConfig.User, hostConfig.Port); err != nil { + if err := newContext(c, domainName); err != nil { logrus.Fatal(err) } diff --git a/pkg/client/context.go b/pkg/client/context.go index f3e6c69d7..66f5e43c4 100644 --- a/pkg/client/context.go +++ b/pkg/client/context.go @@ -14,19 +14,16 @@ import ( type Context = contextStore.Metadata -func CreateContext(contextName string, user string, port string) error { - host := contextName - if user != "" { - host = fmt.Sprintf("%s@%s", user, host) - } - if port != "" { - host = fmt.Sprintf("%s:%s", host, port) - } - host = fmt.Sprintf("ssh://%s", host) +// CreateContext creates a new Docker context. +func CreateContext(contextName string) error { + host := fmt.Sprintf("ssh://%s", contextName) + if err := createContext(contextName, host); err != nil { return err } + logrus.Debugf("created the %s context", contextName) + return nil } diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index a3eff5f18..9a3a9efd3 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -2,73 +2,14 @@ package ssh import ( "fmt" - "os/exec" "strings" - - "github.com/sirupsen/logrus" ) -// HostConfig is a SSH host config. -type HostConfig struct { - Host string - IdentityFile string - Port string - User string -} - -// String presents a human friendly output for the HostConfig. -func (h HostConfig) String() string { - return fmt.Sprintf( - "{host: %s, username: %s, port: %s, identityfile: %s}", - h.Host, - h.User, - h.Port, - h.IdentityFile, - ) -} - -// GetHostConfig retrieves a ~/.ssh/config config for a host using /usr/bin/ssh -// directly. We therefore maintain consistent interop with this standard -// tooling. This is useful because SSH confuses a lot of people and having to -// learn how two tools (`ssh` and `abra`) handle SSH connection details instead -// of one (just `ssh`) is Not Cool. Here's to less bug reports on this topic! -func GetHostConfig(hostname string) (HostConfig, error) { - var hostConfig HostConfig - - out, err := exec.Command("ssh", "-G", hostname).Output() - if err != nil { - return hostConfig, err - } - - for _, line := range strings.Split(string(out), "\n") { - entries := strings.Split(line, " ") - for idx, entry := range entries { - if entry == "hostname" { - hostConfig.Host = entries[idx+1] - } - if entry == "user" { - hostConfig.User = entries[idx+1] - } - if entry == "port" { - hostConfig.Port = entries[idx+1] - } - if entry == "identityfile" { - if hostConfig.IdentityFile == "" { - hostConfig.IdentityFile = entries[idx+1] - } - } - } - } - - logrus.Debugf("retrieved ssh config for %s: %s", hostname, hostConfig.String()) - - return hostConfig, nil -} - // Fatal is a error output wrapper which aims to make SSH failures easier to // parse through re-wording. func Fatal(hostname string, err error) error { out := err.Error() + if strings.Contains(out, "Host key verification failed.") { return fmt.Errorf("SSH host key verification failed for %s", hostname) } else if strings.Contains(out, "Could not resolve hostname") { @@ -79,7 +20,7 @@ func Fatal(hostname string, err error) error { return fmt.Errorf("ssh auth: permission denied for %s", hostname) } else if strings.Contains(out, "Network is unreachable") { return fmt.Errorf("unable to connect to %s, network is unreachable?", hostname) - } else { - return err } + + return err } diff --git a/pkg/upstream/commandconn/connection.go b/pkg/upstream/commandconn/connection.go index b2eebdbe8..63886eb9e 100644 --- a/pkg/upstream/commandconn/connection.go +++ b/pkg/upstream/commandconn/connection.go @@ -16,12 +16,12 @@ import ( // GetConnectionHelper returns Docker-specific connection helper for the given URL. // GetConnectionHelper returns nil without error when no helper is registered for the scheme. // -// ssh://@ URL requires Docker 18.09 or later on the remote host. +// ssh:// URL requires Docker 18.09 or later on the remote host. func GetConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) { - return getConnectionHelper(daemonURL, []string{"-o ConnectTimeout=60"}) + return getConnectionHelper(daemonURL) } -func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.ConnectionHelper, error) { +func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) { url, err := url.Parse(daemonURL) if err != nil { return nil, err @@ -35,7 +35,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne return &connhelper.ConnectionHelper{ Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) { - return New(ctx, "ssh", append(sshFlags, ctxConnDetails.Args("docker", "system", "dial-stdio")...)...) + return New(ctx, "ssh", ctxConnDetails.Args("docker", "system", "dial-stdio")...) }, Host: "http://docker.example.com", }, nil @@ -45,6 +45,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne return nil, err } +// NewConnectionHelper creates new connection helper for a remote docker daemon. func NewConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) { helper, err := GetConnectionHelper(daemonURL) if err != nil { @@ -73,6 +74,7 @@ func getDockerEndpoint(host string) (docker.Endpoint, error) { return ep, nil } +// GetDockerEndpointMetadataAndTLS retrieves the docker endpoint and TLS info for a remote host. func GetDockerEndpointMetadataAndTLS(host string) (docker.EndpointMeta, *dCliContextStore.EndpointTLSData, error) { ep, err := getDockerEndpoint(host) if err != nil {