k6/secrets
Thek6/secrets module provides a secure way to access sensitive information like API keys, passwords, and tokens in your k6 tests without hardcoding them in your scripts.
Overview
The secrets module integrates with k6’s secret sources system, allowing you to retrieve secrets from external providers at runtime.Usage
Basic Example
Running with Secret Sources
To use secrets, you need to specify a secret source when running k6:API
get()
Retrieves a secret from the default secret source.string
required
The identifier/name of the secret to retrieve
Promise<string> - A promise that resolves to the secret value
source()
Accesses a specific named secret source.string
required
The name of the secret source to use
get() method
Secret Sources
k6 supports multiple secret source types:File Secret Source
Store secrets in a JSON file: secrets.json:Environment Variable Secret Source
Use environment variables as secrets:Multiple Secret Sources
You can use multiple secret sources:Real-World Example
From the k6 source examples:Complete Integration Example
Best Practices
Use different secrets for different environments. Maintain separate secret files for development, staging, and production.
Security Considerations
- File permissions: Ensure secret files have restricted permissions (e.g.,
chmod 600 secrets.json) - No logging: Avoid logging secret values to console or files
- Environment separation: Use different secrets for each environment
- Rotation: Regularly rotate your secrets
- Access control: Limit who can access secret files and sources