Added comment to authorized_keys 'sed' calls per review.

This commit is contained in:
Stephen Fox 2019-02-28 10:54:14 -05:00
parent 856810e82e
commit 56c17941fe
1 changed files with 16 additions and 1 deletions

View File

@ -38,6 +38,22 @@ func (s *StepCleanupTempKeys) Run(_ context.Context, state multistep.StateBag) m
ui.Say("Trying to remove ephemeral keys from authorized_keys files")
// Per the OpenSSH manual (https://man.openbsd.org/sshd.8), a typical
// line in the 'authorized_keys' file contains several fields that
// are delimited by spaces. Here is an (abbreviated) example of a line:
// ssh-rsa AAAAB3Nza...LiPk== user@example.net
//
// In the above example, 'ssh-rsa' is the key pair type,
// 'AAAAB3Nza...LiPk==' is the base64 encoded public key,
// and 'user@example.net' is a comment (in this case, describing
// who the key belongs to).
//
// In the following 'sed' calls, the comment field will be equal to
// the value of communicator.Config.SSHTemporaryKeyPairName.
// We can remove an authorized public key using 'sed' by looking
// for a line ending in ' packer-key-pair-comment' (note the
// leading space).
//
// TODO: Why create a backup file if you are going to remove it?
cmd.Command = fmt.Sprintf("sed -i.bak '/ %s$/d' ~/.ssh/authorized_keys; rm ~/.ssh/authorized_keys.bak", s.Comm.SSHTemporaryKeyPairName)
if err := cmd.StartWithUi(comm, ui); err != nil {
@ -45,7 +61,6 @@ func (s *StepCleanupTempKeys) Run(_ context.Context, state multistep.StateBag) m
}
cmd = new(packer.RemoteCmd)
cmd.Command = fmt.Sprintf("sudo sed -i.bak '/ %s$/d' /root/.ssh/authorized_keys; sudo rm /root/.ssh/authorized_keys.bak", s.Comm.SSHTemporaryKeyPairName)
if err := cmd.StartWithUi(comm, ui); err != nil {
log.Printf("Error cleaning up /root/.ssh/authorized_keys; please clean up keys manually: %s", err)
}