K6 针对特定端点 URL 获取请求导致错误

问题描述

我是 K6 的新手,正在尝试使用该工具通过验证 API 来执行 Get 请求。 执行脚本时,我收到终止脚本的警告。据我了解,这个错误与 Go 有点关系(如果我理解正确的话)。

我想要实现的结果是能够对端点 URL 执行 Get 请求,但如果我做错了或应该尝试其他方法,我们将不胜感激。

脚本:

import http from "k6/http";
import { check } from "k6";

export default function () {
  var url =
    "https://endpoint.example.to.cloud/api/reports/v1/SMOKETESTC6KP6NWX";

  var headerParam = {
    headers: {
      "Content-Type": "application/json",},};

  const response = http.get(url,headerParam);

  check(response,{
    "Response status reciving a 200 response ": (r) => r.status === 200,});

  let body = JSON.parse(response.body);
}

输出

WARN[0000] Request Failed error="Get \"https://endpoint.example.to.cloud/api/reports/v1/SMOKETESTC6KP6NWX\": x509: certificate relies on legacy Common Name field,use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0"

更改 URL 端点: 如果我像下面那样更改 URL 端点(模型 URL),就不会出现错误

...
var url = "https://run.mocky.io/v3/16fa8113-57e0-4e47-99b9-b5c55da93d71";
...

更新解决方案以在本地运行:

为了在本地运行,我必须添加认证和密钥: 示例:

export let options = {
    ...
  
    tlsAuth: [
      {
        cert: open(`${__ENV.Certificate}`),key: open(`${__ENV.Key}`),],};

另外用 --insecure-skip-tls-verify 填充执行命令

示例:

k6 run -e Certificate=/home/cert/example_certification.crt -e Key=/home/cert/certification/example_key.key -e example.js --insecure-skip-tls-verify

解决方法

k6 是用 Go 编写的,最新版本的 Go 在处理 X.509 证书的方式上发生了重大变化:https://golang.org/doc/go1.15#commonname

正如错误消息中所说,您可以通过设置 GODEBUG=x509ignoreCN=0 环境变量来暂时允许旧行为,但这很可能是 stop working in a few months with Go 1.17。使用 insecureSkipTLSVerify k6 option 也可能有效,我没有检查过,但顾名思义,它会停止任何 TLS 验证并且不安全。

所以真正的解决方案是正确地重新生成您的服务器端证书。