From 7f78eef08da331b74bfeb5eb91e077a29d7ec549 Mon Sep 17 00:00:00 2001 From: mvaude Date: Thu, 27 Sep 2018 12:04:04 +0200 Subject: [PATCH] scaleway: fix builder problems with ssh keys The builder is currently failing with the last packer version. This commit is fixing: - the ssh keys and configuration management (the config is passed through a pointer and the ssh keys through the config instead of the runner states) - the key size for generated ssh key - the public key added as a tag to the instance fix #6757 --- builder/scaleway/builder.go | 5 ++--- builder/scaleway/step_create_image.go | 2 +- builder/scaleway/step_create_server.go | 7 +++--- builder/scaleway/step_create_ssh_key.go | 30 ++++++++++++------------- builder/scaleway/step_snapshot.go | 2 +- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/builder/scaleway/builder.go b/builder/scaleway/builder.go index 51b2bae56..ac2a981c9 100644 --- a/builder/scaleway/builder.go +++ b/builder/scaleway/builder.go @@ -19,7 +19,7 @@ import ( const BuilderId = "hashicorp.scaleway" type Builder struct { - config Config + config *Config runner multistep.Runner } @@ -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 } @@ -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), diff --git a/builder/scaleway/step_create_image.go b/builder/scaleway/step_create_image.go index 16345e74d..113ebf0fa 100644 --- a/builder/scaleway/step_create_image.go +++ b/builder/scaleway/step_create_image.go @@ -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 := "" diff --git a/builder/scaleway/step_create_server.go b/builder/scaleway/step_create_server.go index 63350548d..8a22c87d0 100644 --- a/builder/scaleway/step_create_server.go +++ b/builder/scaleway/step_create_server.go @@ -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 @@ -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{ diff --git a/builder/scaleway/step_create_ssh_key.go b/builder/scaleway/step_create_ssh_key.go index 2e059c246..c58875724 100644 --- a/builder/scaleway/step_create_ssh_key.go +++ b/builder/scaleway/step_create_ssh_key.go @@ -1,7 +1,6 @@ package scaleway import ( - "bytes" "context" "crypto/rand" "crypto/rsa" @@ -13,7 +12,6 @@ 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" @@ -21,31 +19,31 @@ import ( 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) @@ -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 { diff --git a/builder/scaleway/step_snapshot.go b/builder/scaleway/step_snapshot.go index fd0dcb593..c369ad883 100644 --- a/builder/scaleway/step_snapshot.go +++ b/builder/scaleway/step_snapshot.go @@ -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))