From afd2d224f4db0706f47bfa80263258bbc70c7dd3 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Thu, 25 Jan 2024 10:22:29 -0600 Subject: [PATCH] feat(sns): suffix matching to sub filter (#28795) Closes #28765. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../aws-cdk-sns-lambda.assets.json | 6 +- .../aws-cdk-sns-lambda.template.json | 12 ++ .../test/integ.sns-lambda.js.snapshot/cdk.out | 2 +- .../integ.sns-lambda.js.snapshot/integ.json | 2 +- .../manifest.json | 5 +- .../integ.sns-lambda.js.snapshot/tree.json | 150 ++++++++++-------- .../test/integ.sns-lambda.ts | 2 + .../aws-sns-subscriptions/test/subs.test.ts | 14 ++ packages/aws-cdk-lib/aws-sns/README.md | 1 + .../aws-sns/lib/subscription-filter.ts | 11 ++ .../aws-sns/test/subscription.test.ts | 3 + 11 files changed, 132 insertions(+), 76 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.assets.json index 332534b7ffa25..acc1b5ecd0363 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.assets.json @@ -1,7 +1,7 @@ { - "version": "34.0.0", + "version": "36.0.0", "files": { - "84decb7b7b95d94a486822637e6a08fe80a790b29dabe379981a00db167cbb19": { + "369a260069af1392144b0da8ddcee39bd20a7475ff7d6edb4def34bdf5bbb3e3": { "source": { "path": "aws-cdk-sns-lambda.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "84decb7b7b95d94a486822637e6a08fe80a790b29dabe379981a00db167cbb19.json", + "objectKey": "369a260069af1392144b0da8ddcee39bd20a7475ff7d6edb4def34bdf5bbb3e3.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.template.json index 09d741583f852..f93ff86cb27ed 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.template.json @@ -215,6 +215,12 @@ }, { "prefix": "ye" + }, + { + "suffix": "ue" + }, + { + "suffix": "ow" } ], "size": [ @@ -326,6 +332,12 @@ }, { "prefix": "ye" + }, + { + "suffix": "ue" + }, + { + "suffix": "ow" } ] } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/cdk.out index 2313ab5436501..1f0068d32659a 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/cdk.out +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"34.0.0"} \ No newline at end of file +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/integ.json index a3a067f056f7d..e2eb612daba55 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/integ.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/integ.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "36.0.0", "testCases": { "integ.sns-lambda": { "stacks": [ diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/manifest.json index 31054ad438fca..23bb591eec4cc 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/manifest.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "36.0.0", "artifacts": { "aws-cdk-sns-lambda.assets": { "type": "cdk:asset-manifest", @@ -14,10 +14,11 @@ "environment": "aws://unknown-account/unknown-region", "properties": { "templateFile": "aws-cdk-sns-lambda.template.json", + "terminationProtection": false, "validateOnSynth": false, "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", - "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/84decb7b7b95d94a486822637e6a08fe80a790b29dabe379981a00db167cbb19.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/369a260069af1392144b0da8ddcee39bd20a7475ff7d6edb4def34bdf5bbb3e3.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/tree.json index 86180d2f54ebe..1d30a4bd7072e 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/tree.json @@ -20,14 +20,14 @@ "aws:cdk:cloudformation:props": {} }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.CfnTopic", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.Topic", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Echo": { @@ -42,8 +42,8 @@ "id": "ImportServiceRole", "path": "aws-cdk-sns-lambda/Echo/ServiceRole/ImportServiceRole", "constructInfo": { - "fqn": "aws-cdk-lib.Resource", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Resource": { @@ -81,14 +81,14 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnRole", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Role", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Resource": { @@ -111,8 +111,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "AllowInvoke:awscdksnslambdaMyTopic6C62AB90": { @@ -135,8 +135,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "MyTopic": { @@ -170,20 +170,20 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.CfnSubscription", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.Subscription", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.Function", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "DeadLetterQueue": { @@ -198,8 +198,8 @@ "aws:cdk:cloudformation:props": {} }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sqs.CfnQueue", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Policy": { @@ -245,20 +245,20 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sqs.CfnQueuePolicy", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sqs.QueuePolicy", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sqs.Queue", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Filtered": { @@ -273,8 +273,8 @@ "id": "ImportServiceRole", "path": "aws-cdk-sns-lambda/Filtered/ServiceRole/ImportServiceRole", "constructInfo": { - "fqn": "aws-cdk-lib.Resource", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Resource": { @@ -312,14 +312,14 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnRole", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Role", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Resource": { @@ -342,8 +342,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "AllowInvoke:awscdksnslambdaMyTopic6C62AB90": { @@ -366,8 +366,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "MyTopic": { @@ -394,6 +394,12 @@ }, { "prefix": "ye" + }, + { + "suffix": "ue" + }, + { + "suffix": "ow" } ], "size": [ @@ -422,20 +428,20 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.CfnSubscription", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.Subscription", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.Function", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "FilteredMessageBody": { @@ -450,8 +456,8 @@ "id": "ImportServiceRole", "path": "aws-cdk-sns-lambda/FilteredMessageBody/ServiceRole/ImportServiceRole", "constructInfo": { - "fqn": "aws-cdk-lib.Resource", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Resource": { @@ -489,14 +495,14 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnRole", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Role", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Resource": { @@ -519,8 +525,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "AllowInvoke:awscdksnslambdaMyTopic6C62AB90": { @@ -543,8 +549,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "MyTopic": { @@ -572,6 +578,12 @@ }, { "prefix": "ye" + }, + { + "suffix": "ue" + }, + { + "suffix": "ow" } ] } @@ -584,42 +596,42 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.CfnSubscription", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_sns.Subscription", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.Function", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "BootstrapVersion": { "id": "BootstrapVersion", "path": "aws-cdk-sns-lambda/BootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnParameter", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "CheckBootstrapVersion": { "id": "CheckBootstrapVersion", "path": "aws-cdk-sns-lambda/CheckBootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnRule", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Stack", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "Tree": { @@ -627,13 +639,13 @@ "path": "Tree", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.70" + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.App", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } } \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.ts index 0ecee019467de..6fb8c935f755f 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-sns-subscriptions/test/integ.sns-lambda.ts @@ -32,6 +32,7 @@ class SnsToLambda extends cdk.Stack { color: sns.SubscriptionFilter.stringFilter({ allowlist: ['red'], matchPrefixes: ['bl', 'ye'], + matchSuffixes: ['ue', 'ow'], }), size: sns.SubscriptionFilter.stringFilter({ denylist: ['small', 'medium'], @@ -54,6 +55,7 @@ class SnsToLambda extends cdk.Stack { color: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.stringFilter({ allowlist: ['red'], matchPrefixes: ['bl', 'ye'], + matchSuffixes: ['ue', 'ow'], })), }), }, diff --git a/packages/aws-cdk-lib/aws-sns-subscriptions/test/subs.test.ts b/packages/aws-cdk-lib/aws-sns-subscriptions/test/subs.test.ts index d4d6998fb6fcf..aa70888651178 100644 --- a/packages/aws-cdk-lib/aws-sns-subscriptions/test/subs.test.ts +++ b/packages/aws-cdk-lib/aws-sns-subscriptions/test/subs.test.ts @@ -1924,6 +1924,7 @@ test('with filter policy', () => { color: sns.SubscriptionFilter.stringFilter({ allowlist: ['red'], matchPrefixes: ['bl', 'ye'], + matchSuffixes: ['ue', 'ow'], }), size: sns.SubscriptionFilter.stringFilter({ denylist: ['small', 'medium'], @@ -1944,6 +1945,12 @@ test('with filter policy', () => { { 'prefix': 'ye', }, + { + 'suffix': 'ue', + }, + { + 'suffix': 'ow', + }, ], 'size': [ { @@ -1980,6 +1987,7 @@ test('with filter policy scope MessageBody', () => { background: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.stringFilter({ allowlist: ['red'], matchPrefixes: ['bl', 'ye'], + matchSuffixes: ['ue', 'ow'], })), }), size: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.stringFilter({ @@ -1999,6 +2007,12 @@ test('with filter policy scope MessageBody', () => { { 'prefix': 'ye', }, + { + 'suffix': 'ue', + }, + { + 'suffix': 'ow', + }, ], }, 'size': [ diff --git a/packages/aws-cdk-lib/aws-sns/README.md b/packages/aws-cdk-lib/aws-sns/README.md index dfde7e3312cba..656c743fdb864 100644 --- a/packages/aws-cdk-lib/aws-sns/README.md +++ b/packages/aws-cdk-lib/aws-sns/README.md @@ -70,6 +70,7 @@ myTopic.addSubscription(new subscriptions.LambdaSubscription(fn, { color: sns.SubscriptionFilter.stringFilter({ allowlist: ['red', 'orange'], matchPrefixes: ['bl'], + matchSuffixes: ['ue'], }), size: sns.SubscriptionFilter.stringFilter({ denylist: ['small', 'medium'], diff --git a/packages/aws-cdk-lib/aws-sns/lib/subscription-filter.ts b/packages/aws-cdk-lib/aws-sns/lib/subscription-filter.ts index 69be08bb1db26..fce85aa23fc32 100644 --- a/packages/aws-cdk-lib/aws-sns/lib/subscription-filter.ts +++ b/packages/aws-cdk-lib/aws-sns/lib/subscription-filter.ts @@ -35,6 +35,13 @@ export interface StringConditions { * @default - None */ readonly matchPrefixes?: string[]; + + /** + * Matches values that end with the specified suffixes. + * + * @default - None + */ + readonly matchSuffixes?: string[]; } /** @@ -144,6 +151,10 @@ export class SubscriptionFilter { conditions.push(...stringConditions.matchPrefixes.map(p => ({ prefix: p }))); } + if (stringConditions.matchSuffixes) { + conditions.push(...stringConditions.matchSuffixes.map(s => ({ suffix: s }))); + } + return new SubscriptionFilter(conditions); } diff --git a/packages/aws-cdk-lib/aws-sns/test/subscription.test.ts b/packages/aws-cdk-lib/aws-sns/test/subscription.test.ts index 6723a7fef79ea..193f530b27704 100644 --- a/packages/aws-cdk-lib/aws-sns/test/subscription.test.ts +++ b/packages/aws-cdk-lib/aws-sns/test/subscription.test.ts @@ -113,6 +113,7 @@ describe('Subscription', () => { allowlist: ['red', 'green'], denylist: ['white', 'orange'], matchPrefixes: ['bl', 'ye'], + matchSuffixes: ['ue', 'ow'], }), price: sns.SubscriptionFilter.numericFilter({ allowlist: [100, 200], @@ -137,6 +138,8 @@ describe('Subscription', () => { { 'anything-but': ['white', 'orange'] }, { prefix: 'bl' }, { prefix: 'ye' }, + { suffix: 'ue' }, + { suffix: 'ow' }, ], price: [ { numeric: ['=', 100] },