cdk-real-drift 0.12.8 → 0.12.10

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
@@ -637,7 +637,11 @@ Elastic Beanstalk env-namespace `OptionSettings`, EC2 LaunchTemplate `UserData`
637
637
  are masked as `<redacted:NN chars>` in both text and `--json` output, so a rotated
638
638
  secret surfacing as drift does not leak its plaintext into CI logs. The finding
639
639
  still surfaces (detection is preserved) and the property/key name stays visible —
640
- only the value is hidden (#798).
640
+ only the value is hidden. The same protection covers persistence: `record` writes a
641
+ **hash** of these values into the git-committed baseline instead of the plaintext, so
642
+ the secret never lands in a committed file while a later change still re-surfaces as
643
+ drift (an unchanged one stays recorded). Older baselines holding a plaintext value keep
644
+ comparing correctly and migrate to the hashed form on the next `record` (#798).
641
645
 
642
646
  A **stack that errored or was skipped** before it could be checked still appears as an
643
647
  element so a consumer sees which stacks ran: it carries `"error": "<reason>"` alongside
@@ -873,6 +877,15 @@ covers them. **If you never run `revert`, cdkrd needs no write permissions at al
873
877
  re-surfaces as drift — otherwise invisible (the bundle location is write-only).
874
878
  It fetches the presigned S3 URL the API returns; without the permission (or on
875
879
  any fetch failure) the signal is skipped, never false-flagged.
880
+ - Optional: `lambda:GetFunction` records the code digest of an
881
+ `AWS::Lambda::Function` (`CodeSha256`) so an out-of-band code swap
882
+ (`aws lambda update-function-code`, a console "Deploy", a SAM/hotswap sync)
883
+ re-surfaces as drift — otherwise invisible (the function `Code` is write-only and
884
+ a later `cdk deploy` does not heal it). It folds silently on a clean deploy
885
+ (zero first-run noise), `record` snapshots it, and a later change re-surfaces as
886
+ "changed since record". The old code bytes are gone, so it is detect-only (not
887
+ revertable — redeploy with a forced code update). Without the permission the
888
+ signal is skipped, never false-flagged.
876
889
  - Optional: `kms:ListAliases` enables strict verification that a declared
877
890
  `alias/aws/*` key was not swapped for a customer-managed key. Without it that case
878
891
  is conservatively suppressed AND cdkrd prints a one-line warning per region (the
package/dist/cli.js CHANGED
@@ -24,7 +24,7 @@ import { DescribeServicesCommand, ECSClient, ListServicesCommand, UpdateServiceC
24
24
  import { DescribeMountTargetsCommand, EFSClient } from "@aws-sdk/client-efs";
25
25
  import { DescribeListenersCommand, DescribeRulesCommand, ElasticLoadBalancingV2Client, GetTrustStoreCaCertificatesBundleCommand, ModifyLoadBalancerAttributesCommand, ModifyTargetGroupAttributesCommand } from "@aws-sdk/client-elastic-load-balancing-v2";
26
26
  import { DescribeEventBusCommand, EventBridgeClient, ListRulesCommand, PutPermissionCommand } from "@aws-sdk/client-eventbridge";
27
- import { GetPolicyCommand, LambdaClient, ListAliasesCommand as ListAliasesCommand$1, ListEventSourceMappingsCommand, ListFunctionUrlConfigsCommand, ListVersionsByFunctionCommand } from "@aws-sdk/client-lambda";
27
+ import { GetFunctionCommand, GetPolicyCommand, LambdaClient, ListAliasesCommand as ListAliasesCommand$1, ListEventSourceMappingsCommand, ListFunctionUrlConfigsCommand, ListVersionsByFunctionCommand } from "@aws-sdk/client-lambda";
28
28
  import { ChangeResourceRecordSetsCommand, ListResourceRecordSetsCommand, Route53Client } from "@aws-sdk/client-route-53";
29
29
  import { GetTopicAttributesCommand, ListSubscriptionsByTopicCommand, SNSClient, SetTopicAttributesCommand } from "@aws-sdk/client-sns";
30
30
  import { ACMClient, DescribeCertificateCommand, ListTagsForCertificateCommand } from "@aws-sdk/client-acm";
@@ -2893,6 +2893,7 @@ const GENERATED_TOPLEVEL_PATHS = {
2893
2893
  "ScalableDimension"
2894
2894
  ]),
2895
2895
  "AWS::Lambda::Version": new Set(["CodeSha256"]),
2896
+ "AWS::Lambda::Function": new Set(["CodeSha256"]),
2896
2897
  "AWS::EFS::AccessPoint": new Set(["ClientToken"])
2897
2898
  };
2898
2899
  const DESCEND_UNDECLARED_OBJECT_PATHS = {
@@ -10357,6 +10358,15 @@ const supplementTrustStore = async ({ physicalId, region }) => {
10357
10358
  const hash = hashCaBundle(await resp.text());
10358
10359
  return hash !== void 0 ? { CaCertificatesBundleSha256: hash } : void 0;
10359
10360
  };
10361
+ const supplementLambdaFunction = async ({ physicalId, region }) => {
10362
+ const name = str$1(physicalId);
10363
+ if (!name) return void 0;
10364
+ const sha = str$1((await new LambdaClient({
10365
+ region,
10366
+ ...READ_RETRY
10367
+ }).send(new GetFunctionCommand({ FunctionName: name }))).Configuration?.CodeSha256);
10368
+ return sha !== void 0 ? { CodeSha256: sha } : void 0;
10369
+ };
10360
10370
  function projectLexPrompt(p) {
10361
10371
  const ps = p;
10362
10372
  if (!ps || typeof ps !== "object") return void 0;
@@ -10583,6 +10593,7 @@ const SDK_SUPPLEMENTS = {
10583
10593
  "AWS::Lex::Bot": supplementLexBot,
10584
10594
  "AWS::MSK::Configuration": supplementMskConfiguration,
10585
10595
  "AWS::ElasticLoadBalancingV2::TrustStore": supplementTrustStore,
10596
+ "AWS::Lambda::Function": supplementLambdaFunction,
10586
10597
  "AWS::SSM::Parameter": supplementSsmParameter,
10587
10598
  "AWS::ElastiCache::ReplicationGroup": supplementElastiCacheReplicationGroup,
10588
10599
  "AWS::ECS::Service": supplementEcsService,
@@ -11780,6 +11791,113 @@ function stackCountAnnouncement(stackNames) {
11780
11791
  return `note: checking ${stackNames.length} stack(s): ${stackNames.join(", ")}`;
11781
11792
  }
11782
11793
 
11794
+ //#endregion
11795
+ //#region src/report/redact.ts
11796
+ function maskPlaceholder(value) {
11797
+ if (typeof value === "string") return `<redacted:${value.length} chars>`;
11798
+ return "<redacted>";
11799
+ }
11800
+ const maskWhole = (value) => maskPlaceholder(value);
11801
+ const maskEntryValueField = (value) => {
11802
+ if (value && typeof value === "object" && !Array.isArray(value) && "Value" in value) return {
11803
+ ...value,
11804
+ Value: maskPlaceholder(value.Value)
11805
+ };
11806
+ return maskPlaceholder(value);
11807
+ };
11808
+ const maskMapValues = (value) => {
11809
+ if (value && typeof value === "object" && !Array.isArray(value)) {
11810
+ const out = {};
11811
+ for (const [k, v] of Object.entries(value)) out[k] = maskPlaceholder(v);
11812
+ return out;
11813
+ }
11814
+ return maskPlaceholder(value);
11815
+ };
11816
+ const REDACTED_VALUE_PATHS = {
11817
+ "AWS::Lambda::Function": [{
11818
+ pathRe: /(^|\.)Environment\.Variables\./,
11819
+ redact: maskWhole
11820
+ }, {
11821
+ pathRe: /(^|\.)Environment\.Variables$/,
11822
+ redact: maskMapValues
11823
+ }],
11824
+ "AWS::CodeBuild::Project": [{
11825
+ pathRe: /(^|\.)Environment\.EnvironmentVariables\.\d+\.Value$/,
11826
+ redact: maskWhole
11827
+ }],
11828
+ "AWS::EC2::LaunchTemplate": [{
11829
+ pathRe: /(^|\.)LaunchTemplateData\.UserData$/,
11830
+ redact: maskWhole
11831
+ }],
11832
+ "AWS::ElasticBeanstalk::Environment": [{
11833
+ pathRe: /(^|\.)OptionSettings\[aws:elasticbeanstalk:application:environment\|/,
11834
+ redact: maskEntryValueField
11835
+ }]
11836
+ };
11837
+ function redactValue(resourceType, path, value) {
11838
+ const matchers = REDACTED_VALUE_PATHS[resourceType];
11839
+ if (!matchers) return value;
11840
+ if (isRedactedHashSentinel(value)) return "<redacted>";
11841
+ for (const m of matchers) if (m.pathRe.test(path)) return m.redact(value);
11842
+ return value;
11843
+ }
11844
+ function isRedactedPath(resourceType, path) {
11845
+ const matchers = REDACTED_VALUE_PATHS[resourceType];
11846
+ return !!matchers && matchers.some((m) => m.pathRe.test(path));
11847
+ }
11848
+ function redactFinding(f) {
11849
+ if (!isRedactedPath(f.resourceType, f.path)) return f;
11850
+ const out = { ...f };
11851
+ if ("desired" in f && f.desired !== void 0) out.desired = redactValue(f.resourceType, f.path, f.desired);
11852
+ if ("actual" in f && f.actual !== void 0) out.actual = redactValue(f.resourceType, f.path, f.actual);
11853
+ if (f.arrayDelta && typeof f.arrayDelta === "object") out.arrayDelta = redactArrayDelta(f.resourceType, f.path, f.arrayDelta);
11854
+ return out;
11855
+ }
11856
+ function redactArrayDelta(resourceType, path, d) {
11857
+ const mv = (v) => redactValue(resourceType, path, v);
11858
+ return {
11859
+ identityField: d.identityField,
11860
+ added: d.added.map((a) => ({
11861
+ id: a.id,
11862
+ value: mv(a.value)
11863
+ })),
11864
+ removed: d.removed.map((r) => ({
11865
+ id: r.id,
11866
+ value: mv(r.value)
11867
+ })),
11868
+ changed: d.changed.map((c) => ({
11869
+ id: c.id,
11870
+ recorded: mv(c.recorded),
11871
+ actual: mv(c.actual)
11872
+ }))
11873
+ };
11874
+ }
11875
+ const REDACTION_HASH_KEY = "__cdkrdRedactedSha256__";
11876
+ function stableStringify(v) {
11877
+ const sortDeep = (x) => {
11878
+ if (Array.isArray(x)) return x.map(sortDeep);
11879
+ if (x && typeof x === "object") {
11880
+ const out = {};
11881
+ for (const k of Object.keys(x).sort()) out[k] = sortDeep(x[k]);
11882
+ return out;
11883
+ }
11884
+ return x;
11885
+ };
11886
+ return JSON.stringify(sortDeep(v)) ?? "null";
11887
+ }
11888
+ function hashOfCanonical(canonicalValue) {
11889
+ return "sha256:" + createHash("sha256").update(stableStringify(canonicalValue)).digest("hex");
11890
+ }
11891
+ function redactedHashSentinel(canonicalValue) {
11892
+ return { [REDACTION_HASH_KEY]: hashOfCanonical(canonicalValue) };
11893
+ }
11894
+ function isRedactedHashSentinel(v) {
11895
+ return !!v && typeof v === "object" && !Array.isArray(v) && Object.keys(v).length === 1 && typeof v[REDACTION_HASH_KEY] === "string";
11896
+ }
11897
+ function redactedHashOf(sentinelOrCanonical) {
11898
+ return isRedactedHashSentinel(sentinelOrCanonical) ? sentinelOrCanonical[REDACTION_HASH_KEY] ?? hashOfCanonical(sentinelOrCanonical) : hashOfCanonical(sentinelOrCanonical);
11899
+ }
11900
+
11783
11901
  //#endregion
11784
11902
  //#region src/baseline/baseline-file.ts
11785
11903
  const KNOWN_BASELINE_KEYS = new Set([
@@ -12071,17 +12189,23 @@ function checkBaselineAccount(baseline, currentAccountId, stackName, warn = cons
12071
12189
  }
12072
12190
  if (currentAccountId && baseline.accountId !== currentAccountId) throw new Error(`baseline file for ${stackName} was captured in account ${baseline.accountId}, but the current account is ${currentAccountId} (the file was likely copied or renamed to the wrong account path). Baselines are per-account — run \`cdkrd record\` to write this account's own baseline file, or restore the correct file from git.`);
12073
12191
  }
12192
+ const RECORDABLE_GENERATED_PATHS = { "AWS::Lambda::Function": new Set(["CodeSha256"]) };
12193
+ function isRecordableGenerated(f) {
12194
+ return f.tier === "generated" && (RECORDABLE_GENERATED_PATHS[f.resourceType]?.has(f.path) ?? false);
12195
+ }
12074
12196
  /** Build the recorded set from a check run's findings: undeclared PROPERTIES plus
12075
12197
  * out-of-band `added` RESOURCES (PR4 — `added` is the resource-granularity sibling of
12076
12198
  * undeclared, so `record` snapshots its full live model and a later change to it
12077
12199
  * surfaces as drift). An `added` entry has the synthesized child logicalId and an
12078
- * empty `path` (the whole resource is the value); `recordedKey` keeps it unique. */
12200
+ * empty `path` (the whole resource is the value); `recordedKey` keeps it unique.
12201
+ * Plus RECORDABLE_GENERATED_PATHS: a `generated` content-hash signal (Lambda
12202
+ * CodeSha256, #646) that folds first-run yet must be recorded so a later swap is seen. */
12079
12203
  function buildRecorded(findings) {
12080
- return findings.filter((f) => f.tier === "undeclared" || f.tier === "added").filter((f) => !f.modelReadFailed).map((f) => ({
12204
+ return findings.filter((f) => f.tier === "undeclared" || f.tier === "added" || isRecordableGenerated(f)).filter((f) => !f.modelReadFailed).map((f) => ({
12081
12205
  logicalId: f.logicalId,
12082
12206
  resourceType: f.resourceType,
12083
12207
  path: f.path,
12084
- value: f.actual
12208
+ value: isRedactedPath(f.resourceType, f.path) ? redactedHashSentinel(canonicalizeBaselineForCompare(f.actual, f.resourceType)) : f.actual
12085
12209
  }));
12086
12210
  }
12087
12211
  /** Stable key uniquely identifying an undeclared finding / recorded entry, for
@@ -12191,6 +12315,10 @@ function carryForwardIgnored(recorded, existing, findings) {
12191
12315
  * order-significant declared array, so an order-insensitive sort is safe here.
12192
12316
  */
12193
12317
  function baselineValueMatches(baselineValue, currentCanonicalValue, resourceType) {
12318
+ if (isRedactedHashSentinel(baselineValue) || isRedactedHashSentinel(currentCanonicalValue)) {
12319
+ const asHash = (v) => redactedHashOf(isRedactedHashSentinel(v) ? v : canonicalizeBaselineForCompare(v, resourceType));
12320
+ return asHash(baselineValue) === asHash(currentCanonicalValue);
12321
+ }
12194
12322
  return deepEqual(canonicalizeBaselineForCompare(baselineValue, resourceType), canonicalizeBaselineForCompare(currentCanonicalValue, resourceType));
12195
12323
  }
12196
12324
  const DELTA_IDENTITY_FIELDS = [
@@ -14532,7 +14660,10 @@ function resolveSdkWriter(resourceType, ops) {
14532
14660
  //#endregion
14533
14661
  //#region src/revert/plan.ts
14534
14662
  const WRITEONLY_NESTED_NO_CC_REVERT = {};
14535
- const SYNTHETIC_READ_SIGNAL_PATHS = { "AWS::ElasticLoadBalancingV2::TrustStore": ["CaCertificatesBundleSha256"] };
14663
+ const SYNTHETIC_READ_SIGNAL_PATHS = {
14664
+ "AWS::ElasticLoadBalancingV2::TrustStore": ["CaCertificatesBundleSha256"],
14665
+ "AWS::Lambda::Function": ["CodeSha256"]
14666
+ };
14536
14667
  const CC_REVERTABLE_DESPITE_READ_OVERRIDE = new Set(["AWS::Scheduler::Schedule", "AWS::Cognito::IdentityPool"]);
14537
14668
  const REVERT_SET_DEFAULT_PATHS = new Set([
14538
14669
  "AWS::IAM::Role\0MaxSessionDuration",
@@ -16455,87 +16586,6 @@ function annotateHints(findings) {
16455
16586
  });
16456
16587
  }
16457
16588
 
16458
- //#endregion
16459
- //#region src/report/redact.ts
16460
- function maskPlaceholder(value) {
16461
- if (typeof value === "string") return `<redacted:${value.length} chars>`;
16462
- return "<redacted>";
16463
- }
16464
- const maskWhole = (value) => maskPlaceholder(value);
16465
- const maskEntryValueField = (value) => {
16466
- if (value && typeof value === "object" && !Array.isArray(value) && "Value" in value) return {
16467
- ...value,
16468
- Value: maskPlaceholder(value.Value)
16469
- };
16470
- return maskPlaceholder(value);
16471
- };
16472
- const maskMapValues = (value) => {
16473
- if (value && typeof value === "object" && !Array.isArray(value)) {
16474
- const out = {};
16475
- for (const [k, v] of Object.entries(value)) out[k] = maskPlaceholder(v);
16476
- return out;
16477
- }
16478
- return maskPlaceholder(value);
16479
- };
16480
- const REDACTED_VALUE_PATHS = {
16481
- "AWS::Lambda::Function": [{
16482
- pathRe: /(^|\.)Environment\.Variables\./,
16483
- redact: maskWhole
16484
- }, {
16485
- pathRe: /(^|\.)Environment\.Variables$/,
16486
- redact: maskMapValues
16487
- }],
16488
- "AWS::CodeBuild::Project": [{
16489
- pathRe: /(^|\.)Environment\.EnvironmentVariables\.\d+\.Value$/,
16490
- redact: maskWhole
16491
- }],
16492
- "AWS::EC2::LaunchTemplate": [{
16493
- pathRe: /(^|\.)LaunchTemplateData\.UserData$/,
16494
- redact: maskWhole
16495
- }],
16496
- "AWS::ElasticBeanstalk::Environment": [{
16497
- pathRe: /(^|\.)OptionSettings\[aws:elasticbeanstalk:application:environment\|/,
16498
- redact: maskEntryValueField
16499
- }]
16500
- };
16501
- function redactValue(resourceType, path, value) {
16502
- const matchers = REDACTED_VALUE_PATHS[resourceType];
16503
- if (!matchers) return value;
16504
- for (const m of matchers) if (m.pathRe.test(path)) return m.redact(value);
16505
- return value;
16506
- }
16507
- function isRedactedPath(resourceType, path) {
16508
- const matchers = REDACTED_VALUE_PATHS[resourceType];
16509
- return !!matchers && matchers.some((m) => m.pathRe.test(path));
16510
- }
16511
- function redactFinding(f) {
16512
- if (!isRedactedPath(f.resourceType, f.path)) return f;
16513
- const out = { ...f };
16514
- if ("desired" in f && f.desired !== void 0) out.desired = redactValue(f.resourceType, f.path, f.desired);
16515
- if ("actual" in f && f.actual !== void 0) out.actual = redactValue(f.resourceType, f.path, f.actual);
16516
- if (f.arrayDelta && typeof f.arrayDelta === "object") out.arrayDelta = redactArrayDelta(f.resourceType, f.path, f.arrayDelta);
16517
- return out;
16518
- }
16519
- function redactArrayDelta(resourceType, path, d) {
16520
- const mv = (v) => redactValue(resourceType, path, v);
16521
- return {
16522
- identityField: d.identityField,
16523
- added: d.added.map((a) => ({
16524
- id: a.id,
16525
- value: mv(a.value)
16526
- })),
16527
- removed: d.removed.map((r) => ({
16528
- id: r.id,
16529
- value: mv(r.value)
16530
- })),
16531
- changed: d.changed.map((c) => ({
16532
- id: c.id,
16533
- recorded: mv(c.recorded),
16534
- actual: mv(c.actual)
16535
- }))
16536
- };
16537
- }
16538
-
16539
16589
  //#endregion
16540
16590
  //#region src/report/report.ts
16541
16591
  function stackNameFromHeader(header) {