From 3b3aa562eddea5299c775d96efd98f4331622072 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 30 Oct 2019 13:27:34 -0700 Subject: [PATCH] add insert_key option for toggling whether to add Vagrant's insecure key --- builder/vagrant/builder.go | 5 +++++ builder/vagrant/builder.hcl2spec.go | 2 ++ builder/vagrant/step_create_vagrantfile.go | 5 +++++ .../partials/builder/vagrant/_Config-not-required.html.md | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/builder/vagrant/builder.go b/builder/vagrant/builder.go index 309356102..6804c55b2 100644 --- a/builder/vagrant/builder.go +++ b/builder/vagrant/builder.go @@ -67,6 +67,10 @@ type Config struct { // the name to give it. If left blank, will default to "packer_" plus your // buildname. BoxName string `mapstructure:"box_name" required:"false"` + // If true, Vagrant will automatically insert a keypair to use for SSH, + // replacing Vagrant's default insecure key inside the machine if detected. + // By default, Packer sets this to false. + InsertKey bool `mapstructure:"insert_key" required:"false"` // The vagrant provider. // This parameter is required when source_path have more than one provider, // or when using vagrant-cloud post-processor. Defaults to unset. @@ -255,6 +259,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack BoxName: b.config.BoxName, OutputDir: b.config.OutputDir, GlobalID: b.config.GlobalID, + InsertKey: b.config.InsertKey, }, &StepAddBox{ BoxVersion: b.config.BoxVersion, diff --git a/builder/vagrant/builder.hcl2spec.go b/builder/vagrant/builder.hcl2spec.go index ed1c45fb5..710a6ad3b 100644 --- a/builder/vagrant/builder.hcl2spec.go +++ b/builder/vagrant/builder.hcl2spec.go @@ -79,6 +79,7 @@ type FlatConfig struct { Checksum *string `mapstructure:"checksum" required:"false" cty:"checksum"` ChecksumType *string `mapstructure:"checksum_type" required:"false" cty:"checksum_type"` BoxName *string `mapstructure:"box_name" required:"false" cty:"box_name"` + InsertKey *bool `mapstructure:"insert_key" required:"false" cty:"insert_key"` Provider *string `mapstructure:"provider" required:"false" cty:"provider"` VagrantfileTpl *string `mapstructure:"vagrantfile_template" cty:"vagrantfile_template"` TeardownMethod *string `mapstructure:"teardown_method" required:"false" cty:"teardown_method"` @@ -176,6 +177,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "checksum": &hcldec.AttrSpec{Name: "checksum", Type: cty.String, Required: false}, "checksum_type": &hcldec.AttrSpec{Name: "checksum_type", Type: cty.String, Required: false}, "box_name": &hcldec.AttrSpec{Name: "box_name", Type: cty.String, Required: false}, + "insert_key": &hcldec.AttrSpec{Name: "insert_key", Type: cty.Bool, Required: false}, "provider": &hcldec.AttrSpec{Name: "provider", Type: cty.String, Required: false}, "vagrantfile_template": &hcldec.AttrSpec{Name: "vagrantfile_template", Type: cty.String, Required: false}, "teardown_method": &hcldec.AttrSpec{Name: "teardown_method", Type: cty.String, Required: false}, diff --git a/builder/vagrant/step_create_vagrantfile.go b/builder/vagrant/step_create_vagrantfile.go index 5101c6335..ec6262dcc 100644 --- a/builder/vagrant/step_create_vagrantfile.go +++ b/builder/vagrant/step_create_vagrantfile.go @@ -19,15 +19,18 @@ type StepCreateVagrantfile struct { GlobalID string SourceBox string BoxName string + InsertKey bool } var DEFAULT_TEMPLATE = `Vagrant.configure("2") do |config| config.vm.define "source", autostart: false do |source| source.vm.box = "{{.SourceBox}}" + config.ssh.insert_key = {{.InsertKey}} end config.vm.define "output" do |output| output.vm.box = "{{.BoxName}}" output.vm.box_url = "file://package.box" + config.ssh.insert_key = {{.InsertKey}} end {{ if ne .SyncedFolder "" -}} config.vm.synced_folder "{{.SyncedFolder}}", "/vagrant" @@ -40,6 +43,7 @@ type VagrantfileOptions struct { SyncedFolder string SourceBox string BoxName string + InsertKey bool } func (s *StepCreateVagrantfile) createVagrantfile() (string, error) { @@ -66,6 +70,7 @@ func (s *StepCreateVagrantfile) createVagrantfile() (string, error) { SyncedFolder: s.SyncedFolder, BoxName: s.BoxName, SourceBox: s.SourceBox, + InsertKey: s.InsertKey, } err = tpl.Execute(templateFile, opts) diff --git a/website/source/partials/builder/vagrant/_Config-not-required.html.md b/website/source/partials/builder/vagrant/_Config-not-required.html.md index 7611edca6..6d4ebd969 100644 --- a/website/source/partials/builder/vagrant/_Config-not-required.html.md +++ b/website/source/partials/builder/vagrant/_Config-not-required.html.md @@ -16,6 +16,10 @@ the name to give it. If left blank, will default to "packer_" plus your buildname. +- `insert_key` (bool) - If true, Vagrant will automatically insert a keypair to use for SSH, + replacing Vagrant's default insecure key inside the machine if detected. + By default, Packer sets this to false. + - `provider` (string) - The vagrant provider. This parameter is required when source_path have more than one provider, or when using vagrant-cloud post-processor. Defaults to unset.