From da0bad844142f9e3f1b4a684496e463d5947da5c Mon Sep 17 00:00:00 2001 From: Mikhail Ushanov Date: Tue, 6 Nov 2018 21:59:40 +0300 Subject: [PATCH] communicator/ssh: add private key file read helper Signed-off-by: Mikhail Ushanov --- helper/communicator/config.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/helper/communicator/config.go b/helper/communicator/config.go index 44738d61b..b7041eb40 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -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.