cdk-real-drift 0.2.55 → 0.2.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +141 -34
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
3
3
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
4
|
import { dirname, join, resolve } from "node:path";
|
|
5
5
|
import { YAMLSeq, isSeq, parse, parseDocument } from "yaml";
|
|
@@ -243,6 +243,44 @@ const BOOLEAN_FLAGS = new Set([
|
|
|
243
243
|
"--strict",
|
|
244
244
|
"--all"
|
|
245
245
|
]);
|
|
246
|
+
const CANONICAL_FLAG = {
|
|
247
|
+
"-y": "--yes",
|
|
248
|
+
"-v": "--verbose",
|
|
249
|
+
"-a": "--app",
|
|
250
|
+
"-c": "--context"
|
|
251
|
+
};
|
|
252
|
+
const canonicalFlag = (flag) => CANONICAL_FLAG[flag] ?? flag;
|
|
253
|
+
const GLOBAL_FLAGS = [
|
|
254
|
+
"--region",
|
|
255
|
+
"--profile",
|
|
256
|
+
"--app",
|
|
257
|
+
"--context",
|
|
258
|
+
"--all",
|
|
259
|
+
"--json",
|
|
260
|
+
"--yes"
|
|
261
|
+
];
|
|
262
|
+
const ALLOWED_FLAGS_BY_VERB = {
|
|
263
|
+
check: new Set([
|
|
264
|
+
...GLOBAL_FLAGS,
|
|
265
|
+
"--fail",
|
|
266
|
+
"--strict",
|
|
267
|
+
"--show-all",
|
|
268
|
+
"--pre-deploy",
|
|
269
|
+
"--undeclared-only",
|
|
270
|
+
"--declared-only",
|
|
271
|
+
"--verbose",
|
|
272
|
+
"--remove-unrecorded"
|
|
273
|
+
]),
|
|
274
|
+
record: new Set([...GLOBAL_FLAGS, "--verbose"]),
|
|
275
|
+
ignore: new Set(GLOBAL_FLAGS),
|
|
276
|
+
revert: new Set([
|
|
277
|
+
...GLOBAL_FLAGS,
|
|
278
|
+
"--verbose",
|
|
279
|
+
"--remove-unrecorded",
|
|
280
|
+
"--dry-run",
|
|
281
|
+
"--wait"
|
|
282
|
+
])
|
|
283
|
+
};
|
|
246
284
|
/**
|
|
247
285
|
* Whether prompts may be shown: only in a real TTY. Non-interactive now simply means
|
|
248
286
|
* non-TTY (real CI/cron/pipes). The single source of truth — command code threads this
|
|
@@ -252,7 +290,7 @@ const BOOLEAN_FLAGS = new Set([
|
|
|
252
290
|
function isInteractive() {
|
|
253
291
|
return Boolean(process.stdin.isTTY);
|
|
254
292
|
}
|
|
255
|
-
function parseCommonArgs(args) {
|
|
293
|
+
function parseCommonArgs(args, verb) {
|
|
256
294
|
const values = {};
|
|
257
295
|
const found = /* @__PURE__ */ new Set();
|
|
258
296
|
const stackNames = [];
|
|
@@ -297,6 +335,11 @@ function parseCommonArgs(args) {
|
|
|
297
335
|
}
|
|
298
336
|
throw new Error(`unknown option "${a}" — see cdkrd --help`);
|
|
299
337
|
}
|
|
338
|
+
if (verb !== void 0) {
|
|
339
|
+
const allowed = ALLOWED_FLAGS_BY_VERB[verb];
|
|
340
|
+
for (const flag of found) if (!allowed.has(canonicalFlag(flag))) throw new Error(`option "${flag}" is not valid for the \`${verb}\` command — see cdkrd --help`);
|
|
341
|
+
if (waitMs !== void 0 && !allowed.has("--wait")) throw new Error(`option "--wait" is not valid for the \`${verb}\` command — see cdkrd --help`);
|
|
342
|
+
}
|
|
300
343
|
const has = (flag) => found.has(flag);
|
|
301
344
|
const scopes = [
|
|
302
345
|
"--pre-deploy",
|
|
@@ -9119,12 +9162,16 @@ function validateRecordedPhysicalIds(value, path) {
|
|
|
9119
9162
|
function sortRecorded(recorded) {
|
|
9120
9163
|
return [...recorded].sort((a, b) => (a.logicalId < b.logicalId ? -1 : a.logicalId > b.logicalId ? 1 : 0) || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
9121
9164
|
}
|
|
9122
|
-
/** #674: keep
|
|
9123
|
-
*
|
|
9124
|
-
|
|
9125
|
-
|
|
9165
|
+
/** #674/#792: keep physical ids for logicalIds that either carry a recorded entry OR
|
|
9166
|
+
* are snapshot-`complete` (a complete resource with ZERO undeclared entries has nothing
|
|
9167
|
+
* in `recorded`, but its physical id must still be persisted so #674's replacement void
|
|
9168
|
+
* fires for it — else an out-of-band REPLACEMENT of a zero-entry complete resource, whose
|
|
9169
|
+
* fresh AWS defaults now "appear since record", surfaces as false drift). Sorted keys keep
|
|
9170
|
+
* the JSON byte-stable; a logicalId neither recorded nor complete is dropped as dead. */
|
|
9171
|
+
function sortedPhysicalIds(ids, recorded, complete) {
|
|
9172
|
+
const persist = new Set([...recorded.map((e) => e.logicalId), ...complete]);
|
|
9126
9173
|
const out = {};
|
|
9127
|
-
for (const key of Object.keys(ids).sort()) if (
|
|
9174
|
+
for (const key of Object.keys(ids).sort()) if (persist.has(key) && ids[key] !== void 0) out[key] = ids[key];
|
|
9128
9175
|
return out;
|
|
9129
9176
|
}
|
|
9130
9177
|
async function writeBaselineFile(b) {
|
|
@@ -9134,7 +9181,7 @@ async function writeBaselineFile(b) {
|
|
|
9134
9181
|
...b,
|
|
9135
9182
|
recorded: sortRecorded(b.recorded),
|
|
9136
9183
|
...b.completeResources ? { completeResources: [...b.completeResources].sort() } : {},
|
|
9137
|
-
...b.recordedPhysicalIds ? { recordedPhysicalIds: sortedPhysicalIds(b.recordedPhysicalIds, b.recorded) } : {}
|
|
9184
|
+
...b.recordedPhysicalIds ? { recordedPhysicalIds: sortedPhysicalIds(b.recordedPhysicalIds, b.recorded, b.completeResources ?? []) } : {}
|
|
9138
9185
|
};
|
|
9139
9186
|
await writeFile(p, JSON.stringify(stable, null, 2) + "\n", "utf8");
|
|
9140
9187
|
return p;
|
|
@@ -9170,7 +9217,7 @@ function computeCompleteResources(allLogicalIds, findings, recorded, previousCom
|
|
|
9170
9217
|
* (read clean) would be invisible here; `opts.previous` keeps completeness
|
|
9171
9218
|
* monotonic across re-records. */
|
|
9172
9219
|
async function writeBaseline(stackName, region, accountId, findings, rawTemplate, recorded = buildRecorded(findings), opts = {}) {
|
|
9173
|
-
const
|
|
9220
|
+
const completeResources = computeCompleteResources(opts.allLogicalIds ?? [...new Set([...findings.map((f) => f.logicalId), ...recorded.map((a) => a.logicalId)])], findings, recorded, opts.previous?.completeResources ?? []);
|
|
9174
9221
|
return {
|
|
9175
9222
|
path: await writeBaselineFile({
|
|
9176
9223
|
schemaVersion: 2,
|
|
@@ -9180,26 +9227,32 @@ async function writeBaseline(stackName, region, accountId, findings, rawTemplate
|
|
|
9180
9227
|
capturedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
9181
9228
|
templateHash: hashTemplate(rawTemplate),
|
|
9182
9229
|
recorded,
|
|
9183
|
-
completeResources
|
|
9184
|
-
...buildRecordedPhysicalIds(recorded, opts.physicalIdByLogical, opts.previous)
|
|
9230
|
+
completeResources,
|
|
9231
|
+
...buildRecordedPhysicalIds(recorded, completeResources, opts.physicalIdByLogical, opts.previous)
|
|
9185
9232
|
}),
|
|
9186
9233
|
count: recorded.length
|
|
9187
9234
|
};
|
|
9188
9235
|
}
|
|
9189
9236
|
/**
|
|
9190
|
-
* #674: build the `recordedPhysicalIds` map (logicalId -> physical id) for the
|
|
9191
|
-
* being written.
|
|
9192
|
-
*
|
|
9193
|
-
*
|
|
9194
|
-
*
|
|
9195
|
-
*
|
|
9237
|
+
* #674/#792: build the `recordedPhysicalIds` map (logicalId -> physical id) for the
|
|
9238
|
+
* resources being written. Covers every RECORDED logicalId PLUS every snapshot-`complete`
|
|
9239
|
+
* resource — the latter so a resource recorded as complete but with ZERO undeclared
|
|
9240
|
+
* entries (nothing in `recorded`) still carries its physical id, letting #674's replacement
|
|
9241
|
+
* void fire for it too (an out-of-band REPLACEMENT of such a resource must not surface as
|
|
9242
|
+
* false "appeared since record" drift; see #792). Prefers the physical id resolved THIS
|
|
9243
|
+
* run; falls back to the previous baseline's stored id when this run did not resolve one
|
|
9244
|
+
* (a resource skipped / with no physical id must not lose its previously-captured id on
|
|
9245
|
+
* re-record — else a later replacement could not be detected). A resource with no known
|
|
9246
|
+
* physical id (this run or prior) is simply absent — today's behavior, never invented.
|
|
9247
|
+
* Returns `{}` (spread to nothing) when no id is known, so the field stays absent on
|
|
9248
|
+
* baselines with no physical ids at all.
|
|
9196
9249
|
*/
|
|
9197
|
-
function buildRecordedPhysicalIds(recorded, live, previous) {
|
|
9250
|
+
function buildRecordedPhysicalIds(recorded, complete, live, previous) {
|
|
9198
9251
|
const prior = previous?.recordedPhysicalIds ?? {};
|
|
9199
9252
|
const out = {};
|
|
9200
|
-
for (const
|
|
9201
|
-
const id = live?.get(
|
|
9202
|
-
if (id !== void 0) out[
|
|
9253
|
+
for (const logicalId of new Set([...recorded.map((e) => e.logicalId), ...complete])) {
|
|
9254
|
+
const id = live?.get(logicalId) ?? prior[logicalId];
|
|
9255
|
+
if (id !== void 0) out[logicalId] = id;
|
|
9203
9256
|
}
|
|
9204
9257
|
return Object.keys(out).length > 0 ? { recordedPhysicalIds: out } : {};
|
|
9205
9258
|
}
|
|
@@ -11703,16 +11756,40 @@ function rejectedEmptyStripOps(resourceType, ops, liveRaw) {
|
|
|
11703
11756
|
}
|
|
11704
11757
|
return out;
|
|
11705
11758
|
}
|
|
11706
|
-
|
|
11759
|
+
function hasArrayIndexSegment(pointer) {
|
|
11760
|
+
return pointer.split("/").some((seg) => /^\d+$/.test(seg));
|
|
11761
|
+
}
|
|
11762
|
+
/**
|
|
11763
|
+
* Serialize a RevertItem's ops to an RFC6902 PatchDocument string for Cloud Control.
|
|
11764
|
+
*
|
|
11765
|
+
* #762: the Cloud Control path had no analogue of the SDK-writer guard (writers.ts
|
|
11766
|
+
* `desiredModel` re-reads + re-canonicalizes the live model so an indexed op lands on the
|
|
11767
|
+
* SAME element classify diffed). For an INDEX-BEARING pointer we emit a preceding RFC6902
|
|
11768
|
+
* `test` precondition asserting the addressed location still equals the value classify saw
|
|
11769
|
+
* (`op.prior` = the finding's live `actual`). Cloud Control accepts standard RFC6902 and
|
|
11770
|
+
* evaluates `test` atomically before the mutation, so a shifted index makes it REJECT the
|
|
11771
|
+
* whole patch instead of writing the wrong element — fail-closed, the same intent as the
|
|
11772
|
+
* writer-path re-read. Scalar non-indexed pointers carry no aliasing risk (a named property
|
|
11773
|
+
* is stable regardless of array order), so they get NO `test` op — the patch stays minimal.
|
|
11774
|
+
*/
|
|
11707
11775
|
function toPatchDocument(item) {
|
|
11708
|
-
|
|
11709
|
-
|
|
11710
|
-
path
|
|
11711
|
-
|
|
11712
|
-
|
|
11713
|
-
|
|
11714
|
-
|
|
11715
|
-
|
|
11776
|
+
const doc = [];
|
|
11777
|
+
for (const { op, path, value, prior } of item.ops) {
|
|
11778
|
+
if (hasArrayIndexSegment(path) && prior !== void 0) doc.push({
|
|
11779
|
+
op: "test",
|
|
11780
|
+
path,
|
|
11781
|
+
value: prior
|
|
11782
|
+
});
|
|
11783
|
+
doc.push(op === "remove" ? {
|
|
11784
|
+
op,
|
|
11785
|
+
path
|
|
11786
|
+
} : {
|
|
11787
|
+
op,
|
|
11788
|
+
path,
|
|
11789
|
+
value
|
|
11790
|
+
});
|
|
11791
|
+
}
|
|
11792
|
+
return JSON.stringify(doc);
|
|
11716
11793
|
}
|
|
11717
11794
|
|
|
11718
11795
|
//#endregion
|
|
@@ -12426,7 +12503,7 @@ function availableActions(findings, baseline, schemas, removeUnrecorded) {
|
|
|
12426
12503
|
//#endregion
|
|
12427
12504
|
//#region src/commands/record.ts
|
|
12428
12505
|
async function runRecord(args) {
|
|
12429
|
-
const a = parseCommonArgs(args);
|
|
12506
|
+
const a = parseCommonArgs(args, "record");
|
|
12430
12507
|
if (a.profile) process.env.AWS_PROFILE = a.profile;
|
|
12431
12508
|
let config;
|
|
12432
12509
|
try {
|
|
@@ -12492,7 +12569,7 @@ async function runRecord(args) {
|
|
|
12492
12569
|
//#endregion
|
|
12493
12570
|
//#region src/commands/ignore.ts
|
|
12494
12571
|
async function runIgnore(args) {
|
|
12495
|
-
const a = parseCommonArgs(args);
|
|
12572
|
+
const a = parseCommonArgs(args, "ignore");
|
|
12496
12573
|
if (a.profile) process.env.AWS_PROFILE = a.profile;
|
|
12497
12574
|
let config;
|
|
12498
12575
|
try {
|
|
@@ -13397,8 +13474,30 @@ function strictCoverageExit(strict, findings, resources) {
|
|
|
13397
13474
|
function finalCheckExit(code, fail) {
|
|
13398
13475
|
return fail || code !== 1 ? code : 0;
|
|
13399
13476
|
}
|
|
13477
|
+
/**
|
|
13478
|
+
* #781: does a committed baseline exist for this stack, under ANY account/region axis?
|
|
13479
|
+
* When a stack is gone (a DescribeStacks "does not exist"), we do NOT know the
|
|
13480
|
+
* account/region it was deployed to, so we cannot construct the exact baseline path.
|
|
13481
|
+
* A committed baseline is named `<stackName>.<accountId>.<region>.json` under
|
|
13482
|
+
* `.cdkrd/baselines/`, so match by the `<stackName>.` filename prefix across the whole
|
|
13483
|
+
* directory — a prior baseline is PROOF the stack was once deployed, which distinguishes
|
|
13484
|
+
* "deleted out of band" (real drift) from "never deployed yet" (a benign skip). The
|
|
13485
|
+
* baselines directory is derived from `baselinePath` so the layout stays single-sourced.
|
|
13486
|
+
* Pure (filesystem-only, no AWS) + exported for unit tests. Missing dir → false.
|
|
13487
|
+
*/
|
|
13488
|
+
function hasBaselineForStack(stackName) {
|
|
13489
|
+
const dir = dirname(baselinePath(stackName, "a", "r"));
|
|
13490
|
+
let entries;
|
|
13491
|
+
try {
|
|
13492
|
+
entries = readdirSync(dir);
|
|
13493
|
+
} catch {
|
|
13494
|
+
return false;
|
|
13495
|
+
}
|
|
13496
|
+
const prefix = `${stackName}.`;
|
|
13497
|
+
return entries.some((f) => f.startsWith(prefix) && f.endsWith(".json"));
|
|
13498
|
+
}
|
|
13400
13499
|
async function runCheck(args) {
|
|
13401
|
-
const a = parseCommonArgs(args);
|
|
13500
|
+
const a = parseCommonArgs(args, "check");
|
|
13402
13501
|
if (a.profile) process.env.AWS_PROFILE = a.profile;
|
|
13403
13502
|
let config;
|
|
13404
13503
|
try {
|
|
@@ -13559,6 +13658,14 @@ async function runCheck(args) {
|
|
|
13559
13658
|
worst = Math.max(worst, strictCoverageExit(a.strict, gathered.findings, desired.resources));
|
|
13560
13659
|
} catch (e) {
|
|
13561
13660
|
if (isStackNotDeployed(e)) {
|
|
13661
|
+
if (hasBaselineForStack(stackName)) {
|
|
13662
|
+
const msg = "deployed baseline exists but the stack is gone — deleted out of band";
|
|
13663
|
+
console.error(`[Drift] ${stackName} (${region}): ${msg}`);
|
|
13664
|
+
jsonError(stackName, region, msg);
|
|
13665
|
+
worst = Math.max(worst, finalCheckExit(1, a.fail), a.strict ? 1 : 0);
|
|
13666
|
+
anyDrift = true;
|
|
13667
|
+
continue;
|
|
13668
|
+
}
|
|
13562
13669
|
console.error(`note: ${stackName}: not deployed yet — skipped`);
|
|
13563
13670
|
jsonError(stackName, region, "not deployed yet — skipped");
|
|
13564
13671
|
continue;
|
|
@@ -13581,7 +13688,7 @@ async function runCheck(args) {
|
|
|
13581
13688
|
//#endregion
|
|
13582
13689
|
//#region src/commands/revert.ts
|
|
13583
13690
|
async function runRevert(args) {
|
|
13584
|
-
const a = parseCommonArgs(args);
|
|
13691
|
+
const a = parseCommonArgs(args, "revert");
|
|
13585
13692
|
if (a.profile) process.env.AWS_PROFILE = a.profile;
|
|
13586
13693
|
const dryRun = args.includes("--dry-run");
|
|
13587
13694
|
let config;
|