@aligent/aws-wrappers 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -3
- package/cjs/index.cjs +678 -1428
- package/cjs/package.json +4 -1
- package/cjs/src/dynamodb/dynamodb.d.ts +31 -0
- package/cjs/src/s3/s3.d.ts +19 -4
- package/cjs/src/scheduler/scheduler.d.ts +6 -4
- package/cjs/src/secrets-manager/secrets-manager.d.ts +7 -6
- package/cjs/src/sfn/sfn.d.ts +4 -0
- package/cjs/src/sns/sns.d.ts +12 -2
- package/cjs/src/sqs/sqs.d.ts +12 -2
- package/cjs/src/ssm/ssm.d.ts +4 -0
- package/cjs/src/util/redact.d.ts +14 -4
- package/docs/classes/DynamoDBService.md +41 -12
- package/docs/classes/S3Service.md +33 -19
- package/docs/classes/SNSService.md +9 -6
- package/docs/classes/SQSService.md +12 -9
- package/docs/classes/SSMService.md +11 -7
- package/docs/classes/SchedulerService.md +17 -15
- package/docs/classes/SecretsManagerService.md +15 -14
- package/docs/classes/StepFunctionsService.md +10 -6
- package/esm/index.mjs +680 -1430
- package/esm/package.json +4 -1
- package/esm/src/dynamodb/dynamodb.d.ts +31 -0
- package/esm/src/s3/s3.d.ts +19 -4
- package/esm/src/scheduler/scheduler.d.ts +6 -4
- package/esm/src/secrets-manager/secrets-manager.d.ts +7 -6
- package/esm/src/sfn/sfn.d.ts +4 -0
- package/esm/src/sns/sns.d.ts +12 -2
- package/esm/src/sqs/sqs.d.ts +12 -2
- package/esm/src/ssm/ssm.d.ts +4 -0
- package/esm/src/util/redact.d.ts +14 -4
- package/package.json +4 -1
package/esm/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Logger } from '@aws-lambda-powertools/logger';
|
|
1
|
+
import { LogLevelThreshold, Logger } from '@aws-lambda-powertools/logger';
|
|
2
2
|
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
|
3
|
-
import { DynamoDBDocumentClient,
|
|
3
|
+
import { DynamoDBDocumentClient, GetCommand, PutCommand, UpdateCommand, DeleteCommand, QueryCommand, ScanCommand, BatchGetCommand, BatchWriteCommand, paginateQuery, paginateScan } from '@aws-sdk/lib-dynamodb';
|
|
4
4
|
import xray from 'aws-xray-sdk-core';
|
|
5
5
|
import { S3Client, PutObjectCommand, GetObjectCommand, HeadObjectCommand, CopyObjectCommand, paginateListObjectsV2, DeleteObjectCommand, DeleteObjectsCommand } from '@aws-sdk/client-s3';
|
|
6
6
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
@@ -11,133 +11,25 @@ import { SNSClient, PublishCommand, PublishBatchCommand } from '@aws-sdk/client-
|
|
|
11
11
|
import { SQSClient, SendMessageCommand, ReceiveMessageCommand, DeleteMessageCommand, SendMessageBatchCommand, DeleteMessageBatchCommand } from '@aws-sdk/client-sqs';
|
|
12
12
|
import { SSMClient, GetParameterCommand, GetParametersCommand, PutParameterCommand, DeleteParameterCommand, paginateGetParametersByPath } from '@aws-sdk/client-ssm';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
***************************************************************************** */
|
|
28
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var __assign = function() {
|
|
32
|
-
__assign = Object.assign || function __assign(t) {
|
|
33
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
34
|
-
s = arguments[i];
|
|
35
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
36
|
-
}
|
|
37
|
-
return t;
|
|
38
|
-
};
|
|
39
|
-
return __assign.apply(this, arguments);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
function __rest(s, e) {
|
|
43
|
-
var t = {};
|
|
44
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
45
|
-
t[p] = s[p];
|
|
46
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
47
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
48
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
49
|
-
t[p[i]] = s[p[i]];
|
|
50
|
-
}
|
|
51
|
-
return t;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
55
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
56
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
57
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
58
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
59
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
60
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function __generator(thisArg, body) {
|
|
65
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
66
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
67
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
68
|
-
function step(op) {
|
|
69
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
70
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
71
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
72
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
73
|
-
switch (op[0]) {
|
|
74
|
-
case 0: case 1: t = op; break;
|
|
75
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
76
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
77
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
78
|
-
default:
|
|
79
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
80
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
81
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
82
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
83
|
-
if (t[2]) _.ops.pop();
|
|
84
|
-
_.trys.pop(); continue;
|
|
85
|
-
}
|
|
86
|
-
op = body.call(thisArg, _);
|
|
87
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
88
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function __values(o) {
|
|
93
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
94
|
-
if (m) return m.call(o);
|
|
95
|
-
if (o && typeof o.length === "number") return {
|
|
96
|
-
next: function () {
|
|
97
|
-
if (o && i >= o.length) o = void 0;
|
|
98
|
-
return { value: o && o[i++], done: !o };
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function __await(v) {
|
|
105
|
-
return this instanceof __await ? (this.v = v, this) : new __await(v);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function __asyncGenerator(thisArg, _arguments, generator) {
|
|
109
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
110
|
-
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
111
|
-
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
112
|
-
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
113
|
-
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
114
|
-
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
115
|
-
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
116
|
-
function fulfill(value) { resume("next", value); }
|
|
117
|
-
function reject(value) { resume("throw", value); }
|
|
118
|
-
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function __asyncValues(o) {
|
|
122
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
123
|
-
var m = o[Symbol.asyncIterator], i;
|
|
124
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
125
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
126
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
130
|
-
var e = new Error(message);
|
|
131
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
132
|
-
};
|
|
133
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Whether the logger is verbose enough that full SDK inputs — payloads, secret
|
|
16
|
+
* material and PII — may be written to the log.
|
|
17
|
+
*
|
|
18
|
+
* **This is the only place in the package that reads the log level.** Never
|
|
19
|
+
* compare `logger.getLevelName()` yourself: Powertools orders `TRACE` as *more*
|
|
20
|
+
* verbose than `DEBUG` (thresholds 6 and 8), so the obvious
|
|
21
|
+
* `getLevelName() === 'DEBUG'` check silently redacts at the most verbose level
|
|
22
|
+
* available. Route every disclosure decision through this predicate instead.
|
|
23
|
+
*/
|
|
24
|
+
function shouldLogFullInput(logger) {
|
|
25
|
+
return LogLevelThreshold[logger.getLevelName()] <= LogLevelThreshold.DEBUG;
|
|
26
|
+
}
|
|
134
27
|
/**
|
|
135
28
|
* Return a log-safe projection of `input` based on the logger's configured level.
|
|
136
29
|
*
|
|
137
|
-
* At
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* and PII.
|
|
30
|
+
* At the verbose levels — see {@link shouldLogFullInput} — the full input is
|
|
31
|
+
* returned unchanged: operators who asked for that verbosity have explicitly
|
|
32
|
+
* opted into seeing everything, including payloads, secret material and PII.
|
|
141
33
|
*
|
|
142
34
|
* At any other level, only the fields listed in `safeFields` are included.
|
|
143
35
|
* Missing fields are silently skipped — the result type narrows to
|
|
@@ -148,24 +40,22 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
148
40
|
* the design rationale and conventions on building the safe-field lists.
|
|
149
41
|
*/
|
|
150
42
|
function filterFieldsForLogLevel(logger, input, safeFields) {
|
|
151
|
-
if (logger
|
|
43
|
+
if (shouldLogFullInput(logger))
|
|
152
44
|
return input;
|
|
153
|
-
|
|
154
|
-
for (
|
|
155
|
-
var key = safeFields_1[_i];
|
|
45
|
+
const out = {};
|
|
46
|
+
for (const key of safeFields) {
|
|
156
47
|
if (key in input)
|
|
157
48
|
out[key] = input[key];
|
|
158
49
|
}
|
|
159
50
|
return out;
|
|
160
51
|
}
|
|
161
52
|
|
|
162
|
-
|
|
163
|
-
|
|
53
|
+
const BATCH_WRITE_MAX_ATTEMPTS = 5;
|
|
54
|
+
const BATCH_WRITE_BASE_DELAY_MS = 200;
|
|
164
55
|
/**
|
|
165
56
|
* Fields safe to log at INFO. Omits `Key` (may carry customer IDs / tenant IDs).
|
|
166
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
167
57
|
*/
|
|
168
|
-
|
|
58
|
+
const GET_ITEM_SAFE_FIELDS = [
|
|
169
59
|
'TableName',
|
|
170
60
|
'ConsistentRead',
|
|
171
61
|
'ProjectionExpression',
|
|
@@ -175,9 +65,8 @@ var GET_ITEM_SAFE_FIELDS = [
|
|
|
175
65
|
/**
|
|
176
66
|
* Fields safe to log at INFO. Omits `Item` (the payload itself) and
|
|
177
67
|
* `ExpressionAttributeValues` (values bound to ConditionExpression, often PII).
|
|
178
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
179
68
|
*/
|
|
180
|
-
|
|
69
|
+
const PUT_ITEM_SAFE_FIELDS = [
|
|
181
70
|
'TableName',
|
|
182
71
|
'ConditionExpression',
|
|
183
72
|
'ExpressionAttributeNames',
|
|
@@ -188,9 +77,8 @@ var PUT_ITEM_SAFE_FIELDS = [
|
|
|
188
77
|
];
|
|
189
78
|
/**
|
|
190
79
|
* Fields safe to log at INFO. Omits `Key` and `ExpressionAttributeValues`.
|
|
191
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
192
80
|
*/
|
|
193
|
-
|
|
81
|
+
const UPDATE_ITEM_SAFE_FIELDS = [
|
|
194
82
|
'TableName',
|
|
195
83
|
'UpdateExpression',
|
|
196
84
|
'ConditionExpression',
|
|
@@ -203,9 +91,8 @@ var UPDATE_ITEM_SAFE_FIELDS = [
|
|
|
203
91
|
/**
|
|
204
92
|
* Fields safe to log at INFO. Omits `Key` and `ExpressionAttributeValues`
|
|
205
93
|
* (the latter binds to ConditionExpression and may carry PII).
|
|
206
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
207
94
|
*/
|
|
208
|
-
|
|
95
|
+
const DELETE_ITEM_SAFE_FIELDS = [
|
|
209
96
|
'TableName',
|
|
210
97
|
'ConditionExpression',
|
|
211
98
|
'ExpressionAttributeNames',
|
|
@@ -217,10 +104,9 @@ var DELETE_ITEM_SAFE_FIELDS = [
|
|
|
217
104
|
/**
|
|
218
105
|
* Fields safe to log at INFO for `query` and `paginateItems`. Omits
|
|
219
106
|
* `ExpressionAttributeValues` (values often carry PII) and `ExclusiveStartKey`
|
|
220
|
-
* (pagination cursor includes Key shape).
|
|
221
|
-
* the full input.
|
|
107
|
+
* (pagination cursor includes Key shape).
|
|
222
108
|
*/
|
|
223
|
-
|
|
109
|
+
const QUERY_SAFE_FIELDS = [
|
|
224
110
|
'TableName',
|
|
225
111
|
'IndexName',
|
|
226
112
|
'KeyConditionExpression',
|
|
@@ -236,9 +122,8 @@ var QUERY_SAFE_FIELDS = [
|
|
|
236
122
|
/**
|
|
237
123
|
* Fields safe to log at INFO for `scan` and `paginateScan`. Omits
|
|
238
124
|
* `ExpressionAttributeValues` and `ExclusiveStartKey`.
|
|
239
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
240
125
|
*/
|
|
241
|
-
|
|
126
|
+
const SCAN_SAFE_FIELDS = [
|
|
242
127
|
'TableName',
|
|
243
128
|
'IndexName',
|
|
244
129
|
'FilterExpression',
|
|
@@ -251,19 +136,25 @@ var SCAN_SAFE_FIELDS = [
|
|
|
251
136
|
'TotalSegments',
|
|
252
137
|
'ReturnConsumedCapacity',
|
|
253
138
|
];
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
139
|
+
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
140
|
+
const backoffDelay = (attempt) => {
|
|
141
|
+
const exp = BATCH_WRITE_BASE_DELAY_MS * 2 ** attempt;
|
|
257
142
|
return exp + Math.random() * exp;
|
|
258
143
|
};
|
|
259
144
|
/**
|
|
260
145
|
* Wrapper around the AWS DynamoDB Document client providing structured
|
|
261
146
|
* Powertools logging and X-Ray tracing by default.
|
|
262
147
|
*
|
|
148
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
149
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
150
|
+
* `TRACE`) log full SDK inputs.
|
|
151
|
+
*
|
|
263
152
|
* Items are automatically marshalled / unmarshalled via the document client —
|
|
264
153
|
* callers work with plain TypeScript objects in both directions.
|
|
265
154
|
*/
|
|
266
|
-
|
|
155
|
+
class DynamoDBService {
|
|
156
|
+
client;
|
|
157
|
+
logger;
|
|
267
158
|
/**
|
|
268
159
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
269
160
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
@@ -273,13 +164,13 @@ var DynamoDBService = /** @class */ (function () {
|
|
|
273
164
|
* *before* being passed to `DynamoDBDocumentClient.from`, so X-Ray
|
|
274
165
|
* tracing captures every DynamoDB call.
|
|
275
166
|
*/
|
|
276
|
-
|
|
277
|
-
var _a, _b;
|
|
167
|
+
constructor(opts) {
|
|
278
168
|
this.client =
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
169
|
+
opts?.client ??
|
|
170
|
+
DynamoDBDocumentClient.from(xray.captureAWSv3Client(new DynamoDBClient({})), {
|
|
171
|
+
marshallOptions: { removeUndefinedValues: true },
|
|
172
|
+
});
|
|
173
|
+
this.logger = opts?.logger ?? new Logger();
|
|
283
174
|
}
|
|
284
175
|
/**
|
|
285
176
|
* Get an item from DynamoDB.
|
|
@@ -287,38 +178,24 @@ var DynamoDBService = /** @class */ (function () {
|
|
|
287
178
|
* @template R - Expected unmarshalled item shape.
|
|
288
179
|
* @returns The item, or `undefined` if not found.
|
|
289
180
|
*/
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
return __generator(this, function (_a) {
|
|
294
|
-
switch (_a.label) {
|
|
295
|
-
case 0:
|
|
296
|
-
this.logger.info('Getting DynamoDB item', {
|
|
297
|
-
input: filterFieldsForLogLevel(this.logger, input, GET_ITEM_SAFE_FIELDS),
|
|
298
|
-
});
|
|
299
|
-
return [4 /*yield*/, this.client.send(new GetCommand(input))];
|
|
300
|
-
case 1:
|
|
301
|
-
response = _a.sent();
|
|
302
|
-
return [2 /*return*/, response.Item];
|
|
303
|
-
}
|
|
304
|
-
});
|
|
181
|
+
async getItem(input) {
|
|
182
|
+
this.logger.info('Getting DynamoDB item', {
|
|
183
|
+
input: filterFieldsForLogLevel(this.logger, input, GET_ITEM_SAFE_FIELDS),
|
|
305
184
|
});
|
|
306
|
-
|
|
185
|
+
const response = await this.client.send(new GetCommand(input));
|
|
186
|
+
return response.Item;
|
|
187
|
+
}
|
|
307
188
|
/**
|
|
308
189
|
* Put an item into DynamoDB. The caller's `Item` is typed as `T`, which
|
|
309
190
|
* the document client marshalls automatically.
|
|
310
191
|
* @template T - Type of the item being stored.
|
|
311
192
|
*/
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
this.logger.info('Putting DynamoDB item', {
|
|
316
|
-
input: filterFieldsForLogLevel(this.logger, input, PUT_ITEM_SAFE_FIELDS),
|
|
317
|
-
});
|
|
318
|
-
return [2 /*return*/, this.client.send(new PutCommand(input))];
|
|
319
|
-
});
|
|
193
|
+
async putItem(input) {
|
|
194
|
+
this.logger.info('Putting DynamoDB item', {
|
|
195
|
+
input: filterFieldsForLogLevel(this.logger, input, PUT_ITEM_SAFE_FIELDS),
|
|
320
196
|
});
|
|
321
|
-
|
|
197
|
+
return this.client.send(new PutCommand(input));
|
|
198
|
+
}
|
|
322
199
|
/**
|
|
323
200
|
* Update an item in DynamoDB. The `Attributes` field on the response is
|
|
324
201
|
* typed as `R` — the caller should choose `R` to match their
|
|
@@ -329,69 +206,39 @@ var DynamoDBService = /** @class */ (function () {
|
|
|
329
206
|
* @template K - Shape of the partition / sort key.
|
|
330
207
|
* @template R - Expected shape of the returned `Attributes`.
|
|
331
208
|
*/
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
return __generator(this, function (_a) {
|
|
336
|
-
switch (_a.label) {
|
|
337
|
-
case 0:
|
|
338
|
-
this.logger.info('Updating DynamoDB item', {
|
|
339
|
-
input: filterFieldsForLogLevel(this.logger, input, UPDATE_ITEM_SAFE_FIELDS),
|
|
340
|
-
});
|
|
341
|
-
return [4 /*yield*/, this.client.send(new UpdateCommand(input))];
|
|
342
|
-
case 1:
|
|
343
|
-
response = _a.sent();
|
|
344
|
-
return [2 /*return*/, response];
|
|
345
|
-
}
|
|
346
|
-
});
|
|
209
|
+
async updateItem(input) {
|
|
210
|
+
this.logger.info('Updating DynamoDB item', {
|
|
211
|
+
input: filterFieldsForLogLevel(this.logger, input, UPDATE_ITEM_SAFE_FIELDS),
|
|
347
212
|
});
|
|
348
|
-
|
|
213
|
+
const response = await this.client.send(new UpdateCommand(input));
|
|
214
|
+
return response;
|
|
215
|
+
}
|
|
349
216
|
/**
|
|
350
217
|
* Delete an item from DynamoDB. The `Attributes` field on the response is
|
|
351
218
|
* typed as `R` — relevant when `ReturnValues: 'ALL_OLD'` is set.
|
|
352
219
|
* @template K - Shape of the partition / sort key.
|
|
353
220
|
* @template R - Expected shape of the returned `Attributes`.
|
|
354
221
|
*/
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
return __generator(this, function (_a) {
|
|
359
|
-
switch (_a.label) {
|
|
360
|
-
case 0:
|
|
361
|
-
this.logger.info('Deleting DynamoDB item', {
|
|
362
|
-
input: filterFieldsForLogLevel(this.logger, input, DELETE_ITEM_SAFE_FIELDS),
|
|
363
|
-
});
|
|
364
|
-
return [4 /*yield*/, this.client.send(new DeleteCommand(input))];
|
|
365
|
-
case 1:
|
|
366
|
-
response = _a.sent();
|
|
367
|
-
return [2 /*return*/, response];
|
|
368
|
-
}
|
|
369
|
-
});
|
|
222
|
+
async deleteItem(input) {
|
|
223
|
+
this.logger.info('Deleting DynamoDB item', {
|
|
224
|
+
input: filterFieldsForLogLevel(this.logger, input, DELETE_ITEM_SAFE_FIELDS),
|
|
370
225
|
});
|
|
371
|
-
|
|
226
|
+
const response = await this.client.send(new DeleteCommand(input));
|
|
227
|
+
return response;
|
|
228
|
+
}
|
|
372
229
|
/**
|
|
373
230
|
* Execute a DynamoDB Query. The full `QueryCommandOutput` is returned with
|
|
374
231
|
* `Items` typed as `T[]` so callers retain pagination metadata
|
|
375
232
|
* (`LastEvaluatedKey`, `Count`, etc.).
|
|
376
233
|
* @template T - Expected shape of each unmarshalled item.
|
|
377
234
|
*/
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
return __generator(this, function (_a) {
|
|
382
|
-
switch (_a.label) {
|
|
383
|
-
case 0:
|
|
384
|
-
this.logger.info('Querying DynamoDB', {
|
|
385
|
-
input: filterFieldsForLogLevel(this.logger, input, QUERY_SAFE_FIELDS),
|
|
386
|
-
});
|
|
387
|
-
return [4 /*yield*/, this.client.send(new QueryCommand(input))];
|
|
388
|
-
case 1:
|
|
389
|
-
response = _a.sent();
|
|
390
|
-
return [2 /*return*/, response];
|
|
391
|
-
}
|
|
392
|
-
});
|
|
235
|
+
async query(input) {
|
|
236
|
+
this.logger.info('Querying DynamoDB', {
|
|
237
|
+
input: filterFieldsForLogLevel(this.logger, input, QUERY_SAFE_FIELDS),
|
|
393
238
|
});
|
|
394
|
-
|
|
239
|
+
const response = await this.client.send(new QueryCommand(input));
|
|
240
|
+
return response;
|
|
241
|
+
}
|
|
395
242
|
/**
|
|
396
243
|
* Scan a DynamoDB table. The full `ScanCommandOutput` is returned with
|
|
397
244
|
* `Items` typed as `T[]` so callers retain pagination metadata.
|
|
@@ -411,23 +258,13 @@ var DynamoDBService = /** @class */ (function () {
|
|
|
411
258
|
*
|
|
412
259
|
* @template T - Expected shape of each unmarshalled item.
|
|
413
260
|
*/
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
return __generator(this, function (_a) {
|
|
418
|
-
switch (_a.label) {
|
|
419
|
-
case 0:
|
|
420
|
-
this.logger.info('Scanning DynamoDB', {
|
|
421
|
-
input: filterFieldsForLogLevel(this.logger, input, SCAN_SAFE_FIELDS),
|
|
422
|
-
});
|
|
423
|
-
return [4 /*yield*/, this.client.send(new ScanCommand(input))];
|
|
424
|
-
case 1:
|
|
425
|
-
response = _a.sent();
|
|
426
|
-
return [2 /*return*/, response];
|
|
427
|
-
}
|
|
428
|
-
});
|
|
261
|
+
async scan(input) {
|
|
262
|
+
this.logger.info('Scanning DynamoDB', {
|
|
263
|
+
input: filterFieldsForLogLevel(this.logger, input, SCAN_SAFE_FIELDS),
|
|
429
264
|
});
|
|
430
|
-
|
|
265
|
+
const response = await this.client.send(new ScanCommand(input));
|
|
266
|
+
return response;
|
|
267
|
+
}
|
|
431
268
|
/**
|
|
432
269
|
* Batch-get items from one or more DynamoDB tables.
|
|
433
270
|
*
|
|
@@ -436,214 +273,119 @@ var DynamoDBService = /** @class */ (function () {
|
|
|
436
273
|
* shapes can differ per table — no single `T` can soundly describe it.
|
|
437
274
|
* Callers should narrow the result type at the call site.
|
|
438
275
|
*/
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
});
|
|
448
|
-
return [2 /*return*/, this.client.send(new BatchGetCommand(input))];
|
|
449
|
-
});
|
|
276
|
+
async batchGet(input) {
|
|
277
|
+
// Inline verbosity check rather than `filterFieldsForLogLevel` because
|
|
278
|
+
// `RequestItems` is a `Record<tableName, KeysAndAttributes>` — the
|
|
279
|
+
// payload (`Keys[]`) lives inside the value, not as a top-level key
|
|
280
|
+
// the helper could pick or drop.
|
|
281
|
+
const logFullInput = shouldLogFullInput(this.logger);
|
|
282
|
+
this.logger.info('Batch getting DynamoDB items', {
|
|
283
|
+
input: logFullInput ? input : { tables: Object.keys(input.RequestItems ?? {}) },
|
|
450
284
|
});
|
|
451
|
-
|
|
285
|
+
return this.client.send(new BatchGetCommand(input));
|
|
286
|
+
}
|
|
452
287
|
/**
|
|
453
288
|
* Batch-write items to DynamoDB, retrying `UnprocessedItems` with jittered
|
|
454
289
|
* exponential backoff. Up to 5 attempts (200ms base delay). Throws when
|
|
455
290
|
* items remain unprocessed after the final attempt.
|
|
456
291
|
*/
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
this.logger.info('Batch writing DynamoDB items', {
|
|
466
|
-
input: isDebug ? input : { tables: Object.keys((_a = input.RequestItems) !== null && _a !== void 0 ? _a : {}) },
|
|
467
|
-
});
|
|
468
|
-
current = input;
|
|
469
|
-
attempt = 0;
|
|
470
|
-
_b.label = 1;
|
|
471
|
-
case 1:
|
|
472
|
-
if (!(attempt < BATCH_WRITE_MAX_ATTEMPTS)) return [3 /*break*/, 6];
|
|
473
|
-
return [4 /*yield*/, this.client.send(new BatchWriteCommand(current))];
|
|
474
|
-
case 2:
|
|
475
|
-
response = _b.sent();
|
|
476
|
-
unprocessed = response.UnprocessedItems;
|
|
477
|
-
if (!unprocessed || Object.keys(unprocessed).length === 0)
|
|
478
|
-
return [2 /*return*/, response];
|
|
479
|
-
this.logger.warn('Retrying unprocessed DynamoDB items', {
|
|
480
|
-
attempt: attempt + 1,
|
|
481
|
-
tables: Object.keys(unprocessed),
|
|
482
|
-
});
|
|
483
|
-
if (!(attempt < BATCH_WRITE_MAX_ATTEMPTS - 1)) return [3 /*break*/, 4];
|
|
484
|
-
return [4 /*yield*/, sleep(backoffDelay(attempt))];
|
|
485
|
-
case 3:
|
|
486
|
-
_b.sent();
|
|
487
|
-
_b.label = 4;
|
|
488
|
-
case 4:
|
|
489
|
-
current = __assign(__assign({}, input), { RequestItems: unprocessed });
|
|
490
|
-
_b.label = 5;
|
|
491
|
-
case 5:
|
|
492
|
-
attempt++;
|
|
493
|
-
return [3 /*break*/, 1];
|
|
494
|
-
case 6: throw new Error("batchWrite failed after ".concat(BATCH_WRITE_MAX_ATTEMPTS, " attempts"));
|
|
495
|
-
}
|
|
496
|
-
});
|
|
292
|
+
async batchWrite(input) {
|
|
293
|
+
// Inline verbosity check rather than `filterFieldsForLogLevel` because
|
|
294
|
+
// `RequestItems` is a `Record<tableName, WriteRequest[]>` — the
|
|
295
|
+
// payload (`PutRequest.Item` / `DeleteRequest.Key`) lives inside the
|
|
296
|
+
// value, not as a top-level key the helper could pick or drop.
|
|
297
|
+
const logFullInput = shouldLogFullInput(this.logger);
|
|
298
|
+
this.logger.info('Batch writing DynamoDB items', {
|
|
299
|
+
input: logFullInput ? input : { tables: Object.keys(input.RequestItems ?? {}) },
|
|
497
300
|
});
|
|
498
|
-
|
|
301
|
+
let current = input;
|
|
302
|
+
for (let attempt = 0; attempt < BATCH_WRITE_MAX_ATTEMPTS; attempt++) {
|
|
303
|
+
const response = await this.client.send(new BatchWriteCommand(current));
|
|
304
|
+
const unprocessed = response.UnprocessedItems;
|
|
305
|
+
if (!unprocessed || Object.keys(unprocessed).length === 0)
|
|
306
|
+
return response;
|
|
307
|
+
this.logger.warn('Retrying unprocessed DynamoDB items', {
|
|
308
|
+
attempt: attempt + 1,
|
|
309
|
+
tables: Object.keys(unprocessed),
|
|
310
|
+
});
|
|
311
|
+
if (attempt < BATCH_WRITE_MAX_ATTEMPTS - 1)
|
|
312
|
+
await sleep(backoffDelay(attempt));
|
|
313
|
+
current = { ...input, RequestItems: unprocessed };
|
|
314
|
+
}
|
|
315
|
+
throw new Error(`batchWrite failed after ${BATCH_WRITE_MAX_ATTEMPTS} attempts`);
|
|
316
|
+
}
|
|
499
317
|
/**
|
|
500
318
|
* Paginate over Query results, yielding one unmarshalled item at a time.
|
|
319
|
+
*
|
|
320
|
+
* **`Limit` is a page size, not a total cap.** DynamoDB applies it per
|
|
321
|
+
* request, and this method walks every page until `LastEvaluatedKey` is
|
|
322
|
+
* exhausted — so `paginateItems({ Limit: 10 })` yields *every* matching
|
|
323
|
+
* item, in pages of 10, not the first 10 items.
|
|
324
|
+
*
|
|
325
|
+
* To bound the number of items, `break` out of the loop. This is an
|
|
326
|
+
* `AsyncGenerator`, so `break` calls its `return()`, which unwinds the
|
|
327
|
+
* pagination and issues no further requests:
|
|
328
|
+
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* const firstTen: Item[] = [];
|
|
331
|
+
* for await (const item of ddb.paginateItems<Item>(input)) {
|
|
332
|
+
* firstTen.push(item);
|
|
333
|
+
* if (firstTen.length === 10) break;
|
|
334
|
+
* }
|
|
335
|
+
* ```
|
|
336
|
+
*
|
|
337
|
+
* `Limit` remains the right knob for tuning per-request consumed capacity
|
|
338
|
+
* and page memory — set it deliberately, just don't expect it to stop the
|
|
339
|
+
* walk.
|
|
340
|
+
*
|
|
501
341
|
* @template T - Expected shape of each yielded item.
|
|
502
342
|
*/
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
var _c, e_1, _d, _e;
|
|
507
|
-
return __generator(this, function (_f) {
|
|
508
|
-
switch (_f.label) {
|
|
509
|
-
case 0:
|
|
510
|
-
this.logger.info('Paginating DynamoDB query', {
|
|
511
|
-
input: filterFieldsForLogLevel(this.logger, input, QUERY_SAFE_FIELDS),
|
|
512
|
-
});
|
|
513
|
-
paginator = paginateQuery({ client: this.client }, input);
|
|
514
|
-
_f.label = 1;
|
|
515
|
-
case 1:
|
|
516
|
-
_f.trys.push([1, 10, 11, 16]);
|
|
517
|
-
_a = true, paginator_1 = __asyncValues(paginator);
|
|
518
|
-
_f.label = 2;
|
|
519
|
-
case 2: return [4 /*yield*/, __await(paginator_1.next())];
|
|
520
|
-
case 3:
|
|
521
|
-
if (!(paginator_1_1 = _f.sent(), _c = paginator_1_1.done, !_c)) return [3 /*break*/, 9];
|
|
522
|
-
_e = paginator_1_1.value;
|
|
523
|
-
_a = false;
|
|
524
|
-
page = _e;
|
|
525
|
-
if (!page.Items)
|
|
526
|
-
return [3 /*break*/, 8];
|
|
527
|
-
_i = 0, _b = page.Items;
|
|
528
|
-
_f.label = 4;
|
|
529
|
-
case 4:
|
|
530
|
-
if (!(_i < _b.length)) return [3 /*break*/, 8];
|
|
531
|
-
item = _b[_i];
|
|
532
|
-
return [4 /*yield*/, __await(item)];
|
|
533
|
-
case 5: return [4 /*yield*/, _f.sent()];
|
|
534
|
-
case 6:
|
|
535
|
-
_f.sent();
|
|
536
|
-
_f.label = 7;
|
|
537
|
-
case 7:
|
|
538
|
-
_i++;
|
|
539
|
-
return [3 /*break*/, 4];
|
|
540
|
-
case 8:
|
|
541
|
-
_a = true;
|
|
542
|
-
return [3 /*break*/, 2];
|
|
543
|
-
case 9: return [3 /*break*/, 16];
|
|
544
|
-
case 10:
|
|
545
|
-
e_1_1 = _f.sent();
|
|
546
|
-
e_1 = { error: e_1_1 };
|
|
547
|
-
return [3 /*break*/, 16];
|
|
548
|
-
case 11:
|
|
549
|
-
_f.trys.push([11, , 14, 15]);
|
|
550
|
-
if (!(!_a && !_c && (_d = paginator_1.return))) return [3 /*break*/, 13];
|
|
551
|
-
return [4 /*yield*/, __await(_d.call(paginator_1))];
|
|
552
|
-
case 12:
|
|
553
|
-
_f.sent();
|
|
554
|
-
_f.label = 13;
|
|
555
|
-
case 13: return [3 /*break*/, 15];
|
|
556
|
-
case 14:
|
|
557
|
-
if (e_1) throw e_1.error;
|
|
558
|
-
return [7 /*endfinally*/];
|
|
559
|
-
case 15: return [7 /*endfinally*/];
|
|
560
|
-
case 16: return [2 /*return*/];
|
|
561
|
-
}
|
|
562
|
-
});
|
|
343
|
+
async *paginateItems(input) {
|
|
344
|
+
this.logger.info('Paginating DynamoDB query', {
|
|
345
|
+
input: filterFieldsForLogLevel(this.logger, input, QUERY_SAFE_FIELDS),
|
|
563
346
|
});
|
|
564
|
-
|
|
347
|
+
const paginator = paginateQuery({ client: this.client }, input);
|
|
348
|
+
for await (const page of paginator) {
|
|
349
|
+
if (!page.Items)
|
|
350
|
+
continue;
|
|
351
|
+
for (const item of page.Items)
|
|
352
|
+
yield item;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
565
355
|
/**
|
|
566
356
|
* Paginate over Scan results, yielding one unmarshalled item at a time.
|
|
357
|
+
*
|
|
358
|
+
* **`Limit` is a page size, not a total cap** — see `paginateItems` for the
|
|
359
|
+
* detail. `break` out of the loop to bound the number of items; `Limit`
|
|
360
|
+
* stays the right knob for per-request consumed capacity and page memory.
|
|
361
|
+
*
|
|
567
362
|
* @template T - Expected shape of each yielded item.
|
|
568
363
|
*/
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
var _c, e_2, _d, _e;
|
|
573
|
-
return __generator(this, function (_f) {
|
|
574
|
-
switch (_f.label) {
|
|
575
|
-
case 0:
|
|
576
|
-
this.logger.info('Paginating DynamoDB scan', {
|
|
577
|
-
input: filterFieldsForLogLevel(this.logger, input, SCAN_SAFE_FIELDS),
|
|
578
|
-
});
|
|
579
|
-
paginator = paginateScan({ client: this.client }, input);
|
|
580
|
-
_f.label = 1;
|
|
581
|
-
case 1:
|
|
582
|
-
_f.trys.push([1, 10, 11, 16]);
|
|
583
|
-
_a = true, paginator_2 = __asyncValues(paginator);
|
|
584
|
-
_f.label = 2;
|
|
585
|
-
case 2: return [4 /*yield*/, __await(paginator_2.next())];
|
|
586
|
-
case 3:
|
|
587
|
-
if (!(paginator_2_1 = _f.sent(), _c = paginator_2_1.done, !_c)) return [3 /*break*/, 9];
|
|
588
|
-
_e = paginator_2_1.value;
|
|
589
|
-
_a = false;
|
|
590
|
-
page = _e;
|
|
591
|
-
if (!page.Items)
|
|
592
|
-
return [3 /*break*/, 8];
|
|
593
|
-
_i = 0, _b = page.Items;
|
|
594
|
-
_f.label = 4;
|
|
595
|
-
case 4:
|
|
596
|
-
if (!(_i < _b.length)) return [3 /*break*/, 8];
|
|
597
|
-
item = _b[_i];
|
|
598
|
-
return [4 /*yield*/, __await(item)];
|
|
599
|
-
case 5: return [4 /*yield*/, _f.sent()];
|
|
600
|
-
case 6:
|
|
601
|
-
_f.sent();
|
|
602
|
-
_f.label = 7;
|
|
603
|
-
case 7:
|
|
604
|
-
_i++;
|
|
605
|
-
return [3 /*break*/, 4];
|
|
606
|
-
case 8:
|
|
607
|
-
_a = true;
|
|
608
|
-
return [3 /*break*/, 2];
|
|
609
|
-
case 9: return [3 /*break*/, 16];
|
|
610
|
-
case 10:
|
|
611
|
-
e_2_1 = _f.sent();
|
|
612
|
-
e_2 = { error: e_2_1 };
|
|
613
|
-
return [3 /*break*/, 16];
|
|
614
|
-
case 11:
|
|
615
|
-
_f.trys.push([11, , 14, 15]);
|
|
616
|
-
if (!(!_a && !_c && (_d = paginator_2.return))) return [3 /*break*/, 13];
|
|
617
|
-
return [4 /*yield*/, __await(_d.call(paginator_2))];
|
|
618
|
-
case 12:
|
|
619
|
-
_f.sent();
|
|
620
|
-
_f.label = 13;
|
|
621
|
-
case 13: return [3 /*break*/, 15];
|
|
622
|
-
case 14:
|
|
623
|
-
if (e_2) throw e_2.error;
|
|
624
|
-
return [7 /*endfinally*/];
|
|
625
|
-
case 15: return [7 /*endfinally*/];
|
|
626
|
-
case 16: return [2 /*return*/];
|
|
627
|
-
}
|
|
628
|
-
});
|
|
364
|
+
async *paginateScan(input) {
|
|
365
|
+
this.logger.info('Paginating DynamoDB scan', {
|
|
366
|
+
input: filterFieldsForLogLevel(this.logger, input, SCAN_SAFE_FIELDS),
|
|
629
367
|
});
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
368
|
+
const paginator = paginateScan({ client: this.client }, input);
|
|
369
|
+
for await (const page of paginator) {
|
|
370
|
+
if (!page.Items)
|
|
371
|
+
continue;
|
|
372
|
+
for (const item of page.Items)
|
|
373
|
+
yield item;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
633
377
|
|
|
634
|
-
|
|
635
|
-
|
|
378
|
+
const DEFAULT_PRESIGNED_URL_EXPIRES_IN_SECONDS = 3600;
|
|
379
|
+
const DELETE_OBJECTS_BATCH_LIMIT = 1000;
|
|
636
380
|
/**
|
|
637
381
|
* Fields safe to log at INFO for `putObject`. Omits `Body` (object payload).
|
|
638
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
639
382
|
*/
|
|
640
|
-
|
|
383
|
+
const PUT_OBJECT_SAFE_FIELDS = ['Bucket', 'Key'];
|
|
641
384
|
/**
|
|
642
385
|
* Fields safe to log at INFO for `putJsonObject`. Omits `Body` (the
|
|
643
386
|
* unserialised JSON payload). `Metadata` is included (operational labels).
|
|
644
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
645
387
|
*/
|
|
646
|
-
|
|
388
|
+
const PUT_JSON_OBJECT_SAFE_FIELDS = [
|
|
647
389
|
'Bucket',
|
|
648
390
|
'Key',
|
|
649
391
|
'Metadata',
|
|
@@ -652,21 +394,26 @@ var PUT_JSON_OBJECT_SAFE_FIELDS = [
|
|
|
652
394
|
* Wrapper around the AWS S3 client providing structured Powertools logging
|
|
653
395
|
* and X-Ray tracing by default.
|
|
654
396
|
*
|
|
397
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
398
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
399
|
+
* `TRACE`) log full SDK inputs.
|
|
400
|
+
*
|
|
655
401
|
* Input shapes are intentionally tight (Bucket/Key/Body only). Callers
|
|
656
402
|
* needing SDK-level options not exposed here (server-side encryption,
|
|
657
403
|
* tagging, version IDs) should use `S3Client` directly.
|
|
658
404
|
*/
|
|
659
|
-
|
|
405
|
+
class S3Service {
|
|
406
|
+
client;
|
|
407
|
+
logger;
|
|
660
408
|
/**
|
|
661
409
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
662
410
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
663
411
|
* @param opts.client - Optional pre-configured `S3Client`. When supplied,
|
|
664
412
|
* the wrapper does not apply X-Ray instrumentation.
|
|
665
413
|
*/
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
this.
|
|
669
|
-
this.logger = (_b = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _b !== void 0 ? _b : new Logger();
|
|
414
|
+
constructor(opts) {
|
|
415
|
+
this.client = opts?.client ?? xray.captureAWSv3Client(new S3Client());
|
|
416
|
+
this.logger = opts?.logger ?? new Logger();
|
|
670
417
|
}
|
|
671
418
|
/**
|
|
672
419
|
* Put an object into S3.
|
|
@@ -677,16 +424,12 @@ var S3Service = /** @class */ (function () {
|
|
|
677
424
|
*
|
|
678
425
|
* @param input - Bucket, Key, and Body of the object to store.
|
|
679
426
|
*/
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
this.logger.info('Putting S3 object', {
|
|
684
|
-
input: filterFieldsForLogLevel(this.logger, input, PUT_OBJECT_SAFE_FIELDS),
|
|
685
|
-
});
|
|
686
|
-
return [2 /*return*/, this.client.send(new PutObjectCommand(input))];
|
|
687
|
-
});
|
|
427
|
+
async putObject(input) {
|
|
428
|
+
this.logger.info('Putting S3 object', {
|
|
429
|
+
input: filterFieldsForLogLevel(this.logger, input, PUT_OBJECT_SAFE_FIELDS),
|
|
688
430
|
});
|
|
689
|
-
|
|
431
|
+
return this.client.send(new PutObjectCommand(input));
|
|
432
|
+
}
|
|
690
433
|
/**
|
|
691
434
|
* Serialise a value to JSON and store it as an S3 object.
|
|
692
435
|
*
|
|
@@ -696,228 +439,97 @@ var S3Service = /** @class */ (function () {
|
|
|
696
439
|
*
|
|
697
440
|
* @template T - Type of the value being stored.
|
|
698
441
|
*/
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
this.logger.info('Putting S3 JSON object', {
|
|
703
|
-
input: filterFieldsForLogLevel(this.logger, input, PUT_JSON_OBJECT_SAFE_FIELDS),
|
|
704
|
-
});
|
|
705
|
-
return [2 /*return*/, this.client.send(new PutObjectCommand({
|
|
706
|
-
Bucket: input.Bucket,
|
|
707
|
-
Key: input.Key,
|
|
708
|
-
Body: JSON.stringify(input.Body),
|
|
709
|
-
Metadata: input.Metadata,
|
|
710
|
-
}))];
|
|
711
|
-
});
|
|
442
|
+
async putJsonObject(input) {
|
|
443
|
+
this.logger.info('Putting S3 JSON object', {
|
|
444
|
+
input: filterFieldsForLogLevel(this.logger, input, PUT_JSON_OBJECT_SAFE_FIELDS),
|
|
712
445
|
});
|
|
713
|
-
|
|
446
|
+
return this.client.send(new PutObjectCommand({
|
|
447
|
+
Bucket: input.Bucket,
|
|
448
|
+
Key: input.Key,
|
|
449
|
+
Body: JSON.stringify(input.Body),
|
|
450
|
+
Metadata: input.Metadata,
|
|
451
|
+
}));
|
|
452
|
+
}
|
|
714
453
|
/**
|
|
715
454
|
* Get an object from S3.
|
|
716
455
|
*/
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
return [2 /*return*/, this.client.send(new GetObjectCommand(input))];
|
|
722
|
-
});
|
|
723
|
-
});
|
|
724
|
-
};
|
|
456
|
+
async getObject(input) {
|
|
457
|
+
this.logger.info('Getting S3 object', { input });
|
|
458
|
+
return this.client.send(new GetObjectCommand(input));
|
|
459
|
+
}
|
|
725
460
|
/**
|
|
726
461
|
* Get an object from S3 and return its body as a string.
|
|
727
462
|
* @returns The object body as a string, or `undefined` if the response
|
|
728
463
|
* has no body.
|
|
729
464
|
*/
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
switch (_b.label) {
|
|
736
|
-
case 0:
|
|
737
|
-
this.logger.info('Getting S3 object body', { input: input });
|
|
738
|
-
return [4 /*yield*/, this.client.send(new GetObjectCommand(input))];
|
|
739
|
-
case 1:
|
|
740
|
-
response = _b.sent();
|
|
741
|
-
return [2 /*return*/, (_a = response.Body) === null || _a === void 0 ? void 0 : _a.transformToString()];
|
|
742
|
-
}
|
|
743
|
-
});
|
|
744
|
-
});
|
|
745
|
-
};
|
|
465
|
+
async getObjectBody(input) {
|
|
466
|
+
this.logger.info('Getting S3 object body', { input });
|
|
467
|
+
const response = await this.client.send(new GetObjectCommand(input));
|
|
468
|
+
return response.Body?.transformToString();
|
|
469
|
+
}
|
|
746
470
|
/**
|
|
747
471
|
* Get an object from S3 and parse it as JSON.
|
|
748
472
|
* @template T - Expected type of the parsed value.
|
|
749
473
|
* @returns The parsed value, or `undefined` if the response has no body.
|
|
750
474
|
* @throws If the body is non-empty and not valid JSON.
|
|
751
475
|
*/
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
case 0:
|
|
759
|
-
this.logger.info('Getting S3 JSON object', { input: input });
|
|
760
|
-
return [4 /*yield*/, this.client.send(new GetObjectCommand(input))];
|
|
761
|
-
case 1:
|
|
762
|
-
response = _b.sent();
|
|
763
|
-
return [4 /*yield*/, ((_a = response.Body) === null || _a === void 0 ? void 0 : _a.transformToString())];
|
|
764
|
-
case 2:
|
|
765
|
-
body = _b.sent();
|
|
766
|
-
return [2 /*return*/, body ? JSON.parse(body) : undefined];
|
|
767
|
-
}
|
|
768
|
-
});
|
|
769
|
-
});
|
|
770
|
-
};
|
|
476
|
+
async getJsonObject(input) {
|
|
477
|
+
this.logger.info('Getting S3 JSON object', { input });
|
|
478
|
+
const response = await this.client.send(new GetObjectCommand(input));
|
|
479
|
+
const body = await response.Body?.transformToString();
|
|
480
|
+
return body ? JSON.parse(body) : undefined;
|
|
481
|
+
}
|
|
771
482
|
/**
|
|
772
483
|
* Fetch the metadata for an S3 object without downloading its body.
|
|
773
484
|
*/
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
return [2 /*return*/, this.client.send(new HeadObjectCommand(input))];
|
|
779
|
-
});
|
|
780
|
-
});
|
|
781
|
-
};
|
|
485
|
+
async headObject(input) {
|
|
486
|
+
this.logger.info('Fetching S3 object metadata', { input });
|
|
487
|
+
return this.client.send(new HeadObjectCommand(input));
|
|
488
|
+
}
|
|
782
489
|
/**
|
|
783
490
|
* Copy an object within S3.
|
|
784
491
|
*/
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
return [2 /*return*/, this.client.send(new CopyObjectCommand(input))];
|
|
790
|
-
});
|
|
791
|
-
});
|
|
792
|
-
};
|
|
492
|
+
async copyObject(input) {
|
|
493
|
+
this.logger.info('Copying S3 object', { input });
|
|
494
|
+
return this.client.send(new CopyObjectCommand(input));
|
|
495
|
+
}
|
|
793
496
|
/**
|
|
794
497
|
* List object keys under a bucket and optional prefix, auto-paginating
|
|
795
498
|
* across all pages.
|
|
796
499
|
*/
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
case 1:
|
|
810
|
-
_g.trys.push([1, 6, 7, 12]);
|
|
811
|
-
_a = true, paginator_1 = __asyncValues(paginator);
|
|
812
|
-
_g.label = 2;
|
|
813
|
-
case 2: return [4 /*yield*/, paginator_1.next()];
|
|
814
|
-
case 3:
|
|
815
|
-
if (!(paginator_1_1 = _g.sent(), _c = paginator_1_1.done, !_c)) return [3 /*break*/, 5];
|
|
816
|
-
_e = paginator_1_1.value;
|
|
817
|
-
_a = false;
|
|
818
|
-
page = _e;
|
|
819
|
-
for (_i = 0, _b = (_f = page.Contents) !== null && _f !== void 0 ? _f : []; _i < _b.length; _i++) {
|
|
820
|
-
object = _b[_i];
|
|
821
|
-
if (object.Key)
|
|
822
|
-
keys.push(object.Key);
|
|
823
|
-
}
|
|
824
|
-
_g.label = 4;
|
|
825
|
-
case 4:
|
|
826
|
-
_a = true;
|
|
827
|
-
return [3 /*break*/, 2];
|
|
828
|
-
case 5: return [3 /*break*/, 12];
|
|
829
|
-
case 6:
|
|
830
|
-
e_1_1 = _g.sent();
|
|
831
|
-
e_1 = { error: e_1_1 };
|
|
832
|
-
return [3 /*break*/, 12];
|
|
833
|
-
case 7:
|
|
834
|
-
_g.trys.push([7, , 10, 11]);
|
|
835
|
-
if (!(!_a && !_c && (_d = paginator_1.return))) return [3 /*break*/, 9];
|
|
836
|
-
return [4 /*yield*/, _d.call(paginator_1)];
|
|
837
|
-
case 8:
|
|
838
|
-
_g.sent();
|
|
839
|
-
_g.label = 9;
|
|
840
|
-
case 9: return [3 /*break*/, 11];
|
|
841
|
-
case 10:
|
|
842
|
-
if (e_1) throw e_1.error;
|
|
843
|
-
return [7 /*endfinally*/];
|
|
844
|
-
case 11: return [7 /*endfinally*/];
|
|
845
|
-
case 12: return [2 /*return*/, keys];
|
|
846
|
-
}
|
|
847
|
-
});
|
|
848
|
-
});
|
|
849
|
-
};
|
|
500
|
+
async listObjects(bucket, prefix) {
|
|
501
|
+
this.logger.info('Listing S3 objects', { input: { bucket, prefix } });
|
|
502
|
+
const paginator = paginateListObjectsV2({ client: this.client }, { Bucket: bucket, Prefix: prefix });
|
|
503
|
+
const keys = [];
|
|
504
|
+
for await (const page of paginator) {
|
|
505
|
+
for (const object of page.Contents ?? []) {
|
|
506
|
+
if (object.Key)
|
|
507
|
+
keys.push(object.Key);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return keys;
|
|
511
|
+
}
|
|
850
512
|
/**
|
|
851
513
|
* List and JSON-parse every object under a bucket and optional prefix.
|
|
852
514
|
* Auto-paginated. Objects without a body are skipped.
|
|
853
515
|
* @template T - Expected type of each parsed object.
|
|
854
516
|
*/
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
case 2: return [4 /*yield*/, paginator_2.next()];
|
|
872
|
-
case 3:
|
|
873
|
-
if (!(paginator_2_1 = _h.sent(), _c = paginator_2_1.done, !_c)) return [3 /*break*/, 9];
|
|
874
|
-
_e = paginator_2_1.value;
|
|
875
|
-
_a = false;
|
|
876
|
-
page = _e;
|
|
877
|
-
_i = 0, _b = (_f = page.Contents) !== null && _f !== void 0 ? _f : [];
|
|
878
|
-
_h.label = 4;
|
|
879
|
-
case 4:
|
|
880
|
-
if (!(_i < _b.length)) return [3 /*break*/, 8];
|
|
881
|
-
object = _b[_i];
|
|
882
|
-
if (!object.Key)
|
|
883
|
-
return [3 /*break*/, 7];
|
|
884
|
-
return [4 /*yield*/, this.client.send(new GetObjectCommand({ Bucket: bucket, Key: object.Key }))];
|
|
885
|
-
case 5:
|
|
886
|
-
response = _h.sent();
|
|
887
|
-
return [4 /*yield*/, ((_g = response.Body) === null || _g === void 0 ? void 0 : _g.transformToString())];
|
|
888
|
-
case 6:
|
|
889
|
-
body = _h.sent();
|
|
890
|
-
if (body)
|
|
891
|
-
bodies.push(JSON.parse(body));
|
|
892
|
-
_h.label = 7;
|
|
893
|
-
case 7:
|
|
894
|
-
_i++;
|
|
895
|
-
return [3 /*break*/, 4];
|
|
896
|
-
case 8:
|
|
897
|
-
_a = true;
|
|
898
|
-
return [3 /*break*/, 2];
|
|
899
|
-
case 9: return [3 /*break*/, 16];
|
|
900
|
-
case 10:
|
|
901
|
-
e_2_1 = _h.sent();
|
|
902
|
-
e_2 = { error: e_2_1 };
|
|
903
|
-
return [3 /*break*/, 16];
|
|
904
|
-
case 11:
|
|
905
|
-
_h.trys.push([11, , 14, 15]);
|
|
906
|
-
if (!(!_a && !_c && (_d = paginator_2.return))) return [3 /*break*/, 13];
|
|
907
|
-
return [4 /*yield*/, _d.call(paginator_2)];
|
|
908
|
-
case 12:
|
|
909
|
-
_h.sent();
|
|
910
|
-
_h.label = 13;
|
|
911
|
-
case 13: return [3 /*break*/, 15];
|
|
912
|
-
case 14:
|
|
913
|
-
if (e_2) throw e_2.error;
|
|
914
|
-
return [7 /*endfinally*/];
|
|
915
|
-
case 15: return [7 /*endfinally*/];
|
|
916
|
-
case 16: return [2 /*return*/, bodies];
|
|
917
|
-
}
|
|
918
|
-
});
|
|
919
|
-
});
|
|
920
|
-
};
|
|
517
|
+
async getAllObjects(bucket, prefix) {
|
|
518
|
+
this.logger.info('Getting all S3 objects', { input: { bucket, prefix } });
|
|
519
|
+
const paginator = paginateListObjectsV2({ client: this.client }, { Bucket: bucket, Prefix: prefix });
|
|
520
|
+
const bodies = [];
|
|
521
|
+
for await (const page of paginator) {
|
|
522
|
+
for (const object of page.Contents ?? []) {
|
|
523
|
+
if (!object.Key)
|
|
524
|
+
continue;
|
|
525
|
+
const response = await this.client.send(new GetObjectCommand({ Bucket: bucket, Key: object.Key }));
|
|
526
|
+
const body = await response.Body?.transformToString();
|
|
527
|
+
if (body)
|
|
528
|
+
bodies.push(JSON.parse(body));
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return bodies;
|
|
532
|
+
}
|
|
921
533
|
/**
|
|
922
534
|
* Generate a presigned URL that callers can use to GET or PUT an S3 object
|
|
923
535
|
* directly, without going through the wrapper. The signing happens against
|
|
@@ -936,137 +548,81 @@ var S3Service = /** @class */ (function () {
|
|
|
936
548
|
* @param input.action - `'get'` to download, `'put'` to upload.
|
|
937
549
|
* @param input.expiresIn - URL lifetime in seconds. Defaults to 3600 (1 hour).
|
|
938
550
|
*/
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
return __generator(this, function (_b) {
|
|
944
|
-
expiresIn = (_a = input.expiresIn) !== null && _a !== void 0 ? _a : DEFAULT_PRESIGNED_URL_EXPIRES_IN_SECONDS;
|
|
945
|
-
this.logger.info('Generating S3 presigned URL', {
|
|
946
|
-
input: { Bucket: input.Bucket, Key: input.Key, action: input.action, expiresIn: expiresIn },
|
|
947
|
-
});
|
|
948
|
-
command = input.action === 'get'
|
|
949
|
-
? new GetObjectCommand({
|
|
950
|
-
Bucket: input.Bucket,
|
|
951
|
-
Key: input.Key,
|
|
952
|
-
ResponseContentDisposition: 'attachment',
|
|
953
|
-
})
|
|
954
|
-
: new PutObjectCommand({ Bucket: input.Bucket, Key: input.Key });
|
|
955
|
-
return [2 /*return*/, getSignedUrl(this.client, command, { expiresIn: expiresIn })];
|
|
956
|
-
});
|
|
551
|
+
async getPresignedUrl(input) {
|
|
552
|
+
const expiresIn = input.expiresIn ?? DEFAULT_PRESIGNED_URL_EXPIRES_IN_SECONDS;
|
|
553
|
+
this.logger.info('Generating S3 presigned URL', {
|
|
554
|
+
input: { Bucket: input.Bucket, Key: input.Key, action: input.action, expiresIn },
|
|
957
555
|
});
|
|
958
|
-
|
|
556
|
+
const command = input.action === 'get'
|
|
557
|
+
? new GetObjectCommand({
|
|
558
|
+
Bucket: input.Bucket,
|
|
559
|
+
Key: input.Key,
|
|
560
|
+
ResponseContentDisposition: 'attachment',
|
|
561
|
+
})
|
|
562
|
+
: new PutObjectCommand({ Bucket: input.Bucket, Key: input.Key });
|
|
563
|
+
return getSignedUrl(this.client, command, { expiresIn });
|
|
564
|
+
}
|
|
959
565
|
/**
|
|
960
566
|
* Delete a single object from S3.
|
|
961
567
|
*/
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
return [2 /*return*/, this.client.send(new DeleteObjectCommand(input))];
|
|
967
|
-
});
|
|
968
|
-
});
|
|
969
|
-
};
|
|
568
|
+
async deleteObject(input) {
|
|
569
|
+
this.logger.info('Deleting S3 object', { input });
|
|
570
|
+
return this.client.send(new DeleteObjectCommand(input));
|
|
571
|
+
}
|
|
970
572
|
/**
|
|
971
573
|
* Delete multiple objects from S3, auto-chunking the request into batches
|
|
972
574
|
* of 1000 keys (the S3-enforced DeleteObjects limit). Returns one output
|
|
973
575
|
* per chunk.
|
|
974
576
|
*/
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
this.logger.info('Deleting S3 objects', {
|
|
983
|
-
input: isDebug ? { bucket: bucket, keys: keys } : { bucket: bucket, keyCount: keys.length },
|
|
984
|
-
});
|
|
985
|
-
results = [];
|
|
986
|
-
i = 0;
|
|
987
|
-
_c.label = 1;
|
|
988
|
-
case 1:
|
|
989
|
-
if (!(i < keys.length)) return [3 /*break*/, 4];
|
|
990
|
-
batch = keys.slice(i, i + DELETE_OBJECTS_BATCH_LIMIT);
|
|
991
|
-
_b = (_a = results).push;
|
|
992
|
-
return [4 /*yield*/, this.client.send(new DeleteObjectsCommand({
|
|
993
|
-
Bucket: bucket,
|
|
994
|
-
Delete: { Objects: batch.map(function (Key) { return ({ Key: Key }); }) },
|
|
995
|
-
}))];
|
|
996
|
-
case 2:
|
|
997
|
-
_b.apply(_a, [_c.sent()]);
|
|
998
|
-
_c.label = 3;
|
|
999
|
-
case 3:
|
|
1000
|
-
i += DELETE_OBJECTS_BATCH_LIMIT;
|
|
1001
|
-
return [3 /*break*/, 1];
|
|
1002
|
-
case 4: return [2 /*return*/, results];
|
|
1003
|
-
}
|
|
1004
|
-
});
|
|
1005
|
-
});
|
|
1006
|
-
};
|
|
1007
|
-
/**
|
|
1008
|
-
* Delete every object in a bucket. Streams the listing page-by-page and
|
|
1009
|
-
* delegates each page's deletion to `deleteObjects`, so peak memory stays
|
|
1010
|
-
* bounded by one page (~1000 keys) regardless of bucket size.
|
|
1011
|
-
* @returns The keys of every deleted object.
|
|
1012
|
-
*/
|
|
1013
|
-
S3Service.prototype.emptyBucket = function (bucket) {
|
|
1014
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1015
|
-
var paginator, deletedKeys, _a, paginator_3, paginator_3_1, page, keys, e_3_1;
|
|
1016
|
-
var _b, e_3, _c, _d;
|
|
1017
|
-
var _e;
|
|
1018
|
-
return __generator(this, function (_f) {
|
|
1019
|
-
switch (_f.label) {
|
|
1020
|
-
case 0:
|
|
1021
|
-
this.logger.info('Emptying S3 bucket', { input: { bucket: bucket } });
|
|
1022
|
-
paginator = paginateListObjectsV2({ client: this.client }, { Bucket: bucket });
|
|
1023
|
-
deletedKeys = [];
|
|
1024
|
-
_f.label = 1;
|
|
1025
|
-
case 1:
|
|
1026
|
-
_f.trys.push([1, 7, 8, 13]);
|
|
1027
|
-
_a = true, paginator_3 = __asyncValues(paginator);
|
|
1028
|
-
_f.label = 2;
|
|
1029
|
-
case 2: return [4 /*yield*/, paginator_3.next()];
|
|
1030
|
-
case 3:
|
|
1031
|
-
if (!(paginator_3_1 = _f.sent(), _b = paginator_3_1.done, !_b)) return [3 /*break*/, 6];
|
|
1032
|
-
_d = paginator_3_1.value;
|
|
1033
|
-
_a = false;
|
|
1034
|
-
page = _d;
|
|
1035
|
-
keys = ((_e = page.Contents) !== null && _e !== void 0 ? _e : []).flatMap(function (o) { return (o.Key ? [o.Key] : []); });
|
|
1036
|
-
if (keys.length === 0)
|
|
1037
|
-
return [3 /*break*/, 5];
|
|
1038
|
-
return [4 /*yield*/, this.deleteObjects(bucket, keys)];
|
|
1039
|
-
case 4:
|
|
1040
|
-
_f.sent();
|
|
1041
|
-
deletedKeys.push.apply(deletedKeys, keys);
|
|
1042
|
-
_f.label = 5;
|
|
1043
|
-
case 5:
|
|
1044
|
-
_a = true;
|
|
1045
|
-
return [3 /*break*/, 2];
|
|
1046
|
-
case 6: return [3 /*break*/, 13];
|
|
1047
|
-
case 7:
|
|
1048
|
-
e_3_1 = _f.sent();
|
|
1049
|
-
e_3 = { error: e_3_1 };
|
|
1050
|
-
return [3 /*break*/, 13];
|
|
1051
|
-
case 8:
|
|
1052
|
-
_f.trys.push([8, , 11, 12]);
|
|
1053
|
-
if (!(!_a && !_b && (_c = paginator_3.return))) return [3 /*break*/, 10];
|
|
1054
|
-
return [4 /*yield*/, _c.call(paginator_3)];
|
|
1055
|
-
case 9:
|
|
1056
|
-
_f.sent();
|
|
1057
|
-
_f.label = 10;
|
|
1058
|
-
case 10: return [3 /*break*/, 12];
|
|
1059
|
-
case 11:
|
|
1060
|
-
if (e_3) throw e_3.error;
|
|
1061
|
-
return [7 /*endfinally*/];
|
|
1062
|
-
case 12: return [7 /*endfinally*/];
|
|
1063
|
-
case 13: return [2 /*return*/, deletedKeys];
|
|
1064
|
-
}
|
|
1065
|
-
});
|
|
577
|
+
async deleteObjects(bucket, keys) {
|
|
578
|
+
// Inline verbosity check rather than `filterFieldsForLogLevel` because the
|
|
579
|
+
// safe log shape includes the computed `keyCount`, which isn't a key
|
|
580
|
+
// on any SDK input type.
|
|
581
|
+
const logFullInput = shouldLogFullInput(this.logger);
|
|
582
|
+
this.logger.info('Deleting S3 objects', {
|
|
583
|
+
input: logFullInput ? { bucket, keys } : { bucket, keyCount: keys.length },
|
|
1066
584
|
});
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
585
|
+
const results = [];
|
|
586
|
+
for (let i = 0; i < keys.length; i += DELETE_OBJECTS_BATCH_LIMIT) {
|
|
587
|
+
const batch = keys.slice(i, i + DELETE_OBJECTS_BATCH_LIMIT);
|
|
588
|
+
results.push(await this.client.send(new DeleteObjectsCommand({
|
|
589
|
+
Bucket: bucket,
|
|
590
|
+
Delete: { Objects: batch.map(Key => ({ Key })) },
|
|
591
|
+
})));
|
|
592
|
+
}
|
|
593
|
+
return results;
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Delete every object in an **unversioned** bucket. Streams the listing
|
|
597
|
+
* page-by-page and delegates each page's deletion to `deleteObjects`, so
|
|
598
|
+
* peak memory stays bounded by one page (~1000 keys) regardless of bucket
|
|
599
|
+
* size.
|
|
600
|
+
*
|
|
601
|
+
* **Versioned buckets are out of scope.** This paginates `ListObjectsV2`,
|
|
602
|
+
* which reports only current object versions — never noncurrent versions or
|
|
603
|
+
* delete markers. Against a versioned bucket the call therefore succeeds and
|
|
604
|
+
* the bucket then *looks* empty to `ListObjectsV2`, but the deletes only
|
|
605
|
+
* added delete markers: every object version is still stored, still billed,
|
|
606
|
+
* and `DeleteBucket` will still fail with `BucketNotEmpty`. Emptying a
|
|
607
|
+
* versioned bucket requires `ListObjectVersions` and per-version deletes;
|
|
608
|
+
* use `S3Client` directly for that.
|
|
609
|
+
*
|
|
610
|
+
* @returns The keys this call issued deletes for.
|
|
611
|
+
*/
|
|
612
|
+
async emptyBucket(bucket) {
|
|
613
|
+
this.logger.info('Emptying S3 bucket', { input: { bucket } });
|
|
614
|
+
const paginator = paginateListObjectsV2({ client: this.client }, { Bucket: bucket });
|
|
615
|
+
const deletedKeys = [];
|
|
616
|
+
for await (const page of paginator) {
|
|
617
|
+
const keys = (page.Contents ?? []).flatMap(o => (o.Key ? [o.Key] : []));
|
|
618
|
+
if (keys.length === 0)
|
|
619
|
+
continue;
|
|
620
|
+
await this.deleteObjects(bucket, keys);
|
|
621
|
+
deletedKeys.push(...keys);
|
|
622
|
+
}
|
|
623
|
+
return deletedKeys;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
1070
626
|
|
|
1071
627
|
/**
|
|
1072
628
|
* `CreateSchedule` / `UpdateSchedule` inputs carry the payload handed to the
|
|
@@ -1082,22 +638,26 @@ var S3Service = /** @class */ (function () {
|
|
|
1082
638
|
* bespoke case: keep every top-level field and shallow-strip only
|
|
1083
639
|
* `Target.Input`.
|
|
1084
640
|
*
|
|
1085
|
-
* At
|
|
1086
|
-
*
|
|
641
|
+
* At the verbose levels — see {@link shouldLogFullInput} — the input is
|
|
642
|
+
* returned unchanged: operators who asked for that verbosity have opted into
|
|
643
|
+
* seeing payloads and PII.
|
|
1087
644
|
*/
|
|
1088
645
|
function redactTargetInput(logger, input) {
|
|
1089
|
-
|
|
1090
|
-
if (logger.getLevelName() === 'DEBUG')
|
|
646
|
+
if (shouldLogFullInput(logger))
|
|
1091
647
|
return input;
|
|
1092
|
-
if (
|
|
648
|
+
if (input.Target?.Input === undefined)
|
|
1093
649
|
return input;
|
|
1094
|
-
|
|
1095
|
-
return
|
|
650
|
+
const { Input: _input, ...safeTarget } = input.Target;
|
|
651
|
+
return { ...input, Target: safeTarget };
|
|
1096
652
|
}
|
|
1097
653
|
/**
|
|
1098
654
|
* Wrapper around the AWS EventBridge Scheduler client providing structured
|
|
1099
655
|
* Powertools logging and X-Ray tracing by default.
|
|
1100
656
|
*
|
|
657
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
658
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
659
|
+
* `TRACE`) log full SDK inputs.
|
|
660
|
+
*
|
|
1101
661
|
* Covers CRUD on schedules and their groups. Named `SchedulerService` (not
|
|
1102
662
|
* `EventBridgeSchedulerService`) so it doesn't collide with a future wrapper
|
|
1103
663
|
* over the separate EventBridge bus/rules API (`@aws-sdk/client-eventbridge`).
|
|
@@ -1106,42 +666,34 @@ function redactTargetInput(logger, input) {
|
|
|
1106
666
|
* untouched — the wrapper bakes in no default, so the caller must state their
|
|
1107
667
|
* intent (`{ Mode: 'OFF' }` for a fixed-time schedule).
|
|
1108
668
|
*/
|
|
1109
|
-
|
|
669
|
+
class SchedulerService {
|
|
670
|
+
client;
|
|
671
|
+
logger;
|
|
1110
672
|
/**
|
|
1111
673
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
1112
674
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
1113
675
|
* @param opts.client - Optional pre-configured `SchedulerClient`. When
|
|
1114
676
|
* supplied, the wrapper does not apply X-Ray instrumentation.
|
|
1115
677
|
*/
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
this.
|
|
1119
|
-
this.logger = (_b = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _b !== void 0 ? _b : new Logger();
|
|
678
|
+
constructor(opts) {
|
|
679
|
+
this.client = opts?.client ?? xray.captureAWSv3Client(new SchedulerClient());
|
|
680
|
+
this.logger = opts?.logger ?? new Logger();
|
|
1120
681
|
}
|
|
1121
682
|
/**
|
|
1122
683
|
* Create a new schedule. At INFO the log line omits `Target.Input` (the
|
|
1123
|
-
* target payload, a PII carrier)
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
return
|
|
1128
|
-
|
|
1129
|
-
this.logger.info('Creating schedule', { input: redactTargetInput(this.logger, input) });
|
|
1130
|
-
return [2 /*return*/, this.client.send(new CreateScheduleCommand(input))];
|
|
1131
|
-
});
|
|
1132
|
-
});
|
|
1133
|
-
};
|
|
684
|
+
* target payload, a PII carrier).
|
|
685
|
+
*/
|
|
686
|
+
async createSchedule(input) {
|
|
687
|
+
this.logger.info('Creating schedule', { input: redactTargetInput(this.logger, input) });
|
|
688
|
+
return this.client.send(new CreateScheduleCommand(input));
|
|
689
|
+
}
|
|
1134
690
|
/**
|
|
1135
691
|
* Fetch a schedule's full configuration.
|
|
1136
692
|
*/
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
return [2 /*return*/, this.client.send(new GetScheduleCommand(input))];
|
|
1142
|
-
});
|
|
1143
|
-
});
|
|
1144
|
-
};
|
|
693
|
+
async getSchedule(input) {
|
|
694
|
+
this.logger.info('Getting schedule', { input });
|
|
695
|
+
return this.client.send(new GetScheduleCommand(input));
|
|
696
|
+
}
|
|
1145
697
|
/**
|
|
1146
698
|
* Update an existing schedule.
|
|
1147
699
|
*
|
|
@@ -1153,180 +705,79 @@ var SchedulerService = /** @class */ (function () {
|
|
|
1153
705
|
* your behalf: deep-merging a `Target` (with its mutually-exclusive
|
|
1154
706
|
* target-type params) has no single correct semantics.
|
|
1155
707
|
*
|
|
1156
|
-
* At INFO the log line omits `Target.Input
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
return
|
|
1161
|
-
|
|
1162
|
-
this.logger.info('Updating schedule', { input: redactTargetInput(this.logger, input) });
|
|
1163
|
-
return [2 /*return*/, this.client.send(new UpdateScheduleCommand(input))];
|
|
1164
|
-
});
|
|
1165
|
-
});
|
|
1166
|
-
};
|
|
708
|
+
* At INFO the log line omits `Target.Input`.
|
|
709
|
+
*/
|
|
710
|
+
async updateSchedule(input) {
|
|
711
|
+
this.logger.info('Updating schedule', { input: redactTargetInput(this.logger, input) });
|
|
712
|
+
return this.client.send(new UpdateScheduleCommand(input));
|
|
713
|
+
}
|
|
1167
714
|
/**
|
|
1168
715
|
* Delete a schedule.
|
|
1169
716
|
*/
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
return [2 /*return*/, this.client.send(new DeleteScheduleCommand(input))];
|
|
1175
|
-
});
|
|
1176
|
-
});
|
|
1177
|
-
};
|
|
717
|
+
async deleteSchedule(input) {
|
|
718
|
+
this.logger.info('Deleting schedule', { input });
|
|
719
|
+
return this.client.send(new DeleteScheduleCommand(input));
|
|
720
|
+
}
|
|
1178
721
|
/**
|
|
1179
722
|
* List schedules, auto-paginating across all pages. Bounded in practice by
|
|
1180
723
|
* the account's schedule count and the `GroupName` / `NamePrefix` / `State`
|
|
1181
724
|
* filters, so the flat-array shape is safe.
|
|
1182
725
|
*/
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
_e.label = 1;
|
|
1194
|
-
case 1:
|
|
1195
|
-
_e.trys.push([1, 6, 7, 12]);
|
|
1196
|
-
_a = true, paginator_1 = __asyncValues(paginator);
|
|
1197
|
-
_e.label = 2;
|
|
1198
|
-
case 2: return [4 /*yield*/, paginator_1.next()];
|
|
1199
|
-
case 3:
|
|
1200
|
-
if (!(paginator_1_1 = _e.sent(), _b = paginator_1_1.done, !_b)) return [3 /*break*/, 5];
|
|
1201
|
-
_d = paginator_1_1.value;
|
|
1202
|
-
_a = false;
|
|
1203
|
-
page = _d;
|
|
1204
|
-
if (page.Schedules)
|
|
1205
|
-
schedules.push.apply(schedules, page.Schedules);
|
|
1206
|
-
_e.label = 4;
|
|
1207
|
-
case 4:
|
|
1208
|
-
_a = true;
|
|
1209
|
-
return [3 /*break*/, 2];
|
|
1210
|
-
case 5: return [3 /*break*/, 12];
|
|
1211
|
-
case 6:
|
|
1212
|
-
e_1_1 = _e.sent();
|
|
1213
|
-
e_1 = { error: e_1_1 };
|
|
1214
|
-
return [3 /*break*/, 12];
|
|
1215
|
-
case 7:
|
|
1216
|
-
_e.trys.push([7, , 10, 11]);
|
|
1217
|
-
if (!(!_a && !_b && (_c = paginator_1.return))) return [3 /*break*/, 9];
|
|
1218
|
-
return [4 /*yield*/, _c.call(paginator_1)];
|
|
1219
|
-
case 8:
|
|
1220
|
-
_e.sent();
|
|
1221
|
-
_e.label = 9;
|
|
1222
|
-
case 9: return [3 /*break*/, 11];
|
|
1223
|
-
case 10:
|
|
1224
|
-
if (e_1) throw e_1.error;
|
|
1225
|
-
return [7 /*endfinally*/];
|
|
1226
|
-
case 11: return [7 /*endfinally*/];
|
|
1227
|
-
case 12: return [2 /*return*/, schedules];
|
|
1228
|
-
}
|
|
1229
|
-
});
|
|
1230
|
-
});
|
|
1231
|
-
};
|
|
726
|
+
async listSchedules(input) {
|
|
727
|
+
this.logger.info('Listing schedules', { input });
|
|
728
|
+
const paginator = paginateListSchedules({ client: this.client }, input ?? {});
|
|
729
|
+
const schedules = [];
|
|
730
|
+
for await (const page of paginator) {
|
|
731
|
+
if (page.Schedules)
|
|
732
|
+
schedules.push(...page.Schedules);
|
|
733
|
+
}
|
|
734
|
+
return schedules;
|
|
735
|
+
}
|
|
1232
736
|
/**
|
|
1233
737
|
* Create a new schedule group. Groups are containers for schedules and are
|
|
1234
738
|
* typically IaC-managed; use this for dynamically-provisioned groups only.
|
|
1235
739
|
*/
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
return [2 /*return*/, this.client.send(new CreateScheduleGroupCommand(input))];
|
|
1241
|
-
});
|
|
1242
|
-
});
|
|
1243
|
-
};
|
|
740
|
+
async createScheduleGroup(input) {
|
|
741
|
+
this.logger.info('Creating schedule group', { input });
|
|
742
|
+
return this.client.send(new CreateScheduleGroupCommand(input));
|
|
743
|
+
}
|
|
1244
744
|
/**
|
|
1245
745
|
* Fetch a schedule group's configuration.
|
|
1246
746
|
*/
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
return [2 /*return*/, this.client.send(new GetScheduleGroupCommand(input))];
|
|
1252
|
-
});
|
|
1253
|
-
});
|
|
1254
|
-
};
|
|
747
|
+
async getScheduleGroup(input) {
|
|
748
|
+
this.logger.info('Getting schedule group', { input });
|
|
749
|
+
return this.client.send(new GetScheduleGroupCommand(input));
|
|
750
|
+
}
|
|
1255
751
|
/**
|
|
1256
752
|
* Delete a schedule group. Deleting a group also deletes every schedule it
|
|
1257
753
|
* contains, asynchronously — the API has no `UpdateScheduleGroup`, so this
|
|
1258
754
|
* is the only mutating group operation besides create.
|
|
1259
755
|
*/
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
return [2 /*return*/, this.client.send(new DeleteScheduleGroupCommand(input))];
|
|
1265
|
-
});
|
|
1266
|
-
});
|
|
1267
|
-
};
|
|
756
|
+
async deleteScheduleGroup(input) {
|
|
757
|
+
this.logger.info('Deleting schedule group', { input });
|
|
758
|
+
return this.client.send(new DeleteScheduleGroupCommand(input));
|
|
759
|
+
}
|
|
1268
760
|
/**
|
|
1269
761
|
* List schedule groups, auto-paginating across all pages. Bounded in
|
|
1270
762
|
* practice by the account's group count and the `NamePrefix` filter.
|
|
1271
763
|
*/
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
case 1:
|
|
1284
|
-
_e.trys.push([1, 6, 7, 12]);
|
|
1285
|
-
_a = true, paginator_2 = __asyncValues(paginator);
|
|
1286
|
-
_e.label = 2;
|
|
1287
|
-
case 2: return [4 /*yield*/, paginator_2.next()];
|
|
1288
|
-
case 3:
|
|
1289
|
-
if (!(paginator_2_1 = _e.sent(), _b = paginator_2_1.done, !_b)) return [3 /*break*/, 5];
|
|
1290
|
-
_d = paginator_2_1.value;
|
|
1291
|
-
_a = false;
|
|
1292
|
-
page = _d;
|
|
1293
|
-
if (page.ScheduleGroups)
|
|
1294
|
-
groups.push.apply(groups, page.ScheduleGroups);
|
|
1295
|
-
_e.label = 4;
|
|
1296
|
-
case 4:
|
|
1297
|
-
_a = true;
|
|
1298
|
-
return [3 /*break*/, 2];
|
|
1299
|
-
case 5: return [3 /*break*/, 12];
|
|
1300
|
-
case 6:
|
|
1301
|
-
e_2_1 = _e.sent();
|
|
1302
|
-
e_2 = { error: e_2_1 };
|
|
1303
|
-
return [3 /*break*/, 12];
|
|
1304
|
-
case 7:
|
|
1305
|
-
_e.trys.push([7, , 10, 11]);
|
|
1306
|
-
if (!(!_a && !_b && (_c = paginator_2.return))) return [3 /*break*/, 9];
|
|
1307
|
-
return [4 /*yield*/, _c.call(paginator_2)];
|
|
1308
|
-
case 8:
|
|
1309
|
-
_e.sent();
|
|
1310
|
-
_e.label = 9;
|
|
1311
|
-
case 9: return [3 /*break*/, 11];
|
|
1312
|
-
case 10:
|
|
1313
|
-
if (e_2) throw e_2.error;
|
|
1314
|
-
return [7 /*endfinally*/];
|
|
1315
|
-
case 11: return [7 /*endfinally*/];
|
|
1316
|
-
case 12: return [2 /*return*/, groups];
|
|
1317
|
-
}
|
|
1318
|
-
});
|
|
1319
|
-
});
|
|
1320
|
-
};
|
|
1321
|
-
return SchedulerService;
|
|
1322
|
-
}());
|
|
764
|
+
async listScheduleGroups(input) {
|
|
765
|
+
this.logger.info('Listing schedule groups', { input });
|
|
766
|
+
const paginator = paginateListScheduleGroups({ client: this.client }, input ?? {});
|
|
767
|
+
const groups = [];
|
|
768
|
+
for await (const page of paginator) {
|
|
769
|
+
if (page.ScheduleGroups)
|
|
770
|
+
groups.push(...page.ScheduleGroups);
|
|
771
|
+
}
|
|
772
|
+
return groups;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
1323
775
|
|
|
1324
776
|
/**
|
|
1325
777
|
* Fields safe to log at INFO level. Omits `SecretString` and `SecretBinary` —
|
|
1326
|
-
* the secret material itself.
|
|
1327
|
-
* input.
|
|
778
|
+
* the secret material itself.
|
|
1328
779
|
*/
|
|
1329
|
-
|
|
780
|
+
const CREATE_SECRET_SAFE_FIELDS = [
|
|
1330
781
|
'Name',
|
|
1331
782
|
'Description',
|
|
1332
783
|
'KmsKeyId',
|
|
@@ -1335,13 +786,13 @@ var CREATE_SECRET_SAFE_FIELDS = [
|
|
|
1335
786
|
'AddReplicaRegions',
|
|
1336
787
|
'ForceOverwriteReplicaSecret',
|
|
1337
788
|
];
|
|
1338
|
-
|
|
789
|
+
const UPDATE_SECRET_SAFE_FIELDS = [
|
|
1339
790
|
'SecretId',
|
|
1340
791
|
'Description',
|
|
1341
792
|
'KmsKeyId',
|
|
1342
793
|
'ClientRequestToken',
|
|
1343
794
|
];
|
|
1344
|
-
|
|
795
|
+
const PUT_SECRET_VALUE_SAFE_FIELDS = [
|
|
1345
796
|
'SecretId',
|
|
1346
797
|
'VersionStages',
|
|
1347
798
|
'ClientRequestToken',
|
|
@@ -1351,7 +802,7 @@ var PUT_SECRET_VALUE_SAFE_FIELDS = [
|
|
|
1351
802
|
* explicit allowlist keeps the log shape consistent with the other write
|
|
1352
803
|
* methods and protects against future field additions.
|
|
1353
804
|
*/
|
|
1354
|
-
|
|
805
|
+
const DELETE_SECRET_SAFE_FIELDS = [
|
|
1355
806
|
'SecretId',
|
|
1356
807
|
'RecoveryWindowInDays',
|
|
1357
808
|
'ForceDeleteWithoutRecovery',
|
|
@@ -1360,6 +811,10 @@ var DELETE_SECRET_SAFE_FIELDS = [
|
|
|
1360
811
|
* Wrapper around the AWS Secrets Manager client providing structured
|
|
1361
812
|
* Powertools logging and X-Ray tracing by default.
|
|
1362
813
|
*
|
|
814
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
815
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
816
|
+
* `TRACE`) log full SDK inputs.
|
|
817
|
+
*
|
|
1363
818
|
* Write operations (`createSecret`, `updateSecret`, `putSecretValue`,
|
|
1364
819
|
* `deleteSecret`) are exposed for convenience but should be used with care:
|
|
1365
820
|
* secret lifecycle is usually managed by IaC (CDK / Terraform). Prefer IaC
|
|
@@ -1367,7 +822,9 @@ var DELETE_SECRET_SAFE_FIELDS = [
|
|
|
1367
822
|
* dynamically-issued credentials, rotation flows, or other genuinely
|
|
1368
823
|
* mutable values.
|
|
1369
824
|
*/
|
|
1370
|
-
|
|
825
|
+
class SecretsManagerService {
|
|
826
|
+
client;
|
|
827
|
+
logger;
|
|
1371
828
|
/**
|
|
1372
829
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
1373
830
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
@@ -1375,10 +832,9 @@ var SecretsManagerService = /** @class */ (function () {
|
|
|
1375
832
|
* supplied, the wrapper does not apply X-Ray instrumentation — the caller
|
|
1376
833
|
* owns that decision.
|
|
1377
834
|
*/
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
this.
|
|
1381
|
-
this.logger = (_b = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _b !== void 0 ? _b : new Logger();
|
|
835
|
+
constructor(opts) {
|
|
836
|
+
this.client = opts?.client ?? xray.captureAWSv3Client(new SecretsManagerClient());
|
|
837
|
+
this.logger = opts?.logger ?? new Logger();
|
|
1382
838
|
}
|
|
1383
839
|
/**
|
|
1384
840
|
* Fetch a secret's string value from Secrets Manager.
|
|
@@ -1387,14 +843,10 @@ var SecretsManagerService = /** @class */ (function () {
|
|
|
1387
843
|
* @throws If the response does not contain a `SecretString` (e.g. the secret
|
|
1388
844
|
* stores binary data).
|
|
1389
845
|
*/
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
return [2 /*return*/, this.fetchSecretString(secretId)];
|
|
1395
|
-
});
|
|
1396
|
-
});
|
|
1397
|
-
};
|
|
846
|
+
async getSecret(secretId) {
|
|
847
|
+
this.logger.info('Fetching secret', { input: { secretId } });
|
|
848
|
+
return this.fetchSecretString(secretId);
|
|
849
|
+
}
|
|
1398
850
|
/**
|
|
1399
851
|
* Fetch a secret and parse it as JSON.
|
|
1400
852
|
* @param secretId - The ARN or friendly name of the secret.
|
|
@@ -1402,115 +854,76 @@ var SecretsManagerService = /** @class */ (function () {
|
|
|
1402
854
|
* @returns The parsed secret value.
|
|
1403
855
|
* @throws If the secret has no `SecretString` or the value is not valid JSON.
|
|
1404
856
|
*/
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
case 0:
|
|
1411
|
-
this.logger.info('Fetching JSON secret', { input: { secretId: secretId } });
|
|
1412
|
-
return [4 /*yield*/, this.fetchSecretString(secretId)];
|
|
1413
|
-
case 1:
|
|
1414
|
-
secretString = _a.sent();
|
|
1415
|
-
return [2 /*return*/, JSON.parse(secretString)];
|
|
1416
|
-
}
|
|
1417
|
-
});
|
|
1418
|
-
});
|
|
1419
|
-
};
|
|
857
|
+
async getJsonSecret(secretId) {
|
|
858
|
+
this.logger.info('Fetching JSON secret', { input: { secretId } });
|
|
859
|
+
const secretString = await this.fetchSecretString(secretId);
|
|
860
|
+
return JSON.parse(secretString);
|
|
861
|
+
}
|
|
1420
862
|
/**
|
|
1421
863
|
* Create a new secret. At INFO level the log line includes only identity
|
|
1422
|
-
* and non-secret metadata
|
|
1423
|
-
* input (including `SecretString` / `SecretBinary`).
|
|
864
|
+
* and non-secret metadata.
|
|
1424
865
|
*
|
|
1425
866
|
* Prefer IaC (CDK / Terraform) for secret lifecycle — use this for
|
|
1426
867
|
* dynamically-issued credentials only.
|
|
1427
868
|
*/
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
this.logger.info('Creating secret', {
|
|
1432
|
-
input: filterFieldsForLogLevel(this.logger, input, CREATE_SECRET_SAFE_FIELDS),
|
|
1433
|
-
});
|
|
1434
|
-
return [2 /*return*/, this.client.send(new CreateSecretCommand(input))];
|
|
1435
|
-
});
|
|
869
|
+
async createSecret(input) {
|
|
870
|
+
this.logger.info('Creating secret', {
|
|
871
|
+
input: filterFieldsForLogLevel(this.logger, input, CREATE_SECRET_SAFE_FIELDS),
|
|
1436
872
|
});
|
|
1437
|
-
|
|
873
|
+
return this.client.send(new CreateSecretCommand(input));
|
|
874
|
+
}
|
|
1438
875
|
/**
|
|
1439
876
|
* Update an existing secret's metadata or value. At INFO level the log
|
|
1440
|
-
* line omits `SecretString` / `SecretBinary
|
|
1441
|
-
* unlocks the full input.
|
|
877
|
+
* line omits `SecretString` / `SecretBinary`.
|
|
1442
878
|
*
|
|
1443
879
|
* Prefer IaC (CDK / Terraform) for secret lifecycle — use this for
|
|
1444
880
|
* runtime metadata updates only.
|
|
1445
881
|
*/
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
this.logger.info('Updating secret', {
|
|
1450
|
-
input: filterFieldsForLogLevel(this.logger, input, UPDATE_SECRET_SAFE_FIELDS),
|
|
1451
|
-
});
|
|
1452
|
-
return [2 /*return*/, this.client.send(new UpdateSecretCommand(input))];
|
|
1453
|
-
});
|
|
882
|
+
async updateSecret(input) {
|
|
883
|
+
this.logger.info('Updating secret', {
|
|
884
|
+
input: filterFieldsForLogLevel(this.logger, input, UPDATE_SECRET_SAFE_FIELDS),
|
|
1454
885
|
});
|
|
1455
|
-
|
|
886
|
+
return this.client.send(new UpdateSecretCommand(input));
|
|
887
|
+
}
|
|
1456
888
|
/**
|
|
1457
889
|
* Store a new version of a secret's value. At INFO level the log line
|
|
1458
|
-
* omits `SecretString` / `SecretBinary
|
|
1459
|
-
* unlocks the full input.
|
|
890
|
+
* omits `SecretString` / `SecretBinary`.
|
|
1460
891
|
*
|
|
1461
892
|
* Typically used by rotation flows.
|
|
1462
893
|
*/
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
this.logger.info('Putting secret value', {
|
|
1467
|
-
input: filterFieldsForLogLevel(this.logger, input, PUT_SECRET_VALUE_SAFE_FIELDS),
|
|
1468
|
-
});
|
|
1469
|
-
return [2 /*return*/, this.client.send(new PutSecretValueCommand(input))];
|
|
1470
|
-
});
|
|
894
|
+
async putSecretValue(input) {
|
|
895
|
+
this.logger.info('Putting secret value', {
|
|
896
|
+
input: filterFieldsForLogLevel(this.logger, input, PUT_SECRET_VALUE_SAFE_FIELDS),
|
|
1471
897
|
});
|
|
1472
|
-
|
|
898
|
+
return this.client.send(new PutSecretValueCommand(input));
|
|
899
|
+
}
|
|
1473
900
|
/**
|
|
1474
901
|
* Delete a secret. Pass `ForceDeleteWithoutRecovery: true` to bypass the
|
|
1475
902
|
* default 7-30 day recovery window (irreversible).
|
|
1476
903
|
*
|
|
1477
904
|
* Prefer IaC (CDK / Terraform) for secret lifecycle.
|
|
1478
905
|
*/
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
this.logger.info('Deleting secret', {
|
|
1483
|
-
input: filterFieldsForLogLevel(this.logger, input, DELETE_SECRET_SAFE_FIELDS),
|
|
1484
|
-
});
|
|
1485
|
-
return [2 /*return*/, this.client.send(new DeleteSecretCommand(input))];
|
|
1486
|
-
});
|
|
1487
|
-
});
|
|
1488
|
-
};
|
|
1489
|
-
SecretsManagerService.prototype.fetchSecretString = function (secretId) {
|
|
1490
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1491
|
-
var response;
|
|
1492
|
-
return __generator(this, function (_a) {
|
|
1493
|
-
switch (_a.label) {
|
|
1494
|
-
case 0: return [4 /*yield*/, this.client.send(new GetSecretValueCommand({ SecretId: secretId }))];
|
|
1495
|
-
case 1:
|
|
1496
|
-
response = _a.sent();
|
|
1497
|
-
if (response.SecretString === undefined) {
|
|
1498
|
-
throw new Error("Secret '".concat(secretId, "' does not contain a string value"));
|
|
1499
|
-
}
|
|
1500
|
-
return [2 /*return*/, response.SecretString];
|
|
1501
|
-
}
|
|
1502
|
-
});
|
|
906
|
+
async deleteSecret(input) {
|
|
907
|
+
this.logger.info('Deleting secret', {
|
|
908
|
+
input: filterFieldsForLogLevel(this.logger, input, DELETE_SECRET_SAFE_FIELDS),
|
|
1503
909
|
});
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
910
|
+
return this.client.send(new DeleteSecretCommand(input));
|
|
911
|
+
}
|
|
912
|
+
async fetchSecretString(secretId) {
|
|
913
|
+
const response = await this.client.send(new GetSecretValueCommand({ SecretId: secretId }));
|
|
914
|
+
if (response.SecretString === undefined) {
|
|
915
|
+
throw new Error(`Secret '${secretId}' does not contain a string value`);
|
|
916
|
+
}
|
|
917
|
+
return response.SecretString;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
1507
920
|
|
|
1508
921
|
/**
|
|
1509
922
|
* Fields safe to log at INFO for `startExecution`. Omits `input` — the SFN
|
|
1510
923
|
* execution payload, which routinely carries PII (customer IDs, addresses,
|
|
1511
|
-
* order contents, etc.).
|
|
924
|
+
* order contents, etc.).
|
|
1512
925
|
*/
|
|
1513
|
-
|
|
926
|
+
const START_EXECUTION_SAFE_FIELDS = [
|
|
1514
927
|
'stateMachineArn',
|
|
1515
928
|
'name',
|
|
1516
929
|
'traceHeader',
|
|
@@ -1518,110 +931,63 @@ var START_EXECUTION_SAFE_FIELDS = [
|
|
|
1518
931
|
/**
|
|
1519
932
|
* Wrapper around the AWS Step Functions client providing structured
|
|
1520
933
|
* Powertools logging and X-Ray tracing by default.
|
|
934
|
+
*
|
|
935
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
936
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
937
|
+
* `TRACE`) log full SDK inputs.
|
|
1521
938
|
*/
|
|
1522
|
-
|
|
939
|
+
class StepFunctionsService {
|
|
940
|
+
client;
|
|
941
|
+
logger;
|
|
1523
942
|
/**
|
|
1524
943
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
1525
944
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
1526
945
|
* @param opts.client - Optional pre-configured `SFNClient`. When supplied,
|
|
1527
946
|
* the wrapper does not apply X-Ray instrumentation.
|
|
1528
947
|
*/
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
this.
|
|
1532
|
-
this.logger = (_b = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _b !== void 0 ? _b : new Logger();
|
|
948
|
+
constructor(opts) {
|
|
949
|
+
this.client = opts?.client ?? xray.captureAWSv3Client(new SFNClient());
|
|
950
|
+
this.logger = opts?.logger ?? new Logger();
|
|
1533
951
|
}
|
|
1534
952
|
/**
|
|
1535
953
|
* List all executions for a state machine, auto-paginating across all
|
|
1536
954
|
* pages. Typically bounded by `statusFilter` and state-machine retention,
|
|
1537
955
|
* so the flat-array shape is safe in practice.
|
|
1538
956
|
*/
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
_e.label = 1;
|
|
1550
|
-
case 1:
|
|
1551
|
-
_e.trys.push([1, 6, 7, 12]);
|
|
1552
|
-
_a = true, paginator_1 = __asyncValues(paginator);
|
|
1553
|
-
_e.label = 2;
|
|
1554
|
-
case 2: return [4 /*yield*/, paginator_1.next()];
|
|
1555
|
-
case 3:
|
|
1556
|
-
if (!(paginator_1_1 = _e.sent(), _b = paginator_1_1.done, !_b)) return [3 /*break*/, 5];
|
|
1557
|
-
_d = paginator_1_1.value;
|
|
1558
|
-
_a = false;
|
|
1559
|
-
page = _d;
|
|
1560
|
-
if (page.executions)
|
|
1561
|
-
executions.push.apply(executions, page.executions);
|
|
1562
|
-
_e.label = 4;
|
|
1563
|
-
case 4:
|
|
1564
|
-
_a = true;
|
|
1565
|
-
return [3 /*break*/, 2];
|
|
1566
|
-
case 5: return [3 /*break*/, 12];
|
|
1567
|
-
case 6:
|
|
1568
|
-
e_1_1 = _e.sent();
|
|
1569
|
-
e_1 = { error: e_1_1 };
|
|
1570
|
-
return [3 /*break*/, 12];
|
|
1571
|
-
case 7:
|
|
1572
|
-
_e.trys.push([7, , 10, 11]);
|
|
1573
|
-
if (!(!_a && !_b && (_c = paginator_1.return))) return [3 /*break*/, 9];
|
|
1574
|
-
return [4 /*yield*/, _c.call(paginator_1)];
|
|
1575
|
-
case 8:
|
|
1576
|
-
_e.sent();
|
|
1577
|
-
_e.label = 9;
|
|
1578
|
-
case 9: return [3 /*break*/, 11];
|
|
1579
|
-
case 10:
|
|
1580
|
-
if (e_1) throw e_1.error;
|
|
1581
|
-
return [7 /*endfinally*/];
|
|
1582
|
-
case 11: return [7 /*endfinally*/];
|
|
1583
|
-
case 12: return [2 /*return*/, executions];
|
|
1584
|
-
}
|
|
1585
|
-
});
|
|
1586
|
-
});
|
|
1587
|
-
};
|
|
957
|
+
async listExecutions(input) {
|
|
958
|
+
this.logger.info('Listing Step Functions executions', { input });
|
|
959
|
+
const paginator = paginateListExecutions({ client: this.client }, input);
|
|
960
|
+
const executions = [];
|
|
961
|
+
for await (const page of paginator) {
|
|
962
|
+
if (page.executions)
|
|
963
|
+
executions.push(...page.executions);
|
|
964
|
+
}
|
|
965
|
+
return executions;
|
|
966
|
+
}
|
|
1588
967
|
/**
|
|
1589
968
|
* Start a new Step Functions execution.
|
|
1590
969
|
*/
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
this.logger.info('Starting Step Functions execution', {
|
|
1595
|
-
input: filterFieldsForLogLevel(this.logger, input, START_EXECUTION_SAFE_FIELDS),
|
|
1596
|
-
});
|
|
1597
|
-
return [2 /*return*/, this.client.send(new StartExecutionCommand(input))];
|
|
1598
|
-
});
|
|
970
|
+
async startExecution(input) {
|
|
971
|
+
this.logger.info('Starting Step Functions execution', {
|
|
972
|
+
input: filterFieldsForLogLevel(this.logger, input, START_EXECUTION_SAFE_FIELDS),
|
|
1599
973
|
});
|
|
1600
|
-
|
|
974
|
+
return this.client.send(new StartExecutionCommand(input));
|
|
975
|
+
}
|
|
1601
976
|
/**
|
|
1602
977
|
* Describe an existing Step Functions execution.
|
|
1603
978
|
*/
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
return [2 /*return*/, this.client.send(new DescribeExecutionCommand(input))];
|
|
1609
|
-
});
|
|
1610
|
-
});
|
|
1611
|
-
};
|
|
979
|
+
async describeExecution(input) {
|
|
980
|
+
this.logger.info('Describing Step Functions execution', { input });
|
|
981
|
+
return this.client.send(new DescribeExecutionCommand(input));
|
|
982
|
+
}
|
|
1612
983
|
/**
|
|
1613
984
|
* Stop an in-progress Step Functions execution.
|
|
1614
985
|
*/
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
});
|
|
1621
|
-
});
|
|
1622
|
-
};
|
|
1623
|
-
return StepFunctionsService;
|
|
1624
|
-
}());
|
|
986
|
+
async stopExecution(input) {
|
|
987
|
+
this.logger.info('Stopping Step Functions execution', { input });
|
|
988
|
+
return this.client.send(new StopExecutionCommand(input));
|
|
989
|
+
}
|
|
990
|
+
}
|
|
1625
991
|
|
|
1626
992
|
/**
|
|
1627
993
|
* Truncate a UTF-8 string to fit within `maxBytes` when encoded as UTF-8.
|
|
@@ -1631,12 +997,12 @@ var StepFunctionsService = /** @class */ (function () {
|
|
|
1631
997
|
* it already fits.
|
|
1632
998
|
*/
|
|
1633
999
|
function truncateUtf8(str, maxBytes) {
|
|
1634
|
-
|
|
1000
|
+
const buf = Buffer.from(str, 'utf8');
|
|
1635
1001
|
if (buf.length <= maxBytes)
|
|
1636
1002
|
return str;
|
|
1637
|
-
|
|
1003
|
+
let end = maxBytes;
|
|
1638
1004
|
while (end > 0) {
|
|
1639
|
-
|
|
1005
|
+
const byte = buf[end];
|
|
1640
1006
|
if (byte === undefined || (byte & 0xc0) !== 0x80)
|
|
1641
1007
|
break;
|
|
1642
1008
|
end--;
|
|
@@ -1650,22 +1016,22 @@ function truncateUtf8(str, maxBytes) {
|
|
|
1650
1016
|
* it already fits.
|
|
1651
1017
|
*/
|
|
1652
1018
|
function truncateCodepoints(str, maxChars) {
|
|
1653
|
-
|
|
1019
|
+
const codepoints = Array.from(str);
|
|
1654
1020
|
if (codepoints.length <= maxChars)
|
|
1655
1021
|
return str;
|
|
1656
1022
|
return codepoints.slice(0, maxChars).join('');
|
|
1657
1023
|
}
|
|
1658
1024
|
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1025
|
+
const PUBLISH_BATCH_LIMIT = 10;
|
|
1026
|
+
const SNS_MESSAGE_MAX_BYTES = 256 * 1024;
|
|
1027
|
+
const SNS_SUBJECT_MAX_CHARS = 100;
|
|
1662
1028
|
/**
|
|
1663
1029
|
* Fields safe to log at INFO level for `publish`. Omits `Message`, `Subject`,
|
|
1664
1030
|
* `MessageAttributes`, and `PhoneNumber` — payloads, user-visible content
|
|
1665
1031
|
* (subjects often carry order numbers / customer names), and PII recipient
|
|
1666
|
-
* identifiers.
|
|
1032
|
+
* identifiers.
|
|
1667
1033
|
*/
|
|
1668
|
-
|
|
1034
|
+
const PUBLISH_SAFE_FIELDS = [
|
|
1669
1035
|
'TopicArn',
|
|
1670
1036
|
'TargetArn',
|
|
1671
1037
|
'MessageStructure',
|
|
@@ -1675,8 +1041,15 @@ var PUBLISH_SAFE_FIELDS = [
|
|
|
1675
1041
|
/**
|
|
1676
1042
|
* Wrapper around the AWS SNS client providing structured Powertools logging
|
|
1677
1043
|
* and X-Ray tracing by default.
|
|
1044
|
+
*
|
|
1045
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
1046
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
1047
|
+
* `TRACE`) log full SDK inputs.
|
|
1678
1048
|
*/
|
|
1679
|
-
|
|
1049
|
+
class SNSService {
|
|
1050
|
+
client;
|
|
1051
|
+
logger;
|
|
1052
|
+
truncate;
|
|
1680
1053
|
/**
|
|
1681
1054
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
1682
1055
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
@@ -1688,52 +1061,56 @@ var SNSService = /** @class */ (function () {
|
|
|
1688
1061
|
* usually the right failure mode. Each `publish` call can override via
|
|
1689
1062
|
* its own `truncate` option.
|
|
1690
1063
|
*/
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
this.
|
|
1694
|
-
this.
|
|
1695
|
-
this.truncate = (_c = opts === null || opts === void 0 ? void 0 : opts.truncate) !== null && _c !== void 0 ? _c : false;
|
|
1064
|
+
constructor(opts) {
|
|
1065
|
+
this.client = opts?.client ?? xray.captureAWSv3Client(new SNSClient());
|
|
1066
|
+
this.logger = opts?.logger ?? new Logger();
|
|
1067
|
+
this.truncate = opts?.truncate ?? false;
|
|
1696
1068
|
}
|
|
1697
1069
|
/**
|
|
1698
1070
|
* Publish a single message to an SNS topic.
|
|
1699
1071
|
*
|
|
1700
1072
|
* At INFO level the log line includes only routing / dedup metadata; see
|
|
1701
|
-
* `PUBLISH_SAFE_FIELDS` for the list.
|
|
1702
|
-
* unlocks the full input.
|
|
1073
|
+
* `PUBLISH_SAFE_FIELDS` for the list.
|
|
1703
1074
|
*
|
|
1704
1075
|
* @param input - PublishCommandInput including TopicArn and Message.
|
|
1705
1076
|
*/
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
shouldTruncate = (_a = opts === null || opts === void 0 ? void 0 : opts.truncate) !== null && _a !== void 0 ? _a : this.truncate;
|
|
1712
|
-
effective = shouldTruncate ? this.applyTruncation(input) : input;
|
|
1713
|
-
this.logger.info('Publishing SNS message', {
|
|
1714
|
-
input: filterFieldsForLogLevel(this.logger, effective, PUBLISH_SAFE_FIELDS),
|
|
1715
|
-
});
|
|
1716
|
-
return [2 /*return*/, this.client.send(new PublishCommand(effective))];
|
|
1717
|
-
});
|
|
1077
|
+
async publish(input, opts) {
|
|
1078
|
+
const shouldTruncate = opts?.truncate ?? this.truncate;
|
|
1079
|
+
const effective = shouldTruncate ? this.applyTruncation(input) : input;
|
|
1080
|
+
this.logger.info('Publishing SNS message', {
|
|
1081
|
+
input: filterFieldsForLogLevel(this.logger, effective, PUBLISH_SAFE_FIELDS),
|
|
1718
1082
|
});
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1083
|
+
return this.client.send(new PublishCommand(effective));
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* The truncation helpers return the input unchanged when it already fits, so
|
|
1087
|
+
* a `!==` comparison against their result is what decides whether a field
|
|
1088
|
+
* was modified. Don't reintroduce a size guard here: that would state each
|
|
1089
|
+
* limit twice, encode the string to a `Buffer` twice, and let the two
|
|
1090
|
+
* expressions drift into warning without truncating (or the reverse).
|
|
1091
|
+
*/
|
|
1092
|
+
applyTruncation(input) {
|
|
1093
|
+
const truncated = [];
|
|
1094
|
+
const out = { ...input };
|
|
1095
|
+
if (out.Message !== undefined) {
|
|
1096
|
+
const next = truncateUtf8(out.Message, SNS_MESSAGE_MAX_BYTES);
|
|
1097
|
+
if (next !== out.Message) {
|
|
1098
|
+
out.Message = next;
|
|
1099
|
+
truncated.push('Message');
|
|
1100
|
+
}
|
|
1726
1101
|
}
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1102
|
+
if (out.Subject !== undefined) {
|
|
1103
|
+
const next = truncateCodepoints(out.Subject, SNS_SUBJECT_MAX_CHARS);
|
|
1104
|
+
if (next !== out.Subject) {
|
|
1105
|
+
out.Subject = next;
|
|
1106
|
+
truncated.push('Subject');
|
|
1107
|
+
}
|
|
1731
1108
|
}
|
|
1732
1109
|
if (truncated.length > 0) {
|
|
1733
1110
|
this.logger.warn('Truncated SNS publish input', { fields: truncated });
|
|
1734
1111
|
}
|
|
1735
|
-
return
|
|
1736
|
-
}
|
|
1112
|
+
return out;
|
|
1113
|
+
}
|
|
1737
1114
|
/**
|
|
1738
1115
|
* Publish a batch of messages to an SNS topic. The SNS API caps
|
|
1739
1116
|
* PublishBatch at 10 entries per request, so this method auto-chunks
|
|
@@ -1741,48 +1118,34 @@ var SNSService = /** @class */ (function () {
|
|
|
1741
1118
|
* chunk, returning the array of outputs.
|
|
1742
1119
|
* @param input - PublishBatchCommandInput including TopicArn and entries.
|
|
1743
1120
|
*/
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
isDebug = this.logger.getLevelName() === 'DEBUG';
|
|
1753
|
-
this.logger.info('Publishing SNS message batch', {
|
|
1754
|
-
input: isDebug ? input : { TopicArn: input.TopicArn, entryCount: entries.length },
|
|
1755
|
-
});
|
|
1756
|
-
results = [];
|
|
1757
|
-
i = 0;
|
|
1758
|
-
_d.label = 1;
|
|
1759
|
-
case 1:
|
|
1760
|
-
if (!(i < entries.length)) return [3 /*break*/, 4];
|
|
1761
|
-
chunk = entries.slice(i, i + PUBLISH_BATCH_LIMIT);
|
|
1762
|
-
_b = (_a = results).push;
|
|
1763
|
-
return [4 /*yield*/, this.client.send(new PublishBatchCommand(__assign(__assign({}, input), { PublishBatchRequestEntries: chunk })))];
|
|
1764
|
-
case 2:
|
|
1765
|
-
_b.apply(_a, [_d.sent()]);
|
|
1766
|
-
_d.label = 3;
|
|
1767
|
-
case 3:
|
|
1768
|
-
i += PUBLISH_BATCH_LIMIT;
|
|
1769
|
-
return [3 /*break*/, 1];
|
|
1770
|
-
case 4: return [2 /*return*/, results];
|
|
1771
|
-
}
|
|
1772
|
-
});
|
|
1121
|
+
async publishBatch(input) {
|
|
1122
|
+
const entries = input.PublishBatchRequestEntries ?? [];
|
|
1123
|
+
// Inline verbosity check rather than `filterFieldsForLogLevel` because the
|
|
1124
|
+
// safe log shape includes the computed `entryCount`, which isn't a key
|
|
1125
|
+
// on `PublishBatchCommandInput`.
|
|
1126
|
+
const logFullInput = shouldLogFullInput(this.logger);
|
|
1127
|
+
this.logger.info('Publishing SNS message batch', {
|
|
1128
|
+
input: logFullInput ? input : { TopicArn: input.TopicArn, entryCount: entries.length },
|
|
1773
1129
|
});
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1130
|
+
const results = [];
|
|
1131
|
+
for (let i = 0; i < entries.length; i += PUBLISH_BATCH_LIMIT) {
|
|
1132
|
+
const chunk = entries.slice(i, i + PUBLISH_BATCH_LIMIT);
|
|
1133
|
+
results.push(await this.client.send(new PublishBatchCommand({
|
|
1134
|
+
...input,
|
|
1135
|
+
PublishBatchRequestEntries: chunk,
|
|
1136
|
+
})));
|
|
1137
|
+
}
|
|
1138
|
+
return results;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1777
1141
|
|
|
1778
|
-
|
|
1779
|
-
|
|
1142
|
+
const SQS_BATCH_LIMIT = 10;
|
|
1143
|
+
const SQS_MESSAGE_BODY_MAX_BYTES = 256 * 1024;
|
|
1780
1144
|
/**
|
|
1781
1145
|
* Fields safe to log at INFO level for `sendMessage`. Omits `MessageBody` and
|
|
1782
1146
|
* `MessageAttributes` — both carry payload content.
|
|
1783
|
-
* `POWERTOOLS_LOG_LEVEL=DEBUG` unlocks the full input.
|
|
1784
1147
|
*/
|
|
1785
|
-
|
|
1148
|
+
const SEND_MESSAGE_SAFE_FIELDS = [
|
|
1786
1149
|
'QueueUrl',
|
|
1787
1150
|
'DelaySeconds',
|
|
1788
1151
|
'MessageGroupId',
|
|
@@ -1791,8 +1154,15 @@ var SEND_MESSAGE_SAFE_FIELDS = [
|
|
|
1791
1154
|
/**
|
|
1792
1155
|
* Wrapper around the AWS SQS client providing structured Powertools logging
|
|
1793
1156
|
* and X-Ray tracing by default.
|
|
1157
|
+
*
|
|
1158
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
1159
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
1160
|
+
* `TRACE`) log full SDK inputs.
|
|
1794
1161
|
*/
|
|
1795
|
-
|
|
1162
|
+
class SQSService {
|
|
1163
|
+
client;
|
|
1164
|
+
logger;
|
|
1165
|
+
truncate;
|
|
1796
1166
|
/**
|
|
1797
1167
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
1798
1168
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
@@ -1803,160 +1173,114 @@ var SQSService = /** @class */ (function () {
|
|
|
1803
1173
|
* `false`. Each `sendMessage` call can override via its own `truncate`
|
|
1804
1174
|
* option.
|
|
1805
1175
|
*/
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
this.
|
|
1809
|
-
this.
|
|
1810
|
-
this.truncate = (_c = opts === null || opts === void 0 ? void 0 : opts.truncate) !== null && _c !== void 0 ? _c : false;
|
|
1176
|
+
constructor(opts) {
|
|
1177
|
+
this.client = opts?.client ?? xray.captureAWSv3Client(new SQSClient());
|
|
1178
|
+
this.logger = opts?.logger ?? new Logger();
|
|
1179
|
+
this.truncate = opts?.truncate ?? false;
|
|
1811
1180
|
}
|
|
1812
1181
|
/**
|
|
1813
1182
|
* Send a single message to an SQS queue.
|
|
1814
1183
|
*
|
|
1815
1184
|
* At INFO level the log line includes only queue routing / FIFO metadata;
|
|
1816
|
-
* see `SEND_MESSAGE_SAFE_FIELDS`.
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
return __generator(this, function (_b) {
|
|
1824
|
-
shouldTruncate = (_a = opts === null || opts === void 0 ? void 0 : opts.truncate) !== null && _a !== void 0 ? _a : this.truncate;
|
|
1825
|
-
effective = shouldTruncate ? this.applyTruncation(input) : input;
|
|
1826
|
-
this.logger.info('Sending SQS message', {
|
|
1827
|
-
input: filterFieldsForLogLevel(this.logger, effective, SEND_MESSAGE_SAFE_FIELDS),
|
|
1828
|
-
});
|
|
1829
|
-
return [2 /*return*/, this.client.send(new SendMessageCommand(effective))];
|
|
1830
|
-
});
|
|
1185
|
+
* see `SEND_MESSAGE_SAFE_FIELDS`.
|
|
1186
|
+
*/
|
|
1187
|
+
async sendMessage(input, opts) {
|
|
1188
|
+
const shouldTruncate = opts?.truncate ?? this.truncate;
|
|
1189
|
+
const effective = shouldTruncate ? this.applyTruncation(input) : input;
|
|
1190
|
+
this.logger.info('Sending SQS message', {
|
|
1191
|
+
input: filterFieldsForLogLevel(this.logger, effective, SEND_MESSAGE_SAFE_FIELDS),
|
|
1831
1192
|
});
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1193
|
+
return this.client.send(new SendMessageCommand(effective));
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* `truncateUtf8` returns the input unchanged when it already fits, so a
|
|
1197
|
+
* `!==` comparison against its result is what decides whether the body was
|
|
1198
|
+
* modified. Don't reintroduce a size guard here: that would state the limit
|
|
1199
|
+
* twice, encode the string to a `Buffer` twice, and let the two expressions
|
|
1200
|
+
* drift into warning without truncating (or the reverse).
|
|
1201
|
+
*/
|
|
1202
|
+
applyTruncation(input) {
|
|
1203
|
+
const truncated = [];
|
|
1204
|
+
const out = { ...input };
|
|
1205
|
+
if (out.MessageBody !== undefined) {
|
|
1206
|
+
const next = truncateUtf8(out.MessageBody, SQS_MESSAGE_BODY_MAX_BYTES);
|
|
1207
|
+
if (next !== out.MessageBody) {
|
|
1208
|
+
out.MessageBody = next;
|
|
1209
|
+
truncated.push('MessageBody');
|
|
1210
|
+
}
|
|
1840
1211
|
}
|
|
1841
1212
|
if (truncated.length > 0) {
|
|
1842
1213
|
this.logger.warn('Truncated SQS sendMessage input', { fields: truncated });
|
|
1843
1214
|
}
|
|
1844
|
-
return
|
|
1845
|
-
}
|
|
1215
|
+
return out;
|
|
1216
|
+
}
|
|
1846
1217
|
/**
|
|
1847
1218
|
* Receive messages from an SQS queue. Returns an empty array when no
|
|
1848
1219
|
* messages are available. No automatic deletion is performed — visibility
|
|
1849
1220
|
* timeout semantics are the caller's responsibility.
|
|
1850
1221
|
* @returns The `Messages` array from the response, or `[]` if absent.
|
|
1851
1222
|
*/
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
switch (_b.label) {
|
|
1858
|
-
case 0:
|
|
1859
|
-
this.logger.info('Receiving SQS messages', { input: input });
|
|
1860
|
-
return [4 /*yield*/, this.client.send(new ReceiveMessageCommand(input))];
|
|
1861
|
-
case 1:
|
|
1862
|
-
response = _b.sent();
|
|
1863
|
-
return [2 /*return*/, (_a = response.Messages) !== null && _a !== void 0 ? _a : []];
|
|
1864
|
-
}
|
|
1865
|
-
});
|
|
1866
|
-
});
|
|
1867
|
-
};
|
|
1223
|
+
async receiveMessages(input) {
|
|
1224
|
+
this.logger.info('Receiving SQS messages', { input });
|
|
1225
|
+
const response = await this.client.send(new ReceiveMessageCommand(input));
|
|
1226
|
+
return response.Messages ?? [];
|
|
1227
|
+
}
|
|
1868
1228
|
/**
|
|
1869
1229
|
* Delete a single message from an SQS queue.
|
|
1870
1230
|
*/
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
return [2 /*return*/, this.client.send(new DeleteMessageCommand(input))];
|
|
1876
|
-
});
|
|
1877
|
-
});
|
|
1878
|
-
};
|
|
1231
|
+
async deleteMessage(input) {
|
|
1232
|
+
this.logger.info('Deleting SQS message', { input });
|
|
1233
|
+
return this.client.send(new DeleteMessageCommand(input));
|
|
1234
|
+
}
|
|
1879
1235
|
/**
|
|
1880
1236
|
* Send a batch of messages to an SQS queue. The SQS API caps
|
|
1881
1237
|
* SendMessageBatch at 10 entries per request, so this method auto-chunks
|
|
1882
1238
|
* the caller's entries and sends one request per chunk.
|
|
1883
1239
|
*/
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
isDebug = this.logger.getLevelName() === 'DEBUG';
|
|
1893
|
-
this.logger.info('Sending SQS message batch', {
|
|
1894
|
-
input: isDebug ? input : { QueueUrl: input.QueueUrl, entryCount: entries.length },
|
|
1895
|
-
});
|
|
1896
|
-
results = [];
|
|
1897
|
-
i = 0;
|
|
1898
|
-
_d.label = 1;
|
|
1899
|
-
case 1:
|
|
1900
|
-
if (!(i < entries.length)) return [3 /*break*/, 4];
|
|
1901
|
-
chunk = entries.slice(i, i + SQS_BATCH_LIMIT);
|
|
1902
|
-
_b = (_a = results).push;
|
|
1903
|
-
return [4 /*yield*/, this.client.send(new SendMessageBatchCommand(__assign(__assign({}, input), { Entries: chunk })))];
|
|
1904
|
-
case 2:
|
|
1905
|
-
_b.apply(_a, [_d.sent()]);
|
|
1906
|
-
_d.label = 3;
|
|
1907
|
-
case 3:
|
|
1908
|
-
i += SQS_BATCH_LIMIT;
|
|
1909
|
-
return [3 /*break*/, 1];
|
|
1910
|
-
case 4: return [2 /*return*/, results];
|
|
1911
|
-
}
|
|
1912
|
-
});
|
|
1240
|
+
async sendMessageBatch(input) {
|
|
1241
|
+
const entries = input.Entries ?? [];
|
|
1242
|
+
// Inline verbosity check rather than `filterFieldsForLogLevel` because the
|
|
1243
|
+
// safe log shape includes the computed `entryCount`, which isn't a key
|
|
1244
|
+
// on `SendMessageBatchCommandInput`.
|
|
1245
|
+
const logFullInput = shouldLogFullInput(this.logger);
|
|
1246
|
+
this.logger.info('Sending SQS message batch', {
|
|
1247
|
+
input: logFullInput ? input : { QueueUrl: input.QueueUrl, entryCount: entries.length },
|
|
1913
1248
|
});
|
|
1914
|
-
|
|
1249
|
+
const results = [];
|
|
1250
|
+
for (let i = 0; i < entries.length; i += SQS_BATCH_LIMIT) {
|
|
1251
|
+
const chunk = entries.slice(i, i + SQS_BATCH_LIMIT);
|
|
1252
|
+
results.push(await this.client.send(new SendMessageBatchCommand({ ...input, Entries: chunk })));
|
|
1253
|
+
}
|
|
1254
|
+
return results;
|
|
1255
|
+
}
|
|
1915
1256
|
/**
|
|
1916
1257
|
* Delete a batch of messages from an SQS queue. The SQS API caps
|
|
1917
1258
|
* DeleteMessageBatch at 10 entries per request, so this method auto-chunks
|
|
1918
1259
|
* the caller's entries and sends one request per chunk.
|
|
1919
1260
|
*/
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
isDebug = this.logger.getLevelName() === 'DEBUG';
|
|
1929
|
-
this.logger.info('Deleting SQS message batch', {
|
|
1930
|
-
input: isDebug ? input : { QueueUrl: input.QueueUrl, entryCount: entries.length },
|
|
1931
|
-
});
|
|
1932
|
-
results = [];
|
|
1933
|
-
i = 0;
|
|
1934
|
-
_d.label = 1;
|
|
1935
|
-
case 1:
|
|
1936
|
-
if (!(i < entries.length)) return [3 /*break*/, 4];
|
|
1937
|
-
chunk = entries.slice(i, i + SQS_BATCH_LIMIT);
|
|
1938
|
-
_b = (_a = results).push;
|
|
1939
|
-
return [4 /*yield*/, this.client.send(new DeleteMessageBatchCommand(__assign(__assign({}, input), { Entries: chunk })))];
|
|
1940
|
-
case 2:
|
|
1941
|
-
_b.apply(_a, [_d.sent()]);
|
|
1942
|
-
_d.label = 3;
|
|
1943
|
-
case 3:
|
|
1944
|
-
i += SQS_BATCH_LIMIT;
|
|
1945
|
-
return [3 /*break*/, 1];
|
|
1946
|
-
case 4: return [2 /*return*/, results];
|
|
1947
|
-
}
|
|
1948
|
-
});
|
|
1261
|
+
async deleteMessageBatch(input) {
|
|
1262
|
+
const entries = input.Entries ?? [];
|
|
1263
|
+
// Inline verbosity check rather than `filterFieldsForLogLevel` because the
|
|
1264
|
+
// safe log shape includes the computed `entryCount`, which isn't a key
|
|
1265
|
+
// on `DeleteMessageBatchCommandInput`.
|
|
1266
|
+
const logFullInput = shouldLogFullInput(this.logger);
|
|
1267
|
+
this.logger.info('Deleting SQS message batch', {
|
|
1268
|
+
input: logFullInput ? input : { QueueUrl: input.QueueUrl, entryCount: entries.length },
|
|
1949
1269
|
});
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1270
|
+
const results = [];
|
|
1271
|
+
for (let i = 0; i < entries.length; i += SQS_BATCH_LIMIT) {
|
|
1272
|
+
const chunk = entries.slice(i, i + SQS_BATCH_LIMIT);
|
|
1273
|
+
results.push(await this.client.send(new DeleteMessageBatchCommand({ ...input, Entries: chunk })));
|
|
1274
|
+
}
|
|
1275
|
+
return results;
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1953
1278
|
|
|
1954
1279
|
/**
|
|
1955
1280
|
* Fields safe to log at INFO level for `putParameter`. Omits `Value` (the
|
|
1956
|
-
* parameter content itself).
|
|
1957
|
-
* input.
|
|
1281
|
+
* parameter content itself).
|
|
1958
1282
|
*/
|
|
1959
|
-
|
|
1283
|
+
const PUT_PARAMETER_SAFE_FIELDS = [
|
|
1960
1284
|
'Name',
|
|
1961
1285
|
'Type',
|
|
1962
1286
|
'Description',
|
|
@@ -1974,23 +1298,28 @@ var PUT_PARAMETER_SAFE_FIELDS = [
|
|
|
1974
1298
|
* `WithDecryption` — callers needing plaintext should use `SSMClient`
|
|
1975
1299
|
* directly.
|
|
1976
1300
|
*
|
|
1301
|
+
* Where a method's input carries payloads, secret material or PII, the INFO
|
|
1302
|
+
* log line omits them; the verbose levels (`POWERTOOLS_LOG_LEVEL=DEBUG` or
|
|
1303
|
+
* `TRACE`) log full SDK inputs.
|
|
1304
|
+
*
|
|
1977
1305
|
* Write operations (`putParameter`, `deleteParameter`) are exposed for
|
|
1978
1306
|
* convenience but should be used with care: parameter lifecycle is usually
|
|
1979
1307
|
* managed by IaC (CDK / Terraform). Prefer IaC for anything that exists at
|
|
1980
1308
|
* deploy time; reserve runtime writes for values that genuinely need to
|
|
1981
1309
|
* mutate within the application.
|
|
1982
1310
|
*/
|
|
1983
|
-
|
|
1311
|
+
class SSMService {
|
|
1312
|
+
client;
|
|
1313
|
+
logger;
|
|
1984
1314
|
/**
|
|
1985
1315
|
* @param opts.logger - Optional Powertools logger. Defaults to `new Logger()`,
|
|
1986
1316
|
* which picks up `POWERTOOLS_SERVICE_NAME` from the environment.
|
|
1987
1317
|
* @param opts.client - Optional pre-configured `SSMClient`. When supplied,
|
|
1988
1318
|
* the wrapper does not apply X-Ray instrumentation.
|
|
1989
1319
|
*/
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
this.
|
|
1993
|
-
this.logger = (_b = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _b !== void 0 ? _b : new Logger();
|
|
1320
|
+
constructor(opts) {
|
|
1321
|
+
this.client = opts?.client ?? xray.captureAWSv3Client(new SSMClient());
|
|
1322
|
+
this.logger = opts?.logger ?? new Logger();
|
|
1994
1323
|
}
|
|
1995
1324
|
/**
|
|
1996
1325
|
* Fetch a single SSM parameter's value.
|
|
@@ -1998,22 +1327,11 @@ var SSMService = /** @class */ (function () {
|
|
|
1998
1327
|
* @returns The parameter value, or `undefined` if the parameter has no
|
|
1999
1328
|
* value set.
|
|
2000
1329
|
*/
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
switch (_b.label) {
|
|
2007
|
-
case 0:
|
|
2008
|
-
this.logger.info('Fetching SSM parameter', { input: { name: name } });
|
|
2009
|
-
return [4 /*yield*/, this.client.send(new GetParameterCommand({ Name: name, WithDecryption: true }))];
|
|
2010
|
-
case 1:
|
|
2011
|
-
response = _b.sent();
|
|
2012
|
-
return [2 /*return*/, (_a = response.Parameter) === null || _a === void 0 ? void 0 : _a.Value];
|
|
2013
|
-
}
|
|
2014
|
-
});
|
|
2015
|
-
});
|
|
2016
|
-
};
|
|
1330
|
+
async getParameter(name) {
|
|
1331
|
+
this.logger.info('Fetching SSM parameter', { input: { name } });
|
|
1332
|
+
const response = await this.client.send(new GetParameterCommand({ Name: name, WithDecryption: true }));
|
|
1333
|
+
return response.Parameter?.Value;
|
|
1334
|
+
}
|
|
2017
1335
|
/**
|
|
2018
1336
|
* Fetch multiple SSM parameters in a single request. Callers supply an
|
|
2019
1337
|
* alias-to-path record, and the returned record is keyed by the same
|
|
@@ -2033,36 +1351,23 @@ var SSMService = /** @class */ (function () {
|
|
|
2033
1351
|
* parameter's value, or `undefined` when the parameter is missing or has
|
|
2034
1352
|
* no value.
|
|
2035
1353
|
*/
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
if (parameter.Name !== undefined)
|
|
2054
|
-
byPath.set(parameter.Name, parameter.Value);
|
|
2055
|
-
}
|
|
2056
|
-
result = {};
|
|
2057
|
-
for (_b = 0, _c = Object.keys(aliases); _b < _c.length; _b++) {
|
|
2058
|
-
alias = _c[_b];
|
|
2059
|
-
result[alias] = byPath.get(aliases[alias]);
|
|
2060
|
-
}
|
|
2061
|
-
return [2 /*return*/, result];
|
|
2062
|
-
}
|
|
2063
|
-
});
|
|
2064
|
-
});
|
|
2065
|
-
};
|
|
1354
|
+
async getParameters(aliases) {
|
|
1355
|
+
this.logger.info('Fetching SSM parameters', { input: { aliases } });
|
|
1356
|
+
const response = await this.client.send(new GetParametersCommand({
|
|
1357
|
+
Names: Object.values(aliases),
|
|
1358
|
+
WithDecryption: true,
|
|
1359
|
+
}));
|
|
1360
|
+
const byPath = new Map();
|
|
1361
|
+
for (const parameter of response.Parameters ?? []) {
|
|
1362
|
+
if (parameter.Name !== undefined)
|
|
1363
|
+
byPath.set(parameter.Name, parameter.Value);
|
|
1364
|
+
}
|
|
1365
|
+
const result = {};
|
|
1366
|
+
for (const alias of Object.keys(aliases)) {
|
|
1367
|
+
result[alias] = byPath.get(aliases[alias]);
|
|
1368
|
+
}
|
|
1369
|
+
return result;
|
|
1370
|
+
}
|
|
2066
1371
|
/**
|
|
2067
1372
|
* Create or update an SSM parameter. The log line omits `Value` to avoid
|
|
2068
1373
|
* leaking secret material.
|
|
@@ -2070,36 +1375,22 @@ var SSMService = /** @class */ (function () {
|
|
|
2070
1375
|
* Prefer IaC (CDK / Terraform) for parameter lifecycle — use this for
|
|
2071
1376
|
* runtime values only.
|
|
2072
1377
|
*/
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
this.logger.info('Putting SSM parameter', {
|
|
2077
|
-
input: filterFieldsForLogLevel(this.logger, input, PUT_PARAMETER_SAFE_FIELDS),
|
|
2078
|
-
});
|
|
2079
|
-
return [2 /*return*/, this.client.send(new PutParameterCommand(input))];
|
|
2080
|
-
});
|
|
1378
|
+
async putParameter(input) {
|
|
1379
|
+
this.logger.info('Putting SSM parameter', {
|
|
1380
|
+
input: filterFieldsForLogLevel(this.logger, input, PUT_PARAMETER_SAFE_FIELDS),
|
|
2081
1381
|
});
|
|
2082
|
-
|
|
1382
|
+
return this.client.send(new PutParameterCommand(input));
|
|
1383
|
+
}
|
|
2083
1384
|
/**
|
|
2084
1385
|
* Delete an SSM parameter by name.
|
|
2085
1386
|
*
|
|
2086
1387
|
* Prefer IaC (CDK / Terraform) for parameter lifecycle — use this for
|
|
2087
1388
|
* runtime cleanup only.
|
|
2088
1389
|
*/
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
case 0:
|
|
2094
|
-
this.logger.info('Deleting SSM parameter', { input: { name: name } });
|
|
2095
|
-
return [4 /*yield*/, this.client.send(new DeleteParameterCommand({ Name: name }))];
|
|
2096
|
-
case 1:
|
|
2097
|
-
_a.sent();
|
|
2098
|
-
return [2 /*return*/];
|
|
2099
|
-
}
|
|
2100
|
-
});
|
|
2101
|
-
});
|
|
2102
|
-
};
|
|
1390
|
+
async deleteParameter(name) {
|
|
1391
|
+
this.logger.info('Deleting SSM parameter', { input: { name } });
|
|
1392
|
+
await this.client.send(new DeleteParameterCommand({ Name: name }));
|
|
1393
|
+
}
|
|
2103
1394
|
/**
|
|
2104
1395
|
* Fetch all parameters under an SSM hierarchy path, auto-paginating across
|
|
2105
1396
|
* all pages. `Recursive` defaults to `true` (overriding the AWS SDK
|
|
@@ -2111,59 +1402,18 @@ var SSMService = /** @class */ (function () {
|
|
|
2111
1402
|
* @returns The full `Parameter` objects (including `Version`,
|
|
2112
1403
|
* `LastModifiedDate`, etc.).
|
|
2113
1404
|
*/
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
var _e, _f;
|
|
2119
|
-
return __generator(this, function (_g) {
|
|
2120
|
-
switch (_g.label) {
|
|
2121
|
-
case 0:
|
|
2122
|
-
recursive = (_e = opts === null || opts === void 0 ? void 0 : opts.recursive) !== null && _e !== void 0 ? _e : true;
|
|
2123
|
-
this.logger.info('Fetching SSM parameters by path', {
|
|
2124
|
-
input: { path: path, recursive: recursive },
|
|
2125
|
-
});
|
|
2126
|
-
paginator = paginateGetParametersByPath({ client: this.client }, { Path: path, Recursive: recursive, WithDecryption: true });
|
|
2127
|
-
parameters = [];
|
|
2128
|
-
_g.label = 1;
|
|
2129
|
-
case 1:
|
|
2130
|
-
_g.trys.push([1, 6, 7, 12]);
|
|
2131
|
-
_a = true, paginator_1 = __asyncValues(paginator);
|
|
2132
|
-
_g.label = 2;
|
|
2133
|
-
case 2: return [4 /*yield*/, paginator_1.next()];
|
|
2134
|
-
case 3:
|
|
2135
|
-
if (!(paginator_1_1 = _g.sent(), _b = paginator_1_1.done, !_b)) return [3 /*break*/, 5];
|
|
2136
|
-
_d = paginator_1_1.value;
|
|
2137
|
-
_a = false;
|
|
2138
|
-
page = _d;
|
|
2139
|
-
parameters.push.apply(parameters, ((_f = page.Parameters) !== null && _f !== void 0 ? _f : []));
|
|
2140
|
-
_g.label = 4;
|
|
2141
|
-
case 4:
|
|
2142
|
-
_a = true;
|
|
2143
|
-
return [3 /*break*/, 2];
|
|
2144
|
-
case 5: return [3 /*break*/, 12];
|
|
2145
|
-
case 6:
|
|
2146
|
-
e_1_1 = _g.sent();
|
|
2147
|
-
e_1 = { error: e_1_1 };
|
|
2148
|
-
return [3 /*break*/, 12];
|
|
2149
|
-
case 7:
|
|
2150
|
-
_g.trys.push([7, , 10, 11]);
|
|
2151
|
-
if (!(!_a && !_b && (_c = paginator_1.return))) return [3 /*break*/, 9];
|
|
2152
|
-
return [4 /*yield*/, _c.call(paginator_1)];
|
|
2153
|
-
case 8:
|
|
2154
|
-
_g.sent();
|
|
2155
|
-
_g.label = 9;
|
|
2156
|
-
case 9: return [3 /*break*/, 11];
|
|
2157
|
-
case 10:
|
|
2158
|
-
if (e_1) throw e_1.error;
|
|
2159
|
-
return [7 /*endfinally*/];
|
|
2160
|
-
case 11: return [7 /*endfinally*/];
|
|
2161
|
-
case 12: return [2 /*return*/, parameters];
|
|
2162
|
-
}
|
|
2163
|
-
});
|
|
1405
|
+
async getParametersByPath(path, opts) {
|
|
1406
|
+
const recursive = opts?.recursive ?? true;
|
|
1407
|
+
this.logger.info('Fetching SSM parameters by path', {
|
|
1408
|
+
input: { path, recursive },
|
|
2164
1409
|
});
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
1410
|
+
const paginator = paginateGetParametersByPath({ client: this.client }, { Path: path, Recursive: recursive, WithDecryption: true });
|
|
1411
|
+
const parameters = [];
|
|
1412
|
+
for await (const page of paginator) {
|
|
1413
|
+
parameters.push(...(page.Parameters ?? []));
|
|
1414
|
+
}
|
|
1415
|
+
return parameters;
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
2168
1418
|
|
|
2169
1419
|
export { DynamoDBService, S3Service, SNSService, SQSService, SSMService, SchedulerService, SecretsManagerService, StepFunctionsService };
|