diff --git a/helper/communicator/config.go b/helper/communicator/config.go index 7d66254b5..75ac3b2b6 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net" + "net/url" "os" "time" @@ -329,3 +330,9 @@ func (c *Config) prepareWinRM(ctx *interpolate.Context) []error { return errs } + +// SSHPublicKeyUrlEncoded returns a string representing the SSH public key +// encoded in URL format. +func (c *Config) SSHPublicKeyUrlEncoded() string { + return url.PathEscape(string(c.SSHPublicKey)) +} diff --git a/helper/communicator/config_test.go b/helper/communicator/config_test.go index c5af24114..9f28c2be5 100644 --- a/helper/communicator/config_test.go +++ b/helper/communicator/config_test.go @@ -1,6 +1,7 @@ package communicator import ( + "net/url" "reflect" "testing" @@ -136,6 +137,23 @@ func TestConfig_winrm(t *testing.T) { } } +func TestConfig_SSHPublicKeyUrlEncoded(t *testing.T) { + c := &Config{ + SSHPublicKey: []byte("ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBADulbdHCjXhsH8wGtyLhhi3qVvX6M0tGgtousr/DzArwf2KX0L2Zm1OZfqMWFCrSVD743OFY60YL5CGsN9/PVQP7gApll5yTWyaQJu8lReptR5TMnUDn0u3mJN/QRT5Zs8qS5J5Q3WhXwaMF96kSuu+MwXrBnl8sK+bwxOKQtlKJXowcw==\n"), + } + + encoded := c.SSHPublicKeyUrlEncoded() + + decoded, err := url.PathUnescape(encoded) + if err != nil { + t.Fatal(err.Error()) + } + + if decoded != string(c.SSHPublicKey) { + t.Fatal("resulting public key does not match original public key") + } +} + func testContext(t *testing.T) *interpolate.Context { return nil }