Skip to content

Commit c545bfe

Browse files
authored
feat(iam): add convenience method inOrganization to ArnPrincipal (#20109)
Add a convenience method to ArnPrincipal. ArnPrincipal is extended by AccountPrincipal and AnyPrincipal, which are the only principals that could reasonably want to add a condition on organization. ```ts new AccountPrincipal('123456789012').inOrganization('o-xxxxxxxxxx'); ``` Related: #19975 (comment). With this method, the API in #19975 will look like: ```ts fn.grantInvoke(new AccountPrincipal('123456789012').inOrganization('o-xxxxxxxxxx'); ``` Which is really slick! ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/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 f832227 commit c545bfe

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

packages/@aws-cdk/aws-iam/lib/principals.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,18 @@ export class ArnPrincipal extends PrincipalBase {
368368
public toString() {
369369
return `ArnPrincipal(${this.arn})`;
370370
}
371+
372+
/**
373+
* A convenience method for adding a condition that the principal is part of the specified
374+
* AWS Organization.
375+
*/
376+
public inOrganization(organizationId: string) {
377+
return this.withConditions({
378+
StringEquals: {
379+
'aws:PrincipalOrgID': organizationId,
380+
},
381+
});
382+
}
371383
}
372384

373385
/**
@@ -397,7 +409,7 @@ export interface ServicePrincipalOpts {
397409
/**
398410
* The region in which the service is operating.
399411
*
400-
* @default the current Stack's region.
412+
* @default - the current Stack's region.
401413
* @deprecated You should not need to set this. The stack's region is always correct.
402414
*/
403415
readonly region?: string;

packages/@aws-cdk/aws-iam/test/principals.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,55 @@ test('PrincipalWithConditions inherits principalAccount from AccountPrincipal ',
245245
expect(principalWithConditions.principalAccount).toStrictEqual('123456789012');
246246
});
247247

248+
test('AccountPrincipal can specify an organization', () => {
249+
// GIVEN
250+
const stack = new Stack();
251+
252+
// WHEN
253+
const pol = new iam.PolicyDocument({
254+
statements: [
255+
new iam.PolicyStatement({
256+
actions: ['service:action'],
257+
resources: ['*'],
258+
principals: [
259+
new iam.AccountPrincipal('123456789012').inOrganization('o-xxxxxxxxxx'),
260+
],
261+
}),
262+
],
263+
});
264+
265+
// THEN
266+
expect(stack.resolve(pol)).toEqual({
267+
Statement: [
268+
{
269+
Action: 'service:action',
270+
Effect: 'Allow',
271+
Principal: {
272+
AWS: {
273+
'Fn::Join': [
274+
'',
275+
[
276+
'arn:',
277+
{
278+
Ref: 'AWS::Partition',
279+
},
280+
':iam::123456789012:root',
281+
],
282+
],
283+
},
284+
},
285+
Condition: {
286+
StringEquals: {
287+
'aws:PrincipalOrgID': 'o-xxxxxxxxxx',
288+
},
289+
},
290+
Resource: '*',
291+
},
292+
],
293+
Version: '2012-10-17',
294+
});
295+
});
296+
248297
test('ServicePrincipal in agnostic stack generates lookup table', () => {
249298
// GIVEN
250299
const stack = new Stack();

0 commit comments

Comments
 (0)