Secrets-Loader is a Bash script designed to securely load environment variables from secret stores like AWS SSM and CloudFormation directly into your .env file. This tool simplifies the management of environment variables by securely fetching secrets for local development, testing, and production environments, ensuring sensitive data stays out of version control.
- Automated secret loading: Fetch secrets from AWS SSM and CloudFormation by specifying paths in your
.envfile using simple syntax (ssm:for SSM parameters,cf:for CloudFormation outputs). - Security-first approach: Keep sensitive information, such as API keys and credentials, out of your
.envfiles and version control by fetching them dynamically. - Single-command execution: Load your secrets into your environment with a single command, making it seamless for development and deployment environments.
- Graceful error handling: Warnings are displayed if secrets cannot be fetched, but the script continues processing other secrets.
- Extensible design: New secret management systems can easily be integrated by adding custom prefixes.
-
Clone the repository:
git clone https://github.com/Thavarshan/secrets-loader.git cd secrets-loader -
Make the script executable:
chmod +x secrets.sh
-
Ensure AWS CLI is installed and configured:
If you don't have the AWS CLI installed, you can follow the AWS CLI installation guide.
Then, configure your AWS credentials:
aws configure
-
Define secrets in your
.envfile using a custom syntax:THIRD_PARTY_API_KEY="ssm:/third-party/api/key" THIRD_PARTY_API_SECRET="cf:my-stack:ApiSecret"
- Use the
ssm:prefix for AWS SSM Parameter Store secrets. - Use the
cf:prefix for AWS CloudFormation stack outputs.
- Use the
-
Run the script to load the secrets:
./secrets.sh
This command will automatically fetch the actual values from the specified secret stores and update your
.envfile with the real values. -
View the output:
Upon completion, the script will notify you with a success message or warnings if certain secrets could not be fetched.
# Standard environment variables
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=http://localhost
# Third-party service credentials fetched from AWS SSM
THIRD_PARTY_API_KEY="ssm:/third-party/api/key"
THIRD_PARTY_API_SECRET="ssm:/third-party/api/secret"
# AWS credentials fetched from CloudFormation stack outputs
AWS_ACCESS_KEY_ID="cf:my-stack:AccessKeyId"
AWS_SECRET_ACCESS_KEY="cf:my-stack:SecretAccessKey"After running the script, your .env file will be updated with the actual values for the secrets.
- AWS CLI: Ensure the AWS CLI is installed and configured with the correct credentials.
- Permissions: The AWS IAM role or user associated with the AWS CLI must have sufficient permissions to access AWS SSM Parameters and CloudFormation outputs.
The script gracefully handles errors such as:
- SSM Parameter not found: The script will display a warning and continue loading other secrets.
- CloudFormation output not found: Similar to SSM, a warning is displayed, and the script continues processing other secrets.
- Malformed
.envlines: The script logs warnings if it encounters invalid syntax in the.envfile, ensuring robust processing without breaking.
The Secrets-Loader is designed with extensibility in mind. You can easily extend support to other secret stores (e.g., Azure Key Vault, HashiCorp Vault) by adding new prefixes and integrating the respective CLI or SDK.
For example, to add support for Azure Key Vault, you could:
- Define a new prefix, e.g.,
azkv:. - Use the Azure CLI or SDK to fetch secrets and add corresponding functions to handle the retrieval.
- Error fetching secrets: Ensure that the AWS CLI is properly configured and has sufficient permissions to access the necessary secrets.
- Incorrect syntax in
.env: Double-check the syntax in your.envfile to ensure it follows the correctssm:orcf:format. - No output for certain keys: If a secret can't be fetched (e.g., due to permissions), check the AWS CLI for possible errors using the
--debugoption.
We welcome contributions! Feel free to open an issue or submit a pull request if you have suggestions, improvements, or bug fixes.
When contributing:
- Follow the Contributor Code of Conduct.
- Ensure that tests are included for any new features.
This project is licensed under the MIT License. See the LICENSE file for details.