cdk-real-drift 0.28.1 → 0.28.2

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
@@ -4065,11 +4065,23 @@ function stripAsymmetricIdentityFields(declared, live) {
4065
4065
  const ORDER_SIGNIFICANT_ARRAY_KEYS = {
4066
4066
  "AWS::CodePipeline::Pipeline": /* @__PURE__ */ new Set(["Stages", "Actions"]),
4067
4067
  "AWS::Logs::Transformer": /* @__PURE__ */ new Set(["TransformerConfig"]),
4068
- "AWS::SSMContacts::Plan": /* @__PURE__ */ new Set(["Stages"])
4068
+ "AWS::SSMContacts::Plan": /* @__PURE__ */ new Set(["Stages"]),
4069
+ "AWS::Glue::Table": /* @__PURE__ */ new Set([
4070
+ "Columns",
4071
+ "PartitionKeys",
4072
+ "SortColumns"
4073
+ ]),
4074
+ "AWS::Glue::Partition": /* @__PURE__ */ new Set(["Columns", "SortColumns"])
4069
4075
  };
4070
4076
  const ORDER_SIGNIFICANT_PATHS = {
4071
4077
  "AWS::Scheduler::Schedule": /* @__PURE__ */ new Set(["Target.EcsParameters.PlacementStrategy"]),
4072
- "AWS::AppSync::Resolver": /* @__PURE__ */ new Set(["PipelineConfig.Functions"])
4078
+ "AWS::AppSync::Resolver": /* @__PURE__ */ new Set(["PipelineConfig.Functions"]),
4079
+ "AWS::Glue::Table": /* @__PURE__ */ new Set([
4080
+ "TableInput.StorageDescriptor.Columns",
4081
+ "TableInput.PartitionKeys",
4082
+ "TableInput.StorageDescriptor.SortColumns",
4083
+ "TableInput.StorageDescriptor.BucketColumns"
4084
+ ])
4073
4085
  };
4074
4086
  function canonicalizeTagListsDeep(v, orderSig) {
4075
4087
  return canonicalizeTagLists(v, orderSig, void 0);
@@ -5691,7 +5703,9 @@ function arnMatchesName(arn, name, opts) {
5691
5703
  const afterSlash = afterColon.includes("/") ? afterColon.slice(afterColon.lastIndexOf("/") + 1) : afterColon;
5692
5704
  const seg = arn.split(":");
5693
5705
  const colonId = seg.length >= 7 ? seg.slice(6).join(":") : afterColon;
5694
- if (name !== afterColon && name !== afterSlash && name !== colonId) return false;
5706
+ if (seg.length >= 8) {
5707
+ if (name !== colonId) return false;
5708
+ } else if (name !== afterColon && name !== afterSlash && name !== colonId) return false;
5695
5709
  const arnRegion = seg[3] ?? "";
5696
5710
  const arnAccount = seg[4] ?? "";
5697
5711
  if (opts?.region && arnRegion && arnRegion !== opts.region) return false;
@@ -5700,11 +5714,11 @@ function arnMatchesName(arn, name, opts) {
5700
5714
  }
5701
5715
  const KMS_KEY_ARN_RE = /^arn:aws[a-z-]*:kms:[^:]*:\d*:key\//;
5702
5716
  const keyIdOf$1 = (s) => s.slice(s.lastIndexOf("/") + 1);
5703
- function isManagedKmsAliasMatch(desired, actual, aliasTargets) {
5704
- if (typeof desired !== "string" || typeof actual !== "string" || !desired.startsWith("alias/aws/") || !KMS_KEY_ARN_RE.test(actual)) return false;
5717
+ function managedKmsAliasVerdict(desired, actual, aliasTargets) {
5718
+ if (typeof desired !== "string" || typeof actual !== "string" || !desired.startsWith("alias/aws/") || !KMS_KEY_ARN_RE.test(actual)) return void 0;
5705
5719
  const target = aliasTargets?.[desired];
5706
- if (target) return keyIdOf$1(actual) === keyIdOf$1(target);
5707
- return true;
5720
+ if (!target) return "unresolved";
5721
+ return keyIdOf$1(actual) === keyIdOf$1(target) ? "match" : "mismatch";
5708
5722
  }
5709
5723
  const LOG_GROUP_ARN_RE = /^arn:aws[a-z-]*:logs:[^:]*:\d*:log-group:/;
5710
5724
  const stripLogGroupWildcard = (s) => s.endsWith(":*") ? s.slice(0, -2) : s;
@@ -5811,11 +5825,11 @@ async function fetchManagedAliasTargets(region) {
5811
5825
  }
5812
5826
  }
5813
5827
  /** The one-line warning emitted when ListAliases is denied but the stack declares an
5814
- * AWS-managed alias: the managed-vs-customer key check degrades to shape-only, which
5815
- * is BLIND to a customer-managed key swapped in for an `alias/aws/*` default. Pure +
5828
+ * AWS-managed alias: the managed-vs-customer key check cannot run, so every
5829
+ * `alias/aws/*` vs live-key pair is reported unverified instead of compared. Pure +
5816
5830
  * exported so the wording is unit-tested. */
5817
5831
  function kmsListAliasesDeniedWarning(region) {
5818
- return `warning: ${region}: kms:ListAliases denied — managed-KMS-alias drift detection is degraded. A customer-managed key swapped in for an AWS-managed default (alias/aws/*) will NOT be detected (every alias/aws/* is treated as matching any live key). Grant kms:ListAliases for full coverage.`;
5832
+ return `warning: ${region}: kms:ListAliases denied — managed-KMS-alias drift detection is degraded. Whether a live key is still the AWS-managed default (alias/aws/*) cannot be verified, so those values are reported as unreadable / not compared. Grant kms:ListAliases for full coverage.`;
5819
5833
  }
5820
5834
  /** The one-line warning emitted when ListAliases fails TRANSIENTLY (throttle / network
5821
5835
  * blip / 5xx) for a stack that declares an AWS-managed alias: the managed-vs-customer
@@ -5859,16 +5873,16 @@ const keyIdOf = (s) => s.slice(s.lastIndexOf("/") + 1);
5859
5873
  /** #704 fold decision for an UNDECLARED managed-service-key nested path (DynamoDB SSE,
5860
5874
  * OpenSearch EncryptionAtRest). Returns whether the value-independent fold should still
5861
5875
  * apply for this live value:
5862
- * - `true` → FOLD (atDefault): the live key IS the account/region AWS-managed key, OR the
5863
- * alias could not be resolved (fail OPEN — no ListAliases / denied / transient).
5876
+ * - `true` → FOLD (atDefault): the live key IS the account/region AWS-managed key.
5864
5877
  * - `false` → SURFACE (undeclared): the live key is some OTHER key (a customer-managed key
5865
- * swapped in out of band — a real, security-relevant drift).
5878
+ * swapped in out of band — a real, security-relevant drift), OR the alias could
5879
+ * not be resolved (no ListAliases / denied / transient; #1952).
5866
5880
  * Pure; `aliasTargets` is the resolved alias-name -> target-key-id map (empty when unresolved).*/
5867
5881
  function shouldFoldManagedServiceKey(resourceType, schemaPath, liveValue, aliasTargets) {
5868
5882
  const alias = MANAGED_KEY_ALIAS_PATHS[resourceType]?.[schemaPath];
5869
5883
  if (!alias) return false;
5870
5884
  const target = aliasTargets?.[alias];
5871
- if (!target) return true;
5885
+ if (!target) return false;
5872
5886
  if (typeof liveValue !== "string") return true;
5873
5887
  return keyIdOf(liveValue) === keyIdOf(target);
5874
5888
  }
@@ -5969,7 +5983,6 @@ function canonicalizeStatement(s) {
5969
5983
  const out = {};
5970
5984
  for (const k of Object.keys(s).sort()) {
5971
5985
  const v = s[k];
5972
- if (k === "Sid" && typeof v === "string" && /^\d+$/.test(v)) continue;
5973
5986
  if (ARRAYISH_KEYS.has(k)) out[k] = toSortedArray(v);
5974
5987
  else if (k === "Principal" || k === "NotPrincipal") out[k] = canonicalizePrincipal(v);
5975
5988
  else if (k === "Condition") out[k] = canonicalizeCondition(v);
@@ -5988,7 +6001,7 @@ const AWS_LOG_DELIVERY_ALLOWED_ACTIONS = /* @__PURE__ */ new Set([
5988
6001
  function isAwsManagedLogDeliveryStatement(s) {
5989
6002
  if (!isObj(s)) return false;
5990
6003
  const sid = s.Sid;
5991
- if (typeof sid !== "string" || !/^AWSLogDelivery/.test(sid)) return false;
6004
+ if (typeof sid !== "string" || !LOG_DELIVERY_SID_RE.test(sid)) return false;
5992
6005
  const principal = s.Principal;
5993
6006
  if (!isObj(principal)) return false;
5994
6007
  const svc = principal.Service;
@@ -5997,7 +6010,28 @@ function isAwsManagedLogDeliveryStatement(s) {
5997
6010
  const actionRaw = s.Action;
5998
6011
  const actions = Array.isArray(actionRaw) ? actionRaw : [actionRaw];
5999
6012
  if (actions.length === 0) return false;
6000
- return actions.every((a) => typeof a === "string" && AWS_LOG_DELIVERY_ALLOWED_ACTIONS.has(a));
6013
+ if (!actions.every((a) => typeof a === "string" && AWS_LOG_DELIVERY_ALLOWED_ACTIONS.has(a))) return false;
6014
+ return hasSourceCondition(s.Condition);
6015
+ }
6016
+ const SOURCE_CONDITION_KEYS = /* @__PURE__ */ new Set(["aws:sourceaccount", "aws:sourcearn"]);
6017
+ function hasSourceCondition(cond) {
6018
+ if (!isObj(cond)) return false;
6019
+ return Object.values(cond).some((body) => isObj(body) && Object.keys(body).some((k) => SOURCE_CONDITION_KEYS.has(k.toLowerCase())));
6020
+ }
6021
+ const LOG_DELIVERY_SID_RE = /^AWSLogDelivery/;
6022
+ /** #1952: the Sids of every `AWSLogDelivery*` statement a (declared) value carries, at any
6023
+ * depth and inside JSON-string policies too. Passed back as `keepLogDeliverySids` so a
6024
+ * DECLARED log-delivery statement is compared instead of subtracted on both sides. */
6025
+ function collectLogDeliverySids(v, out = /* @__PURE__ */ new Set()) {
6026
+ if (typeof v === "string") {
6027
+ const p = parsePolicyString(v);
6028
+ if (p) collectLogDeliverySids(p, out);
6029
+ } else if (Array.isArray(v)) for (const el of v) collectLogDeliverySids(el, out);
6030
+ else if (isObj(v)) {
6031
+ if ("Effect" in v && typeof v.Sid === "string" && LOG_DELIVERY_SID_RE.test(v.Sid)) out.add(v.Sid);
6032
+ for (const val of Object.values(v)) collectLogDeliverySids(val, out);
6033
+ }
6034
+ return out;
6001
6035
  }
6002
6036
  function canonicalizePrincipal(v) {
6003
6037
  if (isObj(v)) {
@@ -6014,13 +6048,22 @@ function normalizeAccountPrincipal(v) {
6014
6048
  }
6015
6049
  return v;
6016
6050
  }
6017
- function canonicalizePolicy(doc) {
6018
- const statements = (Array.isArray(doc.Statement) ? doc.Statement : [doc.Statement]).filter((s) => !isAwsManagedLogDeliveryStatement(s));
6051
+ const POLICY_GRAMMAR_KEYS = /* @__PURE__ */ new Set([
6052
+ "Version",
6053
+ "Id",
6054
+ "Statement"
6055
+ ]);
6056
+ const IAM_DEFAULT_POLICY_VERSION = "2008-10-17";
6057
+ function canonicalizePolicy(doc, keepLogDeliverySids) {
6058
+ const statements = (Array.isArray(doc.Statement) ? doc.Statement : [doc.Statement]).filter((s) => !isAwsManagedLogDeliveryStatement(s) || keepLogDeliverySids?.has(s.Sid) === true);
6019
6059
  const out = {
6020
6060
  ...doc,
6021
6061
  Statement: statements.map(canonicalizeStatement).sort(byJson)
6022
6062
  };
6023
- if (doc.Version === void 0) delete out.Version;
6063
+ if (doc.Version === void 0) {
6064
+ if (Object.keys(doc).every((k) => POLICY_GRAMMAR_KEYS.has(k))) out.Version = IAM_DEFAULT_POLICY_VERSION;
6065
+ else delete out.Version;
6066
+ }
6024
6067
  return out;
6025
6068
  }
6026
6069
  const OAI_USER_ARN_RE = /^arn:aws[a-z-]*:iam::cloudfront:user\/CloudFront Origin Access Identity (.+)$/;
@@ -6078,30 +6121,30 @@ function sortDeep(v) {
6078
6121
  * Walk a value, replacing policy-document subtrees with their canonical form and
6079
6122
  * embedded JSON-text strings with a canonical (sorted, minified) form.
6080
6123
  */
6081
- function normalizePoliciesDeep(v) {
6082
- return normWalk(v, false);
6124
+ function normalizePoliciesDeep(v, keepLogDeliverySids) {
6125
+ return normWalk(v, false, keepLogDeliverySids);
6083
6126
  }
6084
- function normWalk(v, freeForm) {
6127
+ function normWalk(v, freeForm, keep) {
6085
6128
  if (typeof v === "string") {
6086
6129
  if (freeForm) return v;
6087
6130
  const p = parsePolicyString(v);
6088
- if (p) return canonicalizePolicy(p);
6131
+ if (p) return canonicalizePolicy(p, keep);
6089
6132
  return canonicalizeJsonText(v);
6090
6133
  }
6091
- if (Array.isArray(v)) return v.map((el) => normWalk(el, freeForm));
6092
- if (!freeForm && isPolicyDoc(v)) return canonicalizePolicy(v);
6134
+ if (Array.isArray(v)) return v.map((el) => normWalk(el, freeForm, keep));
6135
+ if (!freeForm && isPolicyDoc(v)) return canonicalizePolicy(v, keep);
6093
6136
  if (isObj(v)) {
6094
6137
  const out = {};
6095
- for (const [k, val] of Object.entries(v)) out[k] = normWalk(val, freeForm || FREE_FORM_MAP_PARENTS.has(k));
6138
+ for (const [k, val] of Object.entries(v)) out[k] = normWalk(val, freeForm || FREE_FORM_MAP_PARENTS.has(k), keep);
6096
6139
  return out;
6097
6140
  }
6098
6141
  return v;
6099
6142
  }
6100
6143
 
6101
6144
  // src/normalize/pipeline.ts
6102
- function canonicalizeForCompare(v, resourceType) {
6145
+ function canonicalizeForCompare(v, resourceType, keepLogDeliverySids) {
6103
6146
  const orderSig = resourceType ? ORDER_SIGNIFICANT_ARRAY_KEYS[resourceType] : void 0;
6104
- const base = canonicalizeIpv6CidrsDeep(canonicalizeIdArraysDeep(canonicalizeTagListsDeep(normalizePoliciesDeep(v), orderSig)));
6147
+ const base = canonicalizeIpv6CidrsDeep(canonicalizeIdArraysDeep(canonicalizeTagListsDeep(normalizePoliciesDeep(v, keepLogDeliverySids), orderSig)));
6105
6148
  return resourceType === "AWS::WAFv2::WebACL" || resourceType === "AWS::WAFv2::RuleGroup" ? normalizeWafByteMatchDeep(base) : base;
6106
6149
  }
6107
6150
  function sortObjectArraysDeep(v) {
@@ -6994,7 +7037,7 @@ function sortUnorderedSetProps(model, resourceType, schemaObjectArrayKeys = [],
6994
7037
  }
6995
7038
  function normalizeLiveModel(liveRaw, schema, opts = {}) {
6996
7039
  const oaiMap = opts.oaiCanonicalIds ?? {};
6997
- const live = canonicalizeForCompare(rewriteOaiPrincipalsDeep(stripAwsTagsDeep(stripCcApiAwsManagedFields(liveRaw)), oaiMap), opts.resourceType);
7040
+ const live = canonicalizeForCompare(rewriteOaiPrincipalsDeep(stripAwsTagsDeep(stripCcApiAwsManagedFields(liveRaw)), oaiMap), opts.resourceType, opts.keepLogDeliverySids);
6998
7041
  deepStripPaths(live, schema.readOnlyPaths);
6999
7042
  deepStripPaths(live, schema.writeOnlyPaths);
7000
7043
  if (opts.resourceType) sortUnorderedSetProps(live, opts.resourceType, (schema.unorderedObjectArrayPaths ?? []).filter((p) => !p.includes(".")), schema.unorderedScalarPaths ?? []);
@@ -7239,6 +7282,7 @@ function resolveParentObject(root, dottedPath) {
7239
7282
  return node;
7240
7283
  }
7241
7284
  const GETTEMPLATE_MASK_NOTE = "declared value unverifiable — CloudFormation GetTemplate masks non-ASCII characters as \"?\"";
7285
+ const KMS_ALIAS_UNRESOLVED_NOTE = "AWS-managed KMS alias unresolved (kms:ListAliases unavailable) — cannot confirm the live key is the managed key";
7242
7286
  function pushDeclaredFinding(findings, finding) {
7243
7287
  if (!deepEqual(finding.desired, finding.actual) && deepEqualModuloNonAsciiMask(finding.desired, finding.actual)) {
7244
7288
  findings.push({
@@ -7357,11 +7401,13 @@ function classifyResource(resource, liveRaw, schema, opts = {}) {
7357
7401
  const liveForCompare = structuredClone(liveRaw);
7358
7402
  const declaredForCompare = cloneDeepWithSymbols(declaredIn);
7359
7403
  stripAsymmetricIdentityFields(declaredForCompare, liveForCompare);
7404
+ const keepLogDeliverySids = collectLogDeliverySids(declaredForCompare);
7360
7405
  const live = subtractPropagatedStackTags(normalizeLiveModel(liveForCompare, schema, {
7361
7406
  oaiCanonicalIds: oaiMap,
7362
- resourceType
7407
+ resourceType,
7408
+ keepLogDeliverySids
7363
7409
  }), opts.stackTags ?? {}, declaredTagKeys(declaredForCompare));
7364
- let declared = canonicalizeForCompare(rewriteOaiPrincipalsDeep(declaredForCompare, oaiMap), resourceType);
7410
+ let declared = canonicalizeForCompare(rewriteOaiPrincipalsDeep(declaredForCompare, oaiMap), resourceType, keepLogDeliverySids);
7365
7411
  if (resourceType === "AWS::GuardDuty::Filter") declared = canonicalizeGuardDutyCriterionKeys(declared);
7366
7412
  const reflected = REFLECTED_CHILD_PROPS[resourceType];
7367
7413
  if (reflected && !(reflected in declared)) delete live[reflected];
@@ -7720,7 +7766,7 @@ function classifyResource(resource, liveRaw, schema, opts = {}) {
7720
7766
  }
7721
7767
  if (physicalId && opts.accountId !== void 0 && declared["Policy"] && declared["Policy"] !== UNRESOLVED && !hasUnresolved(declared["Policy"])) {
7722
7768
  const arnPrefix = `arn:${opts.region !== void 0 ? partitionForRegion(opts.region).partition : "aws"}:execute-api:${opts.region ?? ""}:${opts.accountId}:${physicalId}`;
7723
- declared["Policy"] = canonicalizePolicy(expandExecuteApiResources(declared["Policy"], arnPrefix));
7769
+ declared["Policy"] = canonicalizePolicy(expandExecuteApiResources(declared["Policy"], arnPrefix), keepLogDeliverySids);
7724
7770
  }
7725
7771
  }
7726
7772
  if (resourceType === "AWS::Cognito::UserPool") {
@@ -8156,7 +8202,19 @@ function classifyResource(resource, liveRaw, schema, opts = {}) {
8156
8202
  }
8157
8203
  }
8158
8204
  if (isArnNameMatch(d.stateValue, d.awsValue, opts)) continue;
8159
- if (isManagedKmsAliasMatch(d.stateValue, d.awsValue, opts.kmsAliasTargets)) continue;
8205
+ const kmsVerdict = managedKmsAliasVerdict(d.stateValue, d.awsValue, opts.kmsAliasTargets);
8206
+ if (kmsVerdict === "match") continue;
8207
+ if (kmsVerdict === "unresolved") {
8208
+ findings.push({
8209
+ tier: "readGap",
8210
+ logicalId,
8211
+ resourceType,
8212
+ path: d.path,
8213
+ note: KMS_ALIAS_UNRESOLVED_NOTE,
8214
+ compareOnly: true
8215
+ });
8216
+ continue;
8217
+ }
8160
8218
  if (isLogGroupArnWildcardMatch(d.stateValue, d.awsValue)) continue;
8161
8219
  if (isStringlyEqualScalar(d.stateValue, d.awsValue)) continue;
8162
8220
  if (isStringlyEqualScalarArray(d.stateValue, d.awsValue)) continue;
@@ -17133,7 +17191,7 @@ function computeCompleteResources(allLogicalIds, findings, recorded, previousCom
17133
17191
  const blocked = /* @__PURE__ */ new Set();
17134
17192
  const blockedByIgnore = /* @__PURE__ */ new Set();
17135
17193
  for (const f of findings) {
17136
- if (f.tier === "skipped" || f.tier === "deleted" || f.tier === "readGap" && !f.writeOnly) blocked.add(f.logicalId);
17194
+ if (f.tier === "skipped" || f.tier === "deleted" || f.tier === "readGap" && !f.writeOnly && !f.compareOnly) blocked.add(f.logicalId);
17137
17195
  if (f.tier === "undeclared" && !recordedKeys.has(recordedKey(f))) blocked.add(f.logicalId);
17138
17196
  if (f.tier === "ignored" && f.ignoredFrom === "undeclared" && !recordedKeys.has(recordedKey(f))) blockedByIgnore.add(f.logicalId);
17139
17197
  }
@@ -18510,7 +18568,7 @@ async function desiredModel(type, ctx, ops) {
18510
18568
  const current = (reader && await reader(ctx)) ?? {};
18511
18569
  const aligned = current.PolicyDocument === void 0 ? current : {
18512
18570
  ...current,
18513
- PolicyDocument: canonicalizeForCompare(current.PolicyDocument)
18571
+ PolicyDocument: canonicalizeForCompare(current.PolicyDocument, void 0, collectLogDeliverySids(ctx.declared))
18514
18572
  };
18515
18573
  assertIndexedPriorsFresh(type, aligned, ops);
18516
18574
  return applyOps(aligned, ops);