All projects require a certain amount of data that must remain secret and private.
This includes external API keys, identifiers to third-party services, certificates and SSH private keys.
The real problem is that the more the projects progress, the more secrets we have to share to run the application.
Don’t use environment variables for API keys and other secrets instead save them in an encrypted JSON file.
- Use Kourou to encrypt/decrypt the files containing your secrets
- Use Kuzzle Vault to load those secrets in your application memory
Before, we had environment variables
The simplest and most common way to share secrets is via environment variables.
The problem is that you can quickly find yourself with a dozen different API keys and other secrets.
Multiplied by the number of target deployment environments, you quickly end up with too many environment variables to manage.
The problem is that it is necessary to provide this set of variables to each person who will have to work on the project.
And we're not even talking about the work needed by our dear Devops to maintain all these variables for several deployment environments (production, staging, development, etc).
One solution is to keep all these secrets in a file that we will add to the repository and commit like any other file.
The only difference is that we will encrypt this file with AES 256 to make sure that nobody will be able to get their hands on the secrets.
This way there is only one secret that you have to share with your teams: the encryption key.
Encrypt with Kourou, the Kuzzle CLI
At Kuzzle we decided to take matters into our own hands and develop our secure secret sharing system.
So we're going to write all our secrets in a JSON file.
This file will of course NEVER be committed in the repository since it contains the secrets in clear text.
Then we're going to encrypt that file.
However we will only encrypt the values so we can always read its contents.
It is this encrypted file that we will save and commit in the project repository.
You can use Kourou for this:
The generated file is similar to the original JSON file except that all values have been encrypted with AES 256.
Decrypt in your application with Kuzzle Vault
Once we have our file containing the encrypted secrets, we will be able to use it in our application using the Kuzzle Vault.
The Kuzzle Vault is a simple NPM package with no dependencies that allows to decrypt and use the secrets in an application.
So we can now share and then load all the secrets needed to run the application directly and securely from the file system.
If you have different secrets according to your deployment environments, this is the time to save them all in different files so that they can be used automatically by your Continuous Integration system at the time of deployment.
Kourou has many methods for managing encrypted secret files.
For example it is possible to add a new secret to an existing file or to display the value of a secret:
If you need to encrypt a regular file, you can also use Kourou:
You can browse the complete list of Kuzzle Vault related commands in Kourou on Github.
Finally, you can also develop your own tools using the same cryptographic primitives.
If you are interested, you can take a look at the Cryptonomicon class of the kuzzle-vault package.
Encryption is difficult, and it's even more complicated to implement it securely in a team development environment with continuous integration.
However, if it is managed correctly and easily, it's an undeniable advantage in facilitating the entire development and deployment process.
The tools described in this article are like Kuzzle and the rest of our projects: open source and freely usable.
Open source is even more important when it comes to cryptography because you have to trust programs to handle sensitive data.
You can take our word for it or even better, you can go and have a look at the code yourself ;-)
If you have any questions or you just want to discuss, you can join us on the Kuzzle official Discord