Skip to content

Commit 476aa46

Browse files
committed
chore(mixins-preview): integration test for Events
1 parent 3d6b204 commit 476aa46

File tree

50 files changed

+9153
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+9153
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
import * as cdk from 'aws-cdk-lib/core';
3+
import { Rule } from 'aws-cdk-lib/aws-events';
4+
import * as integ from '@aws-cdk/integ-tests-alpha';
5+
import { LambdaFunction } from 'aws-cdk-lib/aws-events-targets';
6+
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
7+
import type { Construct } from 'constructs';
8+
import { CfnKey, Key } from 'aws-cdk-lib/aws-kms';
9+
import { KeyEvents } from '../../../lib/services/aws-kms/events.generated';
10+
11+
class L1L2KeyWithL2Rule extends cdk.Stack {
12+
public constructor(scope: Construct, id: string, props: cdk.StackProps) {
13+
super(scope, id, props);
14+
15+
const l1Key = new CfnKey(this, 'KeyL1', {});
16+
const l1KeyWithEvent = KeyEvents.fromKey(l1Key);
17+
18+
const l2Key = new Key(this, 'KeyL2', {});
19+
const l2KeyWithEvent = KeyEvents.fromKey(l2Key);
20+
21+
const fn = new Function(this, 'MyFuncA', {
22+
runtime: Runtime.NODEJS_LATEST,
23+
handler: 'index.handler',
24+
code: Code.fromInline(`
25+
exports.handler = async (event) => {
26+
console.log("New Project event:", JSON.stringify(event, null, 2));
27+
return {};
28+
};
29+
`),
30+
});
31+
32+
const rule = new Rule(this, 'L2RuleForL1', {
33+
targets: [new LambdaFunction(fn)],
34+
});
35+
36+
rule.addEventPattern(l1KeyWithEvent.awsAPICallViaCloudTrailPattern({}));
37+
rule.addEventPattern(l2KeyWithEvent.awsAPICallViaCloudTrailPattern({ eventName: ['RotateKeyOnDemand'] }));
38+
}
39+
}
40+
41+
class L2KeyOtherEventsWithL2Rule extends cdk.Stack {
42+
public constructor(scope: Construct, id: string, props: cdk.StackProps) {
43+
super(scope, id, props);
44+
45+
const l2Key = new Key(this, 'Key', {});
46+
const l2KeyWithEvent = KeyEvents.fromKey(l2Key);
47+
48+
const fn = new Function(this, 'MyFuncA', {
49+
runtime: Runtime.NODEJS_LATEST,
50+
handler: 'index.handler',
51+
code: Code.fromInline(`
52+
exports.handler = async (event) => {
53+
console.log("New Project event:", JSON.stringify(event, null, 2));
54+
return {};
55+
};
56+
`),
57+
});
58+
59+
const rule = new Rule(this, 'L2RuleForL1', {
60+
targets: [new LambdaFunction(fn)],
61+
});
62+
63+
rule.addEventPattern(l2KeyWithEvent.kMSCMKDeletionPattern({}));
64+
rule.addEventPattern(l2KeyWithEvent.kMSCMKRotationPattern({}));
65+
}
66+
}
67+
68+
const app = new cdk.App();
69+
70+
const testStack1 = new L1L2KeyWithL2Rule(app, 'L1L2KeyWithL2Rule', { env: { region: 'us-east-1' } });
71+
const testStack2 = new L2KeyOtherEventsWithL2Rule(app, 'L2KeyOtherEventsWithL2Rule', { env: { region: 'us-east-1' } });
72+
73+
new integ.IntegTest(app, 'Key-Test', {
74+
testCases: [
75+
testStack1,
76+
testStack2,
77+
],
78+
});
79+

packages/@aws-cdk/mixins-preview/test/services/aws-kms/integ.events.test.ts.snapshot/KeyTestDefaultTestDeployAssert2D3BFAA3.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/mixins-preview/test/services/aws-kms/integ.events.test.ts.snapshot/KeyTestDefaultTestDeployAssert2D3BFAA3.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/mixins-preview/test/services/aws-kms/integ.events.test.ts.snapshot/L1L2KeyWithL2Rule.assets.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
{
2+
"Resources": {
3+
"KeyL1": {
4+
"Type": "AWS::KMS::Key"
5+
},
6+
"KeyL25E747C61": {
7+
"Type": "AWS::KMS::Key",
8+
"Properties": {
9+
"KeyPolicy": {
10+
"Statement": [
11+
{
12+
"Action": "kms:*",
13+
"Effect": "Allow",
14+
"Principal": {
15+
"AWS": {
16+
"Fn::Join": [
17+
"",
18+
[
19+
"arn:aws:iam::",
20+
{
21+
"Ref": "AWS::AccountId"
22+
},
23+
":root"
24+
]
25+
]
26+
}
27+
},
28+
"Resource": "*"
29+
}
30+
],
31+
"Version": "2012-10-17"
32+
}
33+
},
34+
"UpdateReplacePolicy": "Retain",
35+
"DeletionPolicy": "Retain"
36+
},
37+
"MyFuncAServiceRole9A674AAA": {
38+
"Type": "AWS::IAM::Role",
39+
"Properties": {
40+
"AssumeRolePolicyDocument": {
41+
"Statement": [
42+
{
43+
"Action": "sts:AssumeRole",
44+
"Effect": "Allow",
45+
"Principal": {
46+
"Service": "lambda.amazonaws.com"
47+
}
48+
}
49+
],
50+
"Version": "2012-10-17"
51+
},
52+
"ManagedPolicyArns": [
53+
{
54+
"Fn::Join": [
55+
"",
56+
[
57+
"arn:",
58+
{
59+
"Ref": "AWS::Partition"
60+
},
61+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
62+
]
63+
]
64+
}
65+
]
66+
}
67+
},
68+
"MyFuncAAF5F3089": {
69+
"Type": "AWS::Lambda::Function",
70+
"Properties": {
71+
"Code": {
72+
"ZipFile": "\nexports.handler = async (event) => {\n console.log(\"New Project event:\", JSON.stringify(event, null, 2));\n return {};\n};\n"
73+
},
74+
"Handler": "index.handler",
75+
"Role": {
76+
"Fn::GetAtt": [
77+
"MyFuncAServiceRole9A674AAA",
78+
"Arn"
79+
]
80+
},
81+
"Runtime": "nodejs22.x"
82+
},
83+
"DependsOn": [
84+
"MyFuncAServiceRole9A674AAA"
85+
]
86+
},
87+
"MyFuncALogGroupD7C330A5": {
88+
"Type": "AWS::Logs::LogGroup",
89+
"Properties": {
90+
"LogGroupName": {
91+
"Fn::Join": [
92+
"",
93+
[
94+
"/aws/lambda/",
95+
{
96+
"Ref": "MyFuncAAF5F3089"
97+
}
98+
]
99+
]
100+
},
101+
"RetentionInDays": 731
102+
},
103+
"UpdateReplacePolicy": "Retain",
104+
"DeletionPolicy": "Retain"
105+
},
106+
"L2RuleForL13AA6354B": {
107+
"Type": "AWS::Events::Rule",
108+
"Properties": {
109+
"EventPattern": {
110+
"source": [
111+
"aws.kms"
112+
],
113+
"detail-type": [
114+
"AWS API Call via CloudTrail"
115+
],
116+
"detail": {
117+
"requestParameters": {
118+
"keyId": [
119+
{
120+
"Ref": "KeyL1"
121+
},
122+
{
123+
"Ref": "KeyL25E747C61"
124+
}
125+
]
126+
},
127+
"eventName": [
128+
"RotateKeyOnDemand"
129+
]
130+
}
131+
},
132+
"State": "ENABLED",
133+
"Targets": [
134+
{
135+
"Arn": {
136+
"Fn::GetAtt": [
137+
"MyFuncAAF5F3089",
138+
"Arn"
139+
]
140+
},
141+
"Id": "Target0"
142+
}
143+
]
144+
}
145+
},
146+
"L2RuleForL1AllowEventRuleL1L2KeyWithL2RuleMyFuncAA680479253AB68ED": {
147+
"Type": "AWS::Lambda::Permission",
148+
"Properties": {
149+
"Action": "lambda:InvokeFunction",
150+
"FunctionName": {
151+
"Fn::GetAtt": [
152+
"MyFuncAAF5F3089",
153+
"Arn"
154+
]
155+
},
156+
"Principal": "events.amazonaws.com",
157+
"SourceArn": {
158+
"Fn::GetAtt": [
159+
"L2RuleForL13AA6354B",
160+
"Arn"
161+
]
162+
}
163+
}
164+
}
165+
},
166+
"Parameters": {
167+
"BootstrapVersion": {
168+
"Type": "AWS::SSM::Parameter::Value<String>",
169+
"Default": "/cdk-bootstrap/hnb659fds/version",
170+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
171+
}
172+
},
173+
"Rules": {
174+
"CheckBootstrapVersion": {
175+
"Assertions": [
176+
{
177+
"Assert": {
178+
"Fn::Not": [
179+
{
180+
"Fn::Contains": [
181+
[
182+
"1",
183+
"2",
184+
"3",
185+
"4",
186+
"5"
187+
],
188+
{
189+
"Ref": "BootstrapVersion"
190+
}
191+
]
192+
}
193+
]
194+
},
195+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
196+
}
197+
]
198+
}
199+
}
200+
}

packages/@aws-cdk/mixins-preview/test/services/aws-kms/integ.events.test.ts.snapshot/L2KeyOtherEventsWithL2Rule.assets.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)