Skip to content

Always generate a unique SSH key pair name #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions builder/tencentcloud/cvm/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions builder/tencentcloud/cvm/step_config_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down