Hard-coding credentials, API keys, or access tokens in automated test suites is one of the most common security risks in software engineering. Ensuring that sensitive variables remain isolated across local developer environments and CI/CD pipelines is critical for keeping your code repositories secure.
Here is a practical guide and best-practices workflow for managing secrets cleanly in test automation frameworks.
Core Recommendations for Secure Test Suites
Zero Source Control Leakage: Always add .env and .env.local files to your .gitignore. Never commit raw tokens or passcodes to git.
Use Managed CI Secret Stores: In build pipelines, leverage platform native secret managers such as GitHub Secrets, Harness Secrets, Azure Key Vault, AWS Secrets Manager, or HashiCorp Vault.
Dynamic Injection via Environment Variables: Read sensitive data dynamically inside tests using standard environment variables (e.g., process.env.API_KEY or process.env.API_BASE_URL).
Enforce Least Privilege: Scope test credentials strictly to non-production environments and configure them to expire periodically.
Implementation Examples
Local Development Usage:
Create a non-committed local environment file (.env.local):
API_BASE_URL=https://staging.example.com/api
API_TOKEN=your_secret_token_here
Execute your test suite while passing or overriding variables inline:
# Setting environment variables directly before execution
$env:API_BASE_URL = 'https://staging.example.com/api'
npm run test:api
CI Pipeline Integration (e.g., GitHub Actions):
Store API_TOKEN under your repository's Settings > Secrets and variables > Actions, then pass it into your execution job step:
- name: Run API Tests
run: npm run test:api
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
API_BASE_URL: ${{ secrets.API_BASE_URL }}
Top comments (0)