cdk-real-drift 0.9.1 → 0.9.3

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 CHANGED
@@ -643,7 +643,16 @@ If the whole invocation fails before any stack is reached (bad config, an
643
643
  un-synthesizable app, or a zero-stack app), stdout is still a valid empty array
644
644
  `[]` (exit code non-zero, the reason on stderr) — never empty bytes.
645
645
 
646
- Each stack element is `{ "stack": "<name> (<region>)", "drifted": <n>, "findings": [...] }`
646
+ Every successfully-checked stack element carries a `"baseline": <bool>` flag —
647
+ `true` when the stack has a committed `.cdkrd` baseline (its undeclared dimension is
648
+ **watched**), `false` when it has never been recorded. Without a baseline the
649
+ classification shifts: appeared-since-record out-of-band values read as `unrecorded`
650
+ and are excluded from `drifted`, so a QUIET never-recorded stack emits the
651
+ byte-identical `"drifted": 0` of a watched-clean one. A consumer summing `drifted`
652
+ uses `baseline` to tell an UNWATCHED stack from a watched-clean one (#1095). It is
653
+ omitted on the `error` / `stackDeleted` shapes (never reconciled against a baseline).
654
+
655
+ Each stack element is `{ "stack": "<name> (<region>)", "drifted": <n>, "baseline": <bool>, "findings": [...] }`
647
656
  (`error` added only on a pre-check failure; `stackDeleted: true` only on a deleted
648
657
  stack). Each **finding** has a stable shape:
649
658
 
package/dist/cli.js CHANGED
@@ -4719,6 +4719,18 @@ function canonicalizeForCompare(v, resourceType) {
4719
4719
  const base = canonicalizeIdArraysDeep(canonicalizeTagListsDeep(normalizePoliciesDeep(v), orderSig));
4720
4720
  return resourceType === "AWS::WAFv2::WebACL" || resourceType === "AWS::WAFv2::RuleGroup" ? normalizeWafByteMatchDeep(base) : base;
4721
4721
  }
4722
+ function sortObjectArraysDeep(v) {
4723
+ if (Array.isArray(v)) return sortUnorderedObjectArray(v.map(sortObjectArraysDeep));
4724
+ if (v && typeof v === "object") {
4725
+ const out = {};
4726
+ for (const [k, val] of Object.entries(v)) out[k] = sortObjectArraysDeep(val);
4727
+ return out;
4728
+ }
4729
+ return v;
4730
+ }
4731
+ function canonicalizeBaselineForCompare(v, resourceType) {
4732
+ return sortObjectArraysDeep(canonicalizeForCompare(stripAwsTagsDeep(stripCcApiAwsManagedFields(v)), resourceType));
4733
+ }
4722
4734
 
4723
4735
  //#endregion
4724
4736
  //#region src/diff/drift-calculator.ts
@@ -11159,17 +11171,8 @@ function carryForwardUnreadable(recorded, existing, findings) {
11159
11171
  * type-agnostic over UNDECLARED snapshot fragments, which never carry an
11160
11172
  * order-significant declared array, so an order-insensitive sort is safe here.
11161
11173
  */
11162
- function baselineValueMatches(baselineValue, currentCanonicalValue) {
11163
- return deepEqual(sortObjectArraysDeep(canonicalizeForCompare(baselineValue)), sortObjectArraysDeep(canonicalizeForCompare(currentCanonicalValue)));
11164
- }
11165
- function sortObjectArraysDeep(v) {
11166
- if (Array.isArray(v)) return sortUnorderedObjectArray(v.map(sortObjectArraysDeep));
11167
- if (v && typeof v === "object") {
11168
- const out = {};
11169
- for (const [k, val] of Object.entries(v)) out[k] = sortObjectArraysDeep(val);
11170
- return out;
11171
- }
11172
- return v;
11174
+ function baselineValueMatches(baselineValue, currentCanonicalValue, resourceType) {
11175
+ return deepEqual(canonicalizeBaselineForCompare(baselineValue, resourceType), canonicalizeBaselineForCompare(currentCanonicalValue, resourceType));
11173
11176
  }
11174
11177
  const DELTA_IDENTITY_FIELDS = [
11175
11178
  "Key",
@@ -11251,7 +11254,7 @@ function splitRecordedByBaseline(recorded, baseline) {
11251
11254
  const changed = [];
11252
11255
  for (const entry of recorded) {
11253
11256
  const baselineEntry = baseline.recorded.find((a) => a.logicalId === entry.logicalId && a.path === entry.path && a.resourceType === entry.resourceType);
11254
- if (baselineEntry && baselineValueMatches(baselineEntry.value, entry.value)) unchanged.push(entry);
11257
+ if (baselineEntry && baselineValueMatches(baselineEntry.value, entry.value, entry.resourceType)) unchanged.push(entry);
11255
11258
  else changed.push(entry);
11256
11259
  }
11257
11260
  return {
@@ -11400,11 +11403,11 @@ function applyBaseline(findings, baseline, opts = {}) {
11400
11403
  });
11401
11404
  continue;
11402
11405
  }
11403
- if (entry && baselineValueMatches(entry.value, f.actual)) continue;
11406
+ if (entry && baselineValueMatches(entry.value, f.actual, f.resourceType)) continue;
11404
11407
  if (entry) {
11405
11408
  kept.push({
11406
11409
  ...f,
11407
- desired: canonicalizeForCompare(entry.value),
11410
+ desired: canonicalizeForCompare(entry.value, f.resourceType),
11408
11411
  note: f.note ? `${f.note}; changed since record` : "changed since record"
11409
11412
  });
11410
11413
  continue;
@@ -11420,13 +11423,13 @@ function applyBaseline(findings, baseline, opts = {}) {
11420
11423
  continue;
11421
11424
  }
11422
11425
  const entry = replacedLogical.has(f.logicalId) ? void 0 : recorded.find((a) => entryMatches(a, f));
11423
- if (entry && baselineValueMatches(entry.value, f.actual)) continue;
11426
+ if (entry && baselineValueMatches(entry.value, f.actual, f.resourceType)) continue;
11424
11427
  if (entry) {
11425
- const delta = identityArrayDelta(canonicalizeForCompare(entry.value), f.actual);
11428
+ const delta = identityArrayDelta(canonicalizeForCompare(entry.value, f.resourceType), f.actual);
11426
11429
  kept.push({
11427
11430
  ...f,
11428
11431
  tier: "undeclared",
11429
- desired: canonicalizeForCompare(entry.value),
11432
+ desired: canonicalizeForCompare(entry.value, f.resourceType),
11430
11433
  ...delta && { arrayDelta: delta }
11431
11434
  });
11432
11435
  continue;
@@ -14795,6 +14798,36 @@ const CONVERGE_RETRY_DELAY_MS = 3e3;
14795
14798
  function includeUnrecordedRemovals(removeUnrecorded, interactive, yes) {
14796
14799
  return removeUnrecorded || interactive && !yes;
14797
14800
  }
14801
+ function augmentCcItemOps(item, schemas, liveByLogical, declaredByLogical) {
14802
+ if (item.kind !== "cc") return item;
14803
+ const liveRaw = liveByLogical.get(item.logicalId);
14804
+ const tagged = tagPreservingOps(item.ops, liveRaw);
14805
+ const extra = writeOnlyReincludeOps(declaredByLogical(item.logicalId), schemas.get(item.resourceType), tagged);
14806
+ const strip = rejectedEmptyStripOps(item.resourceType, [...tagged, ...extra], liveRaw);
14807
+ const combined = [
14808
+ ...tagged,
14809
+ ...extra,
14810
+ ...strip
14811
+ ];
14812
+ const ops = combined.length > tagged.length ? combined : tagged;
14813
+ return {
14814
+ ...item,
14815
+ ops
14816
+ };
14817
+ }
14818
+ function attributeOpFailure(item, error) {
14819
+ if (item.kind === "delete") return error;
14820
+ const paths = [...new Set(item.ops.map((o) => o.path).filter((p) => p.length > 0))];
14821
+ if (paths.length === 0) return error;
14822
+ const prefix = `[${paths.join(", ")}]`;
14823
+ return error.startsWith(prefix) ? error : `${prefix}: ${error}`;
14824
+ }
14825
+ function augmentRevertPlan(plan, schemas, liveByLogical, declaredByLogical) {
14826
+ return {
14827
+ ...plan,
14828
+ items: plan.items.map((item) => augmentCcItemOps(item, schemas, liveByLogical, declaredByLogical))
14829
+ };
14830
+ }
14798
14831
  async function revertStack(p) {
14799
14832
  const { stackName, region, gathered, baseline, config, dryRun, yes, removeUnrecorded, verbose, interactive, autoSelectAll, waitMs, waitNow, waitSleep } = p;
14800
14833
  const out = p.json ? () => {} : console.log;
@@ -14814,6 +14847,9 @@ async function revertStack(p) {
14814
14847
  accountId: gathered.desired.accountId,
14815
14848
  region
14816
14849
  }, config);
14850
+ const resByLogical = new Map(gathered.desired.resources.map((res) => [res.logicalId, res]));
14851
+ const declaredForLogical = (logicalId) => resByLogical.get(logicalId)?.declared;
14852
+ const augment = (pl) => augmentRevertPlan(pl, gathered.schemas, gathered.liveByLogical, declaredForLogical);
14817
14853
  let plan = buildRevertPlan(drifted, baseline, {
14818
14854
  removeUnrecorded: includeRemovals,
14819
14855
  schemas: gathered.schemas,
@@ -14828,7 +14864,7 @@ async function revertStack(p) {
14828
14864
  aborted: false
14829
14865
  };
14830
14866
  }
14831
- if (!p.json) printPlan(stackName, region, plan, {
14867
+ if (!p.json) printPlan(stackName, region, augment(plan), {
14832
14868
  verbose,
14833
14869
  unrecordedGuidance: !includeRemovals && drifted.some((f) => f.unrecorded === true)
14834
14870
  });
@@ -14844,13 +14880,14 @@ async function revertStack(p) {
14844
14880
  };
14845
14881
  }
14846
14882
  if (dryRun) {
14847
- const opCount = plan.items.reduce((n, i) => n + i.ops.length, 0);
14848
- out(`\n(dry-run) would apply ${opCount} op(s) to ${plan.items.length} resource(s). No changes made.`);
14883
+ const augmented = augment(plan);
14884
+ const opCount = augmented.items.reduce((n, i) => n + i.ops.length, 0);
14885
+ out(`\n(dry-run) would apply ${opCount} op(s) to ${augmented.items.length} resource(s). No changes made.`);
14849
14886
  return {
14850
14887
  exit: 0,
14851
14888
  aborted: false,
14852
14889
  plannedOps: opCount,
14853
- plannedResources: plan.items.length
14890
+ plannedResources: augmented.items.length
14854
14891
  };
14855
14892
  }
14856
14893
  if (!yes) {
@@ -14881,8 +14918,9 @@ async function revertStack(p) {
14881
14918
  };
14882
14919
  }
14883
14920
  }
14884
- const opCount = plan.items.reduce((n, i) => n + i.ops.length, 0);
14885
- const deleteCount = plan.items.filter((i) => i.kind === "delete").length;
14921
+ const confirmPlan = augment(plan);
14922
+ const opCount = confirmPlan.items.reduce((n, i) => n + i.ops.length, 0);
14923
+ const deleteCount = confirmPlan.items.filter((i) => i.kind === "delete").length;
14886
14924
  const ok = await confirm({
14887
14925
  message: revertConfirmMessage(stackName, region, opCount, plan.notRevertable.length, deleteCount),
14888
14926
  initialValue: false
@@ -14895,6 +14933,7 @@ async function revertStack(p) {
14895
14933
  };
14896
14934
  }
14897
14935
  }
14936
+ plan = augment(plan);
14898
14937
  const inProgress = await stackInProgressRefusal(stackName, region);
14899
14938
  if (inProgress) {
14900
14939
  console.error("\n" + inProgress);
@@ -14908,7 +14947,6 @@ async function revertStack(p) {
14908
14947
  region,
14909
14948
  ...CLIENT_TIMEOUTS
14910
14949
  });
14911
- const byLogical = new Map(gathered.desired.resources.map((res) => [res.logicalId, res]));
14912
14950
  const failedDeleteIds = /* @__PURE__ */ new Set();
14913
14951
  const failedUpdateIds = /* @__PURE__ */ new Set();
14914
14952
  const applied = [];
@@ -14928,7 +14966,7 @@ async function revertStack(p) {
14928
14966
  if (item.kind === "delete") continue;
14929
14967
  let r;
14930
14968
  if (item.kind === "sdk") {
14931
- const res = byLogical.get(item.logicalId);
14969
+ const res = resByLogical.get(item.logicalId);
14932
14970
  r = await retryTransient(async () => {
14933
14971
  try {
14934
14972
  const writer = resolveSdkWriter(item.resourceType, item.ops);
@@ -14951,26 +14989,14 @@ async function revertStack(p) {
14951
14989
  }
14952
14990
  }, buildRetryOpts(item.displayId));
14953
14991
  } else {
14954
- const res = byLogical.get(item.logicalId);
14955
- const identifier = CC_IDENTIFIER_ADAPTERS[item.resourceType]?.(item.physicalId, res?.declared ?? {}, region, gathered.desired.accountId) ?? item.physicalId;
14956
- const tagged = tagPreservingOps(item.ops, gathered.liveByLogical.get(item.logicalId));
14957
- const extra = writeOnlyReincludeOps(res?.declared, gathered.schemas.get(item.resourceType), tagged);
14958
- const strip = rejectedEmptyStripOps(item.resourceType, [...tagged, ...extra], gathered.liveByLogical.get(item.logicalId));
14959
- const combined = [
14960
- ...tagged,
14961
- ...extra,
14962
- ...strip
14963
- ];
14964
- r = await applyRevertItem(cc, {
14965
- ...item,
14966
- ops: combined.length > tagged.length ? combined : tagged
14967
- }, identifier, buildRetryOpts(item.displayId));
14992
+ const res = resByLogical.get(item.logicalId);
14993
+ r = await applyRevertItem(cc, item, CC_IDENTIFIER_ADAPTERS[item.resourceType]?.(item.physicalId, res?.declared ?? {}, region, gathered.desired.accountId) ?? item.physicalId, buildRetryOpts(item.displayId));
14968
14994
  }
14969
14995
  applied.push({
14970
14996
  logicalId: item.logicalId,
14971
14997
  displayId: item.displayId,
14972
14998
  ok: r.ok,
14973
- ...r.error !== void 0 && { error: r.error },
14999
+ ...r.error !== void 0 && { error: attributeOpFailure(item, r.error) },
14974
15000
  ...r.hint !== void 0 && { hint: r.hint }
14975
15001
  });
14976
15002
  if (!r.ok) worst = Math.max(worst, 2);
@@ -15491,7 +15517,7 @@ function deletedStackReport(label) {
15491
15517
  stackDeleted: true
15492
15518
  };
15493
15519
  }
15494
- function buildStackJson(rawFindings, header) {
15520
+ function buildStackJson(rawFindings, header, hasBaseline) {
15495
15521
  const findings = annotateHints(rawFindings);
15496
15522
  const isDriftHere = (f) => DRIFT_TIERS.includes(f.tier) && !f.unrecorded;
15497
15523
  const drifted = findings.filter(isDriftHere).length;
@@ -15499,7 +15525,8 @@ function buildStackJson(rawFindings, header) {
15499
15525
  json: {
15500
15526
  stack: header,
15501
15527
  drifted,
15502
- findings
15528
+ findings,
15529
+ baseline: hasBaseline === true
15503
15530
  },
15504
15531
  code: drifted === 0 ? 0 : 1
15505
15532
  };
@@ -16527,7 +16554,7 @@ async function runCheck(args) {
16527
16554
  const reportHeader = `${posPrefix}${stackName} (${region})`;
16528
16555
  let code;
16529
16556
  if (a.json) {
16530
- const { json, code: jsonCode } = buildStackJson(reconciled, reportHeader);
16557
+ const { json, code: jsonCode } = buildStackJson(reconciled, reportHeader, baseline !== void 0);
16531
16558
  jsonReports.push(json);
16532
16559
  code = jsonCode;
16533
16560
  } else code = report(reconciled, reportHeader, {
@@ -16556,7 +16583,7 @@ async function runCheck(args) {
16556
16583
  verbose: a.verbose,
16557
16584
  positionPrefix: posPrefix
16558
16585
  });
16559
- else if (!baseline && !hasUnrecorded && !a.json && !a.showAll && !a.preDeploy) console.error(`note: ${stackName}: no .cdkrd baseline yet — run \`cdkrd record\` to establish one (future out-of-band changes then report as drift).`);
16586
+ else if (!baseline && !hasUnrecorded && !a.showAll && !a.preDeploy) console.error(`note: ${stackName}: no .cdkrd baseline yet — run \`cdkrd record\` to establish one (future out-of-band changes then report as drift).`);
16560
16587
  if (code === 1) anyDrift = true;
16561
16588
  worst = Math.max(worst, finalCheckExit(code, a.fail));
16562
16589
  worst = Math.max(worst, strictCoverageExit(a.strict, gathered.findings, desired.resources));