cdk-real-drift 0.12.94 → 0.12.95

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
@@ -8223,18 +8223,102 @@ async function enumerateSnsTopicChildren(ctx) {
8223
8223
  if (!topicArn) return [];
8224
8224
  const declaredSubscriptionArns = [];
8225
8225
  for (const r of desired.resources) if (r.resourceType === "AWS::SNS::Subscription" && parentRefMatches(r.declared.TopicArn, topicArn) && r.physicalId) declaredSubscriptionArns.push(r.physicalId);
8226
+ let hasDeclaredPolicy = false;
8227
+ for (const r of desired.resources) {
8228
+ if (r.resourceType !== "AWS::SNS::TopicPolicy") continue;
8229
+ const topics = r.declared.Topics;
8230
+ if (Array.isArray(topics)) {
8231
+ if (topics.some((t) => t === topicArn)) hasDeclaredPolicy = true;
8232
+ else if (topics.some((t) => hasUnresolved(t))) hasDeclaredPolicy = true;
8233
+ } else if (topics !== void 0 && hasUnresolved(topics)) hasDeclaredPolicy = true;
8234
+ }
8235
+ const client = new SNSClient({
8236
+ region,
8237
+ ...READ_RETRY
8238
+ });
8239
+ const liveSubscriptions = (await pageSubscriptions(client, topicArn)).filter((s) => typeof s.SubscriptionArn === "string" && s.SubscriptionArn.startsWith("arn:")).map((s) => ({
8240
+ arn: s.SubscriptionArn,
8241
+ label: s.Protocol ? `${s.Protocol} ${s.Endpoint ?? ""}`.trim() : s.SubscriptionArn,
8242
+ endpoint: s.Endpoint,
8243
+ owner: s.Owner
8244
+ }));
8245
+ const livePolicy = await readSnsTopicPolicyDocument(client, topicArn);
8226
8246
  return diffSnsTopicChildren({
8227
8247
  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
- });
8248
+ liveSubscriptions
8249
+ }).concat(diffSnsTopicPolicy({
8250
+ topicArn,
8251
+ hasDeclaredPolicy,
8252
+ livePolicy
8253
+ }));
8254
+ }
8255
+ const SNS_DEFAULT_POLICY_ACTIONS = [
8256
+ "SNS:GetTopicAttributes",
8257
+ "SNS:SetTopicAttributes",
8258
+ "SNS:AddPermission",
8259
+ "SNS:RemovePermission",
8260
+ "SNS:DeleteTopic",
8261
+ "SNS:Subscribe",
8262
+ "SNS:ListSubscriptionsByTopic",
8263
+ "SNS:Publish"
8264
+ ];
8265
+ function accountFromTopicArn(topicArn) {
8266
+ const seg = topicArn.split(":");
8267
+ return seg.length >= 6 && seg[4] ? seg[4] : void 0;
8268
+ }
8269
+ function isDefaultSnsTopicPolicy(policy, topicArn) {
8270
+ if (!policy) return false;
8271
+ const account = accountFromTopicArn(topicArn);
8272
+ if (!account) return false;
8273
+ const rawStmts = policy.Statement;
8274
+ const stmts = Array.isArray(rawStmts) ? rawStmts : rawStmts !== void 0 ? [rawStmts] : [];
8275
+ if (stmts.length !== 1) return false;
8276
+ const s = stmts[0];
8277
+ if (typeof s !== "object" || s === null || Array.isArray(s)) return false;
8278
+ const st = s;
8279
+ if (st.Effect !== "Allow") return false;
8280
+ const principal = st.Principal;
8281
+ if (typeof principal !== "object" || principal === null || Array.isArray(principal)) return false;
8282
+ if (principal.AWS !== "*") return false;
8283
+ const actions = Array.isArray(st.Action) ? st.Action : st.Action !== void 0 ? [st.Action] : [];
8284
+ if (actions.length !== SNS_DEFAULT_POLICY_ACTIONS.length) return false;
8285
+ const wantActions = new Set(SNS_DEFAULT_POLICY_ACTIONS);
8286
+ if (!actions.every((a) => typeof a === "string" && wantActions.has(a))) return false;
8287
+ const resources = Array.isArray(st.Resource) ? st.Resource : st.Resource !== void 0 ? [st.Resource] : [];
8288
+ if (resources.length !== 1 || resources[0] !== topicArn) return false;
8289
+ const cond = st.Condition;
8290
+ if (typeof cond !== "object" || cond === null || Array.isArray(cond)) return false;
8291
+ const strEq = cond.StringEquals;
8292
+ if (typeof strEq !== "object" || strEq === null || Array.isArray(strEq)) return false;
8293
+ if (Object.keys(strEq).length !== 1) return false;
8294
+ if (strEq["AWS:SourceOwner"] !== account) return false;
8295
+ return true;
8296
+ }
8297
+ function diffSnsTopicPolicy(input) {
8298
+ if (input.hasDeclaredPolicy) return [];
8299
+ if (!input.livePolicy) return [];
8300
+ if (isDefaultSnsTopicPolicy(input.livePolicy, input.topicArn)) return [];
8301
+ return [{
8302
+ resourceType: "AWS::SNS::TopicPolicy",
8303
+ identifier: input.topicArn,
8304
+ siblingLookupId: input.topicArn,
8305
+ label: `topic policy ${input.topicArn}`,
8306
+ live: {
8307
+ Topics: [input.topicArn],
8308
+ PolicyDocument: input.livePolicy
8309
+ }
8310
+ }];
8311
+ }
8312
+ async function readSnsTopicPolicyDocument(client, topicArn) {
8313
+ const policyJson = (await client.send(new GetTopicAttributesCommand({ TopicArn: topicArn }))).Attributes?.Policy;
8314
+ if (!policyJson) return void 0;
8315
+ let parsed;
8316
+ try {
8317
+ parsed = JSON.parse(policyJson);
8318
+ } catch {
8319
+ return;
8320
+ }
8321
+ return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : void 0;
8238
8322
  }
8239
8323
  function diffLambdaFunctionChildren(input) {
8240
8324
  const declared = new Set(input.declaredMappingIds);
@@ -13030,7 +13114,8 @@ function addedFinding(parent, c, read) {
13030
13114
  const CC_GET_UNSUPPORTED_ADDED_TYPES = new Set([
13031
13115
  "AWS::Route53::RecordSet",
13032
13116
  "AWS::SQS::QueuePolicy",
13033
- "AWS::SecretsManager::ResourcePolicy"
13117
+ "AWS::SecretsManager::ResourcePolicy",
13118
+ "AWS::SNS::TopicPolicy"
13034
13119
  ]);
13035
13120
  async function readAddedModel(cc, cfn, c, schemas, oaiCanonicalIds) {
13036
13121
  const normalizeWith = async (raw) => {
@@ -17234,11 +17319,47 @@ const deleteSecretsManagerResourcePolicy = async (ctx) => {
17234
17319
  ...CLIENT_TIMEOUTS
17235
17320
  }).send(new DeleteResourcePolicyCommand({ SecretId: ctx.physicalId }));
17236
17321
  };
17322
+ const deleteSnsTopicPolicy = async (ctx) => {
17323
+ const topicArn = ctx.physicalId;
17324
+ const seg = topicArn.split(":");
17325
+ const account = seg.length >= 6 ? seg[4] : void 0;
17326
+ if (!account) throw new Error("cannot resolve the owner account for the SNS TopicPolicy revert");
17327
+ const defaultPolicy = JSON.stringify({
17328
+ Version: "2008-10-17",
17329
+ Id: "__default_policy_ID",
17330
+ Statement: [{
17331
+ Sid: "__default_statement_ID",
17332
+ Effect: "Allow",
17333
+ Principal: { AWS: "*" },
17334
+ Action: [
17335
+ "SNS:GetTopicAttributes",
17336
+ "SNS:SetTopicAttributes",
17337
+ "SNS:AddPermission",
17338
+ "SNS:RemovePermission",
17339
+ "SNS:DeleteTopic",
17340
+ "SNS:Subscribe",
17341
+ "SNS:ListSubscriptionsByTopic",
17342
+ "SNS:Publish"
17343
+ ],
17344
+ Resource: topicArn,
17345
+ Condition: { StringEquals: { "AWS:SourceOwner": account } }
17346
+ }]
17347
+ });
17348
+ await new SNSClient({
17349
+ region: ctx.region,
17350
+ ...CLIENT_TIMEOUTS
17351
+ }).send(new SetTopicAttributesCommand({
17352
+ TopicArn: topicArn,
17353
+ AttributeName: "Policy",
17354
+ AttributeValue: defaultPolicy
17355
+ }));
17356
+ };
17237
17357
  const SDK_DELETERS = {
17238
17358
  "AWS::AppSync::ApiKey": deleteAppSyncApiKey,
17239
17359
  "AWS::Route53::RecordSet": deleteRoute53RecordSet,
17240
17360
  "AWS::SQS::QueuePolicy": deleteSqsQueuePolicy,
17241
- "AWS::SecretsManager::ResourcePolicy": deleteSecretsManagerResourcePolicy
17361
+ "AWS::SecretsManager::ResourcePolicy": deleteSecretsManagerResourcePolicy,
17362
+ "AWS::SNS::TopicPolicy": deleteSnsTopicPolicy
17242
17363
  };
17243
17364
 
17244
17365
  //#endregion