Merge pull request #4101 from lwander/f-googlecompute-ssh-private-key-file

builder/googlecompute: Use ssh_private_key_file if provided
This commit is contained in:
Matthew Hooker 2016-11-03 11:04:02 -07:00 committed by GitHub
commit 5d0892bd37
6 changed files with 107 additions and 10 deletions

View File

@ -52,8 +52,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps := []multistep.Step{
new(StepCheckExistingImage),
&StepCreateSSHKey{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
PrivateKeyFile: b.config.Comm.SSHPrivateKey,
},
&StepCreateInstance{
Debug: b.config.PackerDebug,

View File

@ -24,13 +24,17 @@ func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string)
instanceMetadata[k] = v
}
// Merge any existing ssh keys with our public key.
sshMetaKey := "sshKeys"
sshKeys := fmt.Sprintf("%s:%s", c.Comm.SSHUsername, sshPublicKey)
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
// Merge any existing ssh keys with our public key, unless there is no
// supplied public key. This is possible if a private_key_file was
// specified.
if sshPublicKey != "" {
sshMetaKey := "sshKeys"
sshKeys := fmt.Sprintf("%s:%s", c.Comm.SSHUsername, sshPublicKey)
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
}
instanceMetadata[sshMetaKey] = sshKeys
}
instanceMetadata[sshMetaKey] = sshKeys
// Wrap any startup script with our own startup script.
if c.StartupScriptFile != "" {

View File

@ -2,6 +2,7 @@ package googlecompute
import (
"errors"
"strings"
"testing"
"time"
@ -210,3 +211,33 @@ func TestStepCreateInstance_errorTimeout(t *testing.T) {
_, ok = state.GetOk("instance_name")
assert.False(t, ok, "State should not have an instance name.")
}
func TestCreateInstanceMetadata(t *testing.T) {
state := testState(t)
c := state.Get("config").(*Config)
image := StubImage("test-image", "test-project", []string{}, 100)
key := "abcdefgh12345678"
// create our metadata
metadata, err := c.createInstanceMetadata(image, key)
assert.True(t, err == nil, "Metadata creation should have succeeded.")
// ensure our key is listed
assert.True(t, strings.Contains(metadata["sshKeys"], key), "Instance metadata should contain provided key")
}
func TestCreateInstanceMetadata_noPublicKey(t *testing.T) {
state := testState(t)
c := state.Get("config").(*Config)
image := StubImage("test-image", "test-project", []string{}, 100)
sshKeys := c.Metadata["sshKeys"]
// create our metadata
metadata, err := c.createInstanceMetadata(image, "")
assert.True(t, err == nil, "Metadata creation should have succeeded.")
// ensure the ssh metadata hasn't changed
assert.Equal(t, metadata["sshKeys"], sshKeys, "Instance metadata should not have been modified")
}

View File

@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"github.com/mitchellh/multistep"
@ -15,8 +16,9 @@ import (
// StepCreateSSHKey represents a Packer build step that generates SSH key pairs.
type StepCreateSSHKey struct {
Debug bool
DebugKeyPath string
Debug bool
DebugKeyPath string
PrivateKeyFile string
}
// Run executes the Packer build step that generates SSH key pairs.
@ -25,6 +27,21 @@ type StepCreateSSHKey struct {
func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
if s.PrivateKeyFile != "" {
ui.Say("Using existing SSH private key")
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
if err != nil {
state.Put("error", fmt.Errorf(
"Error loading configured private key file: %s", err))
return multistep.ActionHalt
}
state.Put("ssh_private_key", string(privateKeyBytes))
state.Put("ssh_public_key", "")
return multistep.ActionContinue
}
ui.Say("Creating temporary SSH key for instance...")
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {

View File

@ -12,6 +12,23 @@ func TestStepCreateSSHKey_impl(t *testing.T) {
var _ multistep.Step = new(StepCreateSSHKey)
}
func TestStepCreateSSHKey_privateKey(t *testing.T) {
state := testState(t)
step := new(StepCreateSSHKey)
step.PrivateKeyFile = "test-fixtures/fake-key"
defer step.Cleanup(state)
// run the step
if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
// Verify that we have a public/private key
if _, ok := state.GetOk("ssh_private_key"); !ok {
t.Fatal("should have key")
}
}
func TestStepCreateSSHKey(t *testing.T) {
state := testState(t)
step := new(StepCreateSSHKey)

View File

@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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