-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EventBridge): EventBusOpenAccess rule (#636)
* feat(EventBridge): EventBusOpenAccess rule * chore: cdk v2 specific changes
- Loading branch information
Showing
8 changed files
with
231 additions
and
6 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
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,69 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { parse } from 'path'; | ||
import { CfnResource, Stack } from 'aws-cdk-lib'; | ||
import { CfnEventBusPolicy } from 'aws-cdk-lib/aws-events'; | ||
import { NagRuleCompliance } from '../../nag-rules'; | ||
|
||
/** | ||
* EventBridge event bus policies do not allow for open access | ||
* @param node the CfnResource to check | ||
*/ | ||
export default Object.defineProperty( | ||
(node: CfnResource): NagRuleCompliance => { | ||
if (node instanceof CfnEventBusPolicy) { | ||
if (Stack.of(node).resolve(node.principal) === '*') { | ||
const condition = Stack.of(node).resolve(node.condition); | ||
if ( | ||
condition === undefined || | ||
condition.key === undefined || | ||
condition.type === undefined || | ||
condition.value === undefined | ||
) { | ||
return NagRuleCompliance.NON_COMPLIANT; | ||
} | ||
} | ||
const resolvedStatement = Stack.of(node).resolve(node.statement); | ||
if (resolvedStatement !== undefined) { | ||
const condition = Stack.of(node).resolve(resolvedStatement?.Condition); | ||
if ( | ||
resolvedStatement?.Effect === 'Allow' && | ||
checkMatchingPrincipal(resolvedStatement?.Principal) === true && | ||
(condition === undefined || JSON.stringify(condition) === '{}') | ||
) { | ||
return NagRuleCompliance.NON_COMPLIANT; | ||
} | ||
} | ||
return NagRuleCompliance.COMPLIANT; | ||
} else { | ||
return NagRuleCompliance.NOT_APPLICABLE; | ||
} | ||
}, | ||
'name', | ||
{ value: parse(__filename).name } | ||
); | ||
|
||
/** | ||
* Helper function to check whether the event bus policy applies to all principals | ||
* @param node The CfnEventBusPolicy to check | ||
* @param principal The principals in the event bus policy | ||
* @returns Whether the CfnEventBusPolicy applies to all principals | ||
*/ | ||
function checkMatchingPrincipal(principals: any): boolean { | ||
if (principals === '*') { | ||
return true; | ||
} | ||
const awsPrincipal = principals.AWS; | ||
if (Array.isArray(awsPrincipal)) { | ||
for (const account of awsPrincipal) { | ||
if (account === '*') { | ||
return true; | ||
} | ||
} | ||
} else if (awsPrincipal === '*') { | ||
return true; | ||
} | ||
return false; | ||
} |
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,5 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
export { default as EventBusOpenAccess } from './EventBusOpenAccess'; |
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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { Aspects, Stack } from 'aws-cdk-lib'; | ||
import { CfnEventBusPolicy } from 'aws-cdk-lib/aws-events'; | ||
import { EventBusOpenAccess } from '../../src/rules/eventbridge'; | ||
import { validateStack, TestType, TestPack } from './utils'; | ||
|
||
const testPack = new TestPack([EventBusOpenAccess]); | ||
let stack: Stack; | ||
|
||
beforeEach(() => { | ||
stack = new Stack(); | ||
Aspects.of(stack).add(testPack); | ||
}); | ||
|
||
describe('Amazon EventBridge', () => { | ||
describe('EventBusOpenAccess: DMS replication instances are not public', () => { | ||
const ruleId = 'EventBusOpenAccess'; | ||
test('Noncompliance 1', () => { | ||
new CfnEventBusPolicy(stack, 'rPolicy', { | ||
statementId: 'foo', | ||
action: 'events:*', | ||
principal: '*', | ||
}); | ||
validateStack(stack, ruleId, TestType.NON_COMPLIANCE); | ||
}); | ||
test('Noncompliance 2', () => { | ||
new CfnEventBusPolicy(stack, 'rPolicy', { | ||
statementId: 'foo', | ||
statement: { | ||
Effect: 'Allow', | ||
Principal: '*', | ||
Action: 'events:*', | ||
Resource: 'arn:aws:events:us-east-1:111122223333:event-bus/default', | ||
}, | ||
}); | ||
validateStack(stack, ruleId, TestType.NON_COMPLIANCE); | ||
}); | ||
test('Noncompliance 3', () => { | ||
new CfnEventBusPolicy(stack, 'rPolicy', { | ||
statementId: 'foo', | ||
statement: { | ||
Effect: 'Allow', | ||
Principal: { | ||
AWS: ['arn:aws:iam::111122223333:user/alice', '*'], | ||
}, | ||
Action: 'events:*', | ||
Resource: 'arn:aws:events:us-east-1:111122223333:event-bus/default', | ||
}, | ||
}); | ||
validateStack(stack, ruleId, TestType.NON_COMPLIANCE); | ||
}); | ||
test('Noncompliance 4', () => { | ||
new CfnEventBusPolicy(stack, 'rPolicy', { | ||
statementId: 'foo', | ||
statement: { | ||
Effect: 'Allow', | ||
Principal: { AWS: '*' }, | ||
Action: 'events:*', | ||
Condition: {}, | ||
Resource: 'arn:aws:events:us-east-1:111122223333:event-bus/default', | ||
}, | ||
}); | ||
validateStack(stack, ruleId, TestType.NON_COMPLIANCE); | ||
}); | ||
test('Noncompliance 5', () => { | ||
new CfnEventBusPolicy(stack, 'rPolicy', { | ||
statementId: 'foo', | ||
action: 'events:*', | ||
principal: '*', | ||
condition: { | ||
key: 'foo', | ||
type: 'bar', | ||
value: 'baz', | ||
}, | ||
statement: { | ||
Effect: 'Allow', | ||
Principal: '*', | ||
Action: 'events:*', | ||
Resource: 'arn:aws:events:us-east-1:111122223333:event-bus/default', | ||
}, | ||
}); | ||
validateStack(stack, ruleId, TestType.NON_COMPLIANCE); | ||
}); | ||
test('Compliance', () => { | ||
new CfnEventBusPolicy(stack, 'rPolicy1', { | ||
statementId: 'foo', | ||
action: 'events:*', | ||
principal: '*', | ||
condition: { | ||
key: 'foo', | ||
type: 'bar', | ||
value: 'baz', | ||
}, | ||
}); | ||
new CfnEventBusPolicy(stack, 'rPolicy2', { | ||
statementId: 'foo', | ||
statement: { | ||
Effect: 'Allow', | ||
Principal: '*', | ||
Action: 'events:*', | ||
Resource: 'arn:aws:events:us-east-1:111122223333:event-bus/default', | ||
Condition: { | ||
StringEquals: { 'aws:PrincipalOrgID': 'o-1234567890' }, | ||
}, | ||
}, | ||
}); | ||
new CfnEventBusPolicy(stack, 'rPolicy3', { | ||
statementId: 'foo', | ||
statement: { | ||
Effect: 'Allow', | ||
Principal: { | ||
AWS: [ | ||
'arn:aws:iam::111122223333:user/alice', | ||
'arn:aws:iam::111122223333:user/bob', | ||
], | ||
}, | ||
Action: 'events:*', | ||
Resource: 'arn:aws:events:us-east-1:111122223333:event-bus/default', | ||
}, | ||
}); | ||
new CfnEventBusPolicy(stack, 'rPolicy4', { | ||
statementId: 'foo', | ||
statement: { | ||
Effect: 'Deny', | ||
Principal: { | ||
AWS: '*', | ||
}, | ||
Action: 'events:*', | ||
Resource: 'arn:aws:events:us-east-1:111122223333:event-bus/default', | ||
}, | ||
}); | ||
validateStack(stack, ruleId, TestType.COMPLIANCE); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.