cdk-real-drift 0.4.29 → 0.4.31

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
@@ -711,6 +711,10 @@ covers them. **If you never run `revert`, cdkrd needs no write permissions at al
711
711
  `DescribeType`; `DescribeStackResources` (the sibling-ownership check for the
712
712
  `added` tier — see the `added`-tier entry below); `ListExports` (only for
713
713
  templates using `Fn::ImportValue`)
714
+ - `sts:GetCallerIdentity` (resolves the current account so `check` can tell a stack
715
+ never deployed in THIS account from one deleted out of band, under the
716
+ multi-account baseline pattern — allowed for any valid credentials, so no policy
717
+ statement is normally required)
714
718
  - `cloudcontrol:GetResource`: Cloud Control invokes each type's own read handler,
715
719
  so it needs that type's read permissions (this is why `ReadOnlyAccess` is the
716
720
  simple answer). `revert` additionally uses `cloudcontrol:UpdateResource` /
package/dist/cli.js CHANGED
@@ -57,6 +57,7 @@ import { DescribeSignalingChannelCommand, DescribeStreamCommand, KinesisVideoCli
57
57
  import { DescribeDomainConfigCommand, OpenSearchClient, UpdateDomainConfigCommand } from "@aws-sdk/client-opensearch";
58
58
  import { GetWebACLCommand, UpdateWebACLCommand, WAFV2Client } from "@aws-sdk/client-wafv2";
59
59
  import { ConfigServiceClient, DescribeConfigRulesCommand, PutConfigRuleCommand } from "@aws-sdk/client-config-service";
60
+ import { GetCallerIdentityCommand, STSClient } from "@aws-sdk/client-sts";
60
61
 
61
62
  //#region src/exit.ts
62
63
  function drain(stream) {
@@ -1227,6 +1228,9 @@ const DYNAMIC_REFERENCE_RE = /^\{\{resolve:(ssm|ssm-secure|secretsmanager):[\s\S
1227
1228
  function isDynamicReference(v) {
1228
1229
  return DYNAMIC_REFERENCE_RE.test(v);
1229
1230
  }
1231
+ function checkResolvedDynamicRef(r) {
1232
+ return typeof r === "string" && isDynamicReference(r) ? UNRESOLVED : r;
1233
+ }
1230
1234
  function isScalarInterpolant(v) {
1231
1235
  const t = typeof v;
1232
1236
  return t === "string" || t === "number" || t === "boolean";
@@ -1241,7 +1245,7 @@ function resolve$1(node, ctx, inCondition = false) {
1241
1245
  const k = keys[0];
1242
1246
  const v = obj[k];
1243
1247
  switch (k) {
1244
- case "Ref": return resolveRef(String(v), ctx);
1248
+ case "Ref": return checkResolvedDynamicRef(resolveRef(String(v), ctx));
1245
1249
  case "Fn::Sub": return resolveSub(v, ctx);
1246
1250
  case "Fn::Base64": {
1247
1251
  const inner = resolve$1(v, ctx);
@@ -1276,7 +1280,7 @@ function resolve$1(node, ctx, inCondition = false) {
1276
1280
  const i = Number(idx);
1277
1281
  if (!Number.isInteger(i) || i < 0 || i >= arr.length) return UNRESOLVED;
1278
1282
  const sel = arr[i];
1279
- return sel === UNRESOLVED ? UNRESOLVED : sel;
1283
+ return sel === UNRESOLVED ? UNRESOLVED : checkResolvedDynamicRef(sel);
1280
1284
  }
1281
1285
  case "Fn::FindInMap": {
1282
1286
  if (!Array.isArray(v) || v.length < 3) return UNRESOLVED;
@@ -1286,11 +1290,11 @@ function resolve$1(node, ctx, inCondition = false) {
1286
1290
  });
1287
1291
  if (typeof mapName !== "string" || typeof topKey !== "string" || typeof secondKey !== "string") return UNRESOLVED;
1288
1292
  const val = ctx.mappings?.[mapName]?.[topKey]?.[secondKey];
1289
- if (val !== void 0) return val;
1293
+ if (val !== void 0) return checkResolvedDynamicRef(val);
1290
1294
  const fourth = v[3];
1291
1295
  if (fourth !== null && typeof fourth === "object" && !Array.isArray(fourth) && "DefaultValue" in fourth) {
1292
1296
  const def = resolve$1(fourth.DefaultValue, ctx);
1293
- return def === UNRESOLVED ? UNRESOLVED : def;
1297
+ return def === UNRESOLVED ? UNRESOLVED : checkResolvedDynamicRef(def);
1294
1298
  }
1295
1299
  return UNRESOLVED;
1296
1300
  }
@@ -1310,7 +1314,7 @@ function resolve$1(node, ctx, inCondition = false) {
1310
1314
  case "Fn::ImportValue": {
1311
1315
  const name = resolve$1(v, ctx);
1312
1316
  if (typeof name !== "string") return UNRESOLVED;
1313
- return name in ctx.exports ? ctx.exports[name] : UNRESOLVED;
1317
+ return name in ctx.exports ? checkResolvedDynamicRef(ctx.exports[name]) : UNRESOLVED;
1314
1318
  }
1315
1319
  case "Fn::GetAtt": return resolveGetAtt(v, ctx);
1316
1320
  case "Fn::Equals": {
@@ -10130,8 +10134,23 @@ const KNOWN_RECORDED_ENTRY_KEYS = new Set([
10130
10134
  "path",
10131
10135
  "value"
10132
10136
  ]);
10137
+ const RESERVED_DOS_DEVICE_NAMES = new Set([
10138
+ "CON",
10139
+ "PRN",
10140
+ "AUX",
10141
+ "NUL",
10142
+ ...Array.from({ length: 9 }, (_, i) => `COM${i + 1}`),
10143
+ ...Array.from({ length: 9 }, (_, i) => `LPT${i + 1}`)
10144
+ ]);
10145
+ function sanitizeStackNameComponent(stackName) {
10146
+ const firstDot = stackName.indexOf(".");
10147
+ const base = firstDot === -1 ? stackName : stackName.slice(0, firstDot);
10148
+ const rest = firstDot === -1 ? "" : stackName.slice(firstDot);
10149
+ if (RESERVED_DOS_DEVICE_NAMES.has(base.toUpperCase())) return `${base}_${rest}`;
10150
+ return stackName;
10151
+ }
10133
10152
  function baselinePath(stackName, accountId, region) {
10134
- return `.cdkrd/baselines/${stackName}.${accountId}.${region}.json`;
10153
+ return `.cdkrd/baselines/${sanitizeStackNameComponent(stackName)}.${accountId}.${region}.json`;
10135
10154
  }
10136
10155
  function hashTemplate(rawTemplate) {
10137
10156
  return "sha256:" + createHash("sha256").update(rawTemplate).digest("hex");
@@ -13787,7 +13806,10 @@ async function revertStack(p) {
13787
13806
  };
13788
13807
  }
13789
13808
  }
13790
- const ok = await confirm({ message: revertConfirmMessage(stackName, region, plan.items.reduce((n, i) => n + i.ops.length, 0), plan.notRevertable.length) });
13809
+ const ok = await confirm({
13810
+ message: revertConfirmMessage(stackName, region, plan.items.reduce((n, i) => n + i.ops.length, 0), plan.notRevertable.length),
13811
+ initialValue: false
13812
+ });
13791
13813
  if (isCancel(ok) || !ok) {
13792
13814
  out(style.note("aborted."));
13793
13815
  return {
@@ -14301,6 +14323,7 @@ function formatFinding(f, stackName = "") {
14301
14323
  const { a: base, b: act } = jPair(f.desired, f.actual);
14302
14324
  s += `\n baseline=${style.desired(base)}\n actual =${style.actual(act)}`;
14303
14325
  }
14326
+ else if (f.tier === "added" && f.actual !== void 0) s += `\n actual =${style.actual(j(f.actual))}`;
14304
14327
  else if (f.tier === "undeclared" && f.arrayDelta) s += formatArrayDelta(f.arrayDelta);
14305
14328
  else if (f.tier === "undeclared" || f.tier === "atDefault" || f.tier === "generated") s += `\n actual =${style.actual(j(f.actual))}`;
14306
14329
  if (f.hint) s += `\n ${style.note(`↳ ${sanitizeForTerminal(f.hint)}`)}`;
@@ -14495,8 +14518,16 @@ function safeSlice(s, start, end) {
14495
14518
  if (bs % 2 === 1) out = out.slice(0, -1);
14496
14519
  return out;
14497
14520
  }
14521
+ function symbolMarker(v) {
14522
+ return v === UNRESOLVED ? "⟨unresolved⟩" : `⟨${v.description ?? "symbol"}⟩`;
14523
+ }
14524
+ const symbolReplacer = (_k, value) => typeof value === "symbol" ? symbolMarker(value) : value;
14525
+ function stringifySymbolSafe(v) {
14526
+ if (typeof v === "symbol") return JSON.stringify(symbolMarker(v));
14527
+ return JSON.stringify(v, symbolReplacer);
14528
+ }
14498
14529
  function j(v) {
14499
- const s = JSON.stringify(v);
14530
+ const s = stringifySymbolSafe(v);
14500
14531
  return s && s.length > VALUE_CAP ? safeSlice(s, 0, VALUE_CAP) + "…" : s ?? String(v);
14501
14532
  }
14502
14533
  function sanitizeForTerminal(s) {
@@ -14513,8 +14544,8 @@ function isBidiOrZeroWidth(code) {
14513
14544
  return code >= 8234 && code <= 8238 || code >= 8294 && code <= 8297 || code >= 8203 && code <= 8207 || code === 65279;
14514
14545
  }
14515
14546
  function jPair(a, b) {
14516
- const as = JSON.stringify(a) ?? String(a);
14517
- const bs = JSON.stringify(b) ?? String(b);
14547
+ const as = stringifySymbolSafe(a) ?? String(a);
14548
+ const bs = stringifySymbolSafe(b) ?? String(b);
14518
14549
  if (as.length <= VALUE_CAP && bs.length <= VALUE_CAP) return {
14519
14550
  a: as,
14520
14551
  b: bs
@@ -15102,10 +15133,19 @@ function finalCheckExit(code, fail) {
15102
15133
  * A case-sensitive `readdirSync` prefix would MISS a baseline committed as `mystack.….json`
15103
15134
  * when checking `MyStack`, silently downgrading a genuinely deleted stack to a benign skip.
15104
15135
  *
15136
+ * #1046: when the CURRENT account is known (resolved via `resolveCallerAccount`), PIN the
15137
+ * filename's account segment too — else a same-named stack that lives in ANOTHER account
15138
+ * (the common `env: { account: PERSONAL || SHARED }` CDK pattern, which the baseline filename
15139
+ * design explicitly serves) but was never deployed in THIS account is falsely reported
15140
+ * "deleted out of band" (multi-account CI goes permanently red the moment the other account's
15141
+ * baseline is committed). The account is only wildcarded when it genuinely cannot be resolved
15142
+ * (an STS failure), preserving today's behavior as a fallback — the account axis twin of the
15143
+ * region pin the #942 fix applied.
15144
+ *
15105
15145
  * The baselines directory is derived from `baselinePath` so the layout stays single-sourced.
15106
15146
  * Pure (filesystem-only, no AWS) + exported for unit tests. Missing dir → false.
15107
15147
  */
15108
- function hasBaselineForStack(stackName, region) {
15148
+ function hasBaselineForStack(stackName, region, accountId) {
15109
15149
  const dir = dirname(baselinePath(stackName, "a", "r"));
15110
15150
  let entries;
15111
15151
  try {
@@ -15113,13 +15153,32 @@ function hasBaselineForStack(stackName, region) {
15113
15153
  } catch {
15114
15154
  return false;
15115
15155
  }
15116
- const prefix = `${stackName}.`.toLowerCase();
15156
+ const prefix = (accountId ? `${stackName}.${accountId}.` : `${stackName}.`).toLowerCase();
15117
15157
  const suffix = `.${region}.json`.toLowerCase();
15118
15158
  return entries.some((f) => {
15119
15159
  const lower = f.toLowerCase();
15120
15160
  return lower.startsWith(prefix) && lower.endsWith(suffix);
15121
15161
  });
15122
15162
  }
15163
+ /**
15164
+ * #1046: resolve the CURRENT account (region-free `sts:GetCallerIdentity`, a zero-permission
15165
+ * call) so `hasBaselineForStack` can pin the baseline filename's account segment. Returns
15166
+ * `undefined` on ANY failure (expired creds, blocked STS, network) so the caller falls back
15167
+ * to today's account-wildcard rather than erroring — a `check` that reached the not-deployed
15168
+ * catch already has working creds, so the STS call almost always succeeds. `--profile` is
15169
+ * already in `process.env.AWS_PROFILE` (set by the verb entry points), so the default
15170
+ * credential chain resolves it. Exported for unit tests.
15171
+ */
15172
+ async function resolveCallerAccount(region) {
15173
+ try {
15174
+ return (await new STSClient({
15175
+ region,
15176
+ ...CLIENT_TIMEOUTS
15177
+ }).send(new GetCallerIdentityCommand({}))).Account;
15178
+ } catch {
15179
+ return;
15180
+ }
15181
+ }
15123
15182
  function safeStringifyJson(value) {
15124
15183
  const seen = /* @__PURE__ */ new WeakSet();
15125
15184
  const replacer = (_key, v) => {
@@ -15205,6 +15264,8 @@ async function runCheck(args) {
15205
15264
  };
15206
15265
  const separate = stackSeparator();
15207
15266
  const showProgress = !a.json && isInteractive();
15267
+ let callerAccount;
15268
+ let callerAccountResolved = false;
15208
15269
  for (const [idx, { stackName, region, template }] of stacks.entries()) {
15209
15270
  if (!region) {
15210
15271
  const msg = "no region — set env on the stack, pass --region, or set a region for the AWS profile";
@@ -15320,7 +15381,11 @@ async function runCheck(args) {
15320
15381
  worst = Math.max(worst, strictCoverageExit(a.strict, gathered.findings, desired.resources));
15321
15382
  } catch (e) {
15322
15383
  if (isStackNotDeployed(e)) {
15323
- if (hasBaselineForStack(stackName, region)) {
15384
+ if (!callerAccountResolved) {
15385
+ callerAccount = await resolveCallerAccount(region);
15386
+ callerAccountResolved = true;
15387
+ }
15388
+ if (hasBaselineForStack(stackName, region, callerAccount)) {
15324
15389
  const msg = "deployed baseline exists but the stack is gone — deleted out of band";
15325
15390
  const label = `${stackName} (${region})`;
15326
15391
  console.error(`[Drift] ${label}: ${msg}`);