@digitraffic/common 2023.3.10-1 → 2023.3.17-1
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/dist/aws/runtime/environment.d.ts +8 -0
- package/dist/aws/runtime/environment.js +10 -1
- package/dist/aws/runtime/secrets/secret-holder.d.ts +4 -0
- package/dist/aws/runtime/secrets/secret-holder.js +4 -0
- package/dist/aws/runtime/secrets/secret.js +15 -4
- package/dist/test/secrets-manager.js +4 -1
- package/dist/types/aws-env.d.ts +3 -0
- package/dist/types/aws-env.js +3 -0
- package/dist/utils/utils.d.ts +31 -1
- package/dist/utils/utils.js +72 -1
- package/package.json +1 -1
- package/src/aws/runtime/environment.ts +9 -0
- package/src/aws/runtime/secrets/secret-holder.ts +4 -0
- package/src/aws/runtime/secrets/secret.ts +18 -5
- package/src/test/secrets-manager.ts +5 -2
- package/src/types/aws-env.ts +3 -0
- package/src/utils/utils.ts +56 -2
@@ -1 +1,9 @@
|
|
1
|
+
export declare enum EnvKeys {
|
2
|
+
AWS_REGION = "AWS_REGION",
|
3
|
+
SECRET_ID = "SECRET_ID",
|
4
|
+
SECRET_OVERRIDE_AWS_REGION = "SECRET_OVERRIDE_AWS_REGION"
|
5
|
+
}
|
6
|
+
/**
|
7
|
+
* @deprecated Use digitraffic/common/utils/utils#getEnvVariable
|
8
|
+
*/
|
1
9
|
export declare function envValue(key: string, defaultValue?: string): string;
|
@@ -1,6 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.envValue = void 0;
|
3
|
+
exports.envValue = exports.EnvKeys = void 0;
|
4
|
+
var EnvKeys;
|
5
|
+
(function (EnvKeys) {
|
6
|
+
EnvKeys["AWS_REGION"] = "AWS_REGION";
|
7
|
+
EnvKeys["SECRET_ID"] = "SECRET_ID";
|
8
|
+
EnvKeys["SECRET_OVERRIDE_AWS_REGION"] = "SECRET_OVERRIDE_AWS_REGION";
|
9
|
+
})(EnvKeys = exports.EnvKeys || (exports.EnvKeys = {}));
|
10
|
+
/**
|
11
|
+
* @deprecated Use digitraffic/common/utils/utils#getEnvVariable
|
12
|
+
*/
|
4
13
|
function envValue(key, defaultValue) {
|
5
14
|
const value = process.env[key];
|
6
15
|
if (value == null) {
|
@@ -6,6 +6,10 @@ import { GenericSecret } from "./secret";
|
|
6
6
|
* By default, secrets are cached for 5 minutes and then reread from the Secrets Manager(This can be overridden with configuration).
|
7
7
|
*
|
8
8
|
* Supports setting the database environment paramaters from the secret too.
|
9
|
+
*
|
10
|
+
* If you want secret manager to get values from different region than the lambda runtime is running, you can override this by
|
11
|
+
* setting the region with utils setSecretOverideAwsRegionEnv method.
|
12
|
+
*
|
9
13
|
*/
|
10
14
|
export declare class SecretHolder<Secret extends GenericSecret> {
|
11
15
|
private readonly secretId;
|
@@ -18,6 +18,10 @@ const DEFAULT_CONFIGURATION = {
|
|
18
18
|
* By default, secrets are cached for 5 minutes and then reread from the Secrets Manager(This can be overridden with configuration).
|
19
19
|
*
|
20
20
|
* Supports setting the database environment paramaters from the secret too.
|
21
|
+
*
|
22
|
+
* If you want secret manager to get values from different region than the lambda runtime is running, you can override this by
|
23
|
+
* setting the region with utils setSecretOverideAwsRegionEnv method.
|
24
|
+
*
|
21
25
|
*/
|
22
26
|
class SecretHolder {
|
23
27
|
constructor(secretId, prefix = "", expectedKeys = [], configuration = DEFAULT_CONFIGURATION) {
|
@@ -2,11 +2,22 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.getSecret = void 0;
|
4
4
|
const aws_sdk_1 = require("aws-sdk");
|
5
|
-
const
|
6
|
-
|
7
|
-
|
5
|
+
const utils_1 = require("../../../utils/utils");
|
6
|
+
const environment_1 = require("../environment");
|
7
|
+
// SECRET_OVERRIDE_AWS_REGION might not have been set before import of
|
8
|
+
// secret, so we need to lazy initialize SecretsManager
|
9
|
+
let smClient;
|
10
|
+
function getSmClient() {
|
11
|
+
if (!smClient) {
|
12
|
+
smClient = new aws_sdk_1.SecretsManager({
|
13
|
+
region: (0, utils_1.getEnvVariableOrElse)(environment_1.EnvKeys.SECRET_OVERRIDE_AWS_REGION, // this is override secret region
|
14
|
+
(0, utils_1.getEnvVariable)(environment_1.EnvKeys.AWS_REGION)),
|
15
|
+
});
|
16
|
+
}
|
17
|
+
return smClient;
|
18
|
+
}
|
8
19
|
async function getSecret(secretId, prefix = "") {
|
9
|
-
const secretObj = await
|
20
|
+
const secretObj = await getSmClient()
|
10
21
|
.getSecretValue({
|
11
22
|
SecretId: secretId,
|
12
23
|
})
|
@@ -26,6 +26,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.mockSecret = exports.stubSecretsManager = void 0;
|
27
27
|
const AWS = require("aws-sdk");
|
28
28
|
const sinon = __importStar(require("sinon"));
|
29
|
+
const environment_1 = require("../aws/runtime/environment");
|
30
|
+
const utils_1 = require("../utils/utils");
|
31
|
+
(0, utils_1.setEnvVariable)(environment_1.EnvKeys.AWS_REGION, "eu-west-1");
|
29
32
|
const secretValue = sinon.stub();
|
30
33
|
/**
|
31
34
|
* Stub Secrets Manager for tests. You must call this
|
@@ -37,7 +40,7 @@ function stubSecretsManager() {
|
|
37
40
|
const smStub = {
|
38
41
|
getSecretValue: secretValue,
|
39
42
|
};
|
40
|
-
sinon.stub(AWS,
|
43
|
+
sinon.stub(AWS, "SecretsManager").returns(smStub);
|
41
44
|
return smStub.getSecretValue;
|
42
45
|
}
|
43
46
|
exports.stubSecretsManager = stubSecretsManager;
|
package/dist/utils/utils.d.ts
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
import { AwsEnv } from "../types/aws-env";
|
2
|
+
import { Either } from "../types/either";
|
1
3
|
/**
|
2
4
|
* Check if arrays have only elements that also exists also in other array.
|
3
5
|
* Individual element count doesn't matter.
|
@@ -19,7 +21,6 @@
|
|
19
21
|
* @param a first array to compare
|
20
22
|
* @param b second array to compare
|
21
23
|
*/
|
22
|
-
import { Either } from "../types/either";
|
23
24
|
export declare function bothArraysHasSameValues(a: null | undefined | unknown[], b: null | undefined | unknown[]): boolean;
|
24
25
|
/**
|
25
26
|
* Returns the last item on the array. If the array is empty, throws an error!
|
@@ -29,6 +30,14 @@ export declare function getLast<T>(array: T[], sortFunction?: (a: T) => number):
|
|
29
30
|
* Returns the first item on the array. If the array is empty, throws an error!
|
30
31
|
*/
|
31
32
|
export declare function getFirst<T>(array: T[], sortFunction?: (a: T) => number): T;
|
33
|
+
/**
|
34
|
+
* Gets basic AWS environment variables. Throws error if variables are not found.
|
35
|
+
*
|
36
|
+
* @param key Environment key
|
37
|
+
* @return string
|
38
|
+
* @See https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
|
39
|
+
*/
|
40
|
+
export declare function getAwsEnv(): AwsEnv;
|
32
41
|
/**
|
33
42
|
* Gets environment variable. Throws error if variable is not found.
|
34
43
|
*
|
@@ -44,6 +53,13 @@ export declare function getEnvVariable(key: string): string;
|
|
44
53
|
* @return Either<string>
|
45
54
|
*/
|
46
55
|
export declare function getEnvVariableSafe(key: string): Either<string>;
|
56
|
+
/**
|
57
|
+
* Sets environment variable.
|
58
|
+
*
|
59
|
+
* @param key Environment key
|
60
|
+
* @param value Environment variable value
|
61
|
+
*/
|
62
|
+
export declare function setEnvVariable(key: string, value: string): void;
|
47
63
|
/**
|
48
64
|
* Gets environment variable. If environment variable is undefined, returns value of given function.
|
49
65
|
*
|
@@ -59,3 +75,17 @@ export declare function getEnvVariableOr<T>(key: string, fn: () => T): string |
|
|
59
75
|
* @param orElse Alternative value
|
60
76
|
*/
|
61
77
|
export declare function getEnvVariableOrElse<T>(key: string, orElse: T): string | T;
|
78
|
+
export declare function setSecretOverideAwsRegionEnv(region: string): void;
|
79
|
+
/**
|
80
|
+
* ESLint won't allow to call Object.prototype builtin methods.
|
81
|
+
* To call hasOwnProperty we must use Object.prototype.hasOwnProperty.call()
|
82
|
+
* @param object to test for property
|
83
|
+
* @param propertyName property name to check
|
84
|
+
* @see https://eslint.org/docs/latest/rules/no-prototype-builtins
|
85
|
+
*/
|
86
|
+
export declare function hasOwnPropertySafe(object: object, propertyName: string): boolean;
|
87
|
+
/**
|
88
|
+
* Return an error message from the given object hat might be an Error object.
|
89
|
+
* @param maybeError
|
90
|
+
*/
|
91
|
+
export declare function getErrorMessage(maybeError: unknown): string;
|
package/dist/utils/utils.js
CHANGED
@@ -1,6 +1,28 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.getEnvVariableOrElse = exports.getEnvVariableOr = exports.getEnvVariableSafe = exports.getEnvVariable = exports.getFirst = exports.getLast = exports.bothArraysHasSameValues = void 0;
|
3
|
+
exports.getErrorMessage = exports.hasOwnPropertySafe = exports.setSecretOverideAwsRegionEnv = exports.getEnvVariableOrElse = exports.getEnvVariableOr = exports.setEnvVariable = exports.getEnvVariableSafe = exports.getEnvVariable = exports.getAwsEnv = exports.getFirst = exports.getLast = exports.bothArraysHasSameValues = void 0;
|
4
|
+
const environment_1 = require("../aws/runtime/environment");
|
5
|
+
/**
|
6
|
+
* Check if arrays have only elements that also exists also in other array.
|
7
|
+
* Individual element count doesn't matter.
|
8
|
+
* Function works only for primitive types and for other it just checks the reference to object.
|
9
|
+
*
|
10
|
+
* Some examples
|
11
|
+
* bothArraysHasSameValues( [a, b], [b, a] ) => true
|
12
|
+
* bothArraysHasSameValues( [a, a], [a, a, a] ) => true
|
13
|
+
* bothArraysHasSameValues( [a, b], [a] ) => false
|
14
|
+
*
|
15
|
+
* Object references:
|
16
|
+
* const o1 = { a: 1, b: 2};
|
17
|
+
* const o2 = { a: 1, b: 2};
|
18
|
+
* // Arrays has references to same objects
|
19
|
+
* bothArraysHasSameValues([o1], [o1])) => true
|
20
|
+
* Arrays have references to different objects
|
21
|
+
* bothArraysHasSameValues([o1], [o2])) => false
|
22
|
+
*
|
23
|
+
* @param a first array to compare
|
24
|
+
* @param b second array to compare
|
25
|
+
*/
|
4
26
|
function bothArraysHasSameValues(a, b) {
|
5
27
|
if ((a && !b) || (!a && b)) {
|
6
28
|
return false;
|
@@ -40,6 +62,19 @@ function getFirstOrLast(getFirst, array, sortFunction) {
|
|
40
62
|
}
|
41
63
|
return array[index];
|
42
64
|
}
|
65
|
+
/**
|
66
|
+
* Gets basic AWS environment variables. Throws error if variables are not found.
|
67
|
+
*
|
68
|
+
* @param key Environment key
|
69
|
+
* @return string
|
70
|
+
* @See https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
|
71
|
+
*/
|
72
|
+
function getAwsEnv() {
|
73
|
+
return {
|
74
|
+
region: getEnvVariable("AWS_REGION"),
|
75
|
+
};
|
76
|
+
}
|
77
|
+
exports.getAwsEnv = getAwsEnv;
|
43
78
|
/**
|
44
79
|
* Gets environment variable. Throws error if variable is not found.
|
45
80
|
*
|
@@ -72,6 +107,16 @@ function getEnvVariableSafe(key) {
|
|
72
107
|
return { result: "ok", value };
|
73
108
|
}
|
74
109
|
exports.getEnvVariableSafe = getEnvVariableSafe;
|
110
|
+
/**
|
111
|
+
* Sets environment variable.
|
112
|
+
*
|
113
|
+
* @param key Environment key
|
114
|
+
* @param value Environment variable value
|
115
|
+
*/
|
116
|
+
function setEnvVariable(key, value) {
|
117
|
+
process.env[key] = value;
|
118
|
+
}
|
119
|
+
exports.setEnvVariable = setEnvVariable;
|
75
120
|
/**
|
76
121
|
* Gets environment variable. If environment variable is undefined, returns value of given function.
|
77
122
|
*
|
@@ -97,4 +142,30 @@ function getEnvVariableOrElse(key, orElse) {
|
|
97
142
|
return getEnvVariableOr(key, () => orElse);
|
98
143
|
}
|
99
144
|
exports.getEnvVariableOrElse = getEnvVariableOrElse;
|
145
|
+
function setSecretOverideAwsRegionEnv(region) {
|
146
|
+
setEnvVariable(environment_1.EnvKeys.SECRET_OVERRIDE_AWS_REGION, region);
|
147
|
+
}
|
148
|
+
exports.setSecretOverideAwsRegionEnv = setSecretOverideAwsRegionEnv;
|
149
|
+
/**
|
150
|
+
* ESLint won't allow to call Object.prototype builtin methods.
|
151
|
+
* To call hasOwnProperty we must use Object.prototype.hasOwnProperty.call()
|
152
|
+
* @param object to test for property
|
153
|
+
* @param propertyName property name to check
|
154
|
+
* @see https://eslint.org/docs/latest/rules/no-prototype-builtins
|
155
|
+
*/
|
156
|
+
function hasOwnPropertySafe(object, propertyName) {
|
157
|
+
return Object.prototype.hasOwnProperty.call(object, propertyName);
|
158
|
+
}
|
159
|
+
exports.hasOwnPropertySafe = hasOwnPropertySafe;
|
160
|
+
/**
|
161
|
+
* Return an error message from the given object hat might be an Error object.
|
162
|
+
* @param maybeError
|
163
|
+
*/
|
164
|
+
function getErrorMessage(maybeError) {
|
165
|
+
if (maybeError instanceof Error) {
|
166
|
+
return maybeError.name + ": " + maybeError.message;
|
167
|
+
}
|
168
|
+
return String(maybeError);
|
169
|
+
}
|
170
|
+
exports.getErrorMessage = getErrorMessage;
|
100
171
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
export enum EnvKeys {
|
2
|
+
AWS_REGION = "AWS_REGION",
|
3
|
+
SECRET_ID = "SECRET_ID",
|
4
|
+
SECRET_OVERRIDE_AWS_REGION = "SECRET_OVERRIDE_AWS_REGION",
|
5
|
+
}
|
6
|
+
|
7
|
+
/**
|
8
|
+
* @deprecated Use digitraffic/common/utils/utils#getEnvVariable
|
9
|
+
*/
|
1
10
|
export function envValue(key: string, defaultValue?: string): string {
|
2
11
|
const value = process.env[key];
|
3
12
|
|
@@ -18,6 +18,10 @@ const DEFAULT_CONFIGURATION = {
|
|
18
18
|
* By default, secrets are cached for 5 minutes and then reread from the Secrets Manager(This can be overridden with configuration).
|
19
19
|
*
|
20
20
|
* Supports setting the database environment paramaters from the secret too.
|
21
|
+
*
|
22
|
+
* If you want secret manager to get values from different region than the lambda runtime is running, you can override this by
|
23
|
+
* setting the region with utils setSecretOverideAwsRegionEnv method.
|
24
|
+
*
|
21
25
|
*/
|
22
26
|
export class SecretHolder<Secret extends GenericSecret> {
|
23
27
|
private readonly secretId: string;
|
@@ -1,8 +1,21 @@
|
|
1
1
|
import { SecretsManager } from "aws-sdk";
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
import { getEnvVariable, getEnvVariableOrElse } from "../../../utils/utils";
|
3
|
+
import { EnvKeys } from "../environment";
|
4
|
+
|
5
|
+
// SECRET_OVERRIDE_AWS_REGION might not have been set before import of
|
6
|
+
// secret, so we need to lazy initialize SecretsManager
|
7
|
+
let smClient: SecretsManager | undefined;
|
8
|
+
function getSmClient(): SecretsManager {
|
9
|
+
if (!smClient) {
|
10
|
+
smClient = new SecretsManager({
|
11
|
+
region: getEnvVariableOrElse<string>(
|
12
|
+
EnvKeys.SECRET_OVERRIDE_AWS_REGION, // this is override secret region
|
13
|
+
getEnvVariable(EnvKeys.AWS_REGION)
|
14
|
+
),
|
15
|
+
});
|
16
|
+
}
|
17
|
+
return smClient;
|
18
|
+
}
|
6
19
|
|
7
20
|
export type GenericSecret = Record<string, string>;
|
8
21
|
|
@@ -10,7 +23,7 @@ export async function getSecret<Secret>(
|
|
10
23
|
secretId: string,
|
11
24
|
prefix = ""
|
12
25
|
): Promise<Secret> {
|
13
|
-
const secretObj = await
|
26
|
+
const secretObj = await getSmClient()
|
14
27
|
.getSecretValue({
|
15
28
|
SecretId: secretId,
|
16
29
|
})
|
@@ -1,6 +1,9 @@
|
|
1
|
-
import AWS = require(
|
1
|
+
import AWS = require("aws-sdk");
|
2
2
|
import * as sinon from "sinon";
|
3
|
+
import { EnvKeys } from "../aws/runtime/environment";
|
4
|
+
import { setEnvVariable } from "../utils/utils";
|
3
5
|
|
6
|
+
setEnvVariable(EnvKeys.AWS_REGION, "eu-west-1");
|
4
7
|
const secretValue = sinon.stub();
|
5
8
|
|
6
9
|
/**
|
@@ -14,7 +17,7 @@ export function stubSecretsManager() {
|
|
14
17
|
getSecretValue: secretValue,
|
15
18
|
};
|
16
19
|
|
17
|
-
sinon.stub(AWS,
|
20
|
+
sinon.stub(AWS, "SecretsManager").returns(smStub);
|
18
21
|
|
19
22
|
return smStub.getSecretValue;
|
20
23
|
}
|
package/src/utils/utils.ts
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
import { AwsEnv } from "../types/aws-env";
|
2
|
+
import { Either } from "../types/either";
|
3
|
+
import { EnvKeys } from "../aws/runtime/environment";
|
4
|
+
|
1
5
|
/**
|
2
6
|
* Check if arrays have only elements that also exists also in other array.
|
3
7
|
* Individual element count doesn't matter.
|
@@ -19,8 +23,6 @@
|
|
19
23
|
* @param a first array to compare
|
20
24
|
* @param b second array to compare
|
21
25
|
*/
|
22
|
-
import { Either } from "../types/either";
|
23
|
-
|
24
26
|
export function bothArraysHasSameValues(
|
25
27
|
a: null | undefined | unknown[],
|
26
28
|
b: null | undefined | unknown[]
|
@@ -72,6 +74,19 @@ function getFirstOrLast<T>(
|
|
72
74
|
return array[index];
|
73
75
|
}
|
74
76
|
|
77
|
+
/**
|
78
|
+
* Gets basic AWS environment variables. Throws error if variables are not found.
|
79
|
+
*
|
80
|
+
* @param key Environment key
|
81
|
+
* @return string
|
82
|
+
* @See https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
|
83
|
+
*/
|
84
|
+
export function getAwsEnv(): AwsEnv {
|
85
|
+
return {
|
86
|
+
region: getEnvVariable("AWS_REGION"),
|
87
|
+
};
|
88
|
+
}
|
89
|
+
|
75
90
|
/**
|
76
91
|
* Gets environment variable. Throws error if variable is not found.
|
77
92
|
*
|
@@ -104,6 +119,16 @@ export function getEnvVariableSafe(key: string): Either<string> {
|
|
104
119
|
return { result: "ok", value };
|
105
120
|
}
|
106
121
|
|
122
|
+
/**
|
123
|
+
* Sets environment variable.
|
124
|
+
*
|
125
|
+
* @param key Environment key
|
126
|
+
* @param value Environment variable value
|
127
|
+
*/
|
128
|
+
export function setEnvVariable(key: string, value: string) {
|
129
|
+
process.env[key] = value;
|
130
|
+
}
|
131
|
+
|
107
132
|
/**
|
108
133
|
* Gets environment variable. If environment variable is undefined, returns value of given function.
|
109
134
|
*
|
@@ -128,3 +153,32 @@ export function getEnvVariableOr<T>(key: string, fn: () => T): string | T {
|
|
128
153
|
export function getEnvVariableOrElse<T>(key: string, orElse: T): string | T {
|
129
154
|
return getEnvVariableOr(key, () => orElse);
|
130
155
|
}
|
156
|
+
|
157
|
+
export function setSecretOverideAwsRegionEnv(region: string) {
|
158
|
+
setEnvVariable(EnvKeys.SECRET_OVERRIDE_AWS_REGION, region);
|
159
|
+
}
|
160
|
+
|
161
|
+
/**
|
162
|
+
* ESLint won't allow to call Object.prototype builtin methods.
|
163
|
+
* To call hasOwnProperty we must use Object.prototype.hasOwnProperty.call()
|
164
|
+
* @param object to test for property
|
165
|
+
* @param propertyName property name to check
|
166
|
+
* @see https://eslint.org/docs/latest/rules/no-prototype-builtins
|
167
|
+
*/
|
168
|
+
export function hasOwnPropertySafe(
|
169
|
+
object: object,
|
170
|
+
propertyName: string
|
171
|
+
): boolean {
|
172
|
+
return Object.prototype.hasOwnProperty.call(object, propertyName);
|
173
|
+
}
|
174
|
+
|
175
|
+
/**
|
176
|
+
* Return an error message from the given object hat might be an Error object.
|
177
|
+
* @param maybeError
|
178
|
+
*/
|
179
|
+
export function getErrorMessage(maybeError: unknown) {
|
180
|
+
if (maybeError instanceof Error) {
|
181
|
+
return maybeError.name + ": " + maybeError.message;
|
182
|
+
}
|
183
|
+
return String(maybeError);
|
184
|
+
}
|