How to Safeguard Sensitive Data in Load Tests with Grafana Cloud k6 Secrets Management
Step-by-step guide to securely store and use API keys, tokens, and credentials in Grafana Cloud k6 load tests using the new secrets management feature.
Introduction
When simulating real user behavior in performance tests, you often need API keys, tokens, or credentials to interact with actual systems. However, as your test suite grows, managing this sensitive data becomes a challenge—hardcoding secrets in scripts or sharing them manually increases the risk of exposure and makes maintenance difficult. To address this, Grafana Cloud k6 now offers built-in secrets management, allowing you to securely store and inject confidential values into your load tests at runtime. This guide walks you through setting up and using secrets management to keep your tests clean, secure, and reusable across environments.
What You Need
- A Grafana Cloud account with access to the k6 performance testing module (Testing & synthetics > Performance).
- Administrator or editor permissions to create and manage secrets (contact your admin if unsure).
- k6 OSS familiarity (basic scripting knowledge).
- The sensitive data you want to store (e.g., API tokens, credentials).
Step-by-Step Guide
Step 1: Access the Secrets Management Interface
Log in to your Grafana Cloud account. In the left menu, navigate to Testing & synthetics > Performance. Then open Settings and click on the Secrets tab. This is the central place where you can create, edit, and delete secrets for your load tests.
Step 2: Create a New Secret
Click the Add Secret button. You will need to provide three pieces of information:
- Name – A unique identifier you will use to reference the secret in your test scripts (e.g.,
api-token). - Value – The actual sensitive data (e.g.,
sk-123abc456). Once saved, this value cannot be read back from the UI. - Description – (Optional) A helpful note about the secret’s purpose (e.g., “Production API token for payment service”).
- Labels – (Optional) Tags like
env:prodorservice:paymentsto keep secrets organized.
After clicking Save, the secret becomes immediately available to your tests. Remember: the value is write-only—once set, you cannot view it again. This aligns with security best practices.
Step 3: Edit an Existing Secret
If you need to rotate a credential or update a description, go to the Secrets tab and find the secret you want to modify. Click the Edit icon (pencil). You will see the name, description, and labels—but not the current value. To change the value, simply enter a new one. The old value will be overwritten. This allows you to update secrets without exposing them, even to yourself.
Important: Editing a secret doesn’t affect tests that are currently running. Changes take effect the next time a test is started.
Step 4: Delete a Secret
When a secret is no longer needed, navigate to the Secrets tab, find the secret, and click the Delete icon (trash). Confirm the deletion. After deletion, any test that tries to use that secret will fail at runtime with an error. Make sure to update your test scripts to remove any references to deleted secrets.
Step 5: Use Secrets in Your k6 Tests
Grafana Cloud k6 provides a built-in module k6/secrets to retrieve secret values during test execution. Here’s how to use it:
- Import the module at the top of your script:
import secrets from 'k6/secrets'; - Inside your default function or any async function, call
secrets.get('your-secret-name'). This returns a Promise, so you need to useawait. - Use the retrieved value in your HTTP requests or other operations.
Example script:
import http from 'k6/http';
import { check } from 'k6';
import secrets from 'k6/secrets';
export default async function() {
const apiToken = await secrets.get('api-token');
const headers = { Authorization: `Bearer ${apiToken}` };
let res = http.get('https://api.example.com/data', { headers });
check(res, { 'status is 200': (r) => r.status === 200 });
}
Note that secrets.get() can only be called inside async functions. The secret value is fetched at runtime from Grafana Cloud, so your scripts remain free of hardcoded credentials.
Step 6: Reuse Secrets Across Tests and Environments
Because secrets are stored centrally in Grafana Cloud, you can use the same secret in multiple test scripts without duplicating it. To switch between environments (e.g., staging vs. production), create separate secrets with different names (e.g., staging-api-token and prod-api-token) and reference the appropriate one in each test. Labels help you organize secrets by environment or project, making management simpler as your test suite scales.
Tips and Best Practices
- Never hardcode secrets: Always use the secrets module to avoid accidental exposure in version control or logs.
- Rotate credentials regularly: Use the edit feature to overwrite secret values without revealing the old ones.
- Use descriptive names and labels: This makes it easy to find the right secret, especially when you have many.
- Keep secrets write-only: The UI design prevents viewing values after creation, so avoid relying on screenshots or shared screens.
- Test before switching to production: Validate your script using a staging secret first to ensure the integration works.
- Monitor secret usage: Check test logs to confirm that secrets are being injected correctly; errors from
secrets.getwill appear in the output.
By following these steps, you can securely manage sensitive data in your performance tests, reduce the risk of leaks, and simplify your test scripts. For more details, refer to the official Grafana Cloud k6 documentation.