-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codeguruprofiler): ProfilingGroup (#7895)
fixes #6984 by creating L2 construct and functions to allow for policies to be assigned to execution roles. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
44e4d66
commit 995088a
Showing
7 changed files
with
758 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// AWS::CodeGuruProfiler CloudFormation Resources: | ||
export * from './codeguruprofiler.generated'; | ||
export * from './profiling-group'; |
180 changes: 180 additions & 0 deletions
180
packages/@aws-cdk/aws-codeguruprofiler/lib/profiling-group.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { Grant, IGrantable } from '@aws-cdk/aws-iam'; | ||
import { Construct, IResource, Lazy, Resource, Stack } from '@aws-cdk/core'; | ||
import { CfnProfilingGroup } from './codeguruprofiler.generated'; | ||
|
||
/** | ||
* IResource represents a Profiling Group. | ||
*/ | ||
export interface IProfilingGroup extends IResource { | ||
|
||
/** | ||
* A name for the profiling group. | ||
* | ||
* @attribute | ||
*/ | ||
readonly profilingGroupName: string; | ||
|
||
/** | ||
* Grant access to publish profiling information to the Profiling Group to the given identity. | ||
* | ||
* This will grant the following permissions: | ||
* | ||
* - codeguru-profiler:ConfigureAgent | ||
* - codeguru-profiler:PostAgentProfile | ||
* | ||
* @param grantee Principal to grant publish rights to | ||
*/ | ||
grantPublish(grantee: IGrantable): Grant; | ||
|
||
/** | ||
* Grant access to read profiling information from the Profiling Group to the given identity. | ||
* | ||
* This will grant the following permissions: | ||
* | ||
* - codeguru-profiler:GetProfile | ||
* - codeguru-profiler:DescribeProfilingGroup | ||
* | ||
* @param grantee Principal to grant read rights to | ||
*/ | ||
grantRead(grantee: IGrantable): Grant; | ||
|
||
} | ||
|
||
abstract class ProfilingGroupBase extends Resource implements IProfilingGroup { | ||
|
||
public abstract readonly profilingGroupName: string; | ||
|
||
public abstract readonly profilingGroupArn: string; | ||
|
||
/** | ||
* Grant access to publish profiling information to the Profiling Group to the given identity. | ||
* | ||
* This will grant the following permissions: | ||
* | ||
* - codeguru-profiler:ConfigureAgent | ||
* - codeguru-profiler:PostAgentProfile | ||
* | ||
* @param grantee Principal to grant publish rights to | ||
*/ | ||
public grantPublish(grantee: IGrantable) { | ||
// https://docs.aws.amazon.com/codeguru/latest/profiler-ug/security-iam.html#security-iam-access-control | ||
return Grant.addToPrincipal({ | ||
grantee, | ||
actions: ['codeguru-profiler:ConfigureAgent', 'codeguru-profiler:PostAgentProfile'], | ||
resourceArns: [this.profilingGroupArn], | ||
}); | ||
} | ||
|
||
/** | ||
* Grant access to read profiling information from the Profiling Group to the given identity. | ||
* | ||
* This will grant the following permissions: | ||
* | ||
* - codeguru-profiler:GetProfile | ||
* - codeguru-profiler:DescribeProfilingGroup | ||
* | ||
* @param grantee Principal to grant read rights to | ||
*/ | ||
public grantRead(grantee: IGrantable) { | ||
// https://docs.aws.amazon.com/codeguru/latest/profiler-ug/security-iam.html#security-iam-access-control | ||
return Grant.addToPrincipal({ | ||
grantee, | ||
actions: ['codeguru-profiler:GetProfile', 'codeguru-profiler:DescribeProfilingGroup'], | ||
resourceArns: [this.profilingGroupArn], | ||
}); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Properties for creating a new Profiling Group. | ||
*/ | ||
export interface ProfilingGroupProps { | ||
|
||
/** | ||
* A name for the profiling group. | ||
* @default - automatically generated name. | ||
*/ | ||
readonly profilingGroupName?: string; | ||
|
||
} | ||
|
||
/** | ||
* A new Profiling Group. | ||
*/ | ||
export class ProfilingGroup extends ProfilingGroupBase { | ||
|
||
/** | ||
* Import an existing Profiling Group provided a Profiling Group Name. | ||
* | ||
* @param scope The parent creating construct | ||
* @param id The construct's name | ||
* @param profilingGroupName Profiling Group Name | ||
*/ | ||
public static fromProfilingGroupName(scope: Construct, id: string, profilingGroupName: string): IProfilingGroup { | ||
const stack = Stack.of(scope); | ||
|
||
return this.fromProfilingGroupArn(scope, id, stack.formatArn({ | ||
service: 'codeguru-profiler', | ||
resource: 'profilingGroup', | ||
resourceName: profilingGroupName, | ||
})); | ||
} | ||
|
||
/** | ||
* Import an existing Profiling Group provided an ARN. | ||
* | ||
* @param scope The parent creating construct | ||
* @param id The construct's name | ||
* @param profilingGroupArn Profiling Group ARN | ||
*/ | ||
public static fromProfilingGroupArn(scope: Construct, id: string, profilingGroupArn: string): IProfilingGroup { | ||
class Import extends ProfilingGroupBase { | ||
public readonly profilingGroupName = Stack.of(scope).parseArn(profilingGroupArn).resource; | ||
public readonly profilingGroupArn = profilingGroupArn; | ||
} | ||
|
||
return new Import(scope, id); | ||
} | ||
|
||
/** | ||
* The name of the Profiling Group. | ||
* | ||
* @attribute | ||
*/ | ||
public readonly profilingGroupName: string; | ||
|
||
/** | ||
* The ARN of the Profiling Group. | ||
* | ||
* @attribute | ||
*/ | ||
public readonly profilingGroupArn: string; | ||
|
||
constructor(scope: Construct, id: string, props: ProfilingGroupProps = {}) { | ||
super(scope, id, { | ||
physicalName: props.profilingGroupName ?? Lazy.stringValue({ produce: () => this.generateUniqueId() }), | ||
}); | ||
|
||
const profilingGroup = new CfnProfilingGroup(this, 'ProfilingGroup', { | ||
profilingGroupName: this.physicalName, | ||
}); | ||
|
||
this.profilingGroupName = this.getResourceNameAttribute(profilingGroup.ref); | ||
|
||
this.profilingGroupArn = this.getResourceArnAttribute(profilingGroup.attrArn, { | ||
service: 'codeguru-profiler', | ||
resource: 'profilingGroup', | ||
resourceName: this.physicalName, | ||
}); | ||
} | ||
|
||
private generateUniqueId(): string { | ||
const name = this.node.uniqueId; | ||
if (name.length > 240) { | ||
return name.substring(0, 120) + name.substring(name.length - 120); | ||
} | ||
return name; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/@aws-cdk/aws-codeguruprofiler/test/integ.profiler-group.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
{ | ||
"Resources": { | ||
"MyProfilingGroup829F0507": { | ||
"Type": "AWS::CodeGuruProfiler::ProfilingGroup", | ||
"Properties": { | ||
"ProfilingGroupName": "ProfilerGroupIntegrationTestMyProfilingGroup81DA69A3" | ||
} | ||
}, | ||
"PublishAppRole9FEBD682": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":iam::", | ||
{ | ||
"Ref": "AWS::AccountId" | ||
}, | ||
":root" | ||
] | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"PublishAppRoleDefaultPolicyCA1E15C3": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"codeguru-profiler:ConfigureAgent", | ||
"codeguru-profiler:PostAgentProfile" | ||
], | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"MyProfilingGroup829F0507", | ||
"Arn" | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "PublishAppRoleDefaultPolicyCA1E15C3", | ||
"Roles": [ | ||
{ | ||
"Ref": "PublishAppRole9FEBD682" | ||
} | ||
] | ||
} | ||
}, | ||
"ReadAppRole52FE6317": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":iam::", | ||
{ | ||
"Ref": "AWS::AccountId" | ||
}, | ||
":root" | ||
] | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"ReadAppRoleDefaultPolicy4BB8955C": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"codeguru-profiler:GetProfile", | ||
"codeguru-profiler:DescribeProfilingGroup" | ||
], | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"MyProfilingGroup829F0507", | ||
"Arn" | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "ReadAppRoleDefaultPolicy4BB8955C", | ||
"Roles": [ | ||
{ | ||
"Ref": "ReadAppRole52FE6317" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/@aws-cdk/aws-codeguruprofiler/test/integ.profiler-group.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { AccountRootPrincipal, Role } from '@aws-cdk/aws-iam'; | ||
import { App, Stack, StackProps } from '@aws-cdk/core'; | ||
import { ProfilingGroup } from '../lib'; | ||
|
||
class ProfilerGroupIntegrationTest extends Stack { | ||
constructor(scope: App, id: string, props?: StackProps) { | ||
super(scope, id, props); | ||
|
||
const profilingGroup = new ProfilingGroup(this, 'MyProfilingGroup'); | ||
|
||
const publishAppRole = new Role(this, 'PublishAppRole', { | ||
assumedBy: new AccountRootPrincipal(), | ||
}); | ||
profilingGroup.grantPublish(publishAppRole); | ||
|
||
const readAppRole = new Role(this, 'ReadAppRole', { | ||
assumedBy: new AccountRootPrincipal(), | ||
}); | ||
profilingGroup.grantRead(readAppRole); | ||
|
||
} | ||
} | ||
|
||
const app = new App(); | ||
|
||
new ProfilerGroupIntegrationTest(app, 'ProfilerGroupIntegrationTest'); | ||
|
||
app.synth(); |
Oops, something went wrong.