Added 'SSHPublicKeyUrlEncoded()' to comm.Config.

This allows us to get a URL encoded string representing the SSH
public key. This is needed because the key will have spaces when
it is in authorized_keys format.
This commit is contained in:
Stephen Fox 2019-02-03 12:10:27 -05:00
parent 459bd1ea7a
commit 7b857929ab
2 changed files with 25 additions and 0 deletions

View File

@ -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))
}

View File

@ -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
}