From 09af4ec7bdd5cd88b1483a76065ccca4f955f75d Mon Sep 17 00:00:00 2001 From: oleiade Date: Tue, 6 Jun 2023 10:28:31 +0200 Subject: [PATCH] Fix signature default signing options handling --- examples/signature-sign.js | 2 +- src/internal/signature.ts | 44 ++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/examples/signature-sign.js b/examples/signature-sign.js index 27da81f..f4e734f 100644 --- a/examples/signature-sign.js +++ b/examples/signature-sign.js @@ -1,4 +1,4 @@ -import http from 'k6/http.js' +import http from 'k6/http' import { AWSConfig, SignatureV4 } from '../build/aws.js' diff --git a/src/internal/signature.ts b/src/internal/signature.ts index 469254b..e48ed99 100644 --- a/src/internal/signature.ts +++ b/src/internal/signature.ts @@ -75,22 +75,23 @@ export class SignatureV4 { * This method mutates the request object. * * @param request {HTTPRequest} The request to sign. - * @param param1 {SignOptions} Options for signing the request. + * @param options {Partial} Options for signing the request. * @returns {SignedHTTPRequest} The signed request. */ - sign( - request: HTTPRequest, - { - signingDate = new Date(), - signingService, - signingRegion, - unsignableHeaders = new Set(), - signableHeaders = new Set(), - }: RequestSigningOptions - ): SignedHTTPRequest { - const { longDate, shortDate }: DateInfo = formatDate(signingDate) - const service = signingService || this.service - const region = signingRegion || this.region + sign(request: HTTPRequest, options: Partial = {}): SignedHTTPRequest { + // Set default values for options which are not provided by the user. + const defaultOptions = { + signingDate: new Date(), + unsignableHeaders: new Set(), + signableHeaders: new Set(), + } + + // Merge default options with the ones maybe provided by the user. + const finalOptions = { ...defaultOptions, ...options } + + const { longDate, shortDate }: DateInfo = formatDate(finalOptions.signingDate) + const service = finalOptions.signingService || this.service + const region = finalOptions.signingRegion || this.region const scope = `${shortDate}/${region}/${service}/${constants.KEY_TYPE_IDENTIFIER}` // Required by the specification: @@ -126,17 +127,24 @@ export class SignatureV4 { } let payloadHash = this.computePayloadHash(request) - if (!hasHeader(constants.AMZ_CONTENT_SHA256_HEADER, request.headers) && this.applyChecksum) { + if ( + !hasHeader(constants.AMZ_CONTENT_SHA256_HEADER, request.headers) && + this.applyChecksum + ) { request.headers[constants.AMZ_CONTENT_SHA256_HEADER] = payloadHash } - const canonicalHeaders = this.computeCanonicalHeaders(request, unsignableHeaders, signableHeaders); + const canonicalHeaders = this.computeCanonicalHeaders( + request, + finalOptions.unsignableHeaders, + finalOptions.signableHeaders + ) const signature = this.calculateSignature( longDate, scope, this.deriveSigningKey(this.credentials, service, region, shortDate), - this.createCanonicalRequest(request, canonicalHeaders, payloadHash), - ); + this.createCanonicalRequest(request, canonicalHeaders, payloadHash) + ) /** * Step 4 of the signing process: add the signature to the HTTP request's headers.