cdk-real-drift 0.12.94 → 0.12.96

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
@@ -278,7 +278,9 @@ needs `--remove-unrecorded`).
278
278
  - **API Gateway REST**: resources, methods, authorizers, models, request
279
279
  validators, gateway responses, stages
280
280
  - **API Gateway V2** (HTTP / WebSocket): routes, integrations, authorizers, stages
281
- - **SNS**: topic subscriptions
281
+ - **SNS**: topic subscriptions, topic access policies (an out-of-band
282
+ `set-topic-attributes Policy` on a topic with no declared `AWS::SNS::TopicPolicy` —
283
+ the AWS-default policy every topic carries is folded, so only a custom policy surfaces)
282
284
  - **Lambda**: event source mappings, function URLs, aliases, versions,
283
285
  resource-policy statements (an out-of-band `add-permission`)
284
286
  - **EventBridge**: bus rules
@@ -988,7 +990,13 @@ covers them. **If you never run `revert`, cdkrd needs no write permissions at al
988
990
  `GetRequestValidators` / `GetGatewayResponses`; HTTP `GetRoutes` /
989
991
  `GetIntegrations` / `GetAuthorizers` / `GetStages` — every API Gateway read
990
992
  resolves to the single `apigateway:GET` action)
991
- - `AWS::SNS::Topic`: `sns:ListSubscriptionsByTopic`
993
+ - `AWS::SNS::Topic`: `sns:ListSubscriptionsByTopic`, `sns:GetTopicAttributes`
994
+ (the same action listed as an SDK override-reader permission above — here it
995
+ also enumerates a topic's out-of-band access policy as a child
996
+ `AWS::SNS::TopicPolicy`; the AWS-default policy every topic carries is folded, so
997
+ only a custom policy surfaces). Reverting a detected one also needs
998
+ `sns:SetTopicAttributes` (the SDK deleter restores the AWS-default policy — SNS
999
+ rejects an empty `Policy`, so it re-sets the default rather than clearing).
992
1000
  - `AWS::Lambda::Function`: `lambda:ListEventSourceMappings`,
993
1001
  `lambda:ListFunctionUrlConfigs`, `lambda:ListAliases`,
994
1002
  `lambda:ListVersionsByFunction`
package/dist/cli.js CHANGED
@@ -2829,7 +2829,10 @@ const KNOWN_DEFAULTS = {
2829
2829
  },
2830
2830
  "AWS::CodeStarConnections::Connection": { HostArn: "arn:aws:codestar-connections:us-west-2:000000000000:host/00000000-0000-0000-0000-000000000000" }
2831
2831
  };
2832
- const KNOWN_DEFAULT_ONE_OF = { "AWS::ApiGatewayV2::Integration": { TimeoutInMillis: [29e3, 3e4] } };
2832
+ const KNOWN_DEFAULT_ONE_OF = {
2833
+ "AWS::ApiGatewayV2::Integration": { TimeoutInMillis: [29e3, 3e4] },
2834
+ "AWS::SecurityHub::Hub": { ControlFindingGenerator: ["SECURITY_CONTROL", "STANDARD_CONTROL"] }
2835
+ };
2833
2836
  const KNOWN_DEFAULT_ONE_OF_PATHS = {
2834
2837
  "AWS::EKS::Cluster": { "KubernetesNetworkConfig.ServiceIpv4Cidr": ["10.100.0.0/16", "172.20.0.0/16"] },
2835
2838
  "AWS::S3::Bucket": { "LifecycleConfiguration.TransitionDefaultMinimumObjectSize": ["all_storage_classes_128K", "varies_by_storage_class"] }
@@ -5551,16 +5554,22 @@ const MEANINGFUL_WHEN_OFF = {
5551
5554
  "AWS::ElasticLoadBalancingV2::TargetGroup": { HealthCheckEnabled: ({ declared }) => declared["TargetType"] !== "lambda" }
5552
5555
  };
5553
5556
  const MEANINGFUL_WHEN_OFF_NESTED = { "AWS::CloudFront::Distribution": { "DistributionConfig.IPV6Enabled": () => true } };
5554
- const GUARDDUTY_DEFAULT_DISABLED_FEATURES = new Set(["AI_ANALYST"]);
5557
+ const GUARDDUTY_FEATURE_CREATION_STATUS = {
5558
+ AI_ANALYST: ["DISABLED"],
5559
+ RUNTIME_MONITORING: ["DISABLED", "ENABLED"],
5560
+ EKS_RUNTIME_MONITORING: ["DISABLED", "ENABLED"],
5561
+ EKS_ADDON_MANAGEMENT: ["DISABLED"],
5562
+ ECS_FARGATE_AGENT_MANAGEMENT: ["DISABLED"],
5563
+ EC2_AGENT_MANAGEMENT: ["DISABLED"]
5564
+ };
5555
5565
  function guardDutyFeaturesAllAtDefault(features) {
5556
5566
  const atDefault = (node) => {
5557
5567
  if (Array.isArray(node)) return node.every(atDefault);
5558
5568
  if (node !== null && typeof node === "object") {
5559
5569
  const rec = node;
5560
5570
  if ("Status" in rec) {
5561
- const name = typeof rec["Name"] === "string" ? rec["Name"] : "";
5562
- const defaultStatus = GUARDDUTY_DEFAULT_DISABLED_FEATURES.has(name) ? "DISABLED" : "ENABLED";
5563
- if (rec["Status"] !== defaultStatus) return false;
5571
+ const defaults = GUARDDUTY_FEATURE_CREATION_STATUS[typeof rec["Name"] === "string" ? rec["Name"] : ""] ?? ["ENABLED"];
5572
+ if (typeof rec["Status"] !== "string" || !defaults.includes(rec["Status"])) return false;
5564
5573
  }
5565
5574
  return Object.values(rec).every(atDefault);
5566
5575
  }
@@ -8223,18 +8232,102 @@ async function enumerateSnsTopicChildren(ctx) {
8223
8232
  if (!topicArn) return [];
8224
8233
  const declaredSubscriptionArns = [];
8225
8234
  for (const r of desired.resources) if (r.resourceType === "AWS::SNS::Subscription" && parentRefMatches(r.declared.TopicArn, topicArn) && r.physicalId) declaredSubscriptionArns.push(r.physicalId);
8235
+ let hasDeclaredPolicy = false;
8236
+ for (const r of desired.resources) {
8237
+ if (r.resourceType !== "AWS::SNS::TopicPolicy") continue;
8238
+ const topics = r.declared.Topics;
8239
+ if (Array.isArray(topics)) {
8240
+ if (topics.some((t) => t === topicArn)) hasDeclaredPolicy = true;
8241
+ else if (topics.some((t) => hasUnresolved(t))) hasDeclaredPolicy = true;
8242
+ } else if (topics !== void 0 && hasUnresolved(topics)) hasDeclaredPolicy = true;
8243
+ }
8244
+ const client = new SNSClient({
8245
+ region,
8246
+ ...READ_RETRY
8247
+ });
8248
+ const liveSubscriptions = (await pageSubscriptions(client, topicArn)).filter((s) => typeof s.SubscriptionArn === "string" && s.SubscriptionArn.startsWith("arn:")).map((s) => ({
8249
+ arn: s.SubscriptionArn,
8250
+ label: s.Protocol ? `${s.Protocol} ${s.Endpoint ?? ""}`.trim() : s.SubscriptionArn,
8251
+ endpoint: s.Endpoint,
8252
+ owner: s.Owner
8253
+ }));
8254
+ const livePolicy = await readSnsTopicPolicyDocument(client, topicArn);
8226
8255
  return diffSnsTopicChildren({
8227
8256
  declaredSubscriptionArns,
8228
- liveSubscriptions: (await pageSubscriptions(new SNSClient({
8229
- region,
8230
- ...READ_RETRY
8231
- }), topicArn)).filter((s) => typeof s.SubscriptionArn === "string" && s.SubscriptionArn.startsWith("arn:")).map((s) => ({
8232
- arn: s.SubscriptionArn,
8233
- label: s.Protocol ? `${s.Protocol} ${s.Endpoint ?? ""}`.trim() : s.SubscriptionArn,
8234
- endpoint: s.Endpoint,
8235
- owner: s.Owner
8236
- }))
8237
- });
8257
+ liveSubscriptions
8258
+ }).concat(diffSnsTopicPolicy({
8259
+ topicArn,
8260
+ hasDeclaredPolicy,
8261
+ livePolicy
8262
+ }));
8263
+ }
8264
+ const SNS_DEFAULT_POLICY_ACTIONS = [
8265
+ "SNS:GetTopicAttributes",
8266
+ "SNS:SetTopicAttributes",
8267
+ "SNS:AddPermission",
8268
+ "SNS:RemovePermission",
8269
+ "SNS:DeleteTopic",
8270
+ "SNS:Subscribe",
8271
+ "SNS:ListSubscriptionsByTopic",
8272
+ "SNS:Publish"
8273
+ ];
8274
+ function accountFromTopicArn(topicArn) {
8275
+ const seg = topicArn.split(":");
8276
+ return seg.length >= 6 && seg[4] ? seg[4] : void 0;
8277
+ }
8278
+ function isDefaultSnsTopicPolicy(policy, topicArn) {
8279
+ if (!policy) return false;
8280
+ const account = accountFromTopicArn(topicArn);
8281
+ if (!account) return false;
8282
+ const rawStmts = policy.Statement;
8283
+ const stmts = Array.isArray(rawStmts) ? rawStmts : rawStmts !== void 0 ? [rawStmts] : [];
8284
+ if (stmts.length !== 1) return false;
8285
+ const s = stmts[0];
8286
+ if (typeof s !== "object" || s === null || Array.isArray(s)) return false;
8287
+ const st = s;
8288
+ if (st.Effect !== "Allow") return false;
8289
+ const principal = st.Principal;
8290
+ if (typeof principal !== "object" || principal === null || Array.isArray(principal)) return false;
8291
+ if (principal.AWS !== "*") return false;
8292
+ const actions = Array.isArray(st.Action) ? st.Action : st.Action !== void 0 ? [st.Action] : [];
8293
+ if (actions.length !== SNS_DEFAULT_POLICY_ACTIONS.length) return false;
8294
+ const wantActions = new Set(SNS_DEFAULT_POLICY_ACTIONS);
8295
+ if (!actions.every((a) => typeof a === "string" && wantActions.has(a))) return false;
8296
+ const resources = Array.isArray(st.Resource) ? st.Resource : st.Resource !== void 0 ? [st.Resource] : [];
8297
+ if (resources.length !== 1 || resources[0] !== topicArn) return false;
8298
+ const cond = st.Condition;
8299
+ if (typeof cond !== "object" || cond === null || Array.isArray(cond)) return false;
8300
+ const strEq = cond.StringEquals;
8301
+ if (typeof strEq !== "object" || strEq === null || Array.isArray(strEq)) return false;
8302
+ if (Object.keys(strEq).length !== 1) return false;
8303
+ if (strEq["AWS:SourceOwner"] !== account) return false;
8304
+ return true;
8305
+ }
8306
+ function diffSnsTopicPolicy(input) {
8307
+ if (input.hasDeclaredPolicy) return [];
8308
+ if (!input.livePolicy) return [];
8309
+ if (isDefaultSnsTopicPolicy(input.livePolicy, input.topicArn)) return [];
8310
+ return [{
8311
+ resourceType: "AWS::SNS::TopicPolicy",
8312
+ identifier: input.topicArn,
8313
+ siblingLookupId: input.topicArn,
8314
+ label: `topic policy ${input.topicArn}`,
8315
+ live: {
8316
+ Topics: [input.topicArn],
8317
+ PolicyDocument: input.livePolicy
8318
+ }
8319
+ }];
8320
+ }
8321
+ async function readSnsTopicPolicyDocument(client, topicArn) {
8322
+ const policyJson = (await client.send(new GetTopicAttributesCommand({ TopicArn: topicArn }))).Attributes?.Policy;
8323
+ if (!policyJson) return void 0;
8324
+ let parsed;
8325
+ try {
8326
+ parsed = JSON.parse(policyJson);
8327
+ } catch {
8328
+ return;
8329
+ }
8330
+ return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : void 0;
8238
8331
  }
8239
8332
  function diffLambdaFunctionChildren(input) {
8240
8333
  const declared = new Set(input.declaredMappingIds);
@@ -13030,7 +13123,8 @@ function addedFinding(parent, c, read) {
13030
13123
  const CC_GET_UNSUPPORTED_ADDED_TYPES = new Set([
13031
13124
  "AWS::Route53::RecordSet",
13032
13125
  "AWS::SQS::QueuePolicy",
13033
- "AWS::SecretsManager::ResourcePolicy"
13126
+ "AWS::SecretsManager::ResourcePolicy",
13127
+ "AWS::SNS::TopicPolicy"
13034
13128
  ]);
13035
13129
  async function readAddedModel(cc, cfn, c, schemas, oaiCanonicalIds) {
13036
13130
  const normalizeWith = async (raw) => {
@@ -17234,11 +17328,47 @@ const deleteSecretsManagerResourcePolicy = async (ctx) => {
17234
17328
  ...CLIENT_TIMEOUTS
17235
17329
  }).send(new DeleteResourcePolicyCommand({ SecretId: ctx.physicalId }));
17236
17330
  };
17331
+ const deleteSnsTopicPolicy = async (ctx) => {
17332
+ const topicArn = ctx.physicalId;
17333
+ const seg = topicArn.split(":");
17334
+ const account = seg.length >= 6 ? seg[4] : void 0;
17335
+ if (!account) throw new Error("cannot resolve the owner account for the SNS TopicPolicy revert");
17336
+ const defaultPolicy = JSON.stringify({
17337
+ Version: "2008-10-17",
17338
+ Id: "__default_policy_ID",
17339
+ Statement: [{
17340
+ Sid: "__default_statement_ID",
17341
+ Effect: "Allow",
17342
+ Principal: { AWS: "*" },
17343
+ Action: [
17344
+ "SNS:GetTopicAttributes",
17345
+ "SNS:SetTopicAttributes",
17346
+ "SNS:AddPermission",
17347
+ "SNS:RemovePermission",
17348
+ "SNS:DeleteTopic",
17349
+ "SNS:Subscribe",
17350
+ "SNS:ListSubscriptionsByTopic",
17351
+ "SNS:Publish"
17352
+ ],
17353
+ Resource: topicArn,
17354
+ Condition: { StringEquals: { "AWS:SourceOwner": account } }
17355
+ }]
17356
+ });
17357
+ await new SNSClient({
17358
+ region: ctx.region,
17359
+ ...CLIENT_TIMEOUTS
17360
+ }).send(new SetTopicAttributesCommand({
17361
+ TopicArn: topicArn,
17362
+ AttributeName: "Policy",
17363
+ AttributeValue: defaultPolicy
17364
+ }));
17365
+ };
17237
17366
  const SDK_DELETERS = {
17238
17367
  "AWS::AppSync::ApiKey": deleteAppSyncApiKey,
17239
17368
  "AWS::Route53::RecordSet": deleteRoute53RecordSet,
17240
17369
  "AWS::SQS::QueuePolicy": deleteSqsQueuePolicy,
17241
- "AWS::SecretsManager::ResourcePolicy": deleteSecretsManagerResourcePolicy
17370
+ "AWS::SecretsManager::ResourcePolicy": deleteSecretsManagerResourcePolicy,
17371
+ "AWS::SNS::TopicPolicy": deleteSnsTopicPolicy
17242
17372
  };
17243
17373
 
17244
17374
  //#endregion