@aws-lambda-powertools/signer 2.36.0 → 2.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/SigV4Signer.d.ts +11 -0
- package/lib/cjs/SigV4Signer.d.ts.map +1 -1
- package/lib/cjs/SigV4Signer.js +26 -104
- package/lib/cjs/SigV4aSigner.d.ts +34 -0
- package/lib/cjs/SigV4aSigner.d.ts.map +1 -0
- package/lib/cjs/SigV4aSigner.js +73 -0
- package/lib/cjs/constants.d.ts +8 -0
- package/lib/cjs/constants.d.ts.map +1 -0
- package/lib/cjs/constants.js +10 -0
- package/lib/cjs/errors.d.ts +3 -2
- package/lib/cjs/errors.d.ts.map +1 -1
- package/lib/cjs/errors.js +3 -2
- package/lib/cjs/helpers.d.ts +40 -0
- package/lib/cjs/helpers.d.ts.map +1 -0
- package/lib/cjs/helpers.js +145 -0
- package/lib/cjs/sigv4a.d.ts +3 -0
- package/lib/cjs/sigv4a.d.ts.map +1 -0
- package/lib/cjs/sigv4a.js +5 -0
- package/lib/cjs/types/index.d.ts +28 -11
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/esm/SigV4Signer.d.ts +11 -0
- package/lib/esm/SigV4Signer.d.ts.map +1 -1
- package/lib/esm/SigV4Signer.js +25 -103
- package/lib/esm/SigV4aSigner.d.ts +34 -0
- package/lib/esm/SigV4aSigner.d.ts.map +1 -0
- package/lib/esm/SigV4aSigner.js +70 -0
- package/lib/esm/constants.d.ts +8 -0
- package/lib/esm/constants.d.ts.map +1 -0
- package/lib/esm/constants.js +7 -0
- package/lib/esm/errors.d.ts +3 -2
- package/lib/esm/errors.d.ts.map +1 -1
- package/lib/esm/errors.js +3 -2
- package/lib/esm/helpers.d.ts +40 -0
- package/lib/esm/helpers.d.ts.map +1 -0
- package/lib/esm/helpers.js +138 -0
- package/lib/esm/sigv4a.d.ts +3 -0
- package/lib/esm/sigv4a.d.ts.map +1 -0
- package/lib/esm/sigv4a.js +1 -0
- package/lib/esm/types/index.d.ts +28 -11
- package/lib/esm/types/index.d.ts.map +1 -1
- package/package.json +21 -3
package/lib/cjs/SigV4Signer.d.ts
CHANGED
|
@@ -17,7 +17,18 @@ import type { Signer, SigV4SignerOptions } from './types/index.js';
|
|
|
17
17
|
*/
|
|
18
18
|
declare class SigV4Signer implements Signer {
|
|
19
19
|
#private;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a signer for the specified service and AWS region.
|
|
22
|
+
*
|
|
23
|
+
* @param options - The service and optional region and credentials.
|
|
24
|
+
*/
|
|
20
25
|
constructor(options: SigV4SignerOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Signs a request and preserves its fetch options in the returned request.
|
|
28
|
+
*
|
|
29
|
+
* @param input - The URL string, URL, or request to sign.
|
|
30
|
+
* @param init - Optional request options matching the `fetch` init argument.
|
|
31
|
+
*/
|
|
21
32
|
sign(input: string | URL | Request, init?: RequestInit): Promise<Request>;
|
|
22
33
|
}
|
|
23
34
|
export { SigV4Signer };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SigV4Signer.d.ts","sourceRoot":"","sources":["../../src/SigV4Signer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SigV4Signer.d.ts","sourceRoot":"","sources":["../../src/SigV4Signer.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEnE;;;;;;;;;;;;;;;GAeG;AACH,cAAM,WAAY,YAAW,MAAM;;IAKjC;;;;OAIG;gBACgB,OAAO,EAAE,kBAAkB;IAgB9C;;;;;OAKG;IACU,IAAI,CACf,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,CAAC;CA0BpB;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
package/lib/cjs/SigV4Signer.js
CHANGED
|
@@ -1,28 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SigV4Signer = void 0;
|
|
4
|
+
const env_1 = require("@aws-lambda-powertools/commons/utils/env");
|
|
4
5
|
const signature_v4_1 = require("@smithy/signature-v4");
|
|
6
|
+
const constants_js_1 = require("./constants.js");
|
|
5
7
|
const errors_js_1 = require("./errors.js");
|
|
8
|
+
const helpers_js_1 = require("./helpers.js");
|
|
6
9
|
const Sha256_js_1 = require("./Sha256.js");
|
|
7
|
-
/**
|
|
8
|
-
* Credential provider that reads AWS credentials from the standard environment
|
|
9
|
-
* variables that the AWS Lambda runtime always injects.
|
|
10
|
-
*
|
|
11
|
-
* Throws a {@link SignerConfigError | `SignerConfigError`} when the required
|
|
12
|
-
* variables are not present, e.g. when running outside of Lambda.
|
|
13
|
-
*/
|
|
14
|
-
const credentialsFromEnv = async () => {
|
|
15
|
-
const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
|
|
16
|
-
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
|
|
17
|
-
if (accessKeyId === undefined || secretAccessKey === undefined) {
|
|
18
|
-
throw new errors_js_1.SignerConfigError('Unable to resolve AWS credentials to sign the request with. Ensure the standard AWS credentials environment variables are set, or pass the `credentials` option.');
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
accessKeyId,
|
|
22
|
-
secretAccessKey,
|
|
23
|
-
sessionToken: process.env.AWS_SESSION_TOKEN,
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
10
|
/**
|
|
27
11
|
* A {@link Signer | `Signer`} that signs requests using the AWS Signature
|
|
28
12
|
* Version 4 (SigV4) signing process.
|
|
@@ -43,112 +27,50 @@ class SigV4Signer {
|
|
|
43
27
|
#service;
|
|
44
28
|
#region;
|
|
45
29
|
#credentials;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a signer for the specified service and AWS region.
|
|
32
|
+
*
|
|
33
|
+
* @param options - The service and optional region and credentials.
|
|
34
|
+
*/
|
|
46
35
|
constructor(options) {
|
|
47
36
|
this.#service = options.service;
|
|
48
|
-
const region = options.region ??
|
|
49
|
-
|
|
37
|
+
const region = options.region ??
|
|
38
|
+
(0, env_1.getStringFromEnv)({ key: 'AWS_REGION', defaultValue: '' });
|
|
39
|
+
if (region === '') {
|
|
50
40
|
throw new errors_js_1.SignerConfigError('Unable to determine the AWS region to sign the request with. Set the `region` option or the `AWS_REGION` environment variable.');
|
|
51
41
|
}
|
|
52
42
|
this.#region = region;
|
|
53
|
-
this.#credentials = options.credentials ?? credentialsFromEnv;
|
|
43
|
+
this.#credentials = options.credentials ?? helpers_js_1.credentialsFromEnv;
|
|
54
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Signs a request and preserves its fetch options in the returned request.
|
|
47
|
+
*
|
|
48
|
+
* @param input - The URL string, URL, or request to sign.
|
|
49
|
+
* @param init - Optional request options matching the `fetch` init argument.
|
|
50
|
+
*/
|
|
55
51
|
async sign(input, init) {
|
|
56
|
-
const request =
|
|
57
|
-
const credentials = await
|
|
58
|
-
|
|
52
|
+
const request = (0, helpers_js_1.toSigningRequest)(input, init);
|
|
53
|
+
const [credentials, httpRequest] = await Promise.all([
|
|
54
|
+
(0, helpers_js_1.resolveCredentials)(this.#credentials),
|
|
55
|
+
(0, helpers_js_1.toHttpRequest)(request, this.#service),
|
|
56
|
+
]);
|
|
59
57
|
const signer = new signature_v4_1.SignatureV4({
|
|
60
58
|
service: this.#service,
|
|
61
59
|
region: this.#region,
|
|
62
60
|
credentials,
|
|
63
61
|
sha256: Sha256_js_1.Sha256,
|
|
62
|
+
uriEscapePath: this.#service !== constants_js_1.SERVICE_S3,
|
|
64
63
|
});
|
|
65
64
|
let signed;
|
|
66
65
|
try {
|
|
67
|
-
signed =
|
|
66
|
+
signed = await signer.sign(httpRequest);
|
|
68
67
|
}
|
|
69
68
|
catch (error) {
|
|
70
69
|
throw new errors_js_1.RequestSigningError('Failed to sign the request', {
|
|
71
70
|
cause: error,
|
|
72
71
|
});
|
|
73
72
|
}
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Resolve the configured credentials, supporting both static values and
|
|
78
|
-
* async providers. The default env-var provider throws a configuration error
|
|
79
|
-
* when no credentials are available.
|
|
80
|
-
*/
|
|
81
|
-
async #resolveCredentials() {
|
|
82
|
-
return typeof this.#credentials === 'function'
|
|
83
|
-
? await this.#credentials()
|
|
84
|
-
: this.#credentials;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Adapt a web-standard `Request` into the `HttpRequest` shape expected by the
|
|
88
|
-
* underlying signing process, buffering the body so it can be hashed.
|
|
89
|
-
*
|
|
90
|
-
* The signing process accepts a plain object matching the `HttpRequest`
|
|
91
|
-
* interface, so we avoid depending on `@smithy/protocol-http` just to
|
|
92
|
-
* construct one.
|
|
93
|
-
*/
|
|
94
|
-
async #toHttpRequest(request) {
|
|
95
|
-
const url = new URL(request.url);
|
|
96
|
-
const headers = {};
|
|
97
|
-
for (const [key, value] of request.headers) {
|
|
98
|
-
headers[key] = value;
|
|
99
|
-
}
|
|
100
|
-
headers.host = url.host;
|
|
101
|
-
const query = {};
|
|
102
|
-
for (const [key, value] of url.searchParams) {
|
|
103
|
-
query[key] = value;
|
|
104
|
-
}
|
|
105
|
-
const body = await this.#readBody(request);
|
|
106
|
-
return {
|
|
107
|
-
method: request.method,
|
|
108
|
-
protocol: url.protocol,
|
|
109
|
-
hostname: url.hostname,
|
|
110
|
-
port: url.port ? Number(url.port) : undefined,
|
|
111
|
-
path: url.pathname,
|
|
112
|
-
query,
|
|
113
|
-
headers,
|
|
114
|
-
body,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Buffer the request body so it can be both hashed for signing and replayed
|
|
119
|
-
* when the signed request is sent.
|
|
120
|
-
*
|
|
121
|
-
* Most bodies (strings, buffers, and finite streams) are buffered
|
|
122
|
-
* transparently. A body that cannot be read or replayed — for example a
|
|
123
|
-
* stream that errors mid-read — cannot be signed and results in a
|
|
124
|
-
* {@link RequestSigningError | `RequestSigningError`}.
|
|
125
|
-
*/
|
|
126
|
-
async #readBody(request) {
|
|
127
|
-
if (request.body === null || request.body === undefined) {
|
|
128
|
-
return undefined;
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
const buffer = await request.clone().arrayBuffer();
|
|
132
|
-
return buffer.byteLength === 0 ? undefined : new Uint8Array(buffer);
|
|
133
|
-
}
|
|
134
|
-
catch (error) {
|
|
135
|
-
throw new errors_js_1.RequestSigningError('Unable to read the request body to sign it. The body could not be buffered, for example because it is a stream that cannot be replayed.', { cause: error });
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Adapt a signed `HttpRequest` back into a web-standard `Request`, carrying
|
|
140
|
-
* over the signed headers and the original (buffered) body.
|
|
141
|
-
*/
|
|
142
|
-
#toRequest(signed, original) {
|
|
143
|
-
const headers = new Headers();
|
|
144
|
-
for (const [key, value] of Object.entries(signed.headers)) {
|
|
145
|
-
headers.set(key, value);
|
|
146
|
-
}
|
|
147
|
-
return new Request(original.url, {
|
|
148
|
-
method: signed.method,
|
|
149
|
-
headers,
|
|
150
|
-
body: signed.body,
|
|
151
|
-
});
|
|
73
|
+
return (0, helpers_js_1.toRequest)(signed, request);
|
|
152
74
|
}
|
|
153
75
|
}
|
|
154
76
|
exports.SigV4Signer = SigV4Signer;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Signer, SigV4aSignerOptions } from './types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Signs web-standard requests using AWS Signature Version 4A (SigV4a).
|
|
4
|
+
*
|
|
5
|
+
* SigV4a uses ECDSA signatures valid in a set of AWS regions, for services
|
|
6
|
+
* such as Amazon S3 Multi-Region Access Points. The signer performs no network
|
|
7
|
+
* I/O and works with `createSignedFetcher` from the `/fetch` entry point.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { SigV4aSigner } from '@aws-lambda-powertools/signer/sigv4a';
|
|
12
|
+
*
|
|
13
|
+
* const signer = new SigV4aSigner({ service: 's3', regionSet: ['*'] });
|
|
14
|
+
* const signed = await signer.sign('https://example.accesspoint.s3-global.amazonaws.com/object');
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
declare class SigV4aSigner implements Signer {
|
|
18
|
+
#private;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a signer for the specified service and region set.
|
|
21
|
+
*
|
|
22
|
+
* @param options - The service, region set, and optional credentials.
|
|
23
|
+
*/
|
|
24
|
+
constructor(options: SigV4aSignerOptions);
|
|
25
|
+
/**
|
|
26
|
+
* Signs a request and returns a new request with the SigV4a headers.
|
|
27
|
+
*
|
|
28
|
+
* @param input - The URL string, URL, or request to sign.
|
|
29
|
+
* @param init - Optional request options matching the `fetch` init argument.
|
|
30
|
+
*/
|
|
31
|
+
sign(input: string | URL | Request, init?: RequestInit): Promise<Request>;
|
|
32
|
+
}
|
|
33
|
+
export { SigV4aSigner };
|
|
34
|
+
//# sourceMappingURL=SigV4aSigner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SigV4aSigner.d.ts","sourceRoot":"","sources":["../../src/SigV4aSigner.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEpE;;;;;;;;;;;;;;GAcG;AACH,cAAM,YAAa,YAAW,MAAM;;IAKlC;;;;OAIG;gBACgB,OAAO,EAAE,mBAAmB;IAiB/C;;;;;OAKG;IACU,IAAI,CACf,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,CAAC;CAyBpB;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SigV4aSigner = void 0;
|
|
4
|
+
const signature_v4a_1 = require("@smithy/signature-v4a");
|
|
5
|
+
const constants_js_1 = require("./constants.js");
|
|
6
|
+
const errors_js_1 = require("./errors.js");
|
|
7
|
+
const helpers_js_1 = require("./helpers.js");
|
|
8
|
+
const Sha256_js_1 = require("./Sha256.js");
|
|
9
|
+
/**
|
|
10
|
+
* Signs web-standard requests using AWS Signature Version 4A (SigV4a).
|
|
11
|
+
*
|
|
12
|
+
* SigV4a uses ECDSA signatures valid in a set of AWS regions, for services
|
|
13
|
+
* such as Amazon S3 Multi-Region Access Points. The signer performs no network
|
|
14
|
+
* I/O and works with `createSignedFetcher` from the `/fetch` entry point.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { SigV4aSigner } from '@aws-lambda-powertools/signer/sigv4a';
|
|
19
|
+
*
|
|
20
|
+
* const signer = new SigV4aSigner({ service: 's3', regionSet: ['*'] });
|
|
21
|
+
* const signed = await signer.sign('https://example.accesspoint.s3-global.amazonaws.com/object');
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
class SigV4aSigner {
|
|
25
|
+
#service;
|
|
26
|
+
#regionSet;
|
|
27
|
+
#credentials;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a signer for the specified service and region set.
|
|
30
|
+
*
|
|
31
|
+
* @param options - The service, region set, and optional credentials.
|
|
32
|
+
*/
|
|
33
|
+
constructor(options) {
|
|
34
|
+
if (options.regionSet.length === 0 ||
|
|
35
|
+
options.regionSet.some((region) => !/^[a-z0-9*]+(-[a-z0-9*]+)*$/.test(region))) {
|
|
36
|
+
throw new errors_js_1.SignerConfigError('Set `regionSet` to a non-empty array of AWS regions or wildcard patterns, for example ["us-east-1", "us-west-2"] or ["*"].');
|
|
37
|
+
}
|
|
38
|
+
this.#service = options.service;
|
|
39
|
+
this.#regionSet = options.regionSet.join(',');
|
|
40
|
+
this.#credentials = options.credentials ?? helpers_js_1.credentialsFromEnv;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Signs a request and returns a new request with the SigV4a headers.
|
|
44
|
+
*
|
|
45
|
+
* @param input - The URL string, URL, or request to sign.
|
|
46
|
+
* @param init - Optional request options matching the `fetch` init argument.
|
|
47
|
+
*/
|
|
48
|
+
async sign(input, init) {
|
|
49
|
+
const request = (0, helpers_js_1.toSigningRequest)(input, init);
|
|
50
|
+
const [credentials, httpRequest] = await Promise.all([
|
|
51
|
+
(0, helpers_js_1.resolveCredentials)(this.#credentials),
|
|
52
|
+
(0, helpers_js_1.toHttpRequest)(request, this.#service),
|
|
53
|
+
]);
|
|
54
|
+
const signer = new signature_v4a_1.SignatureV4a({
|
|
55
|
+
service: this.#service,
|
|
56
|
+
region: this.#regionSet,
|
|
57
|
+
credentials,
|
|
58
|
+
sha256: Sha256_js_1.Sha256,
|
|
59
|
+
uriEscapePath: this.#service !== constants_js_1.SERVICE_S3,
|
|
60
|
+
});
|
|
61
|
+
let signed;
|
|
62
|
+
try {
|
|
63
|
+
signed = await signer.sign(httpRequest, {});
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new errors_js_1.RequestSigningError('Failed to sign the request', {
|
|
67
|
+
cause: error,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return (0, helpers_js_1.toRequest)(signed, request);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.SigV4aSigner = SigV4aSigner;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,QAAA,MAAM,UAAU,OAAO,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
package/lib/cjs/errors.d.ts
CHANGED
|
@@ -11,8 +11,9 @@ declare class SignerError extends Error {
|
|
|
11
11
|
/**
|
|
12
12
|
* Error thrown when the signer is misconfigured.
|
|
13
13
|
*
|
|
14
|
-
* This is thrown eagerly at construction when the region cannot be determined
|
|
15
|
-
*
|
|
14
|
+
* This is thrown eagerly at construction when the region cannot be determined
|
|
15
|
+
* or the SigV4a region set is invalid, and lazily during signing when the
|
|
16
|
+
* required AWS credentials environment variables are missing.
|
|
16
17
|
*/
|
|
17
18
|
declare class SignerConfigError extends SignerError {
|
|
18
19
|
constructor(message: string, options?: ErrorOptions);
|
package/lib/cjs/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAM,WAAY,SAAQ,KAAK;gBACV,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAI3D;AAED
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAM,WAAY,SAAQ,KAAK;gBACV,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAI3D;AAED;;;;;;GAMG;AACH,cAAM,iBAAkB,SAAQ,WAAW;gBACtB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAI3D;AAED;;;;;GAKG;AACH,cAAM,mBAAoB,SAAQ,WAAW;gBACxB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAQ3D;AAED,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,WAAW,EAAE,CAAC"}
|
package/lib/cjs/errors.js
CHANGED
|
@@ -18,8 +18,9 @@ exports.SignerError = SignerError;
|
|
|
18
18
|
/**
|
|
19
19
|
* Error thrown when the signer is misconfigured.
|
|
20
20
|
*
|
|
21
|
-
* This is thrown eagerly at construction when the region cannot be determined
|
|
22
|
-
*
|
|
21
|
+
* This is thrown eagerly at construction when the region cannot be determined
|
|
22
|
+
* or the SigV4a region set is invalid, and lazily during signing when the
|
|
23
|
+
* required AWS credentials environment variables are missing.
|
|
23
24
|
*/
|
|
24
25
|
class SignerConfigError extends SignerError {
|
|
25
26
|
constructor(message, options) {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { AwsCredentialIdentity, AwsCredentialIdentityProvider, HttpRequest } from '@smithy/types';
|
|
2
|
+
/**
|
|
3
|
+
* Reads credentials from the standard AWS Lambda environment variables.
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
declare const credentialsFromEnv: AwsCredentialIdentityProvider;
|
|
8
|
+
/**
|
|
9
|
+
* Resolves static credentials or invokes the configured credential provider.
|
|
10
|
+
*
|
|
11
|
+
* @param credentials - The configured credentials or provider.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
declare const resolveCredentials: (credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider) => Promise<AwsCredentialIdentity>;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a request and preserves its referrer settings across init overrides.
|
|
17
|
+
*
|
|
18
|
+
* @param input - The URL or request to sign.
|
|
19
|
+
* @param init - The request options to apply.
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
declare const toSigningRequest: (input: string | URL | Request, init?: RequestInit) => Request;
|
|
23
|
+
/**
|
|
24
|
+
* Adapts a web-standard request into the structural Smithy HTTP request shape.
|
|
25
|
+
*
|
|
26
|
+
* @param request - The request to adapt, buffering its body for signing.
|
|
27
|
+
* @param service - The signing service that determines canonical-path rules.
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
declare const toHttpRequest: (request: Request, service: string) => Promise<HttpRequest>;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a web-standard request with the signed headers and buffered body.
|
|
33
|
+
*
|
|
34
|
+
* @param signed - The signed Smithy HTTP request.
|
|
35
|
+
* @param original - The original request supplying the URL.
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
declare const toRequest: (signed: HttpRequest, original: Request) => Request;
|
|
39
|
+
export { credentialsFromEnv, resolveCredentials, toHttpRequest, toRequest, toSigningRequest, };
|
|
40
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,qBAAqB,EACrB,6BAA6B,EAC7B,WAAW,EACZ,MAAM,eAAe,CAAC;AA4BvB;;;;GAIG;AACH,QAAA,MAAM,kBAAkB,EAAE,6BAsBzB,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,kBAAkB,GACtB,aAAa,qBAAqB,GAAG,6BAA6B,KACjE,OAAO,CAAC,qBAAqB,CACuC,CAAC;AAExE;;;;;;GAMG;AACH,QAAA,MAAM,gBAAgB,GACpB,OAAO,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,OAAO,WAAW,KACjB,OAUA,CAAC;AAwBJ;;;;;;GAMG;AACH,QAAA,MAAM,aAAa,GACjB,SAAS,OAAO,EAChB,SAAS,MAAM,KACd,OAAO,CAAC,WAAW,CAyBrB,CAAC;AAEF;;;;;;GAMG;AACH,QAAA,MAAM,SAAS,GAAI,QAAQ,WAAW,EAAE,UAAU,OAAO,KAAG,OAe3D,CAAC;AAEF,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,SAAS,EACT,gBAAgB,GACjB,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toSigningRequest = exports.toRequest = exports.toHttpRequest = exports.resolveCredentials = exports.credentialsFromEnv = void 0;
|
|
4
|
+
const env_1 = require("@aws-lambda-powertools/commons/utils/env");
|
|
5
|
+
const constants_js_1 = require("./constants.js");
|
|
6
|
+
const errors_js_1 = require("./errors.js");
|
|
7
|
+
/**
|
|
8
|
+
* Builds the canonical S3 URI for an already parsed request URL.
|
|
9
|
+
*
|
|
10
|
+
* S3 treats raw plus signs as spaces and decodes escaped slashes in object
|
|
11
|
+
* keys. Preserve other escapes without double-encoding their percent signs.
|
|
12
|
+
*
|
|
13
|
+
* @param path - The URL pathname as it will be sent to S3.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
const canonicalS3Path = (path) => encodeURIComponent(path.replace(/\+/g, ' '))
|
|
17
|
+
.replace(/[!'()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`)
|
|
18
|
+
.replace(/%2F/g, '/')
|
|
19
|
+
.replace(/%25([a-f0-9]{2})/gi, (_match, byte) => {
|
|
20
|
+
const character = String.fromCharCode(Number.parseInt(byte, 16));
|
|
21
|
+
return /^[a-z0-9._~/-]$/i.test(character)
|
|
22
|
+
? character
|
|
23
|
+
: `%${byte.toUpperCase()}`;
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Reads credentials from the standard AWS Lambda environment variables.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
const credentialsFromEnv = async () => {
|
|
31
|
+
const accessKeyId = (0, env_1.getStringFromEnv)({
|
|
32
|
+
key: 'AWS_ACCESS_KEY_ID',
|
|
33
|
+
defaultValue: '',
|
|
34
|
+
});
|
|
35
|
+
const secretAccessKey = (0, env_1.getStringFromEnv)({
|
|
36
|
+
key: 'AWS_SECRET_ACCESS_KEY',
|
|
37
|
+
defaultValue: '',
|
|
38
|
+
});
|
|
39
|
+
if (accessKeyId === '' || secretAccessKey === '') {
|
|
40
|
+
throw new errors_js_1.SignerConfigError('Unable to resolve AWS credentials to sign the request with. Ensure the standard AWS credentials environment variables are set, or pass the `credentials` option.');
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
accessKeyId,
|
|
44
|
+
secretAccessKey,
|
|
45
|
+
sessionToken: (0, env_1.getStringFromEnv)({ key: 'AWS_SESSION_TOKEN', defaultValue: '' }) ||
|
|
46
|
+
undefined,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
exports.credentialsFromEnv = credentialsFromEnv;
|
|
50
|
+
/**
|
|
51
|
+
* Resolves static credentials or invokes the configured credential provider.
|
|
52
|
+
*
|
|
53
|
+
* @param credentials - The configured credentials or provider.
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
const resolveCredentials = async (credentials) => typeof credentials === 'function' ? await credentials() : credentials;
|
|
57
|
+
exports.resolveCredentials = resolveCredentials;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a request and preserves its referrer settings across init overrides.
|
|
60
|
+
*
|
|
61
|
+
* @param input - The URL or request to sign.
|
|
62
|
+
* @param init - The request options to apply.
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
const toSigningRequest = (input, init) => new Request(input, input instanceof Request
|
|
66
|
+
? {
|
|
67
|
+
...init,
|
|
68
|
+
referrer: init?.referrer ?? input.referrer,
|
|
69
|
+
referrerPolicy: init?.referrerPolicy ?? input.referrerPolicy,
|
|
70
|
+
}
|
|
71
|
+
: init);
|
|
72
|
+
exports.toSigningRequest = toSigningRequest;
|
|
73
|
+
/**
|
|
74
|
+
* Buffers the request body so it can be hashed and replayed after signing.
|
|
75
|
+
*
|
|
76
|
+
* @param request - The request whose body to read.
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
const readBody = async (request) => {
|
|
80
|
+
if (request.body === null) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const buffer = await request.clone().arrayBuffer();
|
|
85
|
+
return buffer.byteLength === 0 ? undefined : new Uint8Array(buffer);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw new errors_js_1.RequestSigningError('Unable to read the request body to sign it. The body could not be buffered, for example because it is a stream that cannot be replayed.', { cause: error });
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Adapts a web-standard request into the structural Smithy HTTP request shape.
|
|
93
|
+
*
|
|
94
|
+
* @param request - The request to adapt, buffering its body for signing.
|
|
95
|
+
* @param service - The signing service that determines canonical-path rules.
|
|
96
|
+
* @internal
|
|
97
|
+
*/
|
|
98
|
+
const toHttpRequest = async (request, service) => {
|
|
99
|
+
const url = new URL(request.url);
|
|
100
|
+
const headers = {};
|
|
101
|
+
for (const [key, value] of request.headers) {
|
|
102
|
+
headers[key] = value;
|
|
103
|
+
}
|
|
104
|
+
headers.host = url.host;
|
|
105
|
+
// A repeated query parameter keeps all its values, as SigV4 signs each one.
|
|
106
|
+
const query = {};
|
|
107
|
+
for (const [key, value] of url.searchParams) {
|
|
108
|
+
query[key] ??= [];
|
|
109
|
+
query[key].push(value);
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
method: request.method,
|
|
113
|
+
protocol: url.protocol,
|
|
114
|
+
hostname: url.hostname,
|
|
115
|
+
port: url.port ? Number(url.port) : undefined,
|
|
116
|
+
path: service === constants_js_1.SERVICE_S3 ? canonicalS3Path(url.pathname) : url.pathname,
|
|
117
|
+
query,
|
|
118
|
+
headers,
|
|
119
|
+
body: await readBody(request),
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
exports.toHttpRequest = toHttpRequest;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a web-standard request with the signed headers and buffered body.
|
|
125
|
+
*
|
|
126
|
+
* @param signed - The signed Smithy HTTP request.
|
|
127
|
+
* @param original - The original request supplying the URL.
|
|
128
|
+
* @internal
|
|
129
|
+
*/
|
|
130
|
+
const toRequest = (signed, original) => {
|
|
131
|
+
const headers = new Headers();
|
|
132
|
+
for (const [key, value] of Object.entries(signed.headers)) {
|
|
133
|
+
headers.set(key, value);
|
|
134
|
+
}
|
|
135
|
+
return new Request(original, {
|
|
136
|
+
method: signed.method,
|
|
137
|
+
headers,
|
|
138
|
+
// Request resets referrer settings when init contains overrides.
|
|
139
|
+
referrer: original.referrer,
|
|
140
|
+
referrerPolicy: original.referrerPolicy,
|
|
141
|
+
// Smithy's body type is broader than fetch's; our adapter supplies only bytes.
|
|
142
|
+
body: signed.body,
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
exports.toRequest = toRequest;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sigv4a.d.ts","sourceRoot":"","sources":["../../src/sigv4a.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SigV4aSigner = void 0;
|
|
4
|
+
var SigV4aSigner_js_1 = require("./SigV4aSigner.js");
|
|
5
|
+
Object.defineProperty(exports, "SigV4aSigner", { enumerable: true, get: function () { return SigV4aSigner_js_1.SigV4aSigner; } });
|
package/lib/cjs/types/index.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ import type { AwsCredentialIdentity, AwsCredentialIdentityProvider } from '@smit
|
|
|
2
2
|
/**
|
|
3
3
|
* A request signer.
|
|
4
4
|
*
|
|
5
|
-
* This is the structural contract that every signing variant (
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* This is the structural contract that every signing variant (SigV4 and
|
|
6
|
+
* SigV4a) implements, and the contract that the `createSignedFetcher` factory
|
|
7
|
+
* depends on.
|
|
8
8
|
*
|
|
9
9
|
* Implementations take a web-standard request and return a signed
|
|
10
10
|
* web-standard {@link Request | `Request`}, performing no network I/O.
|
|
@@ -19,10 +19,9 @@ interface Signer {
|
|
|
19
19
|
sign(input: string | URL | Request, init?: RequestInit): Promise<Request>;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
* Signature Version 4 signing process.
|
|
22
|
+
* Configures the service and credentials shared by AWS request signers.
|
|
24
23
|
*/
|
|
25
|
-
interface
|
|
24
|
+
interface AwsSignerOptions {
|
|
26
25
|
/**
|
|
27
26
|
* The service name to use when signing the request, e.g. `execute-api`,
|
|
28
27
|
* `lambda`, or `appsync`.
|
|
@@ -31,19 +30,37 @@ interface SigV4SignerOptions {
|
|
|
31
30
|
* CloudFront hide the underlying service), so it is required.
|
|
32
31
|
*/
|
|
33
32
|
service: string;
|
|
33
|
+
/**
|
|
34
|
+
* The credentials to use when signing the request, either as a static
|
|
35
|
+
* value or as a provider that resolves them.
|
|
36
|
+
*
|
|
37
|
+
* @default credentials read from the standard Lambda environment variables
|
|
38
|
+
*/
|
|
39
|
+
credentials?: AwsCredentialIdentity | AwsCredentialIdentityProvider;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Configures a signer that uses AWS Signature Version 4.
|
|
43
|
+
*/
|
|
44
|
+
interface SigV4SignerOptions extends AwsSignerOptions {
|
|
34
45
|
/**
|
|
35
46
|
* The AWS region to use when signing the request.
|
|
36
47
|
*
|
|
37
48
|
* @default process.env.AWS_REGION
|
|
38
49
|
*/
|
|
39
50
|
region?: string;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Configures a signer that uses AWS Signature Version 4A.
|
|
54
|
+
*/
|
|
55
|
+
interface SigV4aSignerOptions extends AwsSignerOptions {
|
|
40
56
|
/**
|
|
41
|
-
* The
|
|
42
|
-
* value or as a provider that resolves them.
|
|
57
|
+
* The AWS regions in which the signature is valid.
|
|
43
58
|
*
|
|
44
|
-
*
|
|
59
|
+
* Provide at least one region or wildcard pattern, such as
|
|
60
|
+
* `['us-east-1', 'us-west-2']`, `['us-west-*']`, or `['*']` for all regions.
|
|
61
|
+
* This option is required and does not default to `AWS_REGION`.
|
|
45
62
|
*/
|
|
46
|
-
|
|
63
|
+
regionSet: readonly string[];
|
|
47
64
|
}
|
|
48
65
|
/**
|
|
49
66
|
* Options for the `createSignedFetcher` factory.
|
|
@@ -56,5 +73,5 @@ interface SignedFetcherOptions {
|
|
|
56
73
|
*/
|
|
57
74
|
fetch?: typeof fetch;
|
|
58
75
|
}
|
|
59
|
-
export type { SignedFetcherOptions, Signer, SigV4SignerOptions };
|
|
76
|
+
export type { AwsSignerOptions, SignedFetcherOptions, Signer, SigV4aSignerOptions, SigV4SignerOptions, };
|
|
60
77
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,6BAA6B,EAC9B,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;GASG;AACH,UAAU,MAAM;IACd;;;;;OAKG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3E;AAED
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,6BAA6B,EAC9B,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;GASG;AACH,UAAU,MAAM;IACd;;;;;OAKG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3E;AAED;;GAEG;AACH,UAAU,gBAAgB;IACxB;;;;;;OAMG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,qBAAqB,GAAG,6BAA6B,CAAC;CACrE;AAED;;GAEG;AACH,UAAU,kBAAmB,SAAQ,gBAAgB;IACnD;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,UAAU,mBAAoB,SAAQ,gBAAgB;IACpD;;;;;;OAMG;IACH,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CAC9B;AAED;;GAEG;AACH,UAAU,oBAAoB;IAC5B;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,YAAY,EACV,gBAAgB,EAChB,oBAAoB,EACpB,MAAM,EACN,mBAAmB,EACnB,kBAAkB,GACnB,CAAC"}
|