Skip to content

Commit

Permalink
Fix signing when providing an X-ray trace id header - fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jul 9, 2020
1 parent 05438d8 commit dd368ca
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import got from 'got';
import * as AWS from 'aws-sdk';
import * as aws4 from 'aws4';

// Name of the header used for X-ray tracing
const XRAY_TRACE_HEADER = 'x-amzn-trace-id';

export interface GotAWSOptions {
/**
* A provider or a list of providers used to search for AWS credentials. If no providers are provided,
Expand Down Expand Up @@ -53,6 +56,10 @@ const got4aws = (awsOptions: GotAWSOptions = {}) => {

const {url, headers} = options;

// Extract the Amazon trace id from the headers as it shouldn't be used for signing
const amazonTraceId = headers[XRAY_TRACE_HEADER];
delete headers[XRAY_TRACE_HEADER]; // eslint-disable-line @typescript-eslint/no-dynamic-delete

// Map the request to something that is signable by aws4
const request = {
protocol: url.protocol,
Expand All @@ -66,7 +73,11 @@ const got4aws = (awsOptions: GotAWSOptions = {}) => {

aws4.sign(request, credentials);

options.headers = request.headers;
options.headers = {
...request.headers,
// Put back the trace id if we have one
...(amazonTraceId ? {[XRAY_TRACE_HEADER]: amazonTraceId} : {})
};
}
]
}
Expand Down

0 comments on commit dd368ca

Please sign in to comment.