communicator/ssh: add private key file read helper

Signed-off-by: Mikhail Ushanov <gm.mephisto@gmail.com>
This commit is contained in:
Mikhail Ushanov 2018-11-06 21:59:40 +03:00
parent 254e18ece9
commit da0bad8441
1 changed files with 17 additions and 0 deletions

View File

@ -66,6 +66,23 @@ type Config struct {
WinRMTransportDecorator func() winrm.Transporter
}
// ReadSSHPrivateKeyFile returns the SSH private key bytes
func (c *Config) ReadSSHPrivateKeyFile() ([]byte, error) {
var privateKey []byte
if c.SSHPrivateKeyFile != "" {
keyPath, err := homedir.Expand(c.SSHPrivateKeyFile)
if err != nil {
return privateKey, fmt.Errorf("Error expanding path for SSH private key: %s", err)
}
privateKey, err = ioutil.ReadFile(keyPath)
if err != nil {
return privateKey, fmt.Errorf("Error on reading SSH private key: %s", err)
}
}
return privateKey, nil
}
// SSHConfigFunc returns a function that can be used for the SSH communicator
// config for connecting to the instance created over SSH using the private key
// or password.