.env
files that must be excluded from repositories, Orion Env allows you to safely commit your configuration to your codebase using public key, elliptic curve cryptography.
This package works as a standalone package. It does not depend on any other Orion.js packages.
Key Features
- Secure Storage: Encrypt sensitive data with strong cryptography
- Source Control Integration: Store all configurations directly in your repository
- TypeScript Support: Automatically generate TypeScript definitions for your environment variables
- Secret Manager Integration: Read variables from JSON-formatted environment variables populated by secret managers
- Flexible Usage: Access environment variables directly or load them into process.env
Installation
Getting Started
Creating a new Env configuration file
Run the following command in your project. You’ll receive a password that you’ll need to keep secure - it’s required to decrypt your environment variables.Adding environment variables
To add a new environment variable to your configuration:- The variable name
- The variable value
Reading variables in your application
There are two main approaches to using your environment variables:1. Direct access via the env
object
2. Loading into process.env
Configuration Options
Environment Setup
For either approach, you’ll need to configure these environment variables:ORION_ENV_FILE_PATH
: Path to your Orion Env configuration fileORION_ENV_SECRET_KEY
: The password to decrypt encrypted variables
loadEnv Options
TheloadEnv()
function accepts the following options:
TypeScript Integration
Orion Env can automatically generate TypeScript definition files for your environment variables, providing autocomplete and type checking.Secret Manager Integration
You can configure Orion Env to read variables from JSON-formatted environment variables, which can be populated from secret managers like AWS Secrets Manager, HashiCorp Vault, or similar services.Configuration
In your Orion Env configuration file, add areadFromSecret
section that maps environment variable names to arrays of variable keys:
How it works
- Orion Env looks for environment variables specified in the
readFromSecret
object (e.g.,AWS_SECRETS
). - If found, it parses the value as JSON.
- For each variable name in the array (e.g.,
DATABASE_URL
), it looks for corresponding properties in the parsed JSON. - These values become available in the
env
object alongside your other variables.
Secret Key Discovery
If the JSON content contains anORION_ENV_SECRET_KEY
property, Orion Env will use that value as the secret key for decrypting your encrypted variables. This allows you to store your decryption password in the secret manager as well.
Example JSON in process.env.AWS_SECRETS
:
Best Practices
- Rotate Keys Regularly: Periodically update your encryption keys and regenerate your configuration.
- Use CI/CD Variables: For production environments, consider storing your secret key in CI/CD variables.
- Clean vs. Encrypted: Only store sensitive data (API keys, passwords) as encrypted; public URLs and non-sensitive configs can use cleanKeys.
- Version Control: Always include your env configuration file in version control - that’s the whole point!