SSH Key Rotation with Ansible
by Jesse Keating
Introduction to SSH Keys
SSH keys are fantastic things. They provide a 2-part blob of data, a private part and a public part, that can be used to authenticate ssh connections. You keep the private part private, often with a passphrase to "unlock" it, while you can hand out the public part to things like GitHub, compute clouds, other systems that you might wish to connect to via SSH, and remote servers you will ssh to. The public part of your SSH key pair gets stored in a special file that SSH servers on remote systems read, the authorized_keys file. When you connect, your ssh client will provide details about your private key that the remote end can validate against your public key to authenticate you. This is a great convenience over having to provide a password every single time.
This convenience for users is also a necessity for infrastructure administration. SSH is ubiquitous in the Linux world, and the vast majority of administration is accomplished over SSH. Without the ability to use SSH Keys (or similar auth mechanisms) one would not be able to automate actions across many systems easily.
With convenience comes responsibility though. Having a key that an automated process can use to manipulate your fleet of systems is great, but it's also a pretty juicy attack vector. For that reason it is good practice to rotate your keys often. Rotating keys is the act of replacing the keys you're currently using with new keys, and removing the ability for old keys to be used to log into your systems.
Rotating keys requires a new key. Creating a new key is fairly simple. Getting the public part of this key out into your fleet, and removing existing public keys is a bit harder. Thankfully we have orchestration and automation tools such as Ansible. The rest of this blog post will discuss how to use Ansible to automate rotating your ssh credentials across your fleet.
Orchestrating SSH Key Rotation
Lets consider the steps necessary to rotate a key:
- Create a new key
- Add new key to authorized_keys files on your fleet
- Test new key
- Remove previous keys from authorized_keys files
As stated before, step 1 is simple, and for the sake of this post we'll assume that this has been completed, and there is a new key-pair, located at ~/.ssh/id_rsa_new and ~/.ssh/id_rsa_new.pub. The private key part is id_rsa_new, the public is id_rsa_new.pub. It's the pub we need to distribute. For now, we'll also assume that this key has not yet replaced the existing key, and we can still use the existing key to reach our fleet.
Step 2 is adding the new key to the authorized_keys file. This is where our Ansible playbook will begin. First we need a play header and a couple variables defined to reference the public and private parts of our new key-pair.
- name: Rotate SSH Keys hosts: all vars: - new_pub_key: ~/.ssh/id_rsa_new.pub - new_priv_key: ~/.ssh/id_rsa_new tasks:
Next we'll need a task to copy the public part of our new key-pair to the remote hosts. For this we will use the authorized_key module. This module allows us to provide a key to add, which we will do.
- name: Add new key to authorized_keys authorized_key: key="" user=root state=present
Now for step 3, we will want to test this new key, to make sure that our new key addition is working. To do this, we will need to direct Ansible to use our new private key when connecting to our servers. We can use a set_fact task to set ansible_ssh_private_key variable to our new private key.
- name: Make use of new private key when connecting set_fact: ansible_private_ssh_key=
Our next task will make use of this new key when creating the connection (provided ControlPersist is not at play).
The next task is step 4, removing previous keys. Because of our previous task, this step will make use of the new key, and accomplish step 3 along the way.
Currently, the authorized_key Ansible module does not have a method to remove all but the specified ssh key. However I have sent a pull request to accomplish this, by way of the exclusive keyword. The task here will assume that this pull request has merged.
- name: Make our new key exclusive authorized_key: key="" user=root state=present exclusive=yes
This task looks just like the first task, but with the addition of exclusive=yes. If you don't want to use the modified authorized_key module, you could make use of the copy module which could get content from the new_pub_key file similar to how authorized_key gets content from the file.
If all has gone well, all that should be left in the authorized_keys file is the public part of our new key-pair. Our new key has been successfully rotated in and the old key is no longer allowed to log in.
Next Steps
There are more things we could do with our playbook. We could automate the creation of the key itself, which would look something like this:
- name: Create new ssh key-pair command: ssh-keygen -t rsa -N "" -q -f when: inventory_hostname == play_hosts[0] delegate_to: localhost
The when conditional here makes sure that only one key is generated, by only running on the first host. Delegation is also used to make the action happen on the system calling ansible, rather than a remote host.
We could also move the private key file into a location that our local ssh config is prepared to use by default:
- name: Move new key into default location command: mv ~/.ssh/id_rsa when: inventory_hostname == play_hosts[0] delegate_to: localhost
Any number of other tasks could be added around these, or specific options to the existing tasks. This blog post is just enough to get you started.
Conclusion
SSH keys are awesome. Anybody using ssh should be using keys. Keys are powerful, and thus need care. Rotate keys frequently and make sure to invalidate old keys. Automation can make this process a lot easier and more reliable.
For convenience, here is a complete playbook code block:
--- - name: Rotate SSH Keys hosts: all vars: - new_pub_key: ~/.ssh/id_rsa_new.pub - new_priv_key: ~/.ssh/id_rsa_new tasks: - name: Create new ssh key-pair command: ssh-keygen -t rsa -N "" -q -f when: inventory_hostname == play_hosts[0] delegate_to: localhost - name: Add new key to authorized_keys authorized_key: key="" user=root state=present - name: Make use of new private key when connecting set_fact: ansible_private_ssh_key= - name: Make our new key exclusive authorized_key: key="" user=root state=present exclusive=yes - name: Move new key into default location command: mv ~/.ssh/id_rsa when: inventory_hostname == play_hosts[0] delegate_to: localhost
And lastly here is a horn-less unicorn pooping a rainbow I found on photobucket, because this post has been far too serious.
tags: ssh - ansible