diff --git a/builder/googlecompute/builder.go b/builder/googlecompute/builder.go index 25e048543..51704f8eb 100644 --- a/builder/googlecompute/builder.go +++ b/builder/googlecompute/builder.go @@ -50,7 +50,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps. steps := []multistep.Step{ - new(stepCreateSSHKey), + new(StepCreateSSHKey), new(stepCreateInstance), new(stepInstanceInfo), &common.StepConnectSSH{ diff --git a/builder/googlecompute/config_test.go b/builder/googlecompute/config_test.go index 0000fc433..6fa1425db 100644 --- a/builder/googlecompute/config_test.go +++ b/builder/googlecompute/config_test.go @@ -15,6 +15,18 @@ func testConfig(t *testing.T) map[string]interface{} { } } +func testConfigStruct(t *testing.T) *Config { + c, warns, errs := NewConfig(testConfig(t)) + if len(warns) > 0 { + t.Fatalf("bad: %#v", len(warns)) + } + if errs != nil { + t.Fatalf("bad: %#v", errs) + } + + return c +} + func testConfigErr(t *testing.T, warns []string, err error, extra string) { if len(warns) > 0 { t.Fatalf("bad: %#v", warns) diff --git a/builder/googlecompute/step_create_ssh_key.go b/builder/googlecompute/step_create_ssh_key.go index b4ce2ce88..814d5bf04 100644 --- a/builder/googlecompute/step_create_ssh_key.go +++ b/builder/googlecompute/step_create_ssh_key.go @@ -12,28 +12,28 @@ import ( "github.com/mitchellh/packer/packer" ) -// stepCreateSSHKey represents a Packer build step that generates SSH key pairs. -type stepCreateSSHKey int +// StepCreateSSHKey represents a Packer build step that generates SSH key pairs. +type StepCreateSSHKey int // Run executes the Packer build step that generates SSH key pairs. -func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { - var ( - ui = state.Get("ui").(packer.Ui) - ) - ui.Say("Creating temporary ssh key for instance...") - priv, err := rsa.GenerateKey(rand.Reader, 2014) +func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packer.Ui) + + ui.Say("Creating temporary SSH key for instance...") + priv, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { err := fmt.Errorf("Error creating temporary ssh key: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } - priv_der := x509.MarshalPKCS1PrivateKey(priv) + priv_blk := pem.Block{ Type: "RSA PRIVATE KEY", Headers: nil, - Bytes: priv_der, + Bytes: x509.MarshalPKCS1PrivateKey(priv), } + pub, err := ssh.NewPublicKey(&priv.PublicKey) if err != nil { err := fmt.Errorf("Error creating temporary ssh key: %s", err) @@ -41,11 +41,11 @@ func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { ui.Error(err.Error()) return multistep.ActionHalt } + state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk))) state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub))) return multistep.ActionContinue } -// Cleanup. // Nothing to clean up. SSH keys are associated with a single GCE instance. -func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {} +func (s *StepCreateSSHKey) Cleanup(state multistep.StateBag) {} diff --git a/builder/googlecompute/step_create_ssh_key_test.go b/builder/googlecompute/step_create_ssh_key_test.go new file mode 100644 index 000000000..f6a41e66b --- /dev/null +++ b/builder/googlecompute/step_create_ssh_key_test.go @@ -0,0 +1,29 @@ +package googlecompute + +import ( + "github.com/mitchellh/multistep" + "testing" +) + +func TestStepCreateSSHKey_impl(t *testing.T) { + var _ multistep.Step = new(StepCreateSSHKey) +} + +func TestStepCreateSSHKey(t *testing.T) { + state := testState(t) + step := new(StepCreateSSHKey) + 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") + } + if _, ok := state.GetOk("ssh_public_key"); !ok { + t.Fatal("should have key") + } +} diff --git a/builder/googlecompute/step_test.go b/builder/googlecompute/step_test.go new file mode 100644 index 000000000..b9a8ba153 --- /dev/null +++ b/builder/googlecompute/step_test.go @@ -0,0 +1,19 @@ +package googlecompute + +import ( + "bytes" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "testing" +) + +func testState(t *testing.T) multistep.StateBag { + state := new(multistep.BasicStateBag) + state.Put("config", testConfigStruct(t)) + state.Put("hook", &packer.MockHook{}) + state.Put("ui", &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + }) + return state +}