菜单
开源 RSS

Secret source

Secret sources 是 k6 获取用于 k6 的密钥的一种方式。与直接使用环境变量、从文件读取等不同,从 Secret sources 获取的值在传播到系统其他部分之前,会从 k6 发出的日志中进行脱敏处理。

密钥通过 k6/secrets JS API 提供,并将从任何日志中进行脱敏处理。

配置 Secret sources

目前配置 Secret sources 的唯一方式是通过 --secret-source CLI 标志。可以同时配置多个 Secret sources。

Secret sources

当前的内置 Secret sources 功能有限,主要用于本地测试

  • file 从 key=value 文件中读取密钥。
  • mock 从 CLI 标志中读取密钥。

Secret source 扩展

您可以将 Secret source 实现为 k6 的扩展

示例脚本

JavaScript
import http from 'k6/http';
import secrets from 'k6/secrets';

export default async () => {
  const my_secret = await secrets.get('cool'); // get secret from a source with the provided identifier
  console.log(my_secret);
  const response = await http.asyncRequest('GET', 'https://httpbin.org/get', null, {
    headers: {
      'Custom-Authentication': `Bearer ${await secrets.get('else')}`,
    },
  });
  console.log(response.body);
};
如果使用以下文件运行

cool=some
else=source

您会注意到密钥被脱敏处理了,但脚本仍然可以使用它们,例如在协议请求中。

bash
$ k6 run --secret-source=file=file.secret secrets.test.js
...
INFO[0000] ***SECRET_REDACTED***                         source=console
INFO[0001] {
  "args": {},
  "headers": {
    "Custom-Authentication": "Bearer ***SECRET_REDACTED***",
    "Host": "httpbin.org",
    "User-Agent": "k6/0.57.0 (https://k6.io/)",
    "X-Amzn-Trace-Id": "Root=1-67dd638b-4243896a2fa1b1b45bc63eaa"
  },
  "origin": "<my actual IP>",
  "url": "https://httpbin.org/get"
}  ***SECRET_REDACTED***=console