Skip to content
New issue

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

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

Already on GitHub? # to your account

builder: no delete logs OSLogin ssh key if not set #162

Merged
merged 5 commits into from
May 12, 2023

Conversation

lbajolet-hashicorp
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp commented May 9, 2023

Because of SDK updates to the SSHKeyGen step, we automatically set the SSHPublicKey in the communicator when a SSHPrivateKeyFile is set in the template.

This invalidates some conditions in the instance creation logic, and with the OSLogin key upload step, as both relied on the value of SSHPublicKey to make a decision.

To prevent this, we only upload the SSH public key when no private key file is specified in the template, thereby making the plugin behave as it had prior to updating the SDK to newer versions that v0.3.2.

Closes #161

@lbajolet-hashicorp lbajolet-hashicorp requested a review from a team as a code owner May 9, 2023 19:56
@lbajolet-hashicorp lbajolet-hashicorp marked this pull request as draft May 9, 2023 20:29
In the SDK prior to version 0.3.2, the public key for the SSH
communicator was only set if it was specified in the configuration.

This changed however, and now when a private key file is specified in a
packer template, the corresponding public key is set during the
preparation steps.

This changes the login for the instance creation, as with the update,
the public key should only be uploaded to the instance metadata if it
was generated, not if it was configured from a file.

Therefore, this commit adds a predicate to the creation logic for the
metadata of the instance so that it doesn't upload this key to the
instance metadata at creation.
During the import_os_login_ssh_key step, we rely on both a state value
and an extra condition to determine whether or not we should clean-up
some keys from os-login.

This is redundant, as the state key/value is only set when OSLogin is
enabled in the configs, and some other conditions are true.

Because of the redundancy, we can remove the extra check and only rely
on the value being present in the state in order to continue clearing up
the data from os-login.
As for the instance metadata, we relied on the sshPublicKey existing in
order to determine whether or not to upload it to os-login.

This is not true anymore in the case someone specifies a private key
file in their template since v0.3.2 of the SDK, as the public key is
derived from the private key specified in the template, so we cannot
rely on this not to upload the key.

So, in addition to the other checks, we check that the private key file
was not specified in the configurations, and if it is, we skip the step.
@lbajolet-hashicorp lbajolet-hashicorp force-pushed the check_ssh_pubkey_at_cleanup_oslogin branch from c71d645 to a3cd9a0 Compare May 11, 2023 14:37
@lbajolet-hashicorp lbajolet-hashicorp marked this pull request as ready for review May 11, 2023 14:42
@lbajolet-hashicorp lbajolet-hashicorp force-pushed the check_ssh_pubkey_at_cleanup_oslogin branch from a3cd9a0 to 957770e Compare May 11, 2023 14:46
// generateSSHPrivateKey generates a PEM encoded ssh private key file
//
// The file's deletion is the responsibility of the caller.
func generateSSHPrivateKey() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice test

I think we should also update this unit test so that public key is not set to nil. It should still pass

func TestStepImportOSLoginSSHKey_withPrivateSSHKey(t *testing.T) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call! I've updated it with a generated private key file, and it indeed still passes

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

One small suggestion but otherwise good to go.

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

One small suggestion but otherwise good to go.

The private key test for OS Login used to rely on a private key being
defined, and not the public key. Since this is not a workflow that will
happen in normal usage of the plugin, we update the test with a
generated private key so we can use it for that test.
Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

🚀

@lbajolet-hashicorp lbajolet-hashicorp merged commit bd57c53 into main May 12, 2023
@lbajolet-hashicorp lbajolet-hashicorp deleted the check_ssh_pubkey_at_cleanup_oslogin branch May 12, 2023 14:34
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not emit ssh public key removal logs when using ssh private key file
2 participants