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(iam): Adding managedPolicyName to IManagedPolicy #29751

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct } from 'constructs';
import * as codepipeline from '../../../aws-codepipeline';
import { Aws } from '../../../core';
import { ManagedPolicy } from '../../../aws-iam';
import { Action } from '../action';
import { deployArtifactBounds } from '../common';

Expand Down Expand Up @@ -52,7 +52,7 @@ export class ElasticBeanstalkDeployAction extends Action {

// Per https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.iam.managed-policies.html
// it doesn't seem we can scope this down further for the codepipeline action.
options.role.addManagedPolicy({ managedPolicyArn: `arn:${Aws.PARTITION}:iam::aws:policy/AdministratorAccess-AWSElasticBeanstalk` });
options.role.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess-AWSElasticBeanstalk'));

// the Action's Role needs to read from the Bucket to get artifacts
options.bucket.grantRead(options.role);
Expand Down
9 changes: 9 additions & 0 deletions packages/aws-cdk-lib/aws-iam/lib/managed-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export interface IManagedPolicy {
* @attribute
*/
readonly managedPolicyArn: string;

/**
* The name of the managed policy
* @attribute
*/
readonly managedPolicyName: string;
}

/**
Expand Down Expand Up @@ -117,6 +123,7 @@ export class ManagedPolicy extends Resource implements IManagedPolicy, IGrantabl
resource: 'policy',
resourceName: managedPolicyName,
});
public readonly managedPolicyName = managedPolicyName;
}
return new Import(scope, id);
}
Expand All @@ -143,6 +150,7 @@ export class ManagedPolicy extends Resource implements IManagedPolicy, IGrantabl
public static fromManagedPolicyArn(scope: Construct, id: string, managedPolicyArn: string): IManagedPolicy {
class Import extends Resource implements IManagedPolicy {
public readonly managedPolicyArn = managedPolicyArn;
public readonly managedPolicyName = Stack.of(scope).splitArn(managedPolicyArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!;
}
return new Import(scope, id);
}
Expand All @@ -166,6 +174,7 @@ export class ManagedPolicy extends Resource implements IManagedPolicy, IGrantabl
resource: 'policy',
resourceName: managedPolicyName,
});
public readonly managedPolicyName = managedPolicyName;
}
return new AwsManagedPolicy();
}
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-cdk-lib/aws-iam/test/managed-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('managed policy', () => {
':iam::aws:policy/service-role/SomePolicy',
]],
});
expect(stack.resolve(mp.managedPolicyName)).toEqual('service-role/SomePolicy');
});

test('simple customer managed policy', () => {
Expand All @@ -33,12 +34,14 @@ describe('managed policy', () => {
':iam::1234:policy/SomeCustomerPolicy',
]],
});
expect(stack.resolve(mp.managedPolicyName)).toEqual('SomeCustomerPolicy');
});

test('managed policy by arn', () => {
const mp = ManagedPolicy.fromManagedPolicyArn(stack, 'MyManagedPolicyByArn', 'arn:aws:iam::1234:policy/my-policy');

expect(stack.resolve(mp.managedPolicyArn)).toEqual('arn:aws:iam::1234:policy/my-policy');
expect(stack.resolve(mp.managedPolicyName)).toEqual('my-policy');
});

test('managed policy with statements', () => {
Expand Down
Loading