cdk-real-drift 0.2.52 → 0.2.54

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
@@ -473,9 +473,19 @@ ignore:
473
473
  non-CDK stack). Rules written with the older full `<stack>/<constructPath>.<path>`
474
474
  form still match. Widening a stamped scope to a `*` glob (to intentionally ignore a
475
475
  path across every stack/account/region) stays a hand-edit.
476
- - All four fields accept the same `*` / `?` glob, and a parent `path` covers child
477
- paths. The three scope axes — **stack, account, region** — are exactly the
478
- baseline file's identity axes. **Account** keeps a `stack: "Prod*"` rule from
476
+ - All four fields accept `*` / `?` globs, but the `path` axis is **segment-aware**
477
+ while the scope axes are not. In `path`, `*` / `?` match within a single `.`-delimited
478
+ segment (`*.DesiredCount` matches `<anyId>.DesiredCount`, not a deeper
479
+ `Tbl.Config.DesiredCount`) — a parent `path` still covers child paths via the subtree
480
+ walk, so the segment bound does not under-match. Inside a `[...]` bracket key a `*` /
481
+ `?` is **unbounded within that bracket** (the brackets delimit the key, so a `.`
482
+ between them is data): `Alb.LoadBalancerAttributes[*]` and
483
+ `Alb.LoadBalancerAttributes[routing.*]` both match the dotted key
484
+ `[routing.http2.enabled]`. On **stack / account / region**, `*` / `?` are unbounded
485
+ (those names carry no `.`). The three scope axes are exactly the
486
+ baseline file's identity axes; each is **omitted to leave that axis unscoped**
487
+ (match-all) — a present-but-empty `""` is rejected (it would match nothing).
488
+ **Account** keeps a `stack: "Prod*"` rule from
479
489
  leaking into a same-named stack in another account (stack-name uniqueness only
480
490
  holds within one account / App); **region** is independent the same way — the same
481
491
  stack in several accounts or regions may drift in only one.
package/dist/cli.js CHANGED
@@ -353,12 +353,24 @@ function matchesGlob(pattern, name) {
353
353
  * (`X.Policies` covering `X.Policies[Mp].Name`) is handled separately by the ancestor
354
354
  * walk in `pathMatches`, so bounding `*` here does not under-match. The stack/region
355
355
  * axes keep `matchesGlob` (their names contain no `.`/`[`, so it is equivalent there).
356
+ *
357
+ * INSIDE a `[...]` bracket segment a `*` / `?` is UNBOUNDED within that bracket: the
358
+ * brackets already delimit the key, so a `.` between them is DATA, not a segment
359
+ * boundary — cdkrd's own canonical paths carry dotted bracket keys such as
360
+ * `Alb.LoadBalancerAttributes[routing.http2.enabled]`. So `...[*]` and `...[routing.*]`
361
+ * match those keys, while `.` OUTSIDE brackets stays a hard segment boundary. A `*`/`?`
362
+ * inside a bracket may not cross the closing `]` (still one bracket segment).
356
363
  */
357
364
  function matchesPathGlob(pattern, target) {
358
365
  let out = "^";
359
- for (const ch of pattern.replace(/\*+/g, "*")) if (ch === "*") out += "[^.[]*";
360
- else if (ch === "?") out += "[^.[]";
361
- else out += ch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
366
+ let inBracket = false;
367
+ for (const ch of pattern.replace(/\*+/g, "*")) if (ch === "*") out += inBracket ? "[^\\]]*" : "[^.[]*";
368
+ else if (ch === "?") out += inBracket ? "[^\\]]" : "[^.[]";
369
+ else {
370
+ if (ch === "[") inBracket = true;
371
+ else if (ch === "]") inBracket = false;
372
+ out += ch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
373
+ }
362
374
  out += "$";
363
375
  return new RegExp(out).test(target);
364
376
  }
@@ -434,7 +446,10 @@ function validateIgnoreEntry(entry, index) {
434
446
  "stack",
435
447
  "account",
436
448
  "region"
437
- ]) if (obj[k] !== void 0 && typeof obj[k] !== "string") throw new Error(`${at}: "${k}" must be a string`);
449
+ ]) {
450
+ if (obj[k] !== void 0 && typeof obj[k] !== "string") throw new Error(`${at}: "${k}" must be a string`);
451
+ if (obj[k] === "") throw new Error(`${at}: "${k}" must not be empty (omit it to leave the axis unscoped)`);
452
+ }
438
453
  }
439
454
  /**
440
455
  * The exact ignore rule the `ignore` verb (and check's inline ignore) writes for a
@@ -9044,8 +9059,56 @@ async function loadBaseline(stackName, accountId, region) {
9044
9059
  delete parsed.accepted;
9045
9060
  if (typeof parsed.schemaVersion === "number" && parsed.schemaVersion > 2) throw new Error(`baseline file ${path} was written by a newer cdkrd (schemaVersion ${parsed.schemaVersion}); upgrade cdkrd`);
9046
9061
  if (!Array.isArray(parsed.recorded)) throw new Error(`baseline file ${path} is malformed: \`recorded\` is missing or not an array`);
9062
+ parsed.recorded.forEach((entry, i) => validateRecordedEntry(entry, i, path));
9063
+ validateCompleteResources(parsed.completeResources, path);
9064
+ validateRecordedPhysicalIds(parsed.recordedPhysicalIds, path);
9047
9065
  return parsed;
9048
9066
  }
9067
+ /**
9068
+ * #794: validate one `recorded` array element. Each must be a non-null object with a
9069
+ * string `logicalId` and a string `path` (the two fields the match logic reads —
9070
+ * `recorded.find(a => a.logicalId === … && a.path === …)`), plus a string `resourceType`
9071
+ * (used for the #793 type-mismatch guard and stamped onto the synthetic removed-since-record
9072
+ * finding). `value` is intentionally unconstrained (any JSON is a valid recorded value,
9073
+ * including `null`). Errors name the file + the offending index/key, mirroring the
9074
+ * ignore.yaml validation style.
9075
+ */
9076
+ function validateRecordedEntry(entry, index, path) {
9077
+ const at = `baseline file ${path}: \`recorded\`[${index}]`;
9078
+ if (typeof entry !== "object" || entry === null || Array.isArray(entry)) throw new Error(`${at} must be an object { logicalId, resourceType, path, value }`);
9079
+ const obj = entry;
9080
+ for (const k of [
9081
+ "logicalId",
9082
+ "resourceType",
9083
+ "path"
9084
+ ]) if (typeof obj[k] !== "string") throw new Error(`${at}: "${k}" is required and must be a string`);
9085
+ }
9086
+ /**
9087
+ * #794: validate `completeResources` (optional; absent on v1 files). When present it must
9088
+ * be an array of strings — a bare string would have `new Set(...)` iterate its CHARACTERS
9089
+ * (silently mis-scoping completeness), and a number would crash with an opaque
9090
+ * "not iterable" later. Reject both loudly, naming the file + index.
9091
+ */
9092
+ function validateCompleteResources(value, path) {
9093
+ if (value === void 0) return;
9094
+ const at = `baseline file ${path}: \`completeResources\``;
9095
+ if (!Array.isArray(value)) throw new Error(`${at} must be an array of strings`);
9096
+ value.forEach((v, i) => {
9097
+ if (typeof v !== "string") throw new Error(`${at}[${i}] must be a string`);
9098
+ });
9099
+ }
9100
+ /**
9101
+ * #794: validate `recordedPhysicalIds` (optional; #674). When present it must be a plain
9102
+ * object mapping string logicalId -> string physical id — `Object.entries` on a non-object
9103
+ * or a non-string value would otherwise mis-drive the #674 replacement check silently.
9104
+ * Reject non-object shapes and non-string values loudly, naming the file + key.
9105
+ */
9106
+ function validateRecordedPhysicalIds(value, path) {
9107
+ if (value === void 0) return;
9108
+ const at = `baseline file ${path}: \`recordedPhysicalIds\``;
9109
+ if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(`${at} must be an object mapping logicalId to physical id`);
9110
+ for (const [key, v] of Object.entries(value)) if (typeof v !== "string") throw new Error(`${at}: "${key}" must map to a string`);
9111
+ }
9049
9112
  /** Deterministic order for `recorded` entries: lexicographic by (logicalId, path).
9050
9113
  * The single point where order is imposed — read/compare logic is order-independent,
9051
9114
  * so this only affects the bytes on disk. Without it the entries inherit findings
@@ -9293,7 +9356,7 @@ function splitRecordedByBaseline(recorded, baseline) {
9293
9356
  const unchanged = [];
9294
9357
  const changed = [];
9295
9358
  for (const entry of recorded) {
9296
- const baselineEntry = baseline.recorded.find((a) => a.logicalId === entry.logicalId && a.path === entry.path);
9359
+ const baselineEntry = baseline.recorded.find((a) => a.logicalId === entry.logicalId && a.path === entry.path && a.resourceType === entry.resourceType);
9297
9360
  if (baselineEntry && baselineValueMatches(baselineEntry.value, entry.value)) unchanged.push(entry);
9298
9361
  else changed.push(entry);
9299
9362
  }
@@ -9339,9 +9402,16 @@ function applyBaseline(findings, baseline, opts = {}) {
9339
9402
  const liveId = opts.physicalIdByLogical?.get(logicalId);
9340
9403
  if (liveId !== void 0 && liveId !== recordedId) replacedLogical.add(logicalId);
9341
9404
  }
9405
+ const liveTypeByLogical = /* @__PURE__ */ new Map();
9406
+ for (const f of findings) if (f.resourceType) liveTypeByLogical.set(f.logicalId, f.resourceType);
9407
+ const isTypeVoid = (e) => {
9408
+ const liveType = liveTypeByLogical.get(e.logicalId);
9409
+ return liveType !== void 0 && liveType !== e.resourceType;
9410
+ };
9411
+ const entryMatches = (a, f) => a.logicalId === f.logicalId && a.path === f.path && a.resourceType === f.resourceType;
9342
9412
  for (const f of findings) {
9343
9413
  if (f.tier === "added") {
9344
- const entry = recorded.find((a) => a.logicalId === f.logicalId && a.path === f.path);
9414
+ const entry = recorded.find((a) => entryMatches(a, f));
9345
9415
  if (f.modelReadFailed) {
9346
9416
  if (entry) continue;
9347
9417
  kept.push({
@@ -9369,7 +9439,7 @@ function applyBaseline(findings, baseline, opts = {}) {
9369
9439
  kept.push(f);
9370
9440
  continue;
9371
9441
  }
9372
- const entry = replacedLogical.has(f.logicalId) ? void 0 : recorded.find((a) => a.logicalId === f.logicalId && a.path === f.path);
9442
+ const entry = replacedLogical.has(f.logicalId) ? void 0 : recorded.find((a) => entryMatches(a, f));
9373
9443
  if (entry && baselineValueMatches(entry.value, f.actual)) continue;
9374
9444
  if (entry) {
9375
9445
  const delta = identityArrayDelta(canonicalizeForCompare(entry.value), f.actual);
@@ -9407,8 +9477,13 @@ function applyBaseline(findings, baseline, opts = {}) {
9407
9477
  const promotedStale = [];
9408
9478
  const removedFromTemplate = [];
9409
9479
  const replacedStale = [];
9480
+ const typeMismatchStale = [];
9410
9481
  for (const a of recorded) {
9411
9482
  if (a.path === "") continue;
9483
+ if (isTypeVoid(a)) {
9484
+ typeMismatchStale.push(`${a.logicalId}.${a.path}`);
9485
+ continue;
9486
+ }
9412
9487
  if (replacedLogical.has(a.logicalId)) {
9413
9488
  replacedStale.push(`${a.logicalId}.${a.path}`);
9414
9489
  continue;
@@ -9440,6 +9515,7 @@ function applyBaseline(findings, baseline, opts = {}) {
9440
9515
  if (promotedStale.length > 0) opts.warn?.(formatPromotedStaleNote(promotedStale));
9441
9516
  if (removedFromTemplate.length > 0) opts.warn?.(formatRemovedFromTemplateNote(removedFromTemplate));
9442
9517
  if (replacedStale.length > 0) opts.warn?.(formatReplacedStaleNote(replacedStale));
9518
+ if (typeMismatchStale.length > 0) opts.warn?.(formatTypeMismatchStaleNote(typeMismatchStale));
9443
9519
  return kept;
9444
9520
  }
9445
9521
  /**
@@ -9469,6 +9545,16 @@ function formatReplacedStaleNote(paths) {
9469
9545
  const n = paths.length;
9470
9546
  return `note: ${n === 1 ? `baseline entry (${paths[0]}) was` : `${n} baseline entries were`} recorded against a resource since REPLACED by a deploy — re-run \`cdkrd record\`.`;
9471
9547
  }
9548
+ /**
9549
+ * #793: the folded one-line note for baseline entries whose logicalId now hosts a resource
9550
+ * of a DIFFERENT type (the id was recycled across a template refactor). The old-type entries
9551
+ * are void against the new resource. Mirror of formatReplacedStaleNote. Pure + exported for
9552
+ * unit tests.
9553
+ */
9554
+ function formatTypeMismatchStaleNote(paths) {
9555
+ const n = paths.length;
9556
+ return `note: ${n === 1 ? `baseline entry (${paths[0]}) was` : `${n} baseline entries were`} recorded against a resource whose logicalId now has a DIFFERENT type — re-run \`cdkrd record\`.`;
9557
+ }
9472
9558
  /** logicalId -> set of declared top-level keys, for applyBaseline's promotion check. */
9473
9559
  function declaredKeysByLogical(resources) {
9474
9560
  return new Map(resources.map((r) => [r.logicalId, new Set(Object.keys(r.declared))]));