From 61a8968b2279dc469b5edda7471139b895d9c83d Mon Sep 17 00:00:00 2001 From: Stephen Fox Date: Thu, 28 Feb 2019 17:23:33 -0500 Subject: [PATCH] Use 'os.OpenFile' to atomically create debug key file. Per code review feedback. --- builder/virtualbox/common/step_ssh_key_pair.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/builder/virtualbox/common/step_ssh_key_pair.go b/builder/virtualbox/common/step_ssh_key_pair.go index 20de4655b..66577bab1 100644 --- a/builder/virtualbox/common/step_ssh_key_pair.go +++ b/builder/virtualbox/common/step_ssh_key_pair.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "runtime" "github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/helper/communicator" @@ -78,11 +77,9 @@ func (s *StepSshKeyPair) Run(_ context.Context, state multistep.StateBag) multis // If we're in debug mode, output the private key to the working // directory. - // TODO: It would be better if the file was 'chmod' before writing - // the key to the disk - or if umask was set before creating the file. if s.Debug { ui.Message(fmt.Sprintf("Saving communicator private key for debug purposes: %s", s.DebugKeyPath)) - f, err := os.Create(s.DebugKeyPath) + f, err := os.OpenFile(s.DebugKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) return multistep.ActionHalt @@ -94,14 +91,6 @@ func (s *StepSshKeyPair) Run(_ context.Context, state multistep.StateBag) multis state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) return multistep.ActionHalt } - - // Chmod it so that it is SSH ready - if runtime.GOOS != "windows" { - if err := f.Chmod(0600); err != nil { - state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err)) - return multistep.ActionHalt - } - } } return multistep.ActionContinue