Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecs): enable cluster to grant task protection API permissions to IAM entities #28486

Merged
merged 8 commits into from
Dec 27, 2023
Merged
15 changes: 15 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,21 @@ export class Cluster extends Resource implements ICluster {
});
}

/**
* Grants ECS Task Protection API permissions to the specified grantee.
* This method provides a streamlined way to assign the 'ecs:UpdateTaskProtection'
* permission, enabling the grantee to manage task protection in the ECS cluster.
badmintoncryer marked this conversation as resolved.
Show resolved Hide resolved
*
* @param grantee - The entity (e.g., IAM role or user) to grant the permissions to.
badmintoncryer marked this conversation as resolved.
Show resolved Hide resolved
*/
public grantTaskProtection(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions: ['ecs:UpdateTaskProtection'],
resourceArns: [this.arnForTasks('*')],
});
}

private configureWindowsAutoScalingGroup(autoScalingGroup: autoscaling.AutoScalingGroup, options: AddAutoScalingGroupCapacityOptions = {}) {
// clear the cache of the agent
autoScalingGroup.addUserData('Remove-Item -Recurse C:\\ProgramData\\Amazon\\ECS\\Cache');
Expand Down
42 changes: 42 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,48 @@ describe('cluster', () => {
});
});

test('grantTaskProtection grants ecs:UpdateTaskProtection permission', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const role = new iam.Role(stack, 'TestRole', {
assumedBy: new iam.ServicePrincipal('ecs.amazonaws.com'),
});

// WHEN
cluster.grantTaskProtection(role);

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'ecs:UpdateTaskProtection',
Effect: 'Allow',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{ Ref: 'AWS::Partition' },
':ecs:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':task/',
{ Ref: 'EcsCluster97242B84' },
'/*',
],
],
},
},
],
Version: '2012-10-17',
},
});
});

/*
* TODO:v2.0.0 END OF OBSOLETE BLOCK
*/
Expand Down
Loading