@digitraffic/common 2023.2.13-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/infra/api/responses.d.ts +0 -4
- package/dist/aws/infra/api/responses.js +1 -15
- package/dist/aws/infra/stacks/db-dns-stack.d.ts +2 -0
- package/dist/aws/infra/stacks/db-dns-stack.js +2 -0
- package/dist/aws/infra/stacks/db-stack.d.ts +5 -0
- package/dist/aws/infra/stacks/db-stack.js +6 -0
- 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/types/urn.d.ts +1 -0
- package/dist/types/urn.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/infra/api/responses.ts +0 -14
- package/src/aws/infra/stacks/db-dns-stack.ts +2 -0
- package/src/aws/infra/stacks/db-stack.ts +10 -0
- 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/types/urn.ts +1 -0
- package/src/utils/utils.ts +56 -2
@@ -19,10 +19,6 @@ export declare const RESPONSE_404_NOT_FOUND: {
|
|
19
19
|
* @deprecated Use DigitrafficMethodResponse
|
20
20
|
*/
|
21
21
|
export declare function methodResponse(status: string, contentType: MediaType, model: IModel, parameters?: Record<string, boolean>): MethodResponse;
|
22
|
-
/**
|
23
|
-
* @deprecated Use DigitrafficMethodResponse
|
24
|
-
*/
|
25
|
-
export declare function corsMethod(response: MethodResponse): MethodResponse;
|
26
22
|
interface IntegrationOptions {
|
27
23
|
requestParameters?: {
|
28
24
|
[dest: string]: string;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.getResponse = exports.defaultIntegration = exports.
|
3
|
+
exports.getResponse = exports.defaultIntegration = exports.methodResponse = exports.RESPONSE_404_NOT_FOUND = exports.RESPONSE_CORS_INTEGRATION = exports.RESPONSE_500_SERVER_ERROR = exports.RESPONSE_400_BAD_REQUEST = exports.RESPONSE_200_OK = void 0;
|
4
4
|
const response_1 = require("./response");
|
5
5
|
const aws_apigateway_1 = require("aws-cdk-lib/aws-apigateway");
|
6
6
|
const errors_1 = require("../../types/errors");
|
@@ -41,20 +41,6 @@ function methodResponse(status, contentType, model, parameters) {
|
|
41
41
|
};
|
42
42
|
}
|
43
43
|
exports.methodResponse = methodResponse;
|
44
|
-
/**
|
45
|
-
* @deprecated Use DigitrafficMethodResponse
|
46
|
-
*/
|
47
|
-
function corsMethod(response) {
|
48
|
-
return {
|
49
|
-
...response,
|
50
|
-
...{
|
51
|
-
responseParameters: {
|
52
|
-
"method.response.header.Access-Control-Allow-Origin": true,
|
53
|
-
},
|
54
|
-
},
|
55
|
-
};
|
56
|
-
}
|
57
|
-
exports.corsMethod = corsMethod;
|
58
44
|
/**
|
59
45
|
* Creates a default Lambda integration for a REST API resource _root_
|
60
46
|
* @param lambdaFunction The Lambda function
|
@@ -4,6 +4,8 @@ import { InfraStackConfiguration } from "./intra-stack-configuration";
|
|
4
4
|
/**
|
5
5
|
* Creates a dns local zone and creates records for cluster endpoints and proxy endpoints.
|
6
6
|
*
|
7
|
+
* Please note, that created PrivateHostedZone has RETAIN removalPolicy, so if you want to delete this stack,
|
8
|
+
* you must remove the zone by hand after.
|
7
9
|
*/
|
8
10
|
export declare class DbDnsStack extends Stack {
|
9
11
|
constructor(scope: Construct, id: string, isc: InfraStackConfiguration);
|
@@ -10,6 +10,8 @@ const DEFAULT_RECORD_TTL = aws_cdk_lib_1.Duration.seconds(30);
|
|
10
10
|
/**
|
11
11
|
* Creates a dns local zone and creates records for cluster endpoints and proxy endpoints.
|
12
12
|
*
|
13
|
+
* Please note, that created PrivateHostedZone has RETAIN removalPolicy, so if you want to delete this stack,
|
14
|
+
* you must remove the zone by hand after.
|
13
15
|
*/
|
14
16
|
class DbDnsStack extends aws_cdk_lib_1.Stack {
|
15
17
|
constructor(scope, id, isc) {
|
@@ -20,6 +20,11 @@ export interface DbConfiguration {
|
|
20
20
|
};
|
21
21
|
}
|
22
22
|
/**
|
23
|
+
* Stack that creates DatabaseCluster.
|
24
|
+
*
|
25
|
+
* Please not, that created Cluster has RETAIL removalPolicy, so if you want to delete the stack,
|
26
|
+
* you must first deploy without parameter group, then delete stack and manually delete cluster.
|
27
|
+
*
|
23
28
|
* How to upgrade major version?
|
24
29
|
* 0. Set correct SG for db-stack and db-proxy-stack(this step will be removed in the future)
|
25
30
|
* 1. Update db-stack WITHOUT parameter group
|
@@ -6,6 +6,11 @@ const aws_ec2_1 = require("aws-cdk-lib/aws-ec2");
|
|
6
6
|
const aws_rds_1 = require("aws-cdk-lib/aws-rds");
|
7
7
|
const import_util_1 = require("../import-util");
|
8
8
|
/**
|
9
|
+
* Stack that creates DatabaseCluster.
|
10
|
+
*
|
11
|
+
* Please not, that created Cluster has RETAIL removalPolicy, so if you want to delete the stack,
|
12
|
+
* you must first deploy without parameter group, then delete stack and manually delete cluster.
|
13
|
+
*
|
9
14
|
* How to upgrade major version?
|
10
15
|
* 0. Set correct SG for db-stack and db-proxy-stack(this step will be removed in the future)
|
11
16
|
* 1. Update db-stack WITHOUT parameter group
|
@@ -88,6 +93,7 @@ class DbStack extends aws_cdk_lib_1.Stack {
|
|
88
93
|
throw new Error("Couldn't pull CfnDBInstances from the L1 constructs!");
|
89
94
|
}
|
90
95
|
cfnInstances.forEach((cfnInstance) => delete cfnInstance.engineVersion);
|
96
|
+
cluster.node.addDependency(parameterGroup, "Create ParameterGroup before DatabaseCluster");
|
91
97
|
return cluster;
|
92
98
|
}
|
93
99
|
}
|
@@ -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;
|
@@ -0,0 +1 @@
|
|
1
|
+
export type URN<Namespace extends string, NamespaceSpecificString extends string = ""> = `urn:${Namespace}:${NamespaceSpecificString}`;
|
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
@@ -68,20 +68,6 @@ export function methodResponse(
|
|
68
68
|
};
|
69
69
|
}
|
70
70
|
|
71
|
-
/**
|
72
|
-
* @deprecated Use DigitrafficMethodResponse
|
73
|
-
*/
|
74
|
-
export function corsMethod(response: MethodResponse): MethodResponse {
|
75
|
-
return {
|
76
|
-
...response,
|
77
|
-
...{
|
78
|
-
responseParameters: {
|
79
|
-
"method.response.header.Access-Control-Allow-Origin": true,
|
80
|
-
},
|
81
|
-
},
|
82
|
-
};
|
83
|
-
}
|
84
|
-
|
85
71
|
interface IntegrationOptions {
|
86
72
|
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
|
87
73
|
requestParameters?: { [dest: string]: string };
|
@@ -16,6 +16,8 @@ const DEFAULT_RECORD_TTL = Duration.seconds(30);
|
|
16
16
|
/**
|
17
17
|
* Creates a dns local zone and creates records for cluster endpoints and proxy endpoints.
|
18
18
|
*
|
19
|
+
* Please note, that created PrivateHostedZone has RETAIN removalPolicy, so if you want to delete this stack,
|
20
|
+
* you must remove the zone by hand after.
|
19
21
|
*/
|
20
22
|
export class DbDnsStack extends Stack {
|
21
23
|
constructor(scope: Construct, id: string, isc: InfraStackConfiguration) {
|
@@ -42,6 +42,11 @@ export interface DbConfiguration {
|
|
42
42
|
}
|
43
43
|
|
44
44
|
/**
|
45
|
+
* Stack that creates DatabaseCluster.
|
46
|
+
*
|
47
|
+
* Please not, that created Cluster has RETAIL removalPolicy, so if you want to delete the stack,
|
48
|
+
* you must first deploy without parameter group, then delete stack and manually delete cluster.
|
49
|
+
*
|
45
50
|
* How to upgrade major version?
|
46
51
|
* 0. Set correct SG for db-stack and db-proxy-stack(this step will be removed in the future)
|
47
52
|
* 1. Update db-stack WITHOUT parameter group
|
@@ -195,6 +200,11 @@ export class DbStack extends Stack {
|
|
195
200
|
}
|
196
201
|
cfnInstances.forEach((cfnInstance) => delete cfnInstance.engineVersion);
|
197
202
|
|
203
|
+
cluster.node.addDependency(
|
204
|
+
parameterGroup,
|
205
|
+
"Create ParameterGroup before DatabaseCluster"
|
206
|
+
);
|
207
|
+
|
198
208
|
return cluster;
|
199
209
|
}
|
200
210
|
}
|
@@ -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/types/urn.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export type URN<Namespace extends string, NamespaceSpecificString extends string = ""> = `urn:${Namespace}:${NamespaceSpecificString}`;
|
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
|
+
}
|