Skip to content
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

if ssh_key_id is specified don't import ssh public key #118

Merged
merged 1 commit into from
Dec 11, 2023

Conversation

pragnesh
Copy link
Contributor

@pragnesh pragnesh commented Dec 2, 2023

if ssh_key_id is specified , add a check to not import private key

fixes #113

Copy link
Member

@andrewsomething andrewsomething left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pragnesh Thanks for flagging this issue for us and opening this PR!

This change does resolve the problem, but I think there might be a better approach. Here we skip uploading the temporary key pair to DigitalOcean if a key ID is passed, but we're still actually generating the temporary key pair locally. it just never gets used. How about moving the conditional to builder/digitalocean/builder.go to prevent generating the key pair altogether when it's not needed?

Something like:

diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go
index 899dee4..0532410 100644
--- a/builder/digitalocean/builder.go
+++ b/builder/digitalocean/builder.go
@@ -102,19 +102,24 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
 	state.Put("hook", hook)
 	state.Put("ui", ui)
 
+	// Only generate the temp key pair if one is not already provided
+	genTempKeyPair := b.config.SSHKeyID == 0 || b.config.Comm.SSHPrivateKeyFile == ""
+
 	// Build the steps
 	steps := []multistep.Step{
-		&communicator.StepSSHKeyGen{
-			CommConf:            &b.config.Comm,
-			SSHTemporaryKeyPair: b.config.Comm.SSH.SSHTemporaryKeyPair,
-		},
+		multistep.If(genTempKeyPair,
+			&communicator.StepSSHKeyGen{
+				CommConf:            &b.config.Comm,
+				SSHTemporaryKeyPair: b.config.Comm.SSH.SSHTemporaryKeyPair,
+			},
+		),
 		multistep.If(b.config.PackerDebug && b.config.Comm.SSHPrivateKeyFile == "",
 			&communicator.StepDumpSSHKey{
 				Path: fmt.Sprintf("do_%s.pem", b.config.PackerBuildName),
 				SSH:  &b.config.Comm.SSH,
 			},
 		),
-		&stepCreateSSHKey{},
+		multistep.If(genTempKeyPair, new(stepCreateSSHKey)),
 		new(stepCreateDroplet),
 		new(stepDropletInfo),
 		&communicator.StepConnect{
@@ -123,9 +128,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
 			SSHConfig: b.config.Comm.SSHConfigFunc(),
 		},
 		new(commonsteps.StepProvision),
-		&commonsteps.StepCleanupTempKeys{
-			Comm: &b.config.Comm,
-		},
+		multistep.If(genTempKeyPair,
+			&commonsteps.StepCleanupTempKeys{
+				Comm: &b.config.Comm,
+			},
+		),
 		new(stepShutdown),
 		new(stepPowerOff),
 		&stepSnapshot{

@pragnesh
Copy link
Contributor Author

pragnesh commented Dec 9, 2023

@andrewsomething yes that is better approach, do you want me to update pull request ?

I have updated pull requested as you have suggested and tested locally, it looks good to me.

Thanks for review.

Copy link
Member

@andrewsomething andrewsomething left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Thanks again!

@andrewsomething andrewsomething merged commit 4a4ee95 into digitalocean:main Dec 11, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ssh_key_id does not work with ssh_private_key_file in v1.2.0
2 participants