From ec96cacdf3b21f3007db326c8c2113f118823da7 Mon Sep 17 00:00:00 2001 From: Christopher Boumenot Date: Tue, 27 Jun 2017 13:19:21 -0700 Subject: [PATCH] azure: write temporary ssh key to a file --- builder/azure/arm/builder.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index 127a689e4..5377b7bb6 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -7,6 +7,8 @@ import ( "errors" "fmt" "log" + "os" + "runtime" "strings" "time" @@ -182,7 +184,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ui.Message(fmt.Sprintf("temp admin password: '%s'", b.config.Password)) if b.config.sshPrivateKey != "" { - ui.Message(fmt.Sprintf("temp private ssh key: '%s'", b.config.sshPrivateKey)) + debugKeyPath := fmt.Sprintf("%s-%s.pem", b.config.PackerBuildName, b.config.tmpComputeName) + ui.Message(fmt.Sprintf("temp ssh key: %s", debugKeyPath)) + + b.writeSSHPrivateKey(ui, debugKeyPath) } } @@ -219,6 +224,27 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return &Artifact{}, nil } +func (b *Builder) writeSSHPrivateKey(ui packer.Ui, debugKeyPath string) { + f, err := os.Create(debugKeyPath) + if err != nil { + ui.Say(fmt.Sprintf("Error saving debug key: %s", err)) + } + defer f.Close() + + // Write the key out + if _, err := f.Write([]byte(b.config.sshPrivateKey)); err != nil { + ui.Say(fmt.Sprintf("Error saving debug key: %s", err)) + return + } + + // Chmod it so that it is SSH ready + if runtime.GOOS != "windows" { + if err := f.Chmod(0600); err != nil { + ui.Say(fmt.Sprintf("Error setting permissions of debug key: %s", err)) + } + } +} + func (b *Builder) isPrivateNetworkCommunication() bool { return b.config.VirtualNetworkName != "" }