-
Notifications
You must be signed in to change notification settings - Fork 4k
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): validate policies for missing resources/principals #9269
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5e5d31d
feat(iam): validate policy statements during policy synthesis
Chriscbr 90238e2
Fix typos in docs
Chriscbr d2de0af
Add unit tests
Chriscbr a4fb3c6
Fix another typo
Chriscbr 5a78b4d
Add unit tests, and one (temp) commented failing test
Chriscbr c40394b
Merge remote-tracking branch 'refs/remotes/upstream/master'
Chriscbr fdd9a2f
Update README.md
Chriscbr 65c8b88
Merge remote-tracking branch 'refs/remotes/upstream/master'
Chriscbr 33609f5
Add private field to allow inline policies to be validated
Chriscbr 03698ae
Merge remote-tracking branch 'refs/remotes/upstream/master'
Chriscbr 2ac50a2
Update error message
Chriscbr 030da8d
Merge branch 'master' into master
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,6 +292,7 @@ export class Role extends Resource implements IRole { | |
private defaultPolicy?: Policy; | ||
private readonly managedPolicies: IManagedPolicy[] = []; | ||
private readonly attachedPolicies = new AttachedPolicies(); | ||
private readonly inlinePolicies: { [name: string]: PolicyDocument }; | ||
private immutableRole?: IRole; | ||
|
||
constructor(scope: Construct, id: string, props: RoleProps) { | ||
|
@@ -306,6 +307,7 @@ export class Role extends Resource implements IRole { | |
|
||
this.assumeRolePolicy = createAssumeRolePolicy(props.assumedBy, externalIds); | ||
this.managedPolicies.push(...props.managedPolicies || []); | ||
this.inlinePolicies = props.inlinePolicies || {}; | ||
this.permissionsBoundary = props.permissionsBoundary; | ||
const maxSessionDuration = props.maxSessionDuration && props.maxSessionDuration.toSeconds(); | ||
validateMaxSessionDuration(maxSessionDuration); | ||
|
@@ -318,7 +320,7 @@ export class Role extends Resource implements IRole { | |
const role = new CfnRole(this, 'Resource', { | ||
assumeRolePolicyDocument: this.assumeRolePolicy as any, | ||
managedPolicyArns: Lazy.listValue({ produce: () => this.managedPolicies.map(p => p.managedPolicyArn) }, { omitEmpty: true }), | ||
policies: _flatten(props.inlinePolicies), | ||
policies: _flatten(this.inlinePolicies), | ||
path: props.path, | ||
permissionsBoundary: this.permissionsBoundary ? this.permissionsBoundary.managedPolicyArn : undefined, | ||
roleName: this.physicalName, | ||
|
@@ -420,6 +422,15 @@ export class Role extends Resource implements IRole { | |
|
||
return this.immutableRole; | ||
} | ||
|
||
protected validate(): string[] { | ||
const errors = super.validate(); | ||
errors.push(...this.assumeRolePolicy?.validateForResourcePolicy() || []); | ||
for (const policyName of Object.keys(this.inlinePolicies)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can iterate over |
||
errors.push(...this.inlinePolicies[policyName].validateForIdentityPolicy()); | ||
} | ||
return errors; | ||
} | ||
} | ||
|
||
/** | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, however, there is also
effect: Allow | Deny
, so I wonder if this might not be confusing what the actual check is.Allow NotActions
is not the same asDeny Actions
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch - I forgot about this distinction!