Skip to content

Commit

Permalink
Merge pull request hashicorp#6768 from mvaude/fix-scaleway-ssh
Browse files Browse the repository at this point in the history
scaleway: fix builder problems with ssh keys
  • Loading branch information
azr authored Sep 27, 2018
2 parents 9181570 + 7f78eef commit 2d899ff
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
5 changes: 2 additions & 3 deletions builder/scaleway/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
const BuilderId = "hashicorp.scaleway"

type Builder struct {
config Config
config *Config
runner multistep.Runner
}

Expand All @@ -28,7 +28,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
if errs != nil {
return warnings, errs
}
b.config = *c
b.config = c

return nil, nil
}
Expand All @@ -50,7 +50,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps := []multistep.Step{
&stepCreateSSHKey{
Debug: b.config.PackerDebug,
Comm: &b.config.Comm,
DebugKeyPath: fmt.Sprintf("scw_%s.pem", b.config.PackerBuildName),
},
new(stepCreateServer),
Expand Down
2 changes: 1 addition & 1 deletion builder/scaleway/step_create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type stepImage struct{}
func (s *stepImage) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*api.ScalewayAPI)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
c := state.Get("config").(*Config)
snapshotID := state.Get("snapshot_id").(string)
bootscriptID := ""

Expand Down
7 changes: 3 additions & 4 deletions builder/scaleway/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type stepCreateServer struct {
func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*api.ScalewayAPI)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
sshPubKey := state.Get("ssh_pubkey").(string)
c := state.Get("config").(*Config)
tags := []string{}
var bootscript *string

Expand All @@ -28,8 +27,8 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
bootscript = &c.Bootscript
}

if sshPubKey != "" {
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.TrimSpace(sshPubKey))}
if c.Comm.SSHPublicKey != nil {
tags = []string{fmt.Sprintf("AUTHORIZED_KEY=%s", strings.Replace(strings.TrimSpace(string(c.Comm.SSHPublicKey)), " ", "_", -1))}
}

server, err := client.PostServer(api.ScalewayServerDefinition{
Expand Down
30 changes: 15 additions & 15 deletions builder/scaleway/step_create_ssh_key.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package scaleway

import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
Expand All @@ -13,39 +12,38 @@ import (
"os"
"runtime"

"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"golang.org/x/crypto/ssh"
)

type stepCreateSSHKey struct {
Debug bool
Comm *communicator.Config
DebugKeyPath string
}

func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)

if s.Comm.SSHPrivateKeyFile != "" {
if config.Comm.SSHPrivateKeyFile != "" {
ui.Say("Using existing SSH private key")
privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
privateKeyBytes, err := ioutil.ReadFile(config.Comm.SSHPrivateKeyFile)
if err != nil {
state.Put("error", fmt.Errorf(
"Error loading configured private key file: %s", err))
return multistep.ActionHalt
}

s.Comm.SSHPrivateKey = privateKeyBytes
s.Comm.SSHPublicKey = nil
config.Comm.SSHPrivateKey = privateKeyBytes
config.Comm.SSHPublicKey = nil

return multistep.ActionContinue
}

ui.Say("Creating temporary ssh key for server...")

priv, err := rsa.GenerateKey(rand.Reader, 2014)
priv, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
state.Put("error", err)
Expand All @@ -61,17 +59,19 @@ func (s *stepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
Bytes: priv_der,
}

// Set the private key in the config for later
s.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)

pub, _ := ssh.NewPublicKey(&priv.PublicKey)
pub_sshformat := ssh.MarshalAuthorizedKey(pub)
pub_sshformat = bytes.Replace(pub_sshformat, []byte(" "), []byte("_"), -1)
pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil {
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

log.Printf("temporary ssh key created")

// Remember some state for the future
s.Comm.SSHPublicKey = pub_sshformat
config.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
config.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub)

// If we're in debug mode, output the private key to the working directory.
if s.Debug {
Expand Down
2 changes: 1 addition & 1 deletion builder/scaleway/step_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type stepSnapshot struct{}
func (s *stepSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*api.ScalewayAPI)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
c := state.Get("config").(*Config)
volumeID := state.Get("root_volume_id").(string)

ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
Expand Down

0 comments on commit 2d899ff

Please sign in to comment.