Skip to content

Commit 9934619

Browse files
authored
fix(codeguruprofiler): incorrect profiling group name is returned when using importing (#22554)
Closes #22546. Verified via TDD with new unit test. Before the change, the unit test failed, replicating the bug in the issue with output: ```plain FAIL test/profiling-group.test.js profiling group ✓ attach read permission to Profiling group via fromProfilingGroupArn (71 ms) ✓ attach publish permission to Profiling group via fromProfilingGroupName (27 ms) ✕ use name specified via fromProfilingGroupName (8 ms) ✓ default profiling group (21 ms) ✓ allows setting its ComputePlatform (14 ms) ✓ default profiling group without name (13 ms) ✓ default profiling group without name when name exceeding limit is generated (18 ms) ✓ grant publish permissions profiling group (25 ms) ✓ grant read permissions profiling group (25 ms) ● profiling group › use specified via name via fromProfilingGroupName expect(received).toEqual(expected) // deep equality Expected: "MyAwesomeProfilingGroup" Received: "profilingGroup" 174 | 175 | const profilingGroup = ProfilingGroup.fromProfilingGroupName(stack, 'MyProfilingGroup', 'MyAwesomeProfilingGroup'); > 176 | expect(profilingGroup.profilingGroupName).toEqual('MyAwesomeProfilingGroup'); | ^ 177 | }); 178 | 179 | test('default profiling group', () => { at Object.<anonymous> (test/profiling-group.test.ts:176:47) ``` ---- ### All Submissions: * [Yes] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [No] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [N/A] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9139ca9 commit 9934619

File tree

13 files changed

+1665
-1
lines changed

13 files changed

+1665
-1
lines changed

packages/@aws-cdk/aws-codeguruprofiler/lib/profiling-group.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class ProfilingGroup extends ProfilingGroupBase {
154154
*/
155155
public static fromProfilingGroupArn(scope: Construct, id: string, profilingGroupArn: string): IProfilingGroup {
156156
class Import extends ProfilingGroupBase {
157-
public readonly profilingGroupName = Stack.of(scope).splitArn(profilingGroupArn, ArnFormat.SLASH_RESOURCE_NAME).resource;
157+
public readonly profilingGroupName = Stack.of(scope).splitArn(profilingGroupArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!;
158158
public readonly profilingGroupArn = profilingGroupArn;
159159
}
160160

packages/@aws-cdk/aws-codeguruprofiler/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"@aws-cdk/assertions": "0.0.0",
8686
"@aws-cdk/cdk-build-tools": "0.0.0",
8787
"@aws-cdk/integ-runner": "0.0.0",
88+
"@aws-cdk/integ-tests": "0.0.0",
8889
"@aws-cdk/cfn2ts": "0.0.0",
8990
"@aws-cdk/pkglint": "0.0.0",
9091
"@types/jest": "^27.5.2"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { AccountRootPrincipal, Role } from '@aws-cdk/aws-iam';
2+
import { App, CfnOutput, Stack } from '@aws-cdk/core';
3+
import { ExpectedResult, IntegTest } from '@aws-cdk/integ-tests';
4+
import { ProfilingGroup } from '../lib';
5+
6+
const app = new App();
7+
8+
const stack = new Stack(app, 'ProfilingGroupTestStack');
9+
10+
const profilingGroup1 = new ProfilingGroup(stack, 'ProfilingGroupWithExplicitlySetName', {
11+
profilingGroupName: 'ExplicitlySetName',
12+
});
13+
const profilingGroup2 = new ProfilingGroup(stack, 'ProfilingGroupWithImplicitlySetName');
14+
15+
const publishAppRole = new Role(stack, 'PublishAppRole', {
16+
assumedBy: new AccountRootPrincipal(),
17+
});
18+
profilingGroup1.grantPublish(publishAppRole);
19+
profilingGroup2.grantPublish(publishAppRole);
20+
21+
const importedGroupWithExplicitlySetName = ProfilingGroup.fromProfilingGroupName(
22+
stack,
23+
'ImportedProfilingGroupWithExplicitlySetName',
24+
profilingGroup1.profilingGroupName,
25+
);
26+
27+
const importedGroupWithImplicitlySetName = ProfilingGroup.fromProfilingGroupName(
28+
stack,
29+
'ImportedProfilingGroupWithImplicitlySetName',
30+
profilingGroup2.profilingGroupName,
31+
);
32+
33+
new CfnOutput(stack, 'ExplicitlySetProfilingGroupName', {
34+
value: importedGroupWithExplicitlySetName.profilingGroupName,
35+
});
36+
37+
new CfnOutput(stack, 'ImplicitlySetProfilingGroupName', {
38+
value: importedGroupWithImplicitlySetName.profilingGroupName,
39+
});
40+
41+
const testCase = new IntegTest(app, 'test', {
42+
testCases: [stack],
43+
});
44+
45+
const describe = testCase.assertions.awsApiCall('CloudFormation', 'describeStacks', {
46+
StackName: 'ProfilingGroupTestStack',
47+
});
48+
49+
describe.assertAtPath('Stacks.0.Outputs.0.OutputKey', ExpectedResult.stringLikeRegexp('ExplicitlySetProfilingGroupName'));
50+
describe.assertAtPath('Stacks.0.Outputs.0.OutputValue', ExpectedResult.stringLikeRegexp('ExplicitlySetName'));
51+
52+
describe.assertAtPath('Stacks.0.Outputs.1.OutputKey', ExpectedResult.stringLikeRegexp('ImplicitlySetProfilingGroupName'));
53+
describe.assertAtPath('Stacks.0.Outputs.1.OutputValue', ExpectedResult.stringLikeRegexp('ProfilingGroupTestStackProfilingGroupWithImplicitlySetName98463923'));
54+
55+
app.synth();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "21.0.0",
3+
"files": {
4+
"49d6a3151509f39124c2f82b21cf55a8a1364289fce8b6f8b764af6e204c6647": {
5+
"source": {
6+
"path": "ProfilingGroupTestStack.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "49d6a3151509f39124c2f82b21cf55a8a1364289fce8b6f8b764af6e204c6647.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"Resources": {
3+
"ProfilingGroupWithExplicitlySetNameProfilingGroup20552EAE": {
4+
"Type": "AWS::CodeGuruProfiler::ProfilingGroup",
5+
"Properties": {
6+
"ProfilingGroupName": "ExplicitlySetName"
7+
}
8+
},
9+
"ProfilingGroupWithImplicitlySetNameProfilingGroup21CDF1FC": {
10+
"Type": "AWS::CodeGuruProfiler::ProfilingGroup",
11+
"Properties": {
12+
"ProfilingGroupName": "ProfilingGroupTestStackProfilingGroupWithImplicitlySetName98463923"
13+
}
14+
},
15+
"PublishAppRole9FEBD682": {
16+
"Type": "AWS::IAM::Role",
17+
"Properties": {
18+
"AssumeRolePolicyDocument": {
19+
"Statement": [
20+
{
21+
"Action": "sts:AssumeRole",
22+
"Effect": "Allow",
23+
"Principal": {
24+
"AWS": {
25+
"Fn::Join": [
26+
"",
27+
[
28+
"arn:",
29+
{
30+
"Ref": "AWS::Partition"
31+
},
32+
":iam::",
33+
{
34+
"Ref": "AWS::AccountId"
35+
},
36+
":root"
37+
]
38+
]
39+
}
40+
}
41+
}
42+
],
43+
"Version": "2012-10-17"
44+
}
45+
}
46+
},
47+
"PublishAppRoleDefaultPolicyCA1E15C3": {
48+
"Type": "AWS::IAM::Policy",
49+
"Properties": {
50+
"PolicyDocument": {
51+
"Statement": [
52+
{
53+
"Action": [
54+
"codeguru-profiler:ConfigureAgent",
55+
"codeguru-profiler:PostAgentProfile"
56+
],
57+
"Effect": "Allow",
58+
"Resource": [
59+
{
60+
"Fn::GetAtt": [
61+
"ProfilingGroupWithExplicitlySetNameProfilingGroup20552EAE",
62+
"Arn"
63+
]
64+
},
65+
{
66+
"Fn::GetAtt": [
67+
"ProfilingGroupWithImplicitlySetNameProfilingGroup21CDF1FC",
68+
"Arn"
69+
]
70+
}
71+
]
72+
}
73+
],
74+
"Version": "2012-10-17"
75+
},
76+
"PolicyName": "PublishAppRoleDefaultPolicyCA1E15C3",
77+
"Roles": [
78+
{
79+
"Ref": "PublishAppRole9FEBD682"
80+
}
81+
]
82+
}
83+
}
84+
},
85+
"Outputs": {
86+
"ExplicitlySetProfilingGroupName": {
87+
"Value": {
88+
"Ref": "ProfilingGroupWithExplicitlySetNameProfilingGroup20552EAE"
89+
}
90+
},
91+
"ImplicitlySetProfilingGroupName": {
92+
"Value": {
93+
"Ref": "ProfilingGroupWithImplicitlySetNameProfilingGroup21CDF1FC"
94+
}
95+
}
96+
},
97+
"Parameters": {
98+
"BootstrapVersion": {
99+
"Type": "AWS::SSM::Parameter::Value<String>",
100+
"Default": "/cdk-bootstrap/hnb659fds/version",
101+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
102+
}
103+
},
104+
"Rules": {
105+
"CheckBootstrapVersion": {
106+
"Assertions": [
107+
{
108+
"Assert": {
109+
"Fn::Not": [
110+
{
111+
"Fn::Contains": [
112+
[
113+
"1",
114+
"2",
115+
"3",
116+
"4",
117+
"5"
118+
],
119+
{
120+
"Ref": "BootstrapVersion"
121+
}
122+
]
123+
}
124+
]
125+
},
126+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
127+
}
128+
]
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)