-
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.
To allow session tagging, the `sts:TagSession` permission needs to be added to the role's AssumeRolePolicyDocument. Introduce a new principal which enables this, and add a convenience method `.withSessionTags()` to the `PrincipalBase` class so all built-in principals will have this convenience method by default. To build this, we had to get rid of some cruft and assumptions around policy documents and statements, and defer more power to the `IPrincipal` objects themselves. In order not to break existing implementors, introduce a new interface `IAssumeRolePrincipal` which knows how to add itself to an AssumeRolePolicyDocument and gets complete freedom doing so. That same new interface could be used to lift some old limitations on `CompositePrincipal` so did that as well. Fixes #15908, closes #16725, fixes #2041, fixes #1578.
- Loading branch information
Showing
7 changed files
with
288 additions
and
82 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
25 changes: 25 additions & 0 deletions
25
packages/@aws-cdk/aws-iam/lib/private/assume-role-policy.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,25 @@ | ||
import { PolicyDocument } from '../policy-document'; | ||
import { PolicyStatement } from '../policy-statement'; | ||
import { IPrincipal, IAssumeRolePrincipal } from '../principals'; | ||
|
||
/** | ||
* Add a principal to an AssumeRolePolicyDocument in the right way | ||
* | ||
* Delegate to the principal if it can do the job itself, do a default job if it can't. | ||
*/ | ||
export function defaultAddPrincipalToAssumeRole(principal: IPrincipal, doc: PolicyDocument) { | ||
if (isAssumeRolePrincipal(principal)) { | ||
// Principal knows how to add itself | ||
principal.addToAssumeRolePolicy(doc); | ||
} else { | ||
// Principal can't add itself, we do it for them | ||
doc.addStatements(new PolicyStatement({ | ||
actions: [principal.assumeRoleAction], | ||
principals: [principal], | ||
})); | ||
} | ||
} | ||
|
||
function isAssumeRolePrincipal(principal: IPrincipal): principal is IAssumeRolePrincipal { | ||
return !!(principal as IAssumeRolePrincipal).addToAssumeRolePolicy; | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/@aws-cdk/aws-iam/lib/private/policydoc-adapter.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,17 @@ | ||
import { PolicyDocument } from '../policy-document'; | ||
import { PolicyStatement } from '../policy-statement'; | ||
|
||
/** | ||
* A PolicyDocument adapter that can modify statements flowing through it | ||
*/ | ||
export class MutatingPolicyDocumentAdapter extends PolicyDocument { | ||
constructor(private readonly wrapped: PolicyDocument, private readonly mutator: (s: PolicyStatement) => PolicyStatement) { | ||
super(); | ||
} | ||
|
||
public addStatements(...statements: PolicyStatement[]): void { | ||
for (const st of statements) { | ||
this.wrapped.addStatements(this.mutator(st)); | ||
} | ||
} | ||
} |
Oops, something went wrong.