From 93a5cdbdc242574ee81196676ae5aff2c3daf7d5 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Mon, 4 Nov 2024 14:15:28 +0200 Subject: [PATCH] Always generate a unique SSH key pair name Fixes https://github.com/hashicorp/packer-plugin-tencentcloud/issues/136 --- builder/tencentcloud/cvm/run_config.go | 5 +++-- builder/tencentcloud/cvm/step_config_key_pair.go | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/builder/tencentcloud/cvm/run_config.go b/builder/tencentcloud/cvm/run_config.go index e4a45e11..66484d61 100644 --- a/builder/tencentcloud/cvm/run_config.go +++ b/builder/tencentcloud/cvm/run_config.go @@ -117,8 +117,9 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error { packerId := fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()[:8]) if cf.Comm.SSHKeyPairName == "" && cf.Comm.SSHTemporaryKeyPairName == "" && cf.Comm.SSHPrivateKeyFile == "" && cf.Comm.SSHPassword == "" && cf.Comm.WinRMPassword == "" { - //tencentcloud support key pair name length max to 25 - cf.Comm.SSHTemporaryKeyPairName = packerId + // Key pair names must be unique so we can't use packerId here, instead we set a + // dummy value now and a unique name later + cf.Comm.SSHTemporaryKeyPairName = "dummy value" } errs := cf.Comm.Prepare(ctx) diff --git a/builder/tencentcloud/cvm/step_config_key_pair.go b/builder/tencentcloud/cvm/step_config_key_pair.go index 4a97cb1e..b8b8cab4 100644 --- a/builder/tencentcloud/cvm/step_config_key_pair.go +++ b/builder/tencentcloud/cvm/step_config_key_pair.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/packer-plugin-sdk/communicator" "github.com/hashicorp/packer-plugin-sdk/multistep" + "github.com/hashicorp/packer-plugin-sdk/uuid" cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" ) @@ -22,6 +23,12 @@ type stepConfigKeyPair struct { keyID string } +func CreateKeyPairName() string { + uuid := uuid.TimeOrderedUUID() + + return fmt.Sprintf("packer_%s_%s", uuid[:8], uuid[9:13]) +} + func (s *stepConfigKeyPair) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("cvm_client").(*cvm.Client) @@ -51,6 +58,8 @@ func (s *stepConfigKeyPair) Run(ctx context.Context, state multistep.StateBag) m return multistep.ActionContinue } + // Set a unique value for each key pair name + s.Comm.SSHTemporaryKeyPairName = CreateKeyPairName() Say(state, s.Comm.SSHTemporaryKeyPairName, "Trying to create a new keypair") req := cvm.NewCreateKeyPairRequest()