@digitraffic/common 2022.11.22-1 → 2022.11.23-2
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/integration.d.ts +21 -0
- package/dist/aws/infra/api/integration.js +8 -4
- package/dist/aws/infra/api/integration.js.map +1 -0
- package/dist/aws/infra/api/response.d.ts +28 -0
- package/dist/aws/infra/api/response.js +49 -18
- package/dist/aws/infra/api/response.js.map +1 -0
- package/dist/aws/infra/api/responses.d.ts +47 -0
- package/dist/aws/infra/api/responses.js +8 -0
- package/dist/aws/infra/api/responses.js.map +1 -0
- package/dist/aws/infra/api/static-integration.d.ts +15 -0
- package/dist/aws/infra/api/static-integration.js +12 -6
- package/dist/aws/infra/api/static-integration.js.map +1 -0
- package/dist/aws/infra/canaries/canary-alarm.d.ts +6 -0
- package/dist/aws/infra/canaries/canary-alarm.js.map +1 -0
- package/dist/aws/infra/canaries/canary-keys.d.ts +3 -0
- package/dist/aws/infra/canaries/canary-keys.js.map +1 -0
- package/dist/aws/infra/canaries/canary-parameters.d.ts +18 -0
- package/dist/aws/infra/canaries/canary-parameters.js.map +1 -0
- package/dist/aws/infra/canaries/canary-role.d.ts +6 -0
- package/dist/aws/infra/canaries/canary-role.js.map +1 -0
- package/dist/aws/infra/canaries/canary.d.ts +8 -0
- package/dist/aws/infra/canaries/canary.js.map +1 -0
- package/dist/aws/infra/canaries/database-canary.d.ts +18 -0
- package/dist/aws/infra/canaries/database-canary.js.map +1 -0
- package/dist/aws/infra/canaries/database-checker.d.ts +33 -0
- package/dist/aws/infra/canaries/database-checker.js.map +1 -0
- package/dist/aws/infra/canaries/url-canary.d.ts +16 -0
- package/dist/aws/infra/canaries/url-canary.js.map +1 -0
- package/dist/aws/infra/canaries/url-checker.d.ts +46 -0
- package/dist/aws/infra/canaries/url-checker.js.map +1 -0
- package/dist/aws/infra/documentation.d.ts +56 -0
- package/dist/aws/infra/documentation.js.map +1 -0
- package/dist/aws/infra/import-util.d.ts +21 -0
- package/dist/aws/infra/import-util.js.map +1 -0
- package/dist/aws/infra/scheduler.d.ts +12 -0
- package/dist/aws/infra/scheduler.js.map +1 -0
- package/dist/aws/infra/security-rule.d.ts +12 -0
- package/dist/aws/infra/security-rule.js.map +1 -0
- package/dist/aws/infra/sqs-integration.d.ts +7 -0
- package/dist/aws/infra/sqs-integration.js.map +1 -0
- package/dist/aws/infra/sqs-queue.d.ts +16 -0
- package/dist/aws/infra/sqs-queue.js.map +1 -0
- package/dist/aws/infra/stack/lambda-configs.d.ts +64 -0
- package/dist/aws/infra/stack/lambda-configs.js.map +1 -0
- package/dist/aws/infra/stack/monitoredfunction.d.ts +84 -0
- package/dist/aws/infra/stack/monitoredfunction.js +31 -31
- package/dist/aws/infra/stack/monitoredfunction.js.map +1 -0
- package/dist/aws/infra/stack/rest_apis.d.ts +41 -0
- package/dist/aws/infra/stack/rest_apis.js.map +1 -0
- package/dist/aws/infra/stack/stack-checking-aspect.d.ts +21 -0
- package/dist/aws/infra/stack/stack-checking-aspect.js.map +1 -0
- package/dist/aws/infra/stack/stack.d.ts +45 -0
- package/dist/aws/infra/stack/stack.js.map +1 -0
- package/dist/aws/infra/stack/subscription.d.ts +17 -0
- package/dist/aws/infra/stack/subscription.js.map +1 -0
- package/dist/aws/infra/stacks/db-dns-stack.d.ts +11 -0
- package/dist/aws/infra/stacks/db-dns-stack.js.map +1 -0
- package/dist/aws/infra/stacks/db-proxy-stack.d.ts +19 -0
- package/dist/aws/infra/stacks/db-proxy-stack.js.map +1 -0
- package/dist/aws/infra/stacks/db-stack.d.ts +31 -0
- package/dist/aws/infra/stacks/db-stack.js.map +1 -0
- package/dist/aws/infra/stacks/intra-stack-configuration.d.ts +5 -0
- package/dist/aws/infra/stacks/intra-stack-configuration.js.map +1 -0
- package/dist/aws/infra/stacks/network-stack.d.ts +12 -0
- package/dist/aws/infra/stacks/network-stack.js.map +1 -0
- package/dist/aws/infra/usage-plans.d.ts +16 -0
- package/dist/aws/infra/usage-plans.js.map +1 -0
- package/dist/aws/runtime/apikey.d.ts +2 -0
- package/dist/aws/runtime/apikey.js.map +1 -0
- package/dist/aws/runtime/digitraffic-integration-response.d.ts +8 -0
- package/dist/aws/runtime/digitraffic-integration-response.js.map +1 -0
- package/dist/aws/runtime/environment.d.ts +1 -0
- package/dist/aws/runtime/environment.js.map +1 -0
- package/dist/aws/runtime/messaging.d.ts +10 -0
- package/dist/aws/runtime/messaging.js.map +1 -0
- package/dist/aws/runtime/s3.d.ts +2 -0
- package/dist/aws/runtime/s3.js.map +1 -0
- package/dist/aws/runtime/secrets/dbsecret.d.ts +54 -0
- package/dist/aws/runtime/secrets/dbsecret.js +4 -4
- package/dist/aws/runtime/secrets/dbsecret.js.map +1 -0
- package/dist/aws/runtime/secrets/proxy-holder.d.ts +9 -0
- package/dist/aws/runtime/secrets/proxy-holder.js.map +1 -0
- package/dist/aws/runtime/secrets/rds-holder.d.ts +9 -0
- package/dist/aws/runtime/secrets/rds-holder.js.map +1 -0
- package/dist/aws/runtime/secrets/secret-holder.d.ts +26 -0
- package/dist/aws/runtime/secrets/secret-holder.js.map +1 -0
- package/dist/aws/runtime/secrets/secret.d.ts +8 -0
- package/dist/aws/runtime/secrets/secret.js.map +1 -0
- package/dist/aws/types/errors.d.ts +8 -0
- package/dist/aws/types/errors.js.map +1 -0
- package/dist/aws/types/lambda-response.d.ts +13 -0
- package/dist/aws/types/lambda-response.js.map +1 -0
- package/dist/aws/types/mediatypes.d.ts +11 -0
- package/dist/aws/types/mediatypes.js.map +1 -0
- package/dist/aws/types/model-with-reference.d.ts +7 -0
- package/dist/aws/types/model-with-reference.js.map +1 -0
- package/dist/aws/types/proxytypes.d.ts +26 -0
- package/dist/aws/types/proxytypes.js.map +1 -0
- package/dist/aws/types/tags.d.ts +2 -0
- package/dist/aws/types/tags.js.map +1 -0
- package/dist/database/cached.d.ts +7 -0
- package/dist/database/cached.js.map +1 -0
- package/dist/database/database.d.ts +19 -0
- package/dist/database/database.js.map +1 -0
- package/dist/database/last-updated.d.ts +16 -0
- package/dist/database/last-updated.js.map +1 -0
- package/dist/database/models.d.ts +6 -0
- package/dist/database/models.js.map +1 -0
- package/dist/marine/id_utils.d.ts +3 -0
- package/dist/marine/id_utils.js.map +1 -0
- package/dist/marine/rtz.d.ts +48 -0
- package/dist/marine/rtz.js.map +1 -0
- package/dist/test/asserter.d.ts +11 -0
- package/dist/test/asserter.js.map +1 -0
- package/dist/test/db-testutils.d.ts +3 -0
- package/dist/test/db-testutils.js.map +1 -0
- package/dist/test/httpserver.d.ts +19 -0
- package/dist/test/httpserver.js.map +1 -0
- package/dist/test/secret.d.ts +3 -0
- package/dist/test/secret.js.map +1 -0
- package/dist/test/secrets-manager.d.ts +9 -0
- package/dist/test/secrets-manager.js.map +1 -0
- package/dist/test/testutils.d.ts +12 -0
- package/dist/test/testutils.js.map +1 -0
- package/dist/types/either.d.ts +9 -0
- package/dist/types/either.js.map +1 -0
- package/dist/types/input-error.d.ts +2 -0
- package/dist/types/input-error.js.map +1 -0
- package/dist/types/language.d.ts +5 -0
- package/dist/types/language.js.map +1 -0
- package/dist/types/traffictype.d.ts +8 -0
- package/dist/types/traffictype.js.map +1 -0
- package/dist/types/validator.d.ts +4 -0
- package/dist/types/validator.js.map +1 -0
- package/dist/utils/api-model.d.ts +87 -0
- package/dist/utils/api-model.js.map +1 -0
- package/dist/utils/base64.d.ts +12 -0
- package/dist/utils/base64.js.map +1 -0
- package/dist/utils/date-utils.d.ts +17 -0
- package/dist/utils/date-utils.js.map +1 -0
- package/dist/utils/geojson-types.d.ts +14 -0
- package/dist/utils/geojson-types.js.map +1 -0
- package/dist/utils/geometry.d.ts +36 -0
- package/dist/utils/geometry.js.map +1 -0
- package/dist/utils/retry.d.ts +13 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/slack.d.ts +5 -0
- package/dist/utils/slack.js.map +1 -0
- package/dist/utils/utils.d.ts +46 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +19 -29
- package/src/aws/infra/api/integration.ts +30 -14
- package/src/aws/infra/api/response.ts +107 -26
- package/src/aws/infra/api/responses.ts +8 -0
- package/src/aws/infra/api/static-integration.ts +48 -14
- package/src/aws/runtime/secrets/dbsecret.ts +23 -54
- package/src/aws/runtime/secrets/secret-holder.ts +6 -3
- package/src/database/database.ts +1 -1
@@ -1,5 +1,12 @@
|
|
1
|
-
import
|
2
|
-
import {
|
1
|
+
import { MediaType } from "../../types/mediatypes";
|
2
|
+
import {
|
3
|
+
JsonSchemaType,
|
4
|
+
JsonSchemaVersion,
|
5
|
+
JsonSchema,
|
6
|
+
MethodResponse,
|
7
|
+
Model,
|
8
|
+
} from "aws-cdk-lib/aws-apigateway";
|
9
|
+
import { IModel } from "aws-cdk-lib/aws-apigateway/lib/model";
|
3
10
|
|
4
11
|
/**
|
5
12
|
* This is velocity-script, that assumes the response to be LambdaResponse(status and body).
|
@@ -23,45 +30,119 @@ $inputRoot.body
|
|
23
30
|
|
24
31
|
const BODY_FROM_INPUT_PATH = "$input.path('$').body";
|
25
32
|
|
26
|
-
|
27
|
-
const messageSchema:
|
28
|
-
schema:
|
29
|
-
type:
|
30
|
-
description:
|
33
|
+
/// @deprecated
|
34
|
+
const messageSchema: JsonSchema = {
|
35
|
+
schema: JsonSchemaVersion.DRAFT4,
|
36
|
+
type: JsonSchemaType.OBJECT,
|
37
|
+
description: "Response with message",
|
31
38
|
properties: {
|
32
39
|
message: {
|
33
|
-
type:
|
34
|
-
description:
|
40
|
+
type: JsonSchemaType.STRING,
|
41
|
+
description: "Response message",
|
35
42
|
},
|
36
43
|
},
|
37
44
|
};
|
38
45
|
|
39
|
-
|
46
|
+
/// @deprecated
|
40
47
|
export const MessageModel = {
|
41
48
|
contentType: MediaType.APPLICATION_JSON,
|
42
|
-
modelName:
|
49
|
+
modelName: "MessageResponseModel",
|
43
50
|
schema: messageSchema,
|
44
51
|
};
|
45
52
|
|
46
|
-
const NotFoundMessage =
|
47
|
-
export const NotFoundResponse = JSON.stringify({message: NotFoundMessage});
|
53
|
+
const NotFoundMessage = "Not found";
|
54
|
+
export const NotFoundResponse = JSON.stringify({ message: NotFoundMessage });
|
48
55
|
|
49
|
-
const InternalServerErrorMessage =
|
50
|
-
const InternalServerErrorResponse = JSON.stringify({
|
56
|
+
const InternalServerErrorMessage = "Error";
|
57
|
+
const InternalServerErrorResponse = JSON.stringify({
|
58
|
+
message: InternalServerErrorMessage,
|
59
|
+
});
|
51
60
|
|
52
|
-
const BadRequestMessage =
|
53
|
-
const BadRequestResponse = JSON.stringify({message: BadRequestMessage});
|
61
|
+
const BadRequestMessage = "Bad request";
|
62
|
+
const BadRequestResponse = JSON.stringify({ message: BadRequestMessage });
|
54
63
|
|
55
|
-
|
56
|
-
export const
|
57
|
-
|
58
|
-
|
59
|
-
|
64
|
+
/// @deprecated
|
65
|
+
export const BadRequestResponseTemplate = createResponses(
|
66
|
+
MediaType.APPLICATION_JSON,
|
67
|
+
BadRequestResponse
|
68
|
+
);
|
69
|
+
/// @deprecated
|
70
|
+
export const NotFoundResponseTemplate = createResponses(
|
71
|
+
MediaType.APPLICATION_JSON,
|
72
|
+
NotFoundResponse
|
73
|
+
);
|
74
|
+
/// @deprecated
|
75
|
+
export const XmlResponseTemplate = createResponses(
|
76
|
+
MediaType.APPLICATION_XML,
|
77
|
+
BODY_FROM_INPUT_PATH
|
78
|
+
);
|
79
|
+
/// @deprecated
|
80
|
+
export const InternalServerErrorResponseTemplate = createResponses(
|
81
|
+
MediaType.APPLICATION_JSON,
|
82
|
+
InternalServerErrorResponse
|
83
|
+
);
|
60
84
|
|
61
|
-
|
62
|
-
|
85
|
+
/// @deprecated
|
86
|
+
export function createResponses<T>(
|
87
|
+
key: MediaType,
|
88
|
+
value: T
|
89
|
+
): Record<string, T> {
|
90
|
+
return {
|
91
|
+
[key]: value,
|
92
|
+
};
|
93
|
+
}
|
94
|
+
|
95
|
+
export class DigitrafficMethodResponse {
|
96
|
+
static response(
|
97
|
+
statusCode: string,
|
98
|
+
model: IModel,
|
99
|
+
mediaType: MediaType,
|
100
|
+
disableCors = false
|
101
|
+
): MethodResponse {
|
102
|
+
return {
|
103
|
+
statusCode,
|
104
|
+
responseModels: {
|
105
|
+
[mediaType]: model,
|
106
|
+
},
|
107
|
+
responseParameters: disableCors
|
108
|
+
? {}
|
109
|
+
: {
|
110
|
+
"method.response.header.Access-Control-Allow-Origin":
|
111
|
+
true,
|
112
|
+
},
|
113
|
+
};
|
114
|
+
}
|
115
|
+
|
116
|
+
static response200(model: IModel, mediaType = MediaType.APPLICATION_JSON) {
|
117
|
+
return DigitrafficMethodResponse.response(
|
118
|
+
"200",
|
119
|
+
model,
|
120
|
+
mediaType,
|
121
|
+
false
|
122
|
+
);
|
123
|
+
}
|
63
124
|
|
64
|
-
|
125
|
+
static response500(
|
126
|
+
model = Model.EMPTY_MODEL,
|
127
|
+
mediaType = MediaType.APPLICATION_JSON
|
128
|
+
) {
|
129
|
+
return DigitrafficMethodResponse.response(
|
130
|
+
"500",
|
131
|
+
model,
|
132
|
+
mediaType,
|
133
|
+
false
|
134
|
+
);
|
135
|
+
}
|
65
136
|
|
66
|
-
|
137
|
+
static response400(
|
138
|
+
model = Model.EMPTY_MODEL,
|
139
|
+
mediaType = MediaType.APPLICATION_JSON
|
140
|
+
) {
|
141
|
+
return DigitrafficMethodResponse.response(
|
142
|
+
"400",
|
143
|
+
model,
|
144
|
+
mediaType,
|
145
|
+
false
|
146
|
+
);
|
147
|
+
}
|
67
148
|
}
|
@@ -52,6 +52,9 @@ export const RESPONSE_404_NOT_FOUND = {
|
|
52
52
|
responseTemplates: NotFoundResponseTemplate,
|
53
53
|
};
|
54
54
|
|
55
|
+
/**
|
56
|
+
* @deprecated Use DigitrafficMethodResponse
|
57
|
+
*/
|
55
58
|
export function methodResponse(
|
56
59
|
status: string,
|
57
60
|
contentType: MediaType,
|
@@ -65,6 +68,9 @@ export function methodResponse(
|
|
65
68
|
};
|
66
69
|
}
|
67
70
|
|
71
|
+
/**
|
72
|
+
* @deprecated Use DigitrafficMethodResponse
|
73
|
+
*/
|
68
74
|
export function corsMethod(response: MethodResponse): MethodResponse {
|
69
75
|
return {
|
70
76
|
...response,
|
@@ -89,6 +95,8 @@ interface IntegrationOptions {
|
|
89
95
|
* Creates a default Lambda integration for a REST API resource _root_
|
90
96
|
* @param lambdaFunction The Lambda function
|
91
97
|
* @param options Options
|
98
|
+
*
|
99
|
+
* @deprecated Use DigitrafficIntegration
|
92
100
|
*/
|
93
101
|
export function defaultIntegration(
|
94
102
|
lambdaFunction: AWSFunction,
|
@@ -1,13 +1,17 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
1
|
+
import {
|
2
|
+
MockIntegration,
|
3
|
+
PassthroughBehavior,
|
4
|
+
Resource,
|
5
|
+
} from "aws-cdk-lib/aws-apigateway";
|
6
|
+
import { MediaType } from "../../types/mediatypes";
|
7
|
+
import { corsMethod, RESPONSE_CORS_INTEGRATION } from "./responses";
|
4
8
|
|
5
9
|
const INTEGRATION_RESPONSE_200 = `{
|
6
10
|
"statusCode": 200
|
7
11
|
}`;
|
8
12
|
|
9
13
|
const METHOD_RESPONSE_200 = {
|
10
|
-
statusCode:
|
14
|
+
statusCode: "200",
|
11
15
|
};
|
12
16
|
|
13
17
|
/**
|
@@ -19,9 +23,18 @@ const METHOD_RESPONSE_200 = {
|
|
19
23
|
*/
|
20
24
|
export class DigitrafficStaticIntegration extends MockIntegration {
|
21
25
|
constructor(
|
22
|
-
resource: Resource,
|
26
|
+
resource: Resource,
|
27
|
+
mediaType: MediaType,
|
28
|
+
response: string,
|
29
|
+
enableCors = true,
|
30
|
+
apiKeyRequired = true
|
23
31
|
) {
|
24
|
-
const integrationResponse =
|
32
|
+
const integrationResponse =
|
33
|
+
DigitrafficStaticIntegration.createIntegrationResponse(
|
34
|
+
response,
|
35
|
+
mediaType,
|
36
|
+
enableCors
|
37
|
+
);
|
25
38
|
|
26
39
|
super({
|
27
40
|
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES,
|
@@ -31,32 +44,53 @@ export class DigitrafficStaticIntegration extends MockIntegration {
|
|
31
44
|
integrationResponses: [integrationResponse],
|
32
45
|
});
|
33
46
|
|
34
|
-
[
|
47
|
+
["GET", "HEAD"].forEach((httpMethod) => {
|
35
48
|
resource.addMethod(httpMethod, this, {
|
36
49
|
apiKeyRequired,
|
37
|
-
methodResponses: [
|
50
|
+
methodResponses: [
|
51
|
+
DigitrafficStaticIntegration.createMethodResponse(
|
52
|
+
enableCors
|
53
|
+
),
|
54
|
+
],
|
38
55
|
});
|
39
56
|
});
|
40
57
|
}
|
41
58
|
|
42
|
-
static json<K>(
|
59
|
+
static json<K>(
|
60
|
+
resource: Resource,
|
61
|
+
response: K,
|
62
|
+
enableCors = true,
|
63
|
+
apiKeyRequired = true
|
64
|
+
) {
|
43
65
|
return new DigitrafficStaticIntegration(
|
44
|
-
resource,
|
66
|
+
resource,
|
67
|
+
MediaType.APPLICATION_JSON,
|
68
|
+
JSON.stringify(response),
|
69
|
+
enableCors,
|
70
|
+
apiKeyRequired
|
45
71
|
);
|
46
72
|
}
|
47
73
|
|
48
|
-
private static createIntegrationResponse(
|
74
|
+
private static createIntegrationResponse(
|
75
|
+
response: string,
|
76
|
+
mediaType: MediaType,
|
77
|
+
enableCors: boolean
|
78
|
+
) {
|
49
79
|
const integrationResponse = {
|
50
|
-
statusCode:
|
80
|
+
statusCode: "200",
|
51
81
|
responseTemplates: {
|
52
82
|
[mediaType]: response,
|
53
83
|
},
|
54
84
|
};
|
55
85
|
|
56
|
-
return enableCors
|
86
|
+
return enableCors
|
87
|
+
? { ...integrationResponse, ...RESPONSE_CORS_INTEGRATION }
|
88
|
+
: integrationResponse;
|
57
89
|
}
|
58
90
|
|
59
91
|
private static createMethodResponse(enableCors: boolean) {
|
60
|
-
return enableCors
|
92
|
+
return enableCors
|
93
|
+
? corsMethod(METHOD_RESPONSE_200)
|
94
|
+
: METHOD_RESPONSE_200;
|
61
95
|
}
|
62
96
|
}
|
@@ -1,24 +1,18 @@
|
|
1
|
-
import {
|
1
|
+
import {withSecret, withSecretAndPrefix} from "./secret";
|
2
2
|
|
3
3
|
export type DbSecret = {
|
4
|
-
readonly username: string
|
5
|
-
readonly password: string
|
6
|
-
readonly host: string
|
7
|
-
readonly ro_host: string
|
4
|
+
readonly username: string
|
5
|
+
readonly password: string
|
6
|
+
readonly host: string
|
7
|
+
readonly ro_host: string
|
8
8
|
};
|
9
9
|
|
10
10
|
export enum RdsProxySecretKey {
|
11
|
-
username = "username",
|
12
|
-
password = "password",
|
13
|
-
proxy_host = "proxy_host",
|
14
|
-
proxy_ro_host = "proxy_ro_host",
|
11
|
+
username = "username", password = "password", proxy_host = "proxy_host", proxy_ro_host = "proxy_ro_host"
|
15
12
|
}
|
16
13
|
|
17
14
|
export enum RdsSecretKey {
|
18
|
-
username = "username",
|
19
|
-
password = "password",
|
20
|
-
host = "host",
|
21
|
-
ro_host = "ro_host",
|
15
|
+
username = "username", password = "password", host = "host", ro_host = "ro_host"
|
22
16
|
}
|
23
17
|
|
24
18
|
export type RdsProxySecret = Record<RdsProxySecretKey, string>;
|
@@ -43,7 +37,7 @@ function setDbSecret(secret: DbSecret) {
|
|
43
37
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
44
38
|
let cachedSecret: any;
|
45
39
|
|
46
|
-
const missingSecretErrorText =
|
40
|
+
const missingSecretErrorText = 'Missing or empty secretId';
|
47
41
|
|
48
42
|
/**
|
49
43
|
* You can give the following options for retrieving a secret:
|
@@ -54,22 +48,13 @@ const missingSecretErrorText = "Missing or empty secretId";
|
|
54
48
|
|
55
49
|
*/
|
56
50
|
export type SecretOptions = {
|
57
|
-
readonly expectedKeys?: string[]
|
58
|
-
readonly prefix?: string
|
59
|
-
}
|
51
|
+
readonly expectedKeys?: string[],
|
52
|
+
readonly prefix?: string
|
53
|
+
}
|
60
54
|
|
61
|
-
export type SecretToPromiseFunction<Secret, Response = void> = (
|
62
|
-
|
63
|
-
|
64
|
-
export type SecretFunction<Secret, Response = void> = (
|
65
|
-
secretId: string,
|
66
|
-
fn: SecretToPromiseFunction<Secret, Response>,
|
67
|
-
options?: SecretOptions
|
68
|
-
) => Promise<Response | void>;
|
69
|
-
export type EmptySecretFunction<Response = void> = SecretFunction<
|
70
|
-
DbSecret,
|
71
|
-
Response
|
72
|
-
>;
|
55
|
+
export type SecretToPromiseFunction<Secret, Response = void> = (secret: Secret) => Promise<Response> | void;
|
56
|
+
export type SecretFunction<Secret, Response = void> = (secretId: string, fn: SecretToPromiseFunction<Secret, Response>, options?: SecretOptions) => Promise<Response | void>;
|
57
|
+
export type EmptySecretFunction<Response = void> = SecretFunction<DbSecret, Response>;
|
73
58
|
|
74
59
|
/**
|
75
60
|
* Run the given function with secret retrieved from Secrets Manager. Also injects database-credentials into environment.
|
@@ -81,11 +66,7 @@ export type EmptySecretFunction<Response = void> = SecretFunction<
|
|
81
66
|
* @param {function} fn
|
82
67
|
* @param {SecretOptions} options
|
83
68
|
*/
|
84
|
-
export async function withDbSecret<Secret, Response>(
|
85
|
-
secretId: string,
|
86
|
-
fn: SecretToPromiseFunction<Secret, Response>,
|
87
|
-
options?: SecretOptions
|
88
|
-
): Promise<Response | void> {
|
69
|
+
export async function withDbSecret<Secret, Response>(secretId: string, fn: SecretToPromiseFunction<Secret, Response>, options?: SecretOptions): Promise<Response | void> {
|
89
70
|
if (!secretId) {
|
90
71
|
console.error(missingSecretErrorText);
|
91
72
|
return Promise.reject(missingSecretErrorText);
|
@@ -100,13 +81,9 @@ export async function withDbSecret<Secret, Response>(
|
|
100
81
|
});
|
101
82
|
|
102
83
|
// then actual secret
|
103
|
-
await withSecretAndPrefix(
|
104
|
-
|
105
|
-
|
106
|
-
(fetchedSecret: Secret) => {
|
107
|
-
cachedSecret = fetchedSecret;
|
108
|
-
}
|
109
|
-
);
|
84
|
+
await withSecretAndPrefix(secretId, options.prefix, (fetchedSecret: Secret) => {
|
85
|
+
cachedSecret = fetchedSecret;
|
86
|
+
});
|
110
87
|
} else {
|
111
88
|
await withSecret(secretId, (fetchedSecret: DbSecret) => {
|
112
89
|
setDbSecret(fetchedSecret);
|
@@ -120,10 +97,7 @@ export async function withDbSecret<Secret, Response>(
|
|
120
97
|
}
|
121
98
|
return fn(cachedSecret);
|
122
99
|
} catch (error) {
|
123
|
-
console.error(
|
124
|
-
"method=withDbSecret Caught an error, refreshing secret",
|
125
|
-
error
|
126
|
-
);
|
100
|
+
console.error('method=withDbSecret Caught an error, refreshing secret', error);
|
127
101
|
// try to refetch secret in case it has changed
|
128
102
|
await withSecret(secretId, (fetchedSecret: DbSecret) => {
|
129
103
|
setDbSecret(fetchedSecret);
|
@@ -133,15 +107,10 @@ export async function withDbSecret<Secret, Response>(
|
|
133
107
|
}
|
134
108
|
}
|
135
109
|
|
136
|
-
export function checkExpectedSecretKeys<Secret
|
137
|
-
keys
|
138
|
-
secret: Secret
|
139
|
-
) {
|
140
|
-
const missingKeys = keys.filter((key) => !(key in secret));
|
110
|
+
export function checkExpectedSecretKeys<Secret>(keys: string[], secret: Secret) {
|
111
|
+
const missingKeys = keys.filter(key => !(key in secret));
|
141
112
|
if (missingKeys.length) {
|
142
|
-
console.error(
|
143
|
-
|
144
|
-
);
|
145
|
-
throw new Error("Expected keys were not found");
|
113
|
+
console.error(`method=checkExpectedSecretKeys secret didn't contain the key(s) ${missingKeys}`);
|
114
|
+
throw new Error('Expected keys were not found');
|
146
115
|
}
|
147
116
|
}
|
@@ -23,7 +23,7 @@ const DEFAULT_CONFIGURATION = {
|
|
23
23
|
*
|
24
24
|
* Supports setting the database environment paramaters from the secret too.
|
25
25
|
*/
|
26
|
-
export class SecretHolder<Secret
|
26
|
+
export class SecretHolder<Secret> {
|
27
27
|
private readonly secretId: string;
|
28
28
|
private readonly prefix: string;
|
29
29
|
private readonly expectedKeys: string[];
|
@@ -51,7 +51,7 @@ export class SecretHolder<Secret extends GenericSecret> {
|
|
51
51
|
this.secretCache.push(DEFAULT_SECRET_KEY, secretValue);
|
52
52
|
}
|
53
53
|
|
54
|
-
public static create<S
|
54
|
+
public static create<S>(
|
55
55
|
prefix = DEFAULT_PREFIX,
|
56
56
|
expectedKeys: string[] = []
|
57
57
|
) {
|
@@ -67,7 +67,10 @@ export class SecretHolder<Secret extends GenericSecret> {
|
|
67
67
|
const parsedSecret =
|
68
68
|
this.prefix === DEFAULT_PREFIX
|
69
69
|
? secret
|
70
|
-
: this.parseSecret(
|
70
|
+
: this.parseSecret(
|
71
|
+
secret as unknown as GenericSecret,
|
72
|
+
`${this.prefix}.`
|
73
|
+
);
|
71
74
|
|
72
75
|
if (this.expectedKeys.length > 0) {
|
73
76
|
checkExpectedSecretKeys(this.expectedKeys, parsedSecret);
|
package/src/database/database.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { IDatabase, ITask } from "pg-promise";
|
2
2
|
import { DatabaseEnvironmentKeys } from "../aws/runtime/secrets/dbsecret";
|
3
|
-
import { getEnvVariable
|
3
|
+
import { getEnvVariable } from "../utils/utils";
|
4
4
|
import { envValue } from "../aws/runtime/environment";
|
5
5
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|