菜单
开源

sign

SignatureV4.sign() 使用 AWS Signature V4 算法对 HTTP 请求进行签名。给定 HTTP 请求描述,它返回一个添加了 AWS Signature V4 协议头的新 HTTP 请求。它返回一个包含 urlheaders 属性的对象,可在 k6 的 HTTP 调用上下文中使用。

参数

sign 方法的第一个参数是一个包含以下属性的对象。

属性类型描述
methodstring请求的 HTTP 方法
endpointEndpoint请求的端点。Endpoint 构造函数可以从 aws.js bundle 或 signature.js 文件导入,并接受 {scheme}://{hostname}[:{port}] 形式的字符串作为输入,用于定义请求的目标(参见下方提供的示例)。
pathstring请求的路径
headersObjectHTTP 请求头
body (可选)string 或 ArrayBufferHTTP 请求的可选请求体
query (可选)Object.<string, string | Array.<string>>HTTP 请求的可选查询参数

您可以在此特定请求的上下文中覆盖 SignatureV4 选项。为此,请向 sign 方法传入第二个参数,它是一个包含以下参数的对象。

属性类型描述
signingDateDate覆盖签名操作中使用的日期
signingServicestring覆盖 sign 操作上下文中 signer 的 AWS 服务。
signingRegionstring覆盖 sign 操作上下文中 signer 的 AWS 区域
unsignableHeadersSet<string>将请求头排除在签名过程之外
signableHeadersSet<string>标记请求头为已签名

返回值

属性类型描述
headersObject用于 k6 HTTP 请求上下文的已签名请求头
urlstring用于 k6 HTTP 请求上下文的已签名 URL

示例

JavaScript
import http from 'k6/http';

import {
  AWSConfig,
  Endpoint,
  SignatureV4,
} from 'https://jslib.k6.io/aws/0.13.0/signature.js';

const awsConfig = new AWSConfig({
  region: __ENV.AWS_REGION,
  accessKeyId: __ENV.AWS_ACCESS_KEY_ID,
  secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY,
  sessionToken: __ENV.AWS_SESSION_TOKEN,
});

export default function () {
  /**
   * In order to be able to sign an HTTP request's,
   * we need to instantiate a SignatureV4 object.
   */
  const signer = new SignatureV4({
    service: 's3',
    region: awsConfig.region,
    credentials: {
      accessKeyId: awsConfig.accessKeyId,
      secretAccessKey: awsConfig.secretAccessKey,
      sessionToken: awsConfig.sessionToken,
    },

    /**
     * Whether the URI should be escaped or not.
     */
    uriEscapePath: false,

    /**
     * Whether or not the body's hash should be calculated and included
     * in the request.
     */
    applyChecksum: true,
  });

  /**
   * The sign operation will return a new HTTP request with the
   * AWS signature v4 protocol headers added. It returns an Object
   * implementing the SignedHTTPRequest interface, holding a `url` and a `headers`
   * properties, ready to use in the context of k6's http call.
   */
  const signedRequest = signer.sign(
    /**
     * HTTP request description
     */
    {
      /**
       * The HTTP method we will use in the request.
       */
      method: 'GET',

      /**
       * The endpoint of the service we will be making the request to.
       *
       * The endpoint is instantiated from a URL string, of the format: `{scheme}://{hostname}[:{port}]`
       */
      endpoint: new Endpoint('https://s3.us-east-1.amazonaws.com'),

      /**
       * The path of the request.
       */
      path: '/my-bucket/bonjour.txt',

      /**
       * The query parameters to include in the request.
       */
      query: {
        'abc': '123',
        'easy as': ['do', 're', 'mi'],
      },

      /**
       * The headers we will be sending in the request.
       */
      headers: {},

      /**
       * The body of the request.
       */
      body: null,
    },

    /**
     * (optional) Signature operation options allows to override the
     * SignatureV4's options in the context of this specific request.
     */
    {
      /**
       * The date and time to be used as signature metadata. This value should be
       * a Date object, a unix (epoch) timestamp, or a string that can be
       * understood by the JavaScript `Date` constructor.If not supplied, the
       * value returned by `new Date()` will be used.
       */
      signingDate: new Date(),

      /**
       * The service signing name. It will override the service name of the signer
       * in current invocation
       */
      signingService: 's3',

      /**
       * The region name to sign the request. It will override the signing region of the
       * signer in current invocation
       */
      signingRegion: 'us-east-1',
    }
  );

  http.get(signedRequest.url, { headers: signedRequest.headers });
}