lambda-essentials-ts 5.3.2 → 6.0.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/CHANGELOG.md +14 -1
- package/lib/exceptions/clientException.d.ts +1 -1
- package/lib/exceptions/clientException.js +9 -5
- package/lib/httpClient/httpClient.d.ts +6 -0
- package/lib/httpClient/httpClient.js +3 -2
- package/lib/tokenProvider/kmsTokenProvider.d.ts +2 -2
- package/lib/tokenProvider/kmsTokenProvider.js +5 -5
- package/lib/tokenProvider/secretsManagerTokenProvider.d.ts +2 -2
- package/lib/tokenProvider/secretsManagerTokenProvider.js +4 -3
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,11 +4,24 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
|
|
7
|
+
## [6.0.0] - 2022-02-22
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- **[Breaking change]** Upgraded aws-sdk to v3 which has `SecretsManager` and `KMS` replaced by `SecretsManagerClient` and `KMSClient` class.
|
|
12
|
+
The functionality and interface remains the same, the imports need to be changed.
|
|
13
|
+
|
|
14
|
+
## [5.4.0] - 2024-02-08
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
HttpClient options now accept `clientExceptionStatusCodeMapOverride` which can be used to override the default HTTP error status code mapping. This is useful e.g. when a dependent service is not following REST-ful best practices and e.g. returns a 403 when there's an intermittent network error communicating with the authorization service
|
|
19
|
+
|
|
7
20
|
## [5.3.2] - 2024-02-08
|
|
8
21
|
|
|
9
22
|
### Fixed
|
|
10
23
|
|
|
11
|
-
Error details of external HTTP error
|
|
24
|
+
Error details of external HTTP error responses are propagated correctly
|
|
12
25
|
|
|
13
26
|
## [5.3.1] - 2023-10-25
|
|
14
27
|
|
|
@@ -3,6 +3,6 @@ export declare class ClientException extends Exception {
|
|
|
3
3
|
readonly serviceName: string;
|
|
4
4
|
readonly originalStatusCode?: number;
|
|
5
5
|
private static readonly statusCodeMap;
|
|
6
|
-
constructor(serviceName: string, originalStatusCode?: number, details?: any);
|
|
6
|
+
constructor(serviceName: string, originalStatusCode?: number, details?: any, statusCodeMapOverride?: Record<number, number>);
|
|
7
7
|
private static convertStatusCode;
|
|
8
8
|
}
|
|
@@ -3,15 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ClientException = void 0;
|
|
4
4
|
const exception_1 = require("./exception");
|
|
5
5
|
class ClientException extends exception_1.Exception {
|
|
6
|
-
constructor(serviceName, originalStatusCode, details) {
|
|
7
|
-
super(`Dependent service "${serviceName}" returned error`, ClientException.convertStatusCode(originalStatusCode), details);
|
|
6
|
+
constructor(serviceName, originalStatusCode, details, statusCodeMapOverride) {
|
|
7
|
+
super(`Dependent service "${serviceName}" returned error`, ClientException.convertStatusCode(originalStatusCode, statusCodeMapOverride), details);
|
|
8
8
|
this.serviceName = serviceName;
|
|
9
9
|
this.originalStatusCode = originalStatusCode;
|
|
10
10
|
}
|
|
11
|
-
static convertStatusCode(originalStatusCode) {
|
|
11
|
+
static convertStatusCode(originalStatusCode, statusCodeMapOverride) {
|
|
12
12
|
let statusCode = 503;
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
const statusCodeMap = {
|
|
14
|
+
...ClientException.statusCodeMap,
|
|
15
|
+
...(statusCodeMapOverride || {}),
|
|
16
|
+
};
|
|
17
|
+
if (originalStatusCode && statusCodeMap[originalStatusCode]) {
|
|
18
|
+
statusCode = statusCodeMap[originalStatusCode];
|
|
15
19
|
}
|
|
16
20
|
return statusCode;
|
|
17
21
|
}
|
|
@@ -17,6 +17,7 @@ export default class HttpClient {
|
|
|
17
17
|
private readonly enableRetry;
|
|
18
18
|
private readonly enableCache;
|
|
19
19
|
private readonly timeout?;
|
|
20
|
+
private readonly clientExceptionStatusCodeMapOverride?;
|
|
20
21
|
/**
|
|
21
22
|
* Create a new Instance of the HttpClient
|
|
22
23
|
*/
|
|
@@ -100,6 +101,11 @@ export interface HttpClientOptions {
|
|
|
100
101
|
* @link https://github.com/axios/axios/blob/main/README.md#request-config
|
|
101
102
|
*/
|
|
102
103
|
timeout?: number;
|
|
104
|
+
/**
|
|
105
|
+
* Override the default mapping of status code when wrapping error responses returned by dependencies into ClientException.
|
|
106
|
+
* This is useful, when dependent services return incorrect status codes than then drive incorrect behavior upstream (e.g. 403 instead of 503)
|
|
107
|
+
*/
|
|
108
|
+
clientExceptionStatusCodeMapOverride?: Record<number, number>;
|
|
103
109
|
}
|
|
104
110
|
/**
|
|
105
111
|
* Log options object.
|
|
@@ -62,6 +62,7 @@ class HttpClient {
|
|
|
62
62
|
this.enableCache = (_c = options === null || options === void 0 ? void 0 : options.enableCache) !== null && _c !== void 0 ? _c : false;
|
|
63
63
|
this.enableRetry = (_d = options === null || options === void 0 ? void 0 : options.enableRetry) !== null && _d !== void 0 ? _d : false;
|
|
64
64
|
this.timeout = options === null || options === void 0 ? void 0 : options.timeout;
|
|
65
|
+
this.clientExceptionStatusCodeMapOverride = options === null || options === void 0 ? void 0 : options.clientExceptionStatusCodeMapOverride;
|
|
65
66
|
this.client =
|
|
66
67
|
(_e = options === null || options === void 0 ? void 0 : options.client) !== null && _e !== void 0 ? _e : axios_1.default.create({
|
|
67
68
|
adapter: (() => {
|
|
@@ -129,7 +130,7 @@ class HttpClient {
|
|
|
129
130
|
const hostname = ((_a = error.config) === null || _a === void 0 ? void 0 : _a.url)
|
|
130
131
|
? new url_1.URL(error.config.url, error.config.baseURL).hostname
|
|
131
132
|
: 'N/A';
|
|
132
|
-
throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details);
|
|
133
|
+
throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details, this.clientExceptionStatusCodeMapOverride);
|
|
133
134
|
});
|
|
134
135
|
this.client.interceptors.response.use((response) => {
|
|
135
136
|
if (this.logOptions.enabledLogs.includes(HttpLogType.responses)) {
|
|
@@ -168,7 +169,7 @@ class HttpClient {
|
|
|
168
169
|
const hostname = ((_a = error.config) === null || _a === void 0 ? void 0 : _a.url)
|
|
169
170
|
? new url_1.URL(error.config.url, error.config.baseURL).hostname
|
|
170
171
|
: 'N/A';
|
|
171
|
-
throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details);
|
|
172
|
+
throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details, this.clientExceptionStatusCodeMapOverride);
|
|
172
173
|
});
|
|
173
174
|
}
|
|
174
175
|
static extractRequestLogData(requestConfig) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { KMS } from 'aws-sdk';
|
|
2
1
|
import TokenProvider, { Auth0Secret, TokenConfiguration, TokenProviderOptions } from './tokenProvider';
|
|
2
|
+
import { KMSClient } from '@aws-sdk/client-kms';
|
|
3
3
|
export default class KmsTokenProvider extends TokenProvider {
|
|
4
4
|
private kmsClient;
|
|
5
5
|
private kmsConfiguration;
|
|
@@ -10,7 +10,7 @@ export interface KmsTokenProviderOptions extends TokenProviderOptions {
|
|
|
10
10
|
/**
|
|
11
11
|
* AWS KMS Client
|
|
12
12
|
*/
|
|
13
|
-
kmsClient:
|
|
13
|
+
kmsClient: KMSClient;
|
|
14
14
|
/**
|
|
15
15
|
* Configuration needed for the token
|
|
16
16
|
*/
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const tokenProvider_1 = __importDefault(require("./tokenProvider"));
|
|
7
|
+
const client_kms_1 = require("@aws-sdk/client-kms");
|
|
7
8
|
class KmsTokenProvider extends tokenProvider_1.default {
|
|
8
9
|
constructor(options) {
|
|
9
10
|
super(options);
|
|
@@ -11,12 +12,11 @@ class KmsTokenProvider extends tokenProvider_1.default {
|
|
|
11
12
|
this.kmsConfiguration = options.tokenConfiguration;
|
|
12
13
|
}
|
|
13
14
|
async getClientSecret() {
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
var _a;
|
|
16
|
+
const data = await this.kmsClient.send(new client_kms_1.DecryptCommand({
|
|
16
17
|
CiphertextBlob: Buffer.from(this.kmsConfiguration.encryptedClientSecret, 'base64'),
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
.then((data) => { var _a; return (_a = data.Plaintext) === null || _a === void 0 ? void 0 : _a.toString(); });
|
|
18
|
+
}));
|
|
19
|
+
const secret = (_a = data.Plaintext) === null || _a === void 0 ? void 0 : _a.toString();
|
|
20
20
|
if (!secret) {
|
|
21
21
|
throw new Error('Request error: failed to decrypt secret using KMS');
|
|
22
22
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { SecretsManager } from 'aws-sdk';
|
|
2
1
|
import TokenProvider, { Auth0Secret, TokenConfiguration, TokenProviderOptions } from './tokenProvider';
|
|
2
|
+
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
|
|
3
3
|
export default class SecretsManagerTokenProvider extends TokenProvider {
|
|
4
4
|
private secretsManagerClient;
|
|
5
5
|
private secretsManagerConfiguration;
|
|
@@ -10,7 +10,7 @@ export interface SecretsManagerTokenProviderOptions extends TokenProviderOptions
|
|
|
10
10
|
/**
|
|
11
11
|
* AWS Secrets Manager Client
|
|
12
12
|
*/
|
|
13
|
-
secretsManagerClient:
|
|
13
|
+
secretsManagerClient: SecretsManagerClient;
|
|
14
14
|
/**
|
|
15
15
|
* Configuration needed for the token
|
|
16
16
|
*/
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const tokenProvider_1 = __importDefault(require("./tokenProvider"));
|
|
7
|
+
const client_secrets_manager_1 = require("@aws-sdk/client-secrets-manager");
|
|
7
8
|
class SecretsManagerTokenProvider extends tokenProvider_1.default {
|
|
8
9
|
constructor(options) {
|
|
9
10
|
super(options);
|
|
@@ -11,9 +12,9 @@ class SecretsManagerTokenProvider extends tokenProvider_1.default {
|
|
|
11
12
|
this.secretsManagerConfiguration = options.tokenConfiguration;
|
|
12
13
|
}
|
|
13
14
|
async getClientSecret() {
|
|
14
|
-
const secret = await this.secretsManagerClient
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const secret = await this.secretsManagerClient.send(new client_secrets_manager_1.GetSecretValueCommand({
|
|
16
|
+
SecretId: this.secretsManagerConfiguration.clientSecretId,
|
|
17
|
+
}));
|
|
17
18
|
if (!(secret === null || secret === void 0 ? void 0 : secret.SecretString)) {
|
|
18
19
|
throw new Error('Request error: failed to retrieve secret from Secrets Manager');
|
|
19
20
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lambda-essentials-ts",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "A selection of the finest modules supporting authorization, API routing, error handling, logging and sending HTTP requests.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"private": false,
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/Cimpress-MCP/lambda-essentials-ts#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"aws-sdk": "^
|
|
29
|
+
"@aws-sdk/client-kms": "^3.569.0",
|
|
30
|
+
"@aws-sdk/client-secrets-manager": "^3.569.0",
|
|
30
31
|
"axios": "~0.21.3",
|
|
31
32
|
"axios-cache-adapter": "~2.7.3",
|
|
32
33
|
"fast-safe-stringify": "~2.0.7",
|