Skip to content
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

fix(events): now EventBus.grantPutEventsTo correctly handles service principals #33729

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions packages/aws-cdk-lib/aws-events/lib/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ abstract class EventBusBase extends Resource implements IEventBus {
}

public grantPutEventsTo(grantee: iam.IGrantable): iam.Grant {
// Special handling for service principals
if (grantee.grantPrincipal instanceof iam.ServicePrincipal) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a generic solution. We still have issues with other Principle types that does not implement addToPrincipalPolicy (or it can not linked to a Policy resource), like AccountPrincipal, AnyPrincipal.

EventBus resource supports the ResourcePolicy (AWS::Events::EventBusPolicy) .. so we should make the EventBus L2 to implement the interface IResourceWithPolicy, and to implements addToResourcePolicy function which looks it is already there.

Then you can change the implementation of this function from iam.Grant.addToPrincipal to be iam.Grant.addToPrincipalOrResource

Copy link
Contributor

@moelasmar moelasmar Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more thing, I am not sure if it is Ok to create multiple EventBusPolicy resources for the same EventBus or not .. we need to have an integ test case for that.

const policyStatementId = `AllowPutEvents-${this.node.id}`;

new CfnEventBusPolicy(this, policyStatementId, {
action: 'events:PutEvents',
principal: grantee.grantPrincipal.service,
statementId: policyStatementId,
eventBusName: this.eventBusName,
});

return iam.Grant.drop(grantee, 'Service principal permissions handled via EventBusPolicy');
}

// Existing implementation for IAM roles/users
return iam.Grant.addToPrincipal({
grantee,
actions: ['events:PutEvents'],
Expand Down
50 changes: 50 additions & 0 deletions packages/aws-cdk-lib/aws-events/test/event-bus.grant.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Template } from '../../assertions';
import * as iam from '../../aws-iam';
import { Stack } from '../../core';
import { EventBus } from '../lib';

describe('EventBus grants', () => {
test('grantPutEventsTo creates EventBusPolicy for service principal', () => {
// GIVEN
const stack = new Stack();
const eventBus = new EventBus(stack, 'EventBus');
const servicePrincipal = new iam.ServicePrincipal('states.amazonaws.com');

// WHEN
eventBus.grantPutEventsTo(servicePrincipal);

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Events::EventBusPolicy', {
Action: 'events:PutEvents',
Principal: 'states.amazonaws.com',
StatementId: 'AllowPutEvents-EventBus',
EventBusName: { Ref: 'EventBus7B8748AA' },
});
});

test('grantPutEventsTo creates IAM policy for IAM principal', () => {
// GIVEN
const stack = new Stack();
const eventBus = new EventBus(stack, 'EventBus');
const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
});

// WHEN
eventBus.grantPutEventsTo(role);

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [{
Action: 'events:PutEvents',
Effect: 'Allow',
Resource: {
'Fn::GetAtt': ['EventBus7B8748AA', 'Arn'],
},
}],
Version: '2012-10-17',
},
});
});
});
Loading