From 3d46ce6db2287caca4be1f69044bbdee8df1cb1e Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 25 Oct 2021 11:13:41 +0200 Subject: [PATCH] refactor: more seamless SSH connections --- cli/server/add.go | 14 +++++--------- cli/server/new.go | 16 +--------------- pkg/ssh/ssh.go | 19 ++++++------------- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/cli/server/add.go b/cli/server/add.go index d8cdad5a..06c35290 100644 --- a/cli/server/add.go +++ b/cli/server/add.go @@ -316,14 +316,10 @@ to start running Abra commands against it. This command can also provision your server ("--provision/-p") so that it is capable of hosting Co-op Cloud apps. Abra will default to expecting that you -have a working SSH config for the host in your ~/.ssh/config file. E.g. for -"example.com", you'll want to have something like: - -Host example.com - Hostname 192.168.178.31 # domain name also works - User myuserontheserver - Port 12345 - IdentityFile ~/.ssh/mysecretkey.local +have a running ssh-agent and are using SSH keys to connect to your new server. +Abra will also read your SSH config (matching "Host" as ). SSH +connection details precedence follows as such: command-line > SSH config > +guessed defaults. If you have no SSH key configured for this host and are instead using password authentication, you may pass "--ssh-auth password" to have Abra ask you for the @@ -345,7 +341,7 @@ Example: abra server add --provision --traefik varia.zone glodemodem 12345 -Abra will construct the following SSH connection string then: +Abra will construct the following SSH connection and Docker context: ssh://globemodem@varia.zone:12345 diff --git a/cli/server/new.go b/cli/server/new.go index 78eda883..be3b8366 100644 --- a/cli/server/new.go +++ b/cli/server/new.go @@ -84,20 +84,7 @@ Your new Hetzner Cloud VPS has successfully been created! Here are the details: VPS IP address: %s VPS Root Password: %s -Here is what your SSH configuration (~/.ssh/config) might look like: - - Host %s.example.com - Hostname %s - User root - Port 22 - IdentityFile ~/.ssh/ - -Remember, your "Host" value must be a valid publicly accessible domain name as -Abra uses this domain to identity servers. If you specified "--ssh-auth -password" then you may skip the IdentityFile stanza. - -Once your SSH client is configured, you can access this new VPS via SSH using -the following command: +You can access this new VPS via SSH using the following command: ssh root@%s @@ -107,7 +94,6 @@ record new") and add the server to your Abra configuration ("abra server add") to have a working server that you can deploy Co-op Cloud apps to. `, internal.HetznerCloudName, ip, rootPassword, - internal.HetznerCloudName, ip, ip, )) diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index 0832fd5b..da7a4aca 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -1,7 +1,6 @@ package ssh import ( - "fmt" "os/user" "github.com/AlecAivazis/survey/v2" @@ -19,7 +18,7 @@ type HostConfig struct { } // GetHostConfig retrieves a ~/.ssh/config config for a host. -func GetHostConfig(hostname, username, port string, hasIdentityFile bool) (HostConfig, error) { +func GetHostConfig(hostname, username, port string) (HostConfig, error) { var hostConfig HostConfig var host, idf string @@ -47,13 +46,12 @@ func GetHostConfig(hostname, username, port string, hasIdentityFile bool) (HostC } } - dummyVal := "~/.ssh/identity" - if idf = ssh_config.Get(hostname, "IdentityFile"); (idf == dummyVal || idf == "") && hasIdentityFile { - return hostConfig, fmt.Errorf("SSH identity file missing for %s from SSH config", hostname) - } + idf = ssh_config.Get(hostname, "IdentityFile") hostConfig.Host = host - hostConfig.IdentityFile = idf + if idf != "" { + hostConfig.IdentityFile = idf + } hostConfig.Port = port hostConfig.User = username @@ -66,12 +64,7 @@ func GetHostConfig(hostname, username, port string, hasIdentityFile bool) (HostC func New(domainName, sshAuth, username, port string) (*simplessh.Client, error) { var client *simplessh.Client - hasIdentityFile := true - if sshAuth == "password" { - hasIdentityFile = false - } - - hostConfig, err := GetHostConfig(domainName, username, port, hasIdentityFile) + hostConfig, err := GetHostConfig(domainName, username, port) if err != nil { return client, err }