diff --git a/CHANGELOG.md b/CHANGELOG.md index a5996e9cd..3b78a4052 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ FEATURES: IMPROVEMENTS: * core: Most downloads made by Packer now use a custom user agent. [GH-803] +* builder/googlecompute: SSH private key will be saved to disk if `-debug` + is specified. [GH-867] * builder/virtualbox-ovf: Can specify import options such as "keepallmacs". [GH-883] diff --git a/builder/googlecompute/step_create_instance.go b/builder/googlecompute/step_create_instance.go index 3cc1ec528..bfcad4be0 100644 --- a/builder/googlecompute/step_create_instance.go +++ b/builder/googlecompute/step_create_instance.go @@ -12,8 +12,9 @@ import ( // StepCreateInstance represents a Packer build step that creates GCE instances. type StepCreateInstance struct { + Debug bool + instanceName string - Debug bool } // Run executes the Packer build step that creates a GCE instance. diff --git a/builder/googlecompute/step_create_ssh_key.go b/builder/googlecompute/step_create_ssh_key.go index 146f607bc..d8b23498b 100644 --- a/builder/googlecompute/step_create_ssh_key.go +++ b/builder/googlecompute/step_create_ssh_key.go @@ -14,7 +14,6 @@ import ( // StepCreateSSHKey represents a Packer build step that generates SSH key pairs. type StepCreateSSHKey struct { - key int Debug bool DebugKeyPath string } @@ -50,14 +49,19 @@ func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { if s.Debug { ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) - f, err := os.OpenFile(s.DebugKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + f, err := os.Create(s.DebugKeyPath) if err != nil { state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) return multistep.ActionHalt } + // Write out the key - pem.Encode(f, &priv_blk) + err = pem.Encode(f, &priv_blk) f.Close() + if err != nil { + state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) + return multistep.ActionHalt + } } return multistep.ActionContinue } diff --git a/builder/googlecompute/step_create_ssh_key_test.go b/builder/googlecompute/step_create_ssh_key_test.go index f6a41e66b..304d6f1fb 100644 --- a/builder/googlecompute/step_create_ssh_key_test.go +++ b/builder/googlecompute/step_create_ssh_key_test.go @@ -2,6 +2,9 @@ package googlecompute import ( "github.com/mitchellh/multistep" + + "io/ioutil" + "os" "testing" ) @@ -27,3 +30,34 @@ func TestStepCreateSSHKey(t *testing.T) { t.Fatal("should have key") } } + +func TestStepCreateSSHKey_debug(t *testing.T) { + tf, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + tf.Close() + + state := testState(t) + step := new(StepCreateSSHKey) + step.Debug = true + step.DebugKeyPath = tf.Name() + + 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") + } + if _, err := os.Stat(tf.Name()); err != nil { + t.Fatalf("err: %s", err) + } +} diff --git a/builder/googlecompute/step_instance_info.go b/builder/googlecompute/step_instance_info.go index dbe02342d..e4de295d3 100644 --- a/builder/googlecompute/step_instance_info.go +++ b/builder/googlecompute/step_instance_info.go @@ -11,8 +11,9 @@ import ( // stepInstanceInfo represents a Packer build step that gathers GCE instance info. type StepInstanceInfo struct { - info int Debug bool + + info int } // Run executes the Packer build step that gathers GCE instance info.