diff --git a/packages/@aws-cdk/aws-iam/lib/principals.ts b/packages/@aws-cdk/aws-iam/lib/principals.ts index a52e8d1e0dda2..8cb94c33a0b1d 100644 --- a/packages/@aws-cdk/aws-iam/lib/principals.ts +++ b/packages/@aws-cdk/aws-iam/lib/principals.ts @@ -368,6 +368,18 @@ export class ArnPrincipal extends PrincipalBase { public toString() { return `ArnPrincipal(${this.arn})`; } + + /** + * A convenience method for adding a condition that the principal is part of the specified + * AWS Organization. + */ + public inOrganization(organizationId: string) { + return this.withConditions({ + StringEquals: { + 'aws:PrincipalOrgID': organizationId, + }, + }); + } } /** @@ -397,7 +409,7 @@ export interface ServicePrincipalOpts { /** * The region in which the service is operating. * - * @default the current Stack's region. + * @default - the current Stack's region. * @deprecated You should not need to set this. The stack's region is always correct. */ readonly region?: string; diff --git a/packages/@aws-cdk/aws-iam/test/principals.test.ts b/packages/@aws-cdk/aws-iam/test/principals.test.ts index 7a07a50e80fb9..34206540def53 100644 --- a/packages/@aws-cdk/aws-iam/test/principals.test.ts +++ b/packages/@aws-cdk/aws-iam/test/principals.test.ts @@ -245,6 +245,55 @@ test('PrincipalWithConditions inherits principalAccount from AccountPrincipal ', expect(principalWithConditions.principalAccount).toStrictEqual('123456789012'); }); +test('AccountPrincipal can specify an organization', () => { + // GIVEN + const stack = new Stack(); + + // WHEN + const pol = new iam.PolicyDocument({ + statements: [ + new iam.PolicyStatement({ + actions: ['service:action'], + resources: ['*'], + principals: [ + new iam.AccountPrincipal('123456789012').inOrganization('o-xxxxxxxxxx'), + ], + }), + ], + }); + + // THEN + expect(stack.resolve(pol)).toEqual({ + Statement: [ + { + Action: 'service:action', + Effect: 'Allow', + Principal: { + AWS: { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':iam::123456789012:root', + ], + ], + }, + }, + Condition: { + StringEquals: { + 'aws:PrincipalOrgID': 'o-xxxxxxxxxx', + }, + }, + Resource: '*', + }, + ], + Version: '2012-10-17', + }); +}); + test('ServicePrincipal in agnostic stack generates lookup table', () => { // GIVEN const stack = new Stack();