Skip to content

Commit 7ff1deb

Browse files
Merge pull request #2127 from grafana/add-url-secret-source-docs
[docs] Add url secret-source docs
2 parents c57ad3b + e7abc20 commit 7ff1deb

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-0
lines changed

docs/sources/k6/next/using-k6/secret-source/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following built-in secret sources are available for local testing:
2020

2121
- [`file`](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/secret-source/file): Reads secrets from a `key=value` file.
2222
- [`mock`](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/secret-source/mock): Reads secrets from CLI arguments.
23+
- [`url`](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/secret-source/url): Fetches secrets from HTTP endpoints.
2324

2425
## Secret source extensions
2526

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
---
2+
title: 'URL secret source'
3+
menuTitle: 'URL'
4+
description: 'The URL secret source fetches secrets from HTTP endpoints'
5+
weight: 03
6+
---
7+
8+
# URL secret source
9+
10+
The URL secret source fetches secrets from generic HTTP endpoints.
11+
12+
This source fetches secrets via HTTP requests and supports custom headers, JSON path extraction, rate limiting, and automatic retries.
13+
14+
## Basic usage
15+
16+
To use the URL secret source, specify a URL template with the `--secret-source` flag:
17+
18+
{{< code >}}
19+
20+
```bash
21+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key} script.js
22+
```
23+
24+
```docker
25+
docker run -it --rm \
26+
-v <SCRIPT_DIR>:/scripts \
27+
grafana/k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key} /scripts/script.js
28+
```
29+
30+
{{< /code >}}
31+
32+
k6 replaces the `{key}` placeholder with the secret identifier when it fetches secrets.
33+
34+
## Configuration options
35+
36+
The URL secret source supports three configuration methods:
37+
38+
1. **Inline CLI arguments**: Key-value pairs in the `--secret-source` flag
39+
2. **JSON configuration file**: Referenced via `config=path/to/file.json`
40+
3. **Environment variables**: Prefixed with `K6_SECRET_SOURCE_URL_`
41+
42+
### Configuration parameters
43+
44+
| Parameter | Type | Default | Description |
45+
|-----------|------|---------|-------------|
46+
| `urlTemplate` | string | (required) | URL template with `{key}` placeholder |
47+
| `method` | string | `GET` | HTTP method to use |
48+
| `headers.*` | string | - | Custom headers. For example, `headers.Authorization=Bearer token` |
49+
| `responsePath` | string | - | JSON path to extract the secret from the response. If empty, k6 uses the entire response |
50+
| `timeout` | string | `30s` | Request timeout (e.g., `30s`, `1m`, `500ms`) |
51+
| `requestsPerMinuteLimit` | int | `300` | Maximum requests per minute |
52+
| `requestsBurst` | int | `10` | Burst of requests above rate limit |
53+
| `maxRetries` | int | `3` | Maximum retry attempts for failed requests |
54+
| `retryBackoff` | string | `1s` | Base backoff duration for retries |
55+
56+
### Inline configuration
57+
58+
Provide configuration as comma-separated key-value pairs:
59+
60+
```bash
61+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key},headers.Authorization=Bearer mytoken,timeout=10s script.js
62+
```
63+
64+
### File-based configuration
65+
66+
Create a JSON configuration file:
67+
68+
```json
69+
{
70+
"urlTemplate": "https://api.example.com/secrets/{key}",
71+
"method": "GET",
72+
"headers": {
73+
"Authorization": "Bearer mytoken",
74+
"X-Custom-Header": "value"
75+
},
76+
"responsePath": "data.value",
77+
"timeout": "30s",
78+
"requestsPerMinuteLimit": 300,
79+
"requestsBurst": 10,
80+
"maxRetries": 3,
81+
"retryBackoff": "1s"
82+
}
83+
```
84+
85+
Reference the file with the `config` parameter:
86+
87+
```bash
88+
k6 run --secret-source=url=config=secrets-config.json script.js
89+
```
90+
91+
You can override file settings with inline parameters:
92+
93+
```bash
94+
k6 run --secret-source=url=config=secrets-config.json,timeout=5s script.js
95+
```
96+
97+
### Environment variables
98+
99+
Configure using environment variables with the prefix `K6_SECRET_SOURCE_URL_`:
100+
101+
```bash
102+
export K6_SECRET_SOURCE_URL_URL_TEMPLATE="https://api.example.com/secrets/{key}"
103+
export K6_SECRET_SOURCE_URL_HEADER_AUTHORIZATION="Bearer mytoken"
104+
export K6_SECRET_SOURCE_URL_METHOD="GET"
105+
export K6_SECRET_SOURCE_URL_RESPONSE_PATH="data.value"
106+
export K6_SECRET_SOURCE_URL_TIMEOUT="30s"
107+
export K6_SECRET_SOURCE_URL_MAX_RETRIES="3"
108+
export K6_SECRET_SOURCE_URL_RETRY_BACKOFF="1s"
109+
export K6_SECRET_SOURCE_URL_REQUESTS_PER_MINUTE_LIMIT="300"
110+
export K6_SECRET_SOURCE_URL_REQUESTS_BURST="10"
111+
112+
k6 run script.js --secret-source=url
113+
```
114+
115+
### Configuration precedence
116+
117+
When you use multiple configuration methods, k6 applies them in this order:
118+
119+
1. Default values
120+
1. Environment variables
121+
1. Configuration file
122+
1. Inline CLI arguments
123+
124+
## Response handling
125+
126+
### Plain text responses
127+
128+
If no `responsePath` is specified, the entire response body is treated as the secret:
129+
130+
```bash
131+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key} script.js
132+
```
133+
134+
### JSON responses
135+
136+
Extract specific values from JSON responses using dot notation:
137+
138+
```bash
139+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key},responsePath=data.value script.js
140+
```
141+
142+
For this JSON response:
143+
144+
```json
145+
{
146+
"data": {
147+
"value": "my-secret-value"
148+
}
149+
}
150+
```
151+
152+
The secret source extracts `"my-secret-value"`.
153+
154+
## Rate limiting
155+
156+
The URL secret source includes built-in rate limiting to prevent overwhelming the secret service:
157+
158+
- `requestsPerMinuteLimit`: Maximum requests per minute (default: 300)
159+
- `requestsBurst`: Allows a burst of requests above the limit (default: 10)
160+
161+
k6 automatically queues requests that exceed the rate limit.
162+
163+
## Retry behavior
164+
165+
k6 automatically retries failed requests with exponential backoff:
166+
167+
- k6 retries server errors (5xx), rate limiting (429), network errors, and timeouts
168+
- k6 doesn't retry client errors (4xx except 429)
169+
- Backoff calculation: `wait = (base ^ attempt) + random jitter up to 1 second`
170+
- Default settings: 3 retries with 1 second base backoff
171+
172+
## Examples
173+
174+
### Basic API with authentication
175+
176+
Fetch secrets from an API that requires a bearer token:
177+
178+
```bash
179+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key},headers.Authorization=Bearer mytoken script.js
180+
```
181+
182+
### JSON API with nested response
183+
184+
Extract a specific value from a nested JSON response:
185+
186+
```bash
187+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key},responsePath=secret.data.value,headers.X-API-Key=apikey123 script.js
188+
```
189+
190+
### Custom timeout and retry settings
191+
192+
Configure shorter timeouts and more aggressive retries:
193+
194+
```bash
195+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key},timeout=5s,maxRetries=5,retryBackoff=2s script.js
196+
```
197+
198+
### POST request with custom headers
199+
200+
Use POST method with multiple custom headers:
201+
202+
```bash
203+
k6 run --secret-source=url=urlTemplate=https://api.example.com/secrets/{key},method=POST,headers.Content-Type=application/json,headers.X-Custom-Header=value script.js
204+
```
205+
206+
### Multiple URL sources
207+
208+
Configure multiple URL secret sources with different names:
209+
210+
```bash
211+
k6 run \
212+
--secret-source=url=default,urlTemplate=https://api1.example.com/secrets/{key},headers.Authorization=Bearer token1 \
213+
--secret-source=url=name=backup,urlTemplate=https://api2.example.com/secrets/{key},headers.Authorization=Bearer token2 \
214+
script.js
215+
```
216+
217+
Access different sources in your script:
218+
219+
<!-- md-k6:skip -->
220+
221+
```javascript
222+
import secrets from 'k6/secrets';
223+
224+
export default async () => {
225+
// Default source
226+
const secret1 = await secrets.get('my-key');
227+
228+
// Named source
229+
const backupSource = await secrets.source('backup');
230+
const secret2 = await backupSource.get('my-key');
231+
};
232+
```

0 commit comments

Comments
 (0)