-
Notifications
You must be signed in to change notification settings - Fork 57
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
Conversation
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.
c71d645
to
a3cd9a0
Compare
a3cd9a0
to
957770e
Compare
// generateSSHPrivateKey generates a PEM encoded ssh private key file | ||
// | ||
// The file's deletion is the responsibility of the caller. | ||
func generateSSHPrivateKey() (string, error) { |
There was a problem hiding this comment.
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
packer-plugin-googlecompute/builder/googlecompute/step_import_os_login_ssh_key_test.go
Line 200 in 957770e
func TestStepImportOSLoginSSHKey_withPrivateSSHKey(t *testing.T) { |
There was a problem hiding this comment.
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
There was a problem hiding this 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.
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
Because of SDK updates to the
SSHKeyGen
step, we automatically set theSSHPublicKey
in the communicator when aSSHPrivateKeyFile
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