How to deploy encrypted copies of your SSL keys and other files with Ansible and OpenSSL
I've been working on fully automating server provisioning and deployment of my Django app, GlucoseTracker, the last couple of weeks with Ansible. Since I made this project open-source, I needed to make sure that passwords, secret keys, and other sensitive information are encrypted when I push my code to my repository.
Of course, I have the option to not commit them to the repo, but I want to be able to build my entire infrastructure from code and maintain all configuration in one place, the git repo.
Fortunately, Ansible has a command line tool called Ansible Vault (comes with the core package) that will allow you to encrypt your configuration files and decrypt them during deployment by passing in the password or password file in the command. This is mainly useful for encrypting your environment variables files that contain the passwords/keys for your application.
For example, in my Django settings, instead of assigning the values directly in the settings file, I do something like this:
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
Django would then read the value from the server's environment variables. In my case, since I use virtualenv, I have a postactivate script that sets the environment variables for that virtualenv.
Since I want Ansible to fully automate my server configuration and store all the information that I need to do so in a git repo, I have to encrypt the variables that my app use in production. For example, I have a file called production in the env_vars folder of my repo that the playbook will use. I encrypt this file with a password using Ansible Vault and when running the playbook I decrypt it by passing in the password in the command argument. If you use CI tools like Jenkins you can then just set this password in Jenkins (perhaps as an environment variable) so you won't have to type it in manually.
If I need to make a change in the vars file, I can simply just type in:
ansible-vault edit env_vars/production
This will prompt me for the password and display the decrypted values in vim for me where I can make my changes and save it back in the encrypted version. This is a nice option if you just need to make small changes as you won't forget to encrypt the file again after decrypting it.
Here's what an Ansible Vault encrypted vars file will look like:
$ANSIBLE_VAULT;1.0;AES 53616c7465645f5f5d2e57e60827b394fec9e16fef1954b9578542cc3098c72cf30cfe25dbf1fb49 0c0ace757c1b4f9e60860bf91988b21dc2636bf5cf5295396c22e7ba34af68a702ce2b224091aaa7 7e579aff2159f5cbfa2e05caf432cef3a32729aef212f5509c89f2a681d113b4b2ffc01dab88e5a6 9e8092f59b9adb9960af646551490111131912ece4df55c6045e5d4e49ee8d3c143ca6ba95492f12 ddaf044ef02aa25d78a14ba60411ecdc72aa68c6a756d5000906cfeac62e2e975a2f72b172a1a386 b0e8431213018e074c810b7851e82c1770c985fbae8bd3f1c15367aeebd674da7e228e0a0864467e 88a67ce40646f39d43ea9810f8e5f4273d7c035704f2087b85a18e6f2ea37f054ca1deae8de5a588 99727f9116540552cc4ed107bb8ba133787bb0321bdd0f6464ece4a88e60cf301cf24c6b646a931b 5b8caa27dc5b6cc6f3c40bff172b0ea778b6ced036d45acb24a34e016ff9d55a2a75de65307eaf97 593e88a6c492f97427d3536f7ca0f15f5a253d4e16903efdcbb92b2cf20c74bc45d2cc2ee90ecd0d 257e8f334416de3369735d7e5b2b48afc2c7d34948523c8e429a7e15f2c62c1c7de0b88d87af6096 d44581bfd1d547362874fabd5ae188be92cd8c38fbdaeabb2af217c11eb6010579fb8ae0f1a24608 94504a5e07e866b9c3daa7335d0f18cc7cec914790b39d17a2c2c8c76d3cfd24903d58817290b873 92101ee222074488c3a9b61d2f8ccaf6261dd897f1e49abe6f5c45945d3d84bcce3acaf8c9f16c17 536f3aea2f289395ef6987908b20b99d3377b4b50f8cb2064c628b5cc281b609fcba7f49a35b2c73 e1d232dbc107f311db7dd2391f7e8f77c187bcef2904b11ab0c24ebf69af37901e8ad178ff383414 335a448f44b63e39cbcbabaef94a0f33024393c16e78240bfd33b3f031a9461287ef1aef1d2959fd ec8fb7c8be7f7601bb4fc9fbefd5f6f62b7d44dde16ad59ac6144a4b08339efeb9bc39a106082eb9 da14ab70e43b261ae1d717f97edaff5a1a40ef5455da5a2ee69e83893a11b859b758ffe9eb2b09ff bc9169086fcf3b6e66c15ba5ae2c6b98705c0a486fa3e6a05cfbc1bff5006fc3abe078643b372655 0c47e02238f533d877e1fd764fa7eb0772d3fb75532156c928d5ecf3fd0980f9ac274f43cab3ca71 42b8130b116ecf497771f927bed2f1e9f0a39a96f27dd8d894d15821614ad4953785767080492d2c e898145cc0430c19bcc2f8b139b864307867940b32a8f1adb5fa39114b18304b595a3240611ccac4 9988715adcab5420eb68e0abf2cf8133467fe2f54680102ef5ab3b8b158af1b8048bac65b178a847 30ab48c579aee1a820e2b386ae3719d4a923e6d2a3440b55672bb872774386d8a10ddd8d347aaabc 2dfecacbb1b7018aa79ead9cb820cfcf519efdf31e956be89b1b13a659dab3a769f24100b226da6b 87d4793ef44b3157984681455dfa00e295b50f7ae7d2ed5e1070f296a9d297e4c05190d65537ec10 3a7684ce2da75ecd8c6aebfe0616e67dd1d64ac216db208ba8afdb701d4402c203f0238a69443d71
What about files that get copied to the server such as private keys for SSL certificates?
This is where I had to do something extra as the Ansible copy module will copy these files as they're stored in the repository (i.e. the encrypted version). To get around this, I simply used OpenSSL to encrypt these files using symmetric encryption and set the password to decrypt it in my vars file (which Ansible Vault will encrypt).
To encrypt a file with OpenSSL using AES 256 encryption:
openssl aes-256-cbc -salt -a -e -in ssl_signed/unencrypted.key -out ssl_signed/encrypted.key -k MysupasecuresecretPasswordZ.x!!
To decrypt an AES 256 encrypted file with OpenSSL:
openssl aes-256-cbc -salt -a -d -in ssl_signed/encrypted.key -out ssl_signed/unencrypted.key -k MysupasecuresecretPasswordZ.x!!
Example vars file:
# Nginx settings. nginx_server_name: <a href="http://www.glucosetracker.net">www.glucosetracker.net</a> ssl_src_dir: ssl_signed ssl_dest_dir: /etc/ssl ssl_key_password: MysupasecuresecretPasswordZ.x!!
I have a task in my playbook that would copy my SSL keys to the remote server and run the OpenSSL command to decrypt it (using the password from the ssl_key_password variable in my vars file):
- name: Copy the SSL cert and key to the remote server copy: src={{ ssl_src_dir }}/ dest={{ ssl_dest_dir }} - name: Decrypt the SSL key command: openssl aes-256-cbc -salt -a -d -in {{ ssl_dest_dir }}/nginx.key -out {{ ssl_dest_dir }}/decrypted.key -k {{ ssl_key_password }} creates={{ ssl_dest_dir }}/decrypted.key - name: Rename the decrypted SSL key command: mv {{ ssl_dest_dir }}/decrypted.key {{ ssl_dest_dir }}/nginx.key removes={{ ssl_dest_dir }}/decrypted.key
Now let's run the production playbook:
ansible-playbook -i inventory/production –private-key=/aws-keys/ec2-glucosetracker.pem –vault-password-file=~/ansible/decryption_password -vvvv production.yml
This is just one example and this same simple concept can be applied to different scenarios. Just to summarize the steps:
- Encrypt your files with OpenSSL using symmetric encryption.
- Assign the decryption password to a variable in your Ansible vars file.
- Encrypt your vars file using Ansible Vault.
- Create a task in your playbook to decrypt the encrypted files using OpenSSL and the password in the encrypted vars file.
- Run your Ansible playbook, passing in the Ansible Vault password in the command or specifying the file where the password is stored.