cdk-real-drift 0.9.4 → 0.9.6

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 CHANGED
@@ -3016,7 +3016,11 @@ const VALUE_INDEPENDENT_DEFAULT_TOPLEVEL_PATHS = {
3016
3016
  "SecurityGroups",
3017
3017
  "SubnetIds"
3018
3018
  ]),
3019
- "AWS::EC2::Instance": new Set(["PrivateIpAddress"]),
3019
+ "AWS::EC2::Instance": new Set([
3020
+ "PrivateIpAddress",
3021
+ "CpuOptions",
3022
+ "SubnetId"
3023
+ ]),
3020
3024
  "AWS::EC2::NetworkInterface": new Set([
3021
3025
  "PrivateIpAddress",
3022
3026
  "PrivateIpAddresses",
@@ -3589,6 +3593,7 @@ function alignNameValueSubset(declared, live, spec) {
3589
3593
  }
3590
3594
  const UNORDERED_ARRAY_PROPS = {
3591
3595
  "AWS::CloudFront::Distribution": new Set(["DistributionConfig.Restrictions.GeoRestriction.Locations"]),
3596
+ "AWS::Cognito::UserPool": new Set(["EnabledMfas"]),
3592
3597
  "AWS::Cognito::UserPoolClient": new Set([
3593
3598
  "AllowedOAuthFlows",
3594
3599
  "AllowedOAuthScopes",
@@ -4981,6 +4986,15 @@ function twinOverlapEchoes(source, twin) {
4981
4986
  }
4982
4987
  return true;
4983
4988
  }
4989
+ function applyDefinitionSubstitutions(definitionString, substitutions) {
4990
+ let out = definitionString;
4991
+ for (const [key, value] of Object.entries(substitutions)) {
4992
+ if (value === null || typeof value === "object") continue;
4993
+ const token = `\${${key}}`;
4994
+ out = out.split(token).join(String(value));
4995
+ }
4996
+ return out;
4997
+ }
4984
4998
  const IAM_ATTACHMENT_SUBSET = { "AWS::IAM::ManagedPolicy": new Set([
4985
4999
  "Roles",
4986
5000
  "Users",
@@ -5370,7 +5384,10 @@ function classifyResource(resource, liveRaw, schema, opts = {}) {
5370
5384
  });
5371
5385
  const twinSources = {};
5372
5386
  for (const src of Object.values(SHAPE_ECHO_TWIN[resourceType] ?? {})) if (isNestedObject(declared[src])) twinSources[src] = declared[src];
5387
+ const sfnDefinitionObject = resourceType === "AWS::StepFunctions::StateMachine" && isNestedObject(declared["Definition"]) ? declared["Definition"] : void 0;
5388
+ const sfnDefinitionSubstitutions = resourceType === "AWS::StepFunctions::StateMachine" && isNestedObject(declared["DefinitionSubstitutions"]) ? declared["DefinitionSubstitutions"] : void 0;
5373
5389
  deepStripPaths(declared, schema.writeOnlyPaths);
5390
+ if (sfnDefinitionSubstitutions !== void 0 && typeof declared["DefinitionString"] === "string") declared["DefinitionString"] = applyDefinitionSubstitutions(declared["DefinitionString"], sfnDefinitionSubstitutions);
5374
5391
  const sibling = resource.siblingPolicyNames;
5375
5392
  if (sibling !== void 0 && sibling !== "unresolved" && Array.isArray(live.Policies)) {
5376
5393
  const names = new Set(sibling);
@@ -5635,6 +5652,26 @@ function classifyResource(resource, liveRaw, schema, opts = {}) {
5635
5652
  });
5636
5653
  continue;
5637
5654
  }
5655
+ if (resourceType === "AWS::StepFunctions::StateMachine" && k === "DefinitionString" && typeof v === "string" && typeof live[k] === "string") {
5656
+ let declaredParsed;
5657
+ let liveParsed;
5658
+ try {
5659
+ declaredParsed = JSON.parse(v);
5660
+ liveParsed = JSON.parse(live[k]);
5661
+ } catch {
5662
+ declaredParsed = void 0;
5663
+ }
5664
+ if (declaredParsed !== void 0 && deepEqual(declaredParsed, liveParsed)) continue;
5665
+ findings.push({
5666
+ tier: "declared",
5667
+ logicalId,
5668
+ resourceType,
5669
+ path: k,
5670
+ desired: v,
5671
+ actual: live[k]
5672
+ });
5673
+ continue;
5674
+ }
5638
5675
  const jsonFills = JSON_STRING_DEFAULT_FILLS[resourceType]?.[k];
5639
5676
  if (jsonFills !== void 0 && typeof v === "string" && typeof live[k] === "string") try {
5640
5677
  const dvParsed = JSON.parse(v);
@@ -6110,6 +6147,24 @@ function classifyResource(resource, liveRaw, schema, opts = {}) {
6110
6147
  });
6111
6148
  continue;
6112
6149
  }
6150
+ if (sfnDefinitionObject !== void 0 && k === "DefinitionString" && typeof v === "string") {
6151
+ let liveDefParsed;
6152
+ try {
6153
+ liveDefParsed = JSON.parse(v);
6154
+ } catch {
6155
+ liveDefParsed = void 0;
6156
+ }
6157
+ if (liveDefParsed !== void 0 && deepEqual(liveDefParsed, sfnDefinitionObject)) {
6158
+ findings.push({
6159
+ tier: "atDefault",
6160
+ logicalId,
6161
+ resourceType,
6162
+ path: k,
6163
+ actual: v
6164
+ });
6165
+ continue;
6166
+ }
6167
+ }
6113
6168
  const twinSourceKey = SHAPE_ECHO_TWIN[resourceType]?.[k];
6114
6169
  const twinSource = twinSourceKey !== void 0 ? twinSources[twinSourceKey] ?? declared[twinSourceKey] : void 0;
6115
6170
  if (isNestedObject(v) && isNestedObject(twinSource) && twinOverlapEchoes(twinSource, v)) {
@@ -6904,6 +6959,7 @@ function diffEventBusChildren(input) {
6904
6959
  const added = [];
6905
6960
  for (const r of input.liveRules) {
6906
6961
  if (declared.has(r.name)) continue;
6962
+ const siblingLookupId = input.isDefaultBus ? r.name : `${input.busName}|${r.name}`;
6907
6963
  added.push({
6908
6964
  resourceType: "AWS::Events::Rule",
6909
6965
  identifier: r.arn,
@@ -6911,7 +6967,8 @@ function diffEventBusChildren(input) {
6911
6967
  live: {
6912
6968
  Name: r.name,
6913
6969
  Arn: r.arn
6914
- }
6970
+ },
6971
+ siblingLookupId
6915
6972
  });
6916
6973
  }
6917
6974
  return added;
@@ -6943,16 +7000,19 @@ async function enumerateEventBusChildren(ctx) {
6943
7000
  const raw = r.physicalId ?? (typeof r.declared.Name === "string" ? r.declared.Name : void 0);
6944
7001
  if (raw) declaredRuleNames.push(raw.includes("|") ? raw.slice(raw.lastIndexOf("|") + 1) : raw);
6945
7002
  }
7003
+ const liveRules = (await pageRules(new EventBridgeClient({
7004
+ region,
7005
+ ...READ_RETRY
7006
+ }), busName)).filter((r) => typeof r.Name === "string" && typeof r.Arn === "string" && !r.ManagedBy).map((r) => ({
7007
+ name: r.Name,
7008
+ arn: r.Arn,
7009
+ label: r.Name
7010
+ }));
6946
7011
  return diffEventBusChildren({
7012
+ busName,
7013
+ isDefaultBus: busName === "default",
6947
7014
  declaredRuleNames,
6948
- liveRules: (await pageRules(new EventBridgeClient({
6949
- region,
6950
- ...READ_RETRY
6951
- }), busName)).filter((r) => typeof r.Name === "string" && typeof r.Arn === "string" && !r.ManagedBy).map((r) => ({
6952
- name: r.Name,
6953
- arn: r.Arn,
6954
- label: r.Name
6955
- }))
7015
+ liveRules
6956
7016
  });
6957
7017
  }
6958
7018
  function diffUserPoolChildren(input) {
@@ -10358,8 +10418,8 @@ async function readAddedModel(cc, cfn, c, schemas, oaiCanonicalIds) {
10358
10418
  }
10359
10419
  }
10360
10420
  async function isManagedBySiblingStack(cfn, c, cache) {
10361
- const physicalId = c.identifier;
10362
- if (physicalId.includes("|")) return "notManaged";
10421
+ const physicalId = c.siblingLookupId ?? c.identifier;
10422
+ if (c.siblingLookupId === void 0 && physicalId.includes("|")) return "notManaged";
10363
10423
  const cached = cache.get(physicalId);
10364
10424
  if (cached !== void 0) return cached;
10365
10425
  let managed = false;
@@ -11013,22 +11073,40 @@ async function writeBaselineFile(b) {
11013
11073
  * be read this run (#795), so undeclared values hidden behind that gap were NOT
11014
11074
  * captured — the resource must not claim completeness, else a later cdkrd that
11015
11075
  * closes the read gap would surface those newly-visible values as false "appeared
11016
- * since record" drift. Ignored-tier values do not block completeness: they were
11017
- * visible and deliberately ruled out.
11076
+ * since record" drift.
11077
+ *
11078
+ * #1078: an `ignored`-tier value whose path is ABSENT from `recorded` also blocks
11079
+ * completeness (and — unlike the other blocks — DEMOTES a previously-complete
11080
+ * resource, overriding monotonicity). An ignore rule re-tags an undeclared finding
11081
+ * `ignored`, so `buildRecorded` drops it and this value never enters the snapshot;
11082
+ * marking the resource complete anyway would treat that value as "known-absent". But
11083
+ * an ignore rule can be DELETED (the documented un-ignore) — and when it dies the
11084
+ * value (unchanged all along) would then read as false CONFIRMED "appeared since
11085
+ * record" drift against a complete resource, instead of returning to `unrecorded`
11086
+ * `[Potential Drift]`. So an ignored-and-unrecorded path keeps its resource
11087
+ * INCOMPLETE, so un-ignoring lands the untouched value back at `unrecorded`. This
11088
+ * pairs with `carryForwardIgnored` (WRITE side): a value with a PRIOR recorded entry
11089
+ * IS carried into `recorded`, so its path IS present, so it does NOT block here — a
11090
+ * previously-endorsed value survives an ignore-era record and stays complete. Only a
11091
+ * NEVER-recorded ignored path (no entry to carry) demotes completeness.
11018
11092
  * Monotonic via `previousComplete`: once complete, a resource stays complete
11019
11093
  * (declining to record an appeared-since-record value keeps it drift, instead of
11020
- * demoting it back to unrecorded) — pruned to ids still in the template.
11094
+ * demoting it back to unrecorded) — pruned to ids still in the template. The #1078
11095
+ * ignored-and-unrecorded demotion is the one deliberate exception (the un-ignore
11096
+ * lifecycle must round-trip).
11021
11097
  */
11022
11098
  function computeCompleteResources(allLogicalIds, findings, recorded, previousComplete = []) {
11023
11099
  const recordedKeys = new Set(recorded.map(recordedKey));
11024
11100
  const blocked = /* @__PURE__ */ new Set();
11101
+ const blockedByIgnore = /* @__PURE__ */ new Set();
11025
11102
  for (const f of findings) {
11026
11103
  if (f.tier === "skipped" || f.tier === "deleted" || f.tier === "readGap") blocked.add(f.logicalId);
11027
11104
  if (f.tier === "undeclared" && !recordedKeys.has(recordedKey(f))) blocked.add(f.logicalId);
11105
+ if (f.tier === "ignored" && !recordedKeys.has(recordedKey(f))) blockedByIgnore.add(f.logicalId);
11028
11106
  }
11029
11107
  const all = new Set(allLogicalIds);
11030
- const complete = new Set(previousComplete.filter((id) => all.has(id)));
11031
- for (const id of allLogicalIds) if (!blocked.has(id)) complete.add(id);
11108
+ const complete = new Set(previousComplete.filter((id) => all.has(id) && !blockedByIgnore.has(id)));
11109
+ for (const id of allLogicalIds) if (!blocked.has(id) && !blockedByIgnore.has(id)) complete.add(id);
11032
11110
  return [...complete].sort();
11033
11111
  }
11034
11112
  /** Write a baseline for a stack from a check run's findings + raw template.
@@ -11148,6 +11226,37 @@ function carryForwardUnreadable(recorded, existing, findings) {
11148
11226
  return preserved.length === 0 ? recorded : [...recorded, ...preserved];
11149
11227
  }
11150
11228
  /**
11229
+ * #1078: carry forward a previously-recorded entry for a path that is CURRENTLY
11230
+ * suppressed by an ignore rule (tier `ignored` this run), so an ignore-era `record`
11231
+ * does not silently PRUNE a value the user already endorsed.
11232
+ *
11233
+ * The `findings` reaching `record` are `applyIgnores`'d first (record.ts /
11234
+ * interactive-resolve.ts), so a value with a live ignore rule arrives tier `ignored`
11235
+ * — and `buildRecorded` keeps only `undeclared`/`added`, so it produces NO entry.
11236
+ * Because `writeBaseline` full-replaces `recorded`, the endorsed entry for that path
11237
+ * would vanish from the git-committed baseline (an unexplained deletion to the PR
11238
+ * reviewer). Then, when the user later DELETES the rule (the documented un-ignore),
11239
+ * the value — unchanged all along — would false-surface as confirmed drift.
11240
+ *
11241
+ * So for every ignored finding whose path already has an entry in the EXISTING
11242
+ * baseline, carry that entry forward. While the rule lives the entry is inert
11243
+ * (`applyIgnores` runs AFTER `applyBaseline` everywhere, so ignore keeps winning — the
11244
+ * value is never reported); when the rule dies, watching resumes against the real,
11245
+ * preserved snapshot (a later genuine change still correctly reports "changed since
11246
+ * record"). Only carries an EXISTING entry — a never-recorded ignored path has none to
11247
+ * carry, and its completeness demotion (see `computeCompleteResources`) makes the
11248
+ * un-ignore land at `unrecorded`, not false "appeared since record".
11249
+ */
11250
+ function carryForwardIgnored(recorded, existing, findings) {
11251
+ const prior = existing?.recorded;
11252
+ if (!prior || prior.length === 0) return recorded;
11253
+ const ignoredKeys = new Set(findings.filter((f) => f.tier === "ignored").map((f) => recordedKey(f)));
11254
+ if (ignoredKeys.size === 0) return recorded;
11255
+ const present = new Set(recorded.map(recordedKey));
11256
+ const preserved = prior.filter((e) => ignoredKeys.has(recordedKey(e)) && !present.has(recordedKey(e)));
11257
+ return preserved.length === 0 ? recorded : [...recorded, ...preserved];
11258
+ }
11259
+ /**
11151
11260
  * The single source of truth for "is this baseline value equal to the current
11152
11261
  * value?". `applyBaseline` (suppress vs surface) and `splitRecordedByBaseline`
11153
11262
  * (unchanged vs changed) MUST share this exact predicate. BOTH sides are
@@ -14289,6 +14398,7 @@ async function applyRevertDelete(cc, item, identifier = item.physicalId, retry =
14289
14398
  const isDrift = (f) => (f.tier === "deleted" || f.tier === "added" || f.tier === "declared" || f.tier === "undeclared") && !f.unrecorded;
14290
14399
  const driftCount = (findings) => findings.filter(isDrift).length;
14291
14400
  const unrecordedCount = (findings) => findings.filter((f) => f.unrecorded === true).length;
14401
+ const findingKeyOf = (f) => `${f.logicalId}::${f.path}${f.attributeKey !== void 0 ? `[${f.attributeKey}]` : ""}`;
14292
14402
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
14293
14403
  /**
14294
14404
  * The per-stack label shown in a DECISION prompt — the SAME `Name (region)` form the
@@ -14559,7 +14669,7 @@ async function recordStack(p) {
14559
14669
  const existing = await loadBaseline(stackName, desired.accountId, region);
14560
14670
  if (existing) checkBaselineAccount(existing, desired.accountId, stackName);
14561
14671
  if (!yes && existing) console.error(`note: ${stackLabel$1(stackName, region)}: will overwrite the existing baseline file on confirm (nothing written yet; it is git-tracked — review the diff afterwards). Pass --yes to silence.`);
14562
- let recorded = carryForwardUnreadable(buildRecorded(findings), existing, findings);
14672
+ let recorded = carryForwardIgnored(carryForwardUnreadable(buildRecorded(findings), existing, findings), existing, findings);
14563
14673
  const dropCandidates = baselineOnlyEntries(recorded, existing, findings, {
14564
14674
  declaredByLogical: declaredKeysByLogical(desired.resources),
14565
14675
  allLogicalIds: desired.resources.map((r) => r.logicalId)
@@ -14873,7 +14983,7 @@ function augmentRevertPlan(plan, schemas, liveByLogical, declaredByLogical) {
14873
14983
  };
14874
14984
  }
14875
14985
  async function revertStack(p) {
14876
- const { stackName, region, gathered, baseline, config, dryRun, yes, removeUnrecorded, verbose, interactive, autoSelectAll, waitMs, waitNow, waitSleep } = p;
14986
+ const { stackName, region, gathered, baseline, config, dryRun, yes, removeUnrecorded, verbose, interactive, autoSelectAll, selectedFindingKeys, waitMs, waitNow, waitSleep } = p;
14877
14987
  const out = p.json ? () => {} : console.log;
14878
14988
  let worst = 0;
14879
14989
  const includeRemovals = includeUnrecordedRemovals(removeUnrecorded, interactive, yes);
@@ -14883,7 +14993,7 @@ async function revertStack(p) {
14883
14993
  constructPathByLogical: constructPathsByLogical(gathered.desired.resources),
14884
14994
  allLogicalIds: gathered.desired.resources.map((r) => r.logicalId)
14885
14995
  };
14886
- const drifted = applyIgnores(applyBaseline(gathered.findings, baseline, {
14996
+ const reconciled = applyIgnores(applyBaseline(gathered.findings, baseline, {
14887
14997
  ...baselineOpts,
14888
14998
  warn: console.error
14889
14999
  }), {
@@ -14891,6 +15001,7 @@ async function revertStack(p) {
14891
15001
  accountId: gathered.desired.accountId,
14892
15002
  region
14893
15003
  }, config);
15004
+ const drifted = selectedFindingKeys === void 0 ? reconciled : reconciled.filter((f) => selectedFindingKeys.has(findingKeyOf(f)));
14894
15005
  const resByLogical = new Map(gathered.desired.resources.map((res) => [res.logicalId, res]));
14895
15006
  const declaredForLogical = (logicalId) => resByLogical.get(logicalId)?.declared;
14896
15007
  const augment = (pl) => augmentRevertPlan(pl, gathered.schemas, gathered.liveByLogical, declaredForLogical);
@@ -16207,11 +16318,12 @@ async function perFinding(p, decidable) {
16207
16318
  region: p.region,
16208
16319
  gathered: {
16209
16320
  desired: p.desired,
16210
- findings: p.findings.filter((f) => revertKeys.has(keyOf(f))),
16321
+ findings: p.findings,
16211
16322
  schemas: p.schemas,
16212
16323
  liveByLogical: p.liveByLogical
16213
16324
  },
16214
16325
  baseline: p.baseline,
16326
+ selectedFindingKeys: revertKeys,
16215
16327
  config: p.config,
16216
16328
  dryRun: false,
16217
16329
  yes: false,