if ssh_key_id is specified don't import ssh public key#118
Merged
andrewsomething merged 1 commit intodigitalocean:mainfrom Dec 11, 2023
Merged
if ssh_key_id is specified don't import ssh public key#118andrewsomething merged 1 commit intodigitalocean:mainfrom
andrewsomething merged 1 commit intodigitalocean:mainfrom
Conversation
andrewsomething
requested changes
Dec 8, 2023
Member
andrewsomething
left a comment
There was a problem hiding this comment.
@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{
Contributor
Author
|
@andrewsomething yes that is better approach, I have updated pull requested as you have suggested and tested locally, it looks good to me. Thanks for review. |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
if ssh_key_id is specified , add a check to not import private key
fixes #113