@digitraffic/common 2024.1.24-3 → 2024.3.11-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/__test__/api/handler-factory.test.d.mts +1 -0
- package/dist/__test__/api/handler-factory.test.mjs +43 -0
- package/dist/__test__/api/response.test.d.mts +1 -0
- package/dist/__test__/api/response.test.mjs +86 -0
- package/dist/__test__/imports.test.d.mts +1 -0
- package/dist/__test__/imports.test.mjs +332 -0
- package/dist/__test__/marine/id_utils.test.d.mts +1 -0
- package/dist/__test__/marine/id_utils.test.mjs +44 -0
- package/dist/__test__/promise/promise.test.d.mts +1 -0
- package/dist/__test__/promise/promise.test.mjs +130 -0
- package/dist/__test__/runtime/dt-logger.test.d.mts +1 -0
- package/dist/__test__/runtime/dt-logger.test.mjs +108 -0
- package/dist/__test__/secrets/secret-holder.test.d.mts +1 -0
- package/dist/__test__/secrets/secret-holder.test.mjs +86 -0
- package/dist/__test__/secrets/secret.test.d.mts +1 -0
- package/dist/__test__/secrets/secret.test.mjs +38 -0
- package/dist/__test__/test/httpserver.test.d.mts +1 -0
- package/dist/__test__/test/httpserver.test.mjs +154 -0
- package/dist/__test__/test/mock-ky.test.d.mts +1 -0
- package/dist/__test__/test/mock-ky.test.mjs +46 -0
- package/dist/__test__/types/lambda-response.test.d.mts +1 -0
- package/dist/__test__/types/lambda-response.test.mjs +58 -0
- package/dist/__test__/utils/date-utils.test.d.mts +1 -0
- package/dist/__test__/utils/date-utils.test.mjs +27 -0
- package/dist/__test__/utils/geometry.test.d.mts +1 -0
- package/dist/__test__/utils/geometry.test.mjs +24 -0
- package/dist/__test__/utils/logging.test.d.mts +1 -0
- package/dist/__test__/utils/logging.test.mjs +78 -0
- package/dist/__test__/utils/utils.test.d.mts +1 -0
- package/dist/__test__/utils/utils.test.mjs +43 -0
- package/dist/aws/infra/api/handler-factory.mjs +4 -0
- package/dist/aws/infra/api/integration.d.mts +2 -2
- package/dist/aws/infra/api/integration.mjs +4 -1
- package/dist/aws/infra/api/response.d.mts +1 -1
- package/dist/aws/infra/api/responses.d.mts +1 -1
- package/dist/aws/infra/api/responses.mjs +2 -0
- package/dist/aws/infra/api/static-integration.mjs +1 -1
- package/dist/aws/infra/canaries/canary-alarm.d.mts +1 -1
- package/dist/aws/infra/canaries/canary-alarm.mjs +2 -0
- package/dist/aws/infra/canaries/canary-parameters.mjs +1 -1
- package/dist/aws/infra/canaries/canary-role.mjs +1 -0
- package/dist/aws/infra/canaries/canary.d.mts +2 -2
- package/dist/aws/infra/canaries/canary.mjs +2 -0
- package/dist/aws/infra/canaries/database-canary.d.mts +2 -2
- package/dist/aws/infra/canaries/database-canary.mjs +2 -0
- package/dist/aws/infra/canaries/database-checker.d.mts +1 -1
- package/dist/aws/infra/canaries/database-checker.mjs +7 -1
- package/dist/aws/infra/canaries/url-canary.d.mts +2 -2
- package/dist/aws/infra/canaries/url-canary.mjs +3 -0
- package/dist/aws/infra/canaries/url-checker.d.mts +1 -1
- package/dist/aws/infra/canaries/url-checker.mjs +4 -1
- package/dist/aws/infra/documentation.mjs +5 -1
- package/dist/aws/infra/import-util.d.mts +1 -1
- package/dist/aws/infra/import-util.mjs +4 -3
- package/dist/aws/infra/scheduler.mjs +2 -0
- package/dist/aws/infra/security-rule.d.mts +1 -1
- package/dist/aws/infra/security-rule.mjs +1 -0
- package/dist/aws/infra/sqs-integration.d.mts +1 -1
- package/dist/aws/infra/sqs-integration.mjs +3 -1
- package/dist/aws/infra/sqs-queue.d.mts +1 -1
- package/dist/aws/infra/sqs-queue.mjs +2 -1
- package/dist/aws/infra/stack/lambda-configs.d.mts +4 -4
- package/dist/aws/infra/stack/lambda-configs.mjs +4 -2
- package/dist/aws/infra/stack/monitoredfunction.d.mts +3 -3
- package/dist/aws/infra/stack/monitoredfunction.mjs +23 -18
- package/dist/aws/infra/stack/parameters.mjs +1 -0
- package/dist/aws/infra/stack/rest_apis.d.mts +2 -2
- package/dist/aws/infra/stack/rest_apis.mjs +6 -1
- package/dist/aws/infra/stack/stack-checking-aspect.d.mts +2 -2
- package/dist/aws/infra/stack/stack-checking-aspect.mjs +6 -1
- package/dist/aws/infra/stack/stack.d.mts +5 -5
- package/dist/aws/infra/stack/stack.mjs +9 -0
- package/dist/aws/infra/stack/subscription.mjs +4 -0
- package/dist/aws/infra/stacks/db-dns-stack.d.mts +1 -1
- package/dist/aws/infra/stacks/db-dns-stack.mjs +1 -0
- package/dist/aws/infra/stacks/db-proxy-stack.d.mts +3 -3
- package/dist/aws/infra/stacks/db-proxy-stack.mjs +4 -2
- package/dist/aws/infra/stacks/db-stack.d.mts +3 -3
- package/dist/aws/infra/stacks/db-stack.mjs +11 -7
- package/dist/aws/infra/stacks/intra-stack-configuration.d.mts +1 -1
- package/dist/aws/infra/stacks/network-stack.d.mts +2 -2
- package/dist/aws/infra/stacks/network-stack.mjs +8 -0
- package/dist/aws/infra/usage-plans.d.mts +1 -1
- package/dist/aws/infra/usage-plans.mjs +1 -0
- package/dist/aws/runtime/apikey.d.mts +2 -2
- package/dist/aws/runtime/apikey.mjs +2 -2
- package/dist/aws/runtime/digitraffic-integration-response.d.mts +1 -1
- package/dist/aws/runtime/dt-logger.mjs +6 -2
- package/dist/aws/runtime/messaging.d.mts +2 -2
- package/dist/aws/runtime/messaging.mjs +5 -4
- package/dist/aws/runtime/s3.d.mts +4 -2
- package/dist/aws/runtime/s3.mjs +15 -10
- package/dist/aws/runtime/secrets/dbsecret.d.mts +1 -1
- package/dist/aws/runtime/secrets/proxy-holder.mjs +1 -0
- package/dist/aws/runtime/secrets/rds-holder.mjs +1 -0
- package/dist/aws/runtime/secrets/secret-holder.d.mts +1 -1
- package/dist/aws/runtime/secrets/secret-holder.mjs +6 -1
- package/dist/aws/runtime/secrets/secret.mjs +5 -6
- package/dist/aws/types/errors.mjs +1 -0
- package/dist/aws/types/lambda-response.mjs +5 -0
- package/dist/aws/types/model-with-reference.mjs +1 -1
- package/dist/database/cached.d.mts +1 -1
- package/dist/database/database.mjs +1 -0
- package/dist/database/last-updated.d.mts +1 -1
- package/dist/test/db-testutils.d.mts +1 -1
- package/dist/test/db-testutils.mjs +1 -1
- package/dist/test/httpserver.mjs +7 -3
- package/dist/test/mock-ky.d.mts +2 -0
- package/dist/test/mock-ky.mjs +15 -0
- package/dist/test/secrets-manager.d.mts +3 -2
- package/dist/test/secrets-manager.mjs +14 -16
- package/dist/test/testutils.mjs +1 -1
- package/dist/types/http-error.mjs +1 -0
- package/dist/types/nullable.d.mts +1 -1
- package/dist/utils/api-model.d.mts +2 -2
- package/dist/utils/api-model.mjs +1 -1
- package/dist/utils/geojson-types.d.mts +1 -1
- package/dist/utils/geojson-types.mjs +4 -2
- package/dist/utils/geometry.d.mts +1 -1
- package/dist/utils/geometry.mjs +3 -0
- package/dist/utils/logging.mjs +2 -2
- package/dist/utils/retry.d.mts +2 -2
- package/dist/utils/retry.mjs +2 -2
- package/dist/utils/slack.mjs +4 -3
- package/dist/utils/utils.d.mts +2 -2
- package/package.json +25 -15
- package/src/@types/geojson-validation/index.d.mts +0 -4
- package/src/aws/infra/api/handler-factory.mts +0 -86
- package/src/aws/infra/api/integration.mts +0 -147
- package/src/aws/infra/api/response.mts +0 -165
- package/src/aws/infra/api/responses.mts +0 -127
- package/src/aws/infra/api/static-integration.mts +0 -108
- package/src/aws/infra/canaries/Synthetics.d.mts +0 -21
- package/src/aws/infra/canaries/canary-alarm.mts +0 -33
- package/src/aws/infra/canaries/canary-keys.mts +0 -3
- package/src/aws/infra/canaries/canary-parameters.mts +0 -19
- package/src/aws/infra/canaries/canary-role.mts +0 -73
- package/src/aws/infra/canaries/canary.mts +0 -44
- package/src/aws/infra/canaries/database-canary.mts +0 -98
- package/src/aws/infra/canaries/database-checker.mts +0 -163
- package/src/aws/infra/canaries/url-canary.mts +0 -98
- package/src/aws/infra/canaries/url-checker.mts +0 -388
- package/src/aws/infra/documentation.mts +0 -142
- package/src/aws/infra/import-util.mts +0 -57
- package/src/aws/infra/scheduler.mts +0 -59
- package/src/aws/infra/security-rule.mts +0 -38
- package/src/aws/infra/sqs-integration.mts +0 -106
- package/src/aws/infra/sqs-queue.mts +0 -162
- package/src/aws/infra/stack/lambda-configs.mts +0 -135
- package/src/aws/infra/stack/monitoredfunction.mts +0 -352
- package/src/aws/infra/stack/parameters.mts +0 -74
- package/src/aws/infra/stack/rest_apis.mts +0 -322
- package/src/aws/infra/stack/stack-checking-aspect.mts +0 -233
- package/src/aws/infra/stack/stack.mts +0 -144
- package/src/aws/infra/stack/subscription.mts +0 -58
- package/src/aws/infra/stacks/db-dns-stack.mts +0 -77
- package/src/aws/infra/stacks/db-proxy-stack.mts +0 -134
- package/src/aws/infra/stacks/db-stack.mts +0 -292
- package/src/aws/infra/stacks/intra-stack-configuration.mts +0 -6
- package/src/aws/infra/stacks/network-stack.mts +0 -76
- package/src/aws/infra/usage-plans.mts +0 -50
- package/src/aws/runtime/apikey.mts +0 -9
- package/src/aws/runtime/digitraffic-integration-response.mts +0 -35
- package/src/aws/runtime/dt-logger-default.mts +0 -11
- package/src/aws/runtime/dt-logger.mts +0 -184
- package/src/aws/runtime/environment.mts +0 -22
- package/src/aws/runtime/messaging.mts +0 -26
- package/src/aws/runtime/s3.mts +0 -44
- package/src/aws/runtime/secrets/dbsecret.mts +0 -31
- package/src/aws/runtime/secrets/node-ttl.d.mts +0 -12
- package/src/aws/runtime/secrets/proxy-holder.mts +0 -34
- package/src/aws/runtime/secrets/rds-holder.mts +0 -34
- package/src/aws/runtime/secrets/secret-holder.mts +0 -106
- package/src/aws/runtime/secrets/secret.mts +0 -58
- package/src/aws/types/errors.mts +0 -14
- package/src/aws/types/lambda-response.mts +0 -100
- package/src/aws/types/mediatypes.mts +0 -12
- package/src/aws/types/model-with-reference.mts +0 -8
- package/src/aws/types/proxytypes.mts +0 -27
- package/src/aws/types/tags.mts +0 -3
- package/src/database/cached.mts +0 -64
- package/src/database/database.mts +0 -107
- package/src/database/last-updated.mts +0 -103
- package/src/database/models.mts +0 -7
- package/src/index.mts +0 -2
- package/src/marine/id_utils.mts +0 -30
- package/src/marine/rtz.mts +0 -57
- package/src/test/asserter.mts +0 -58
- package/src/test/db-testutils.mts +0 -52
- package/src/test/httpserver.mts +0 -111
- package/src/test/secrets-manager.mts +0 -37
- package/src/test/testutils.mts +0 -39
- package/src/types/async-timeout-error.mts +0 -5
- package/src/types/aws-env.mts +0 -3
- package/src/types/either.mts +0 -9
- package/src/types/http-error.mts +0 -8
- package/src/types/input-error.mts +0 -2
- package/src/types/language.mts +0 -3
- package/src/types/nullable.mts +0 -21
- package/src/types/traffictype.mts +0 -8
- package/src/types/urn.mts +0 -1
- package/src/types/util-types.mts +0 -10
- package/src/types/validator.mts +0 -10
- package/src/utils/api-model.mts +0 -133
- package/src/utils/base64.mts +0 -16
- package/src/utils/date-utils.mts +0 -53
- package/src/utils/geojson-types.mts +0 -22
- package/src/utils/geometry.mts +0 -171
- package/src/utils/logging.mts +0 -75
- package/src/utils/retry.mts +0 -200
- package/src/utils/slack.mts +0 -26
- package/src/utils/utils.mts +0 -184
@@ -1,35 +0,0 @@
|
|
1
|
-
import { IntegrationResponse } from "aws-cdk-lib/aws-apigateway";
|
2
|
-
import { MediaType } from "../types/mediatypes.mjs";
|
3
|
-
import {
|
4
|
-
getDeprecatedDefaultLambdaResponse,
|
5
|
-
RESPONSE_DEFAULT_LAMBDA,
|
6
|
-
} from "../infra/api/response.mjs";
|
7
|
-
|
8
|
-
export abstract class DigitrafficIntegrationResponse {
|
9
|
-
static ok(mediaType: MediaType, sunset?: string): IntegrationResponse {
|
10
|
-
return this.create("200", mediaType, sunset);
|
11
|
-
}
|
12
|
-
|
13
|
-
static badRequest(mediaType?: MediaType): IntegrationResponse {
|
14
|
-
return this.create("400", mediaType ?? MediaType.TEXT_PLAIN);
|
15
|
-
}
|
16
|
-
|
17
|
-
static notImplemented(mediaType?: MediaType): IntegrationResponse {
|
18
|
-
return this.create("501", mediaType ?? MediaType.TEXT_PLAIN);
|
19
|
-
}
|
20
|
-
|
21
|
-
static create(
|
22
|
-
statusCode: string,
|
23
|
-
mediaType: MediaType,
|
24
|
-
sunset?: string
|
25
|
-
): IntegrationResponse {
|
26
|
-
return {
|
27
|
-
statusCode,
|
28
|
-
responseTemplates: {
|
29
|
-
[mediaType]: sunset
|
30
|
-
? getDeprecatedDefaultLambdaResponse(sunset)
|
31
|
-
: RESPONSE_DEFAULT_LAMBDA,
|
32
|
-
},
|
33
|
-
};
|
34
|
-
}
|
35
|
-
}
|
@@ -1,11 +0,0 @@
|
|
1
|
-
import { DtLogger } from "./dt-logger.mjs";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* You can use this for method name definition to match DtLogger LoggableType.method parameter.
|
5
|
-
*/
|
6
|
-
export type { LoggerMethodType } from "./dt-logger.mjs";
|
7
|
-
|
8
|
-
/**
|
9
|
-
* You can use this for your logging needs or create one locally and configure it as you wish.
|
10
|
-
*/
|
11
|
-
export const logger = new DtLogger();
|
@@ -1,184 +0,0 @@
|
|
1
|
-
import { Writable } from "stream";
|
2
|
-
import _ from "lodash";
|
3
|
-
|
4
|
-
/** Logging level */
|
5
|
-
export type LOG_LEVEL = "DEBUG" | "INFO" | "WARN" | "ERROR";
|
6
|
-
export type LoggerMethodType = `${string}.${string}`;
|
7
|
-
|
8
|
-
/**
|
9
|
-
* Configuration object for configuring the Digitraffic logging utility
|
10
|
-
* @see {@link DtLogger}
|
11
|
-
*/
|
12
|
-
export interface LoggerConfiguration {
|
13
|
-
/** Name of the lambda */
|
14
|
-
lambdaName?: string;
|
15
|
-
/** The file name where the logging occurs */
|
16
|
-
fileName?: string;
|
17
|
-
/** The lambda runtime environment */
|
18
|
-
runTime?: string;
|
19
|
-
/** Custom end point to write the logs to */
|
20
|
-
writeStream?: Writable;
|
21
|
-
}
|
22
|
-
|
23
|
-
interface LoggableTypeInternal extends LoggableType {
|
24
|
-
level: LOG_LEVEL;
|
25
|
-
}
|
26
|
-
|
27
|
-
/**
|
28
|
-
* CustomParams allows to add keys prefixed with `custom` keyword to be added to an
|
29
|
-
* object.
|
30
|
-
*/
|
31
|
-
export interface CustomParams {
|
32
|
-
/** do not log your apikey! */
|
33
|
-
customApikey?: never;
|
34
|
-
/** do not log your apikey! */
|
35
|
-
customApiKey?: never;
|
36
|
-
[key: `custom${Capitalize<string>}Count`]: number;
|
37
|
-
|
38
|
-
[key: `custom${Capitalize<string>}`]:
|
39
|
-
| string
|
40
|
-
| number
|
41
|
-
| bigint
|
42
|
-
| boolean
|
43
|
-
| Date
|
44
|
-
| null
|
45
|
-
| undefined;
|
46
|
-
}
|
47
|
-
|
48
|
-
/**
|
49
|
-
* Digitraffic logging object.
|
50
|
-
*
|
51
|
-
* `method` property is the only required propetry. {@link CustomParams} can be added by
|
52
|
-
* prefixin the property with keyword `custom`. The prefix is removed before writing to
|
53
|
-
* logging end point.
|
54
|
-
*
|
55
|
-
* @see {@link CustomParams}
|
56
|
-
*/
|
57
|
-
export interface LoggableType extends CustomParams {
|
58
|
-
/** Name of the method logging the message */
|
59
|
-
method: LoggerMethodType;
|
60
|
-
/** Message to log, optional */
|
61
|
-
message?: string;
|
62
|
-
/** Type of message, optional */
|
63
|
-
type?: string;
|
64
|
-
/** Stack trace, optional */
|
65
|
-
stack?: string | undefined;
|
66
|
-
/** Amount of time some operation took in milliseconds, optional */
|
67
|
-
tookMs?: number;
|
68
|
-
/** Pass error object, which will be stringified before logging */
|
69
|
-
error?: unknown;
|
70
|
-
}
|
71
|
-
|
72
|
-
/**
|
73
|
-
* Helper class for json-logging.
|
74
|
-
*
|
75
|
-
* Logged line will include:
|
76
|
-
* * log-level
|
77
|
-
* * lambdaName (taken from process environment)
|
78
|
-
* * runtime (taken from process environment)
|
79
|
-
* * the actual message (as json or as string)
|
80
|
-
*/
|
81
|
-
export class DtLogger {
|
82
|
-
readonly lambdaName?: string;
|
83
|
-
readonly runtime?: string;
|
84
|
-
|
85
|
-
readonly writeStream: Writable;
|
86
|
-
|
87
|
-
/**
|
88
|
-
* Create a new Logger instance.
|
89
|
-
* @constructor
|
90
|
-
* @param {LoggerConfiguration?} [config] - Accepts configuration options @see {@link LoggerConfiguration}
|
91
|
-
*/
|
92
|
-
constructor(config?: LoggerConfiguration) {
|
93
|
-
this.lambdaName =
|
94
|
-
config?.lambdaName ?? process.env.AWS_LAMBDA_FUNCTION_NAME;
|
95
|
-
this.runtime = config?.runTime ?? process.env.AWS_EXECUTION_ENV;
|
96
|
-
this.writeStream = config?.writeStream ?? process.stdout;
|
97
|
-
}
|
98
|
-
|
99
|
-
/**
|
100
|
-
* Log given message with level DEBUG. This will not be forwarded to centralized logging system!.
|
101
|
-
*
|
102
|
-
* @param message anything
|
103
|
-
* @see {@link LoggableType}
|
104
|
-
* @see {@link DtLogger.log}
|
105
|
-
*/
|
106
|
-
debug(message: unknown): void {
|
107
|
-
const logMessage = {
|
108
|
-
message,
|
109
|
-
level: "DEBUG",
|
110
|
-
lambdaName: this.lambdaName,
|
111
|
-
runtime: this.runtime,
|
112
|
-
};
|
113
|
-
|
114
|
-
this.writeStream.write(JSON.stringify(logMessage) + "\n");
|
115
|
-
}
|
116
|
-
|
117
|
-
/**
|
118
|
-
* Log given message with level INFO
|
119
|
-
*
|
120
|
-
* @param message Json-object to log
|
121
|
-
* @see {@link LoggableType}
|
122
|
-
* @see {@link DtLogger.log}
|
123
|
-
*/
|
124
|
-
info(message: LoggableType): void {
|
125
|
-
this.log({ ...message, level: "INFO" });
|
126
|
-
}
|
127
|
-
|
128
|
-
/**
|
129
|
-
* Log given message with level WARN
|
130
|
-
*
|
131
|
-
* @param message Json-object to log
|
132
|
-
* @see {@link LoggableType}
|
133
|
-
* @see {@link DtLogger.log}
|
134
|
-
*/
|
135
|
-
warn(message: LoggableType): void {
|
136
|
-
this.log({ ...message, level: "WARN" });
|
137
|
-
}
|
138
|
-
/**
|
139
|
-
* Log given message with level INFO
|
140
|
-
*
|
141
|
-
* @param message Json-object to log
|
142
|
-
* @see {@link LoggableType}
|
143
|
-
* @see {@link DtLogger.log}
|
144
|
-
*/
|
145
|
-
error(message: LoggableType): void {
|
146
|
-
this.log({
|
147
|
-
...message,
|
148
|
-
level: "ERROR",
|
149
|
-
});
|
150
|
-
}
|
151
|
-
|
152
|
-
/**
|
153
|
-
* Log message with given log level.
|
154
|
-
*
|
155
|
-
* Some metadata is also added to the message:
|
156
|
-
* * runtime - can be configured with constructor or inferred from environment
|
157
|
-
* * lambdaName - can be configured with constructor or inferred from environment
|
158
|
-
*
|
159
|
-
* @param message Json-object to log
|
160
|
-
* @see {@link LoggableType}
|
161
|
-
*/
|
162
|
-
private log(message: LoggableTypeInternal): void {
|
163
|
-
const error = message.error
|
164
|
-
? typeof message.error === "string"
|
165
|
-
? message.error
|
166
|
-
: JSON.stringify(message.error)
|
167
|
-
: undefined;
|
168
|
-
|
169
|
-
const logMessage = {
|
170
|
-
...removePrefix("custom", message),
|
171
|
-
error,
|
172
|
-
lambdaName: this.lambdaName,
|
173
|
-
runtime: this.runtime,
|
174
|
-
};
|
175
|
-
|
176
|
-
this.writeStream.write(JSON.stringify(logMessage) + "\n");
|
177
|
-
}
|
178
|
-
}
|
179
|
-
|
180
|
-
function removePrefix(prefix: string, loggable: LoggableType) {
|
181
|
-
return _.mapKeys(loggable, (_index, key: string) =>
|
182
|
-
key.startsWith(prefix) ? _.lowerFirst(key.replace(prefix, "")) : key
|
183
|
-
);
|
184
|
-
}
|
@@ -1,22 +0,0 @@
|
|
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
|
-
*/
|
10
|
-
export function envValue(key: string, defaultValue?: string): string {
|
11
|
-
const value = process.env[key];
|
12
|
-
|
13
|
-
if (value == null) {
|
14
|
-
if (defaultValue) {
|
15
|
-
return defaultValue;
|
16
|
-
}
|
17
|
-
|
18
|
-
throw new Error(`Missing environment value ${key}`);
|
19
|
-
}
|
20
|
-
|
21
|
-
return value;
|
22
|
-
}
|
@@ -1,26 +0,0 @@
|
|
1
|
-
import {SNS} from "aws-sdk";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Utility function for publishing SNS messages.
|
5
|
-
* Made because using *await* with AWS APIs doesn't require calling promise() but nothing works if it isn't called.
|
6
|
-
* Retries a single time in case of failure.
|
7
|
-
* @param message
|
8
|
-
* @param topicArn
|
9
|
-
* @param sns
|
10
|
-
*/
|
11
|
-
export async function snsPublish(message: string, topicArn: string, sns: SNS) {
|
12
|
-
const publishParams = {
|
13
|
-
Message: message,
|
14
|
-
TopicArn: topicArn,
|
15
|
-
};
|
16
|
-
try {
|
17
|
-
await sns.publish(publishParams).promise();
|
18
|
-
} catch (error) {
|
19
|
-
console.error('method=snsPublish error, retrying', error);
|
20
|
-
try {
|
21
|
-
await sns.publish(publishParams).promise();
|
22
|
-
} catch (e2) {
|
23
|
-
console.error('method=snsPublish error after retry', e2);
|
24
|
-
}
|
25
|
-
}
|
26
|
-
}
|
package/src/aws/runtime/s3.mts
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
import {S3} from "aws-sdk";
|
2
|
-
|
3
|
-
export async function uploadToS3<Body extends S3.Body | undefined>(
|
4
|
-
bucketName: string,
|
5
|
-
body: Body,
|
6
|
-
objectName: string,
|
7
|
-
cannedAcl?: string,
|
8
|
-
contentType?: string,
|
9
|
-
) {
|
10
|
-
|
11
|
-
const s3 = new S3();
|
12
|
-
try {
|
13
|
-
await doUpload(
|
14
|
-
s3, bucketName, body, objectName, cannedAcl, contentType,
|
15
|
-
);
|
16
|
-
} catch (error) {
|
17
|
-
console.warn('method=uploadToS3 retrying upload to bucket %s', bucketName);
|
18
|
-
try {
|
19
|
-
await doUpload(
|
20
|
-
s3, bucketName, body, objectName, cannedAcl, contentType,
|
21
|
-
);
|
22
|
-
} catch (e2) {
|
23
|
-
console.error('method=uploadToS3 failed retrying upload to bucket %s', bucketName);
|
24
|
-
}
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
function doUpload<Body extends S3.Body | undefined>(
|
29
|
-
s3: S3,
|
30
|
-
bucketName: string,
|
31
|
-
body: Body,
|
32
|
-
filename: string,
|
33
|
-
cannedAcl?: string,
|
34
|
-
contentType?: string,
|
35
|
-
) {
|
36
|
-
|
37
|
-
return s3.upload({
|
38
|
-
Bucket: bucketName,
|
39
|
-
Body: body,
|
40
|
-
Key: filename,
|
41
|
-
ACL: cannedAcl,
|
42
|
-
ContentType: contentType,
|
43
|
-
}).promise();
|
44
|
-
}
|
@@ -1,31 +0,0 @@
|
|
1
|
-
import { GenericSecret } from "./secret.mjs";
|
2
|
-
|
3
|
-
export enum RdsProxySecretKey {
|
4
|
-
username = "username",
|
5
|
-
password = "password",
|
6
|
-
proxy_host = "proxy_host",
|
7
|
-
proxy_ro_host = "proxy_ro_host",
|
8
|
-
}
|
9
|
-
|
10
|
-
export enum RdsSecretKey {
|
11
|
-
username = "username",
|
12
|
-
password = "password",
|
13
|
-
host = "host",
|
14
|
-
ro_host = "ro_host",
|
15
|
-
}
|
16
|
-
|
17
|
-
export type RdsProxySecret = Record<RdsProxySecretKey, string>;
|
18
|
-
export type RdsSecret = Record<RdsSecretKey, string>;
|
19
|
-
|
20
|
-
export function checkExpectedSecretKeys<Secret extends GenericSecret>(
|
21
|
-
keys: string[],
|
22
|
-
secret: Secret
|
23
|
-
) {
|
24
|
-
const missingKeys = keys.filter((key) => !(key in secret));
|
25
|
-
if (missingKeys.length) {
|
26
|
-
console.error(
|
27
|
-
`method=checkExpectedSecretKeys secret didn't contain the key(s) ${missingKeys.toString()}`
|
28
|
-
);
|
29
|
-
throw new Error("Expected keys were not found");
|
30
|
-
}
|
31
|
-
}
|
@@ -1,34 +0,0 @@
|
|
1
|
-
import { SecretHolder } from "./secret-holder.mjs";
|
2
|
-
import { RdsProxySecretKey, RdsProxySecret } from "./dbsecret.mjs";
|
3
|
-
import { getEnvVariable } from "../../../utils/utils.mjs";
|
4
|
-
import { DatabaseEnvironmentKeys } from "../../../database/database.mjs";
|
5
|
-
|
6
|
-
const RDS_PROXY_SECRET_KEYS = Object.values(RdsProxySecretKey);
|
7
|
-
|
8
|
-
/**
|
9
|
-
* Holds credentials for RDS Proxy access.
|
10
|
-
*/
|
11
|
-
export class ProxyHolder {
|
12
|
-
private readonly secretHolder;
|
13
|
-
|
14
|
-
constructor(secretId: string) {
|
15
|
-
this.secretHolder = new SecretHolder<RdsProxySecret>(
|
16
|
-
secretId,
|
17
|
-
"",
|
18
|
-
RDS_PROXY_SECRET_KEYS
|
19
|
-
);
|
20
|
-
}
|
21
|
-
|
22
|
-
static create() {
|
23
|
-
return new ProxyHolder(getEnvVariable("SECRET_ID"));
|
24
|
-
}
|
25
|
-
|
26
|
-
public async setCredentials() {
|
27
|
-
const secret = await this.secretHolder.get();
|
28
|
-
|
29
|
-
process.env[DatabaseEnvironmentKeys.DB_USER] = secret.username;
|
30
|
-
process.env[DatabaseEnvironmentKeys.DB_PASS] = secret.password;
|
31
|
-
process.env[DatabaseEnvironmentKeys.DB_URI] = secret.proxy_host;
|
32
|
-
process.env[DatabaseEnvironmentKeys.DB_RO_URI] = secret.proxy_ro_host;
|
33
|
-
}
|
34
|
-
}
|
@@ -1,34 +0,0 @@
|
|
1
|
-
import { SecretHolder } from "./secret-holder.mjs";
|
2
|
-
import { RdsSecret, RdsSecretKey } from "./dbsecret.mjs";
|
3
|
-
import { getEnvVariable } from "../../../utils/utils.mjs";
|
4
|
-
import { DatabaseEnvironmentKeys } from "../../../database/database.mjs";
|
5
|
-
|
6
|
-
const RDS_SECRET_KEYS = Object.values(RdsSecretKey);
|
7
|
-
|
8
|
-
/**
|
9
|
-
* Holds credentials for RDS access.
|
10
|
-
*/
|
11
|
-
export class RdsHolder {
|
12
|
-
private readonly secretHolder;
|
13
|
-
|
14
|
-
constructor(secretId: string) {
|
15
|
-
this.secretHolder = new SecretHolder<RdsSecret>(
|
16
|
-
secretId,
|
17
|
-
"",
|
18
|
-
RDS_SECRET_KEYS
|
19
|
-
);
|
20
|
-
}
|
21
|
-
|
22
|
-
static create() {
|
23
|
-
return new RdsHolder(getEnvVariable("SECRET_ID"));
|
24
|
-
}
|
25
|
-
|
26
|
-
public async setCredentials() {
|
27
|
-
const secret = await this.secretHolder.get();
|
28
|
-
|
29
|
-
process.env[DatabaseEnvironmentKeys.DB_USER] = secret.username;
|
30
|
-
process.env[DatabaseEnvironmentKeys.DB_PASS] = secret.password;
|
31
|
-
process.env[DatabaseEnvironmentKeys.DB_URI] = secret.host;
|
32
|
-
process.env[DatabaseEnvironmentKeys.DB_RO_URI] = secret.ro_host;
|
33
|
-
}
|
34
|
-
}
|
@@ -1,106 +0,0 @@
|
|
1
|
-
import { GenericSecret, getSecret } from "./secret.mjs";
|
2
|
-
import { checkExpectedSecretKeys } from "./dbsecret.mjs";
|
3
|
-
import { getEnvVariable } from "../../../utils/utils.mjs";
|
4
|
-
import { logger } from "../dt-logger-default.mjs";
|
5
|
-
|
6
|
-
import NodeTtl from "node-ttl";
|
7
|
-
|
8
|
-
const DEFAULT_PREFIX = "";
|
9
|
-
const DEFAULT_SECRET_KEY = "SECRET";
|
10
|
-
const DEFAULT_CONFIGURATION = {
|
11
|
-
ttl: 5 * 60, // timeout secrets in 5 minutes
|
12
|
-
};
|
13
|
-
|
14
|
-
/**
|
15
|
-
* Utility class for getting secrets from Secret Manager.
|
16
|
-
* Supports prefix for secrets, checking of expected keys and ttl-configuration.
|
17
|
-
*
|
18
|
-
* By default, secrets are cached for 5 minutes and then reread from the Secrets Manager(This can be overridden with configuration).
|
19
|
-
*
|
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
|
-
*
|
25
|
-
*/
|
26
|
-
export class SecretHolder<Secret extends GenericSecret> {
|
27
|
-
private readonly secretId: string;
|
28
|
-
private readonly prefix: string;
|
29
|
-
private readonly expectedKeys: string[];
|
30
|
-
|
31
|
-
private readonly secretCache;
|
32
|
-
|
33
|
-
constructor(
|
34
|
-
secretId: string,
|
35
|
-
prefix = "",
|
36
|
-
expectedKeys: string[] = [],
|
37
|
-
configuration = DEFAULT_CONFIGURATION
|
38
|
-
) {
|
39
|
-
this.secretId = secretId;
|
40
|
-
this.prefix = prefix;
|
41
|
-
this.expectedKeys = expectedKeys;
|
42
|
-
|
43
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
44
|
-
this.secretCache = new NodeTtl(configuration);
|
45
|
-
}
|
46
|
-
|
47
|
-
private async initSecret() {
|
48
|
-
const secretValue = await getSecret<Secret>(this.secretId);
|
49
|
-
|
50
|
-
logger.info({
|
51
|
-
method: "SecretHolder.initSecret",
|
52
|
-
message: "Refreshing secret " + this.secretId,
|
53
|
-
});
|
54
|
-
|
55
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
56
|
-
this.secretCache.push(DEFAULT_SECRET_KEY, secretValue);
|
57
|
-
}
|
58
|
-
|
59
|
-
public static create<S extends GenericSecret>(
|
60
|
-
prefix = DEFAULT_PREFIX,
|
61
|
-
expectedKeys: string[] = []
|
62
|
-
) {
|
63
|
-
return new SecretHolder<S>(
|
64
|
-
getEnvVariable("SECRET_ID"),
|
65
|
-
prefix,
|
66
|
-
expectedKeys
|
67
|
-
);
|
68
|
-
}
|
69
|
-
|
70
|
-
public async get(): Promise<Secret> {
|
71
|
-
const secret = await this.getSecret<Secret>();
|
72
|
-
const parsedSecret =
|
73
|
-
this.prefix === DEFAULT_PREFIX
|
74
|
-
? secret
|
75
|
-
: this.parseSecret(secret, `${this.prefix}.`);
|
76
|
-
|
77
|
-
if (this.expectedKeys.length > 0) {
|
78
|
-
checkExpectedSecretKeys(this.expectedKeys, parsedSecret);
|
79
|
-
}
|
80
|
-
|
81
|
-
return parsedSecret;
|
82
|
-
}
|
83
|
-
|
84
|
-
private parseSecret(secret: GenericSecret, prefix: string): Secret {
|
85
|
-
const parsed: GenericSecret = {};
|
86
|
-
const skip = prefix.length;
|
87
|
-
|
88
|
-
for (const key in secret) {
|
89
|
-
if (key.startsWith(prefix)) {
|
90
|
-
parsed[key.substring(skip)] = secret[key];
|
91
|
-
}
|
92
|
-
}
|
93
|
-
|
94
|
-
return parsed as unknown as Secret;
|
95
|
-
}
|
96
|
-
|
97
|
-
private async getSecret<S>(): Promise<S> {
|
98
|
-
const secret: S | undefined = this.secretCache.get(DEFAULT_SECRET_KEY);
|
99
|
-
|
100
|
-
if (!secret) {
|
101
|
-
await this.initSecret();
|
102
|
-
}
|
103
|
-
|
104
|
-
return secret ?? (this.secretCache.get(DEFAULT_SECRET_KEY));
|
105
|
-
}
|
106
|
-
}
|
@@ -1,58 +0,0 @@
|
|
1
|
-
import { SecretsManager } from "aws-sdk";
|
2
|
-
import { getEnvVariable, getEnvVariableOrElse } from "../../../utils/utils.mjs";
|
3
|
-
import { EnvKeys } from "../environment.mjs";
|
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
|
-
}
|
19
|
-
|
20
|
-
export type GenericSecret = Record<string, string>;
|
21
|
-
|
22
|
-
export async function getSecret<Secret>(
|
23
|
-
secretId: string,
|
24
|
-
prefix = ""
|
25
|
-
): Promise<Secret> {
|
26
|
-
const secretObj = await getSmClient()
|
27
|
-
.getSecretValue({
|
28
|
-
SecretId: secretId,
|
29
|
-
})
|
30
|
-
.promise();
|
31
|
-
|
32
|
-
if (!secretObj.SecretString) {
|
33
|
-
throw new Error("No secret found!");
|
34
|
-
}
|
35
|
-
|
36
|
-
const secret: GenericSecret | Secret = JSON.parse(
|
37
|
-
secretObj.SecretString
|
38
|
-
) as unknown as GenericSecret | Secret;
|
39
|
-
|
40
|
-
if (!prefix) {
|
41
|
-
return secret as Secret;
|
42
|
-
}
|
43
|
-
|
44
|
-
return parseSecret(secret as GenericSecret, `${prefix}.`);
|
45
|
-
}
|
46
|
-
|
47
|
-
function parseSecret<Secret>(secret: GenericSecret, prefix: string): Secret {
|
48
|
-
const parsed: GenericSecret = {};
|
49
|
-
const skip = prefix.length;
|
50
|
-
|
51
|
-
for (const key in secret) {
|
52
|
-
if (key.startsWith(prefix)) {
|
53
|
-
parsed[key.substring(skip)] = secret[key];
|
54
|
-
}
|
55
|
-
}
|
56
|
-
|
57
|
-
return parsed as unknown as Secret;
|
58
|
-
}
|
package/src/aws/types/errors.mts
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
// DEPRECATED, remove these!
|
2
|
-
export const NOT_FOUND_MESSAGE = "NOT_FOUND";
|
3
|
-
export const ERROR_MESSAGE = "ERROR";
|
4
|
-
export const OK_MESSAGE = "OK";
|
5
|
-
export const BAD_REQUEST_MESSAGE = "BAD REQUEST";
|
6
|
-
|
7
|
-
export class ValidationError extends Error {
|
8
|
-
statusCode: number;
|
9
|
-
|
10
|
-
constructor(statusCode: number, body: string) {
|
11
|
-
super(body);
|
12
|
-
this.statusCode = statusCode;
|
13
|
-
}
|
14
|
-
}
|