Merge pull request #8942 from desolatorxxl/google-fix-ssh-keys-metadata

[builder/google] Use "ssh-keys" metadata rather than deprecated "sshKeys"
pull/8958/head
Wilken Rivera 6 years ago committed by GitHub
commit 413e19b842
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings"
"time" "time"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -30,8 +31,9 @@ func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string)
// supplied public key. This is possible if a private_key_file was // supplied public key. This is possible if a private_key_file was
// specified. // specified.
if sshPublicKey != "" { if sshPublicKey != "" {
sshMetaKey := "sshKeys" sshMetaKey := "ssh-keys"
sshKeys := fmt.Sprintf("%s:%s", c.Comm.SSHUsername, sshPublicKey) sshPublicKey = strings.TrimSuffix(sshPublicKey, "\n")
sshKeys := fmt.Sprintf("%s:%s %s", c.Comm.SSHUsername, sshPublicKey, c.Comm.SSHUsername)
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists { if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys) sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
} }

@ -308,14 +308,14 @@ func TestCreateInstanceMetadata(t *testing.T) {
assert.True(t, err == nil, "Metadata creation should have succeeded.") assert.True(t, err == nil, "Metadata creation should have succeeded.")
// ensure our key is listed // ensure our key is listed
assert.True(t, strings.Contains(metadata["sshKeys"], key), "Instance metadata should contain provided key") assert.True(t, strings.Contains(metadata["ssh-keys"], key), "Instance metadata should contain provided key")
} }
func TestCreateInstanceMetadata_noPublicKey(t *testing.T) { func TestCreateInstanceMetadata_noPublicKey(t *testing.T) {
state := testState(t) state := testState(t)
c := state.Get("config").(*Config) c := state.Get("config").(*Config)
image := StubImage("test-image", "test-project", []string{}, 100) image := StubImage("test-image", "test-project", []string{}, 100)
sshKeys := c.Metadata["sshKeys"] sshKeys := c.Metadata["ssh-keys"]
// create our metadata // create our metadata
metadata, err := c.createInstanceMetadata(image, "") metadata, err := c.createInstanceMetadata(image, "")
@ -323,7 +323,7 @@ func TestCreateInstanceMetadata_noPublicKey(t *testing.T) {
assert.True(t, err == nil, "Metadata creation should have succeeded.") assert.True(t, err == nil, "Metadata creation should have succeeded.")
// ensure the ssh metadata hasn't changed // ensure the ssh metadata hasn't changed
assert.Equal(t, metadata["sshKeys"], sshKeys, "Instance metadata should not have been modified") assert.Equal(t, metadata["ssh-keys"], sshKeys, "Instance metadata should not have been modified")
} }
func TestCreateInstanceMetadata_metadataFile(t *testing.T) { func TestCreateInstanceMetadata_metadataFile(t *testing.T) {

Loading…
Cancel
Save