Getting Started
Installation
Install and configure the package using the following command :
Configure your Env schema
This step is done automatically when you first install Crypt.
In your start/env.ts
file, import @foadonis/crypt
to modify default Env
behavior.
Generate your keypair
Crypt works with asymmetric cryptography. It will generate two keys, one for encrypting (public key) and one for decrypting (public key).
Start generating a key for your development environment using the crypt:init
command:
Private key (decrypt)
This key is used to decrypt your environment variables. It is stored in a .env.keys
file that MUST NOT be committed.
Public key (encrypt)
This key is used to encrypt your environment variables. It is stored directly in your .env
file and can be safely committed.
As the public key can only be used to encrypt variables you can safely store it in your repository, allowing other members of your team to encrypt new variables without decrypting the existing ones.
Encrypt your first secret
Now that our schema is expecting an encrypted DB_PASSWORD
and we have everything to encrypt and decrypt credentials, you can use the crypt:set
command to encrypt your database password and store it in your .env
file.
This will add the encrypted credential in your .env
file alongside unencrypted variables:
You can now start your Adonis application. During startup, Crypt will automatically decrypt your DB_PASSWORD
using the private key (CRYPT_PRIVATE_KEY
) stored in the .env.keys
file.
Encrypt for production
Crypt select the environment variable file depending on your NODE_ENV
. Each environment will have its own keypair named CRYPT_PRIVATE_KEY_<environment>
/CRYPT_PUBLIC_KEY_<environment>
.
Update Gitignore
Now that you are allowed to commit your dotenv files lets update the .gitignore
: