From 5a6851415dac04569aba9739465897a7e14bcc45 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 29 Dec 2016 07:16:38 +0800 Subject: [PATCH] Support key path. Signed-off-by: Bo-Yi Wu --- easyssh/easyssh.go | 27 ++++++++++++++++++++++----- main.go | 6 ++++++ plugin.go | 4 +++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/easyssh/easyssh.go b/easyssh/easyssh.go index f33c6c3..2593a86 100644 --- a/easyssh/easyssh.go +++ b/easyssh/easyssh.go @@ -8,12 +8,11 @@ import ( "bufio" "fmt" "io" - "net" + "io/ioutil" "os" "path/filepath" "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/agent" ) // MakeConfig Contains main authority information. @@ -27,10 +26,27 @@ type MakeConfig struct { User string Server string Key string + KeyPath string Port string Password string } +// returns ssh.Signer from user you running app home path + cutted key path. +// (ex. pubkey,err := getKeyFile("/.ssh/id_rsa") ) +func getKeyFile(keypath string) (ssh.Signer, error) { + buf, err := ioutil.ReadFile(keypath) + if err != nil { + return nil, err + } + + pubkey, err := ssh.ParsePrivateKey(buf) + if err != nil { + return nil, err + } + + return pubkey, nil +} + // connects to remote server using MakeConfig struct and returns *ssh.Session func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { // auths holds the detected ssh auth methods @@ -41,9 +57,10 @@ func (ssh_conf *MakeConfig) connect() (*ssh.Session, error) { auths = append(auths, ssh.Password(ssh_conf.Password)) } - if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil { - auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)) - defer sshAgent.Close() + if ssh_conf.KeyPath != "" { + if pubkey, err := getKeyFile(ssh_conf.KeyPath); err == nil { + auths = append(auths, ssh.PublicKeys(pubkey)) + } } if ssh_conf.Key != "" { diff --git a/main.go b/main.go index 8ed23e8..90cef10 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,11 @@ func main() { Usage: "ssh private key", EnvVar: "PLUGIN_KEY,SCP_KEY", }, + cli.StringFlag{ + Name: "key-path", + Usage: "ssh private key path", + EnvVar: "PLUGIN_KEY_PATH,SCP_KEY_PATH", + }, cli.StringSliceFlag{ Name: "target", Usage: "Target path on the server", @@ -156,6 +161,7 @@ func run(c *cli.Context) error { Username: c.String("username"), Password: c.String("password"), Key: c.String("key"), + KeyPath: c.String("key-path"), Target: c.StringSlice("target"), Source: c.StringSlice("source"), Debug: c.Bool("debug"), diff --git a/plugin.go b/plugin.go index 35530b1..dc25997 100644 --- a/plugin.go +++ b/plugin.go @@ -41,6 +41,7 @@ type ( Username string Password string Key string + KeyPath string Target []string Source []string Debug bool @@ -79,7 +80,7 @@ func (p Plugin) log(host string, message ...interface{}) { // Exec executes the plugin. func (p Plugin) Exec() error { - if len(p.Config.Host) == 0 || len(p.Config.Username) == 0 || (len(p.Config.Password) == 0 && len(p.Config.Key) == 0) { + if len(p.Config.Host) == 0 || len(p.Config.Username) == 0 || (len(p.Config.Password) == 0 && len(p.Config.Key) == 0 && len(p.Config.KeyPath) == 0) { return errors.New("missing ssh config (Host, Username, Password or Key)") } @@ -119,6 +120,7 @@ func (p Plugin) Exec() error { Password: p.Config.Password, Port: p.Config.Port, Key: p.Config.Key, + KeyPath: p.Config.KeyPath, } // Call Scp method with file you want to upload to remote server.