Skip to main content

k6/secrets

The k6/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.
Never hardcode secrets directly in your test scripts. Use the secrets module with external secret sources to keep sensitive data secure.

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
Returns: 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
Returns: Secret source object with get() method

Secret Sources

k6 supports multiple secret source types:

File Secret Source

Store secrets in a JSON file: secrets.json:
Usage:

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

Never commit secrets to version control. Add your secret files to .gitignore and use environment-specific configurations.
Use different secrets for different environments. Maintain separate secret files for development, staging, and production.
Secrets are loaded per VU. Each virtual user will make separate calls to retrieve secrets. Consider caching secrets if you need to optimize performance.

Security Considerations

  1. File permissions: Ensure secret files have restricted permissions (e.g., chmod 600 secrets.json)
  2. No logging: Avoid logging secret values to console or files
  3. Environment separation: Use different secrets for each environment
  4. Rotation: Regularly rotate your secrets
  5. Access control: Limit who can access secret files and sources

Error Handling