-
Notifications
You must be signed in to change notification settings - Fork 180
[WIP] Allow ssh key override #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,18 +16,21 @@ import ( | |
|
|
||
| // Client is a wrapper over the SSH connection/sessions. | ||
| type SSHClient struct { | ||
| conn *ssh.Client | ||
| sess *ssh.Session | ||
| user string | ||
| host string | ||
| remoteStdin io.WriteCloser | ||
| remoteStdout io.Reader | ||
| remoteStderr io.Reader | ||
| connOpened bool | ||
| sessOpened bool | ||
| running bool | ||
| env string //export FOO="bar"; export BAR="baz"; | ||
| color string | ||
| conn *ssh.Client | ||
| sess *ssh.Session | ||
| user string | ||
| host string | ||
| sshKeys []string // ssh key to use | ||
| remoteStdin io.WriteCloser | ||
| remoteStdout io.Reader | ||
| remoteStderr io.Reader | ||
| connOpened bool | ||
| sessOpened bool | ||
| running bool | ||
| env string //export FOO="bar"; export BAR="baz"; | ||
| color string | ||
| initAuthMethodOnce sync.Once | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might slow down the init phase of sup. Imagine running 100 remote SSH connections, you'd have to init Auth method 100 times. Do we really need that? Imho, if the identity settings is per network, we don't need to do this per-host here, do we?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes perfect sense, I'll re-evaluate the approach. |
||
| authMethod ssh.AuthMethod | ||
| } | ||
|
|
||
| type ErrConnect struct { | ||
|
|
@@ -40,6 +43,15 @@ func (e ErrConnect) Error() string { | |
| return fmt.Sprintf(`Connect("%v@%v"): %v`, e.User, e.Host, e.Reason) | ||
| } | ||
|
|
||
| func newSSHClient() *SSHClient { | ||
| return &SSHClient{ | ||
| sshKeys: []string{ | ||
| os.Getenv("HOME") + "/.ssh/id_rsa", | ||
| os.Getenv("HOME") + "/.ssh/id_dsa", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // parseHost parses and normalizes <user>@<host:port> from a given string. | ||
| func (c *SSHClient) parseHost(host string) error { | ||
| c.host = host | ||
|
|
@@ -75,11 +87,8 @@ func (c *SSHClient) parseHost(host string) error { | |
| return nil | ||
| } | ||
|
|
||
| var initAuthMethodOnce sync.Once | ||
| var authMethod ssh.AuthMethod | ||
|
|
||
| // initAuthMethod initiates SSH authentication method. | ||
| func initAuthMethod() { | ||
| func (c *SSHClient) initAuthMethod() { | ||
| var signers []ssh.Signer | ||
|
|
||
| // If there's a running SSH Agent, try to use its Private keys. | ||
|
|
@@ -90,11 +99,7 @@ func initAuthMethod() { | |
| } | ||
|
|
||
| // Try to read user's SSH private keys form the standard paths. | ||
| files := []string{ | ||
| os.Getenv("HOME") + "/.ssh/id_rsa", | ||
| os.Getenv("HOME") + "/.ssh/id_dsa", | ||
| } | ||
| for _, file := range files { | ||
| for _, file := range c.sshKeys { | ||
| data, err := ioutil.ReadFile(file) | ||
| if err != nil { | ||
| continue | ||
|
|
@@ -106,7 +111,7 @@ func initAuthMethod() { | |
| signers = append(signers, signer) | ||
|
|
||
| } | ||
| authMethod = ssh.PublicKeys(signers...) | ||
| c.authMethod = ssh.PublicKeys(signers...) | ||
| } | ||
|
|
||
| // SSHDialFunc can dial an ssh server and return a client | ||
|
|
@@ -126,7 +131,7 @@ func (c *SSHClient) ConnectWith(host string, dialer SSHDialFunc) error { | |
| return fmt.Errorf("Already connected") | ||
| } | ||
|
|
||
| initAuthMethodOnce.Do(initAuthMethod) | ||
| c.initAuthMethodOnce.Do(c.initAuthMethod) | ||
|
|
||
| err := c.parseHost(host) | ||
| if err != nil { | ||
|
|
@@ -136,7 +141,7 @@ func (c *SSHClient) ConnectWith(host string, dialer SSHDialFunc) error { | |
| config := &ssh.ClientConfig{ | ||
| User: c.user, | ||
| Auth: []ssh.AuthMethod{ | ||
| authMethod, | ||
| c.authMethod, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ type Network struct { | |
| Env EnvList `yaml:"env"` | ||
| Inventory string `yaml:"inventory"` | ||
| Hosts []string `yaml:"hosts"` | ||
| SSHKey string `yaml:"ssh-key"` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're trying to avoid dashes and underscores in the Supfile API. Can we think of one word here? identity ... or sshkey ... any other suggestions?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
| Bastion string `yaml:"bastion"` // Jump host for the environment | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They call it
identity_filein ssh command. I'm thinking if we should be consistent with them.