cdk-real-drift 0.2.56 → 0.2.58
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 +116 -37
- 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";
|
|
@@ -9162,12 +9162,16 @@ function validateRecordedPhysicalIds(value, path) {
|
|
|
9162
9162
|
function sortRecorded(recorded) {
|
|
9163
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));
|
|
9164
9164
|
}
|
|
9165
|
-
/** #674: keep
|
|
9166
|
-
*
|
|
9167
|
-
|
|
9168
|
-
|
|
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]);
|
|
9169
9173
|
const out = {};
|
|
9170
|
-
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];
|
|
9171
9175
|
return out;
|
|
9172
9176
|
}
|
|
9173
9177
|
async function writeBaselineFile(b) {
|
|
@@ -9177,7 +9181,7 @@ async function writeBaselineFile(b) {
|
|
|
9177
9181
|
...b,
|
|
9178
9182
|
recorded: sortRecorded(b.recorded),
|
|
9179
9183
|
...b.completeResources ? { completeResources: [...b.completeResources].sort() } : {},
|
|
9180
|
-
...b.recordedPhysicalIds ? { recordedPhysicalIds: sortedPhysicalIds(b.recordedPhysicalIds, b.recorded) } : {}
|
|
9184
|
+
...b.recordedPhysicalIds ? { recordedPhysicalIds: sortedPhysicalIds(b.recordedPhysicalIds, b.recorded, b.completeResources ?? []) } : {}
|
|
9181
9185
|
};
|
|
9182
9186
|
await writeFile(p, JSON.stringify(stable, null, 2) + "\n", "utf8");
|
|
9183
9187
|
return p;
|
|
@@ -9213,7 +9217,7 @@ function computeCompleteResources(allLogicalIds, findings, recorded, previousCom
|
|
|
9213
9217
|
* (read clean) would be invisible here; `opts.previous` keeps completeness
|
|
9214
9218
|
* monotonic across re-records. */
|
|
9215
9219
|
async function writeBaseline(stackName, region, accountId, findings, rawTemplate, recorded = buildRecorded(findings), opts = {}) {
|
|
9216
|
-
const
|
|
9220
|
+
const completeResources = computeCompleteResources(opts.allLogicalIds ?? [...new Set([...findings.map((f) => f.logicalId), ...recorded.map((a) => a.logicalId)])], findings, recorded, opts.previous?.completeResources ?? []);
|
|
9217
9221
|
return {
|
|
9218
9222
|
path: await writeBaselineFile({
|
|
9219
9223
|
schemaVersion: 2,
|
|
@@ -9223,26 +9227,32 @@ async function writeBaseline(stackName, region, accountId, findings, rawTemplate
|
|
|
9223
9227
|
capturedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
9224
9228
|
templateHash: hashTemplate(rawTemplate),
|
|
9225
9229
|
recorded,
|
|
9226
|
-
completeResources
|
|
9227
|
-
...buildRecordedPhysicalIds(recorded, opts.physicalIdByLogical, opts.previous)
|
|
9230
|
+
completeResources,
|
|
9231
|
+
...buildRecordedPhysicalIds(recorded, completeResources, opts.physicalIdByLogical, opts.previous)
|
|
9228
9232
|
}),
|
|
9229
9233
|
count: recorded.length
|
|
9230
9234
|
};
|
|
9231
9235
|
}
|
|
9232
9236
|
/**
|
|
9233
|
-
* #674: build the `recordedPhysicalIds` map (logicalId -> physical id) for the
|
|
9234
|
-
* being written.
|
|
9235
|
-
*
|
|
9236
|
-
*
|
|
9237
|
-
*
|
|
9238
|
-
*
|
|
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.
|
|
9239
9249
|
*/
|
|
9240
|
-
function buildRecordedPhysicalIds(recorded, live, previous) {
|
|
9250
|
+
function buildRecordedPhysicalIds(recorded, complete, live, previous) {
|
|
9241
9251
|
const prior = previous?.recordedPhysicalIds ?? {};
|
|
9242
9252
|
const out = {};
|
|
9243
|
-
for (const
|
|
9244
|
-
const id = live?.get(
|
|
9245
|
-
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;
|
|
9246
9256
|
}
|
|
9247
9257
|
return Object.keys(out).length > 0 ? { recordedPhysicalIds: out } : {};
|
|
9248
9258
|
}
|
|
@@ -11746,16 +11756,40 @@ function rejectedEmptyStripOps(resourceType, ops, liveRaw) {
|
|
|
11746
11756
|
}
|
|
11747
11757
|
return out;
|
|
11748
11758
|
}
|
|
11749
|
-
|
|
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
|
+
*/
|
|
11750
11775
|
function toPatchDocument(item) {
|
|
11751
|
-
|
|
11752
|
-
|
|
11753
|
-
path
|
|
11754
|
-
|
|
11755
|
-
|
|
11756
|
-
|
|
11757
|
-
|
|
11758
|
-
|
|
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);
|
|
11759
11793
|
}
|
|
11760
11794
|
|
|
11761
11795
|
//#endregion
|
|
@@ -13418,19 +13452,30 @@ function nestedStackWarning(resources, stackName) {
|
|
|
13418
13452
|
const names = nested.map((r) => r.constructPath ? withinStackPath(r.constructPath, stackName) : r.logicalId).sort();
|
|
13419
13453
|
return `warning: ${stackName} has ${nested.length} nested CloudFormation stack(s) — cdkrd does not recurse into them, so the resources INSIDE them are NOT checked: ${names.join(", ")}`;
|
|
13420
13454
|
}
|
|
13421
|
-
function
|
|
13422
|
-
|
|
13455
|
+
function isPendingCreationSkip(f) {
|
|
13456
|
+
return f.tier === "skipped" && f.note === "no physical id";
|
|
13457
|
+
}
|
|
13458
|
+
function coverageWarning(findings, stackName, preDeploy = false) {
|
|
13459
|
+
const skipped = findings.filter((f) => f.tier === "skipped" && !(preDeploy && isPendingCreationSkip(f)));
|
|
13423
13460
|
if (skipped.length === 0) return null;
|
|
13424
13461
|
const names = skipped.map((f) => f.constructPath ? withinStackPath(f.constructPath, stackName) : f.logicalId).sort();
|
|
13425
13462
|
const shown = names.slice(0, 10);
|
|
13426
13463
|
const more = names.length > shown.length ? `, …(+${names.length - shown.length} more)` : "";
|
|
13427
13464
|
return `warning: ${stackName}: ${skipped.length} resource(s) were NOT checked (coverage incomplete) — ${shown.join(", ")}${more}; see the skipped breakdown (--verbose)`;
|
|
13428
13465
|
}
|
|
13429
|
-
function
|
|
13430
|
-
|
|
13466
|
+
function pendingCreationInfo(findings, stackName) {
|
|
13467
|
+
const pending = findings.filter(isPendingCreationSkip);
|
|
13468
|
+
if (pending.length === 0) return null;
|
|
13469
|
+
const names = pending.map((f) => f.constructPath ? withinStackPath(f.constructPath, stackName) : f.logicalId).sort();
|
|
13470
|
+
const shown = names.slice(0, 10);
|
|
13471
|
+
const more = names.length > shown.length ? `, …(+${names.length - shown.length} more)` : "";
|
|
13472
|
+
return `info: ${stackName}: ${pending.length} resource(s) not yet deployed — will be created by the next deploy (pending creation, not a coverage gap): ${shown.join(", ")}${more}`;
|
|
13473
|
+
}
|
|
13474
|
+
function hasCoverageGap(findings, resources, preDeploy = false) {
|
|
13475
|
+
return findings.some((f) => f.tier === "skipped" && !(preDeploy && isPendingCreationSkip(f))) || resources.some((r) => r.resourceType === "AWS::CloudFormation::Stack");
|
|
13431
13476
|
}
|
|
13432
|
-
function strictCoverageExit(strict, findings, resources) {
|
|
13433
|
-
return strict && hasCoverageGap(findings, resources) ? 1 : 0;
|
|
13477
|
+
function strictCoverageExit(strict, findings, resources, preDeploy = false) {
|
|
13478
|
+
return strict && hasCoverageGap(findings, resources, preDeploy) ? 1 : 0;
|
|
13434
13479
|
}
|
|
13435
13480
|
/**
|
|
13436
13481
|
* Map a stack's drift code to check's final exit (R53, the `cdk diff --fail` /
|
|
@@ -13440,6 +13485,28 @@ function strictCoverageExit(strict, findings, resources) {
|
|
|
13440
13485
|
function finalCheckExit(code, fail) {
|
|
13441
13486
|
return fail || code !== 1 ? code : 0;
|
|
13442
13487
|
}
|
|
13488
|
+
/**
|
|
13489
|
+
* #781: does a committed baseline exist for this stack, under ANY account/region axis?
|
|
13490
|
+
* When a stack is gone (a DescribeStacks "does not exist"), we do NOT know the
|
|
13491
|
+
* account/region it was deployed to, so we cannot construct the exact baseline path.
|
|
13492
|
+
* A committed baseline is named `<stackName>.<accountId>.<region>.json` under
|
|
13493
|
+
* `.cdkrd/baselines/`, so match by the `<stackName>.` filename prefix across the whole
|
|
13494
|
+
* directory — a prior baseline is PROOF the stack was once deployed, which distinguishes
|
|
13495
|
+
* "deleted out of band" (real drift) from "never deployed yet" (a benign skip). The
|
|
13496
|
+
* baselines directory is derived from `baselinePath` so the layout stays single-sourced.
|
|
13497
|
+
* Pure (filesystem-only, no AWS) + exported for unit tests. Missing dir → false.
|
|
13498
|
+
*/
|
|
13499
|
+
function hasBaselineForStack(stackName) {
|
|
13500
|
+
const dir = dirname(baselinePath(stackName, "a", "r"));
|
|
13501
|
+
let entries;
|
|
13502
|
+
try {
|
|
13503
|
+
entries = readdirSync(dir);
|
|
13504
|
+
} catch {
|
|
13505
|
+
return false;
|
|
13506
|
+
}
|
|
13507
|
+
const prefix = `${stackName}.`;
|
|
13508
|
+
return entries.some((f) => f.startsWith(prefix) && f.endsWith(".json"));
|
|
13509
|
+
}
|
|
13443
13510
|
async function runCheck(args) {
|
|
13444
13511
|
const a = parseCommonArgs(args, "check");
|
|
13445
13512
|
if (a.profile) process.env.AWS_PROFILE = a.profile;
|
|
@@ -13515,9 +13582,13 @@ async function runCheck(args) {
|
|
|
13515
13582
|
const nestedWarn = nestedStackWarning(desired.resources, stackName);
|
|
13516
13583
|
if (nestedWarn) console.error(nestedWarn);
|
|
13517
13584
|
if (a.json) {
|
|
13518
|
-
const covWarn = coverageWarning(gathered.findings, stackName);
|
|
13585
|
+
const covWarn = coverageWarning(gathered.findings, stackName, a.preDeploy);
|
|
13519
13586
|
if (covWarn) console.error(covWarn);
|
|
13520
13587
|
}
|
|
13588
|
+
if (a.preDeploy) {
|
|
13589
|
+
const pendingInfo = pendingCreationInfo(gathered.findings, stackName);
|
|
13590
|
+
if (pendingInfo) console.error(pendingInfo);
|
|
13591
|
+
}
|
|
13521
13592
|
if (desired.stackStatusWarning) console.error(`warning: ${stackName}: ${desired.stackStatusWarning}`);
|
|
13522
13593
|
if (a.undeclaredOnly) {
|
|
13523
13594
|
findings = undeclaredOnlyFindings(findings);
|
|
@@ -13545,7 +13616,7 @@ async function runCheck(args) {
|
|
|
13545
13616
|
} else preDeployCode = report(preDeployReconciled, preDeployHeader, { verbose: a.verbose });
|
|
13546
13617
|
if (preDeployCode === 1) anyDrift = true;
|
|
13547
13618
|
worst = Math.max(worst, finalCheckExit(preDeployCode, a.fail));
|
|
13548
|
-
worst = Math.max(worst, strictCoverageExit(a.strict, gathered.findings, desired.resources));
|
|
13619
|
+
worst = Math.max(worst, strictCoverageExit(a.strict, gathered.findings, desired.resources, true));
|
|
13549
13620
|
continue;
|
|
13550
13621
|
}
|
|
13551
13622
|
const baseline = a.showAll ? void 0 : await loadBaseline(stackName, desired.accountId, region);
|
|
@@ -13602,6 +13673,14 @@ async function runCheck(args) {
|
|
|
13602
13673
|
worst = Math.max(worst, strictCoverageExit(a.strict, gathered.findings, desired.resources));
|
|
13603
13674
|
} catch (e) {
|
|
13604
13675
|
if (isStackNotDeployed(e)) {
|
|
13676
|
+
if (hasBaselineForStack(stackName)) {
|
|
13677
|
+
const msg = "deployed baseline exists but the stack is gone — deleted out of band";
|
|
13678
|
+
console.error(`[Drift] ${stackName} (${region}): ${msg}`);
|
|
13679
|
+
jsonError(stackName, region, msg);
|
|
13680
|
+
worst = Math.max(worst, finalCheckExit(1, a.fail), a.strict ? 1 : 0);
|
|
13681
|
+
anyDrift = true;
|
|
13682
|
+
continue;
|
|
13683
|
+
}
|
|
13605
13684
|
console.error(`note: ${stackName}: not deployed yet — skipped`);
|
|
13606
13685
|
jsonError(stackName, region, "not deployed yet — skipped");
|
|
13607
13686
|
continue;
|