Skip to content

Commit 23241d6

Browse files
author
Victor Korzunin
committed
feat: add put events policy if eventbridge task is used by machine
fix #432
1 parent e0a391d commit 23241d6

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

lib/deploy/stepFunctions/compileIamRole.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,26 @@ function getSageMakerPermissions(state) {
315315
];
316316
}
317317

318+
function getEventBridgePermissions(state) {
319+
const eventBuses = new Set();
320+
321+
for (const entry of state.Parameters.Entries) {
322+
eventBuses.add(entry.EventBusName || 'default');
323+
}
324+
325+
return [
326+
{
327+
action: 'events:PutEvents',
328+
resource: [...eventBuses].map(eventBus => ({
329+
'Fn::Sub': [
330+
'arn:aws:events:${AWS::Region}:${AWS::AccountId}:event-bus/${eventBus}',
331+
{ eventBus },
332+
],
333+
})),
334+
},
335+
];
336+
}
337+
318338
// if there are multiple permissions with the same action, then collapsed them into one
319339
// permission instead, and collect the resources into an array
320340
function consolidatePermissionsByAction(permissions) {
@@ -402,6 +422,10 @@ function getIamPermissions(taskStates) {
402422
case 'arn:aws:states:::sagemaker:createTransformJob.sync':
403423
return getSageMakerPermissions(state);
404424

425+
case 'arn:aws:states:::events:putEvents':
426+
case 'arn:aws:states:::events:putEvents.waitForTaskToken':
427+
return getEventBridgePermissions(state);
428+
405429
default:
406430
if (isIntrinsic(state.Resource) || state.Resource.startsWith('arn:aws:lambda')) {
407431
const trimmedArn = trimAliasFromLambdaArn(state.Resource);

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,4 +2107,104 @@ describe('#compileIamRole', () => {
21072107
],
21082108
}]);
21092109
});
2110+
2111+
it('should give event bridge putEvents permissions', () => {
2112+
const genStateMachine = id => ({
2113+
id,
2114+
definition: {
2115+
StartAt: 'A',
2116+
States: {
2117+
A: {
2118+
Type: 'Task',
2119+
Resource: 'arn:aws:states:::events:putEvents',
2120+
Parameters: {
2121+
Entries: [{
2122+
Source: 'source',
2123+
DetailType: 'DetailType',
2124+
}],
2125+
},
2126+
End: true,
2127+
},
2128+
},
2129+
},
2130+
});
2131+
2132+
serverless.service.stepFunctions = {
2133+
stateMachines: {
2134+
myStateMachine1: genStateMachine('StateMachine1'),
2135+
},
2136+
};
2137+
2138+
serverlessStepFunctions.compileIamRole();
2139+
const statements = serverlessStepFunctions.serverless.service
2140+
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
2141+
.Properties.Policies[0].PolicyDocument.Statement;
2142+
2143+
const eventPermissions = statements.filter(s => _.isEqual(s.Action, ['events:PutEvents']));
2144+
expect(eventPermissions).to.has.lengthOf(1);
2145+
expect(eventPermissions[0].Resource).to.deep.eq([{
2146+
'Fn::Sub': [
2147+
'arn:aws:events:${AWS::Region}:${AWS::AccountId}:event-bus/${eventBus}',
2148+
{ eventBus: 'default' },
2149+
],
2150+
}]);
2151+
});
2152+
2153+
it('should give event bridge putEvents multiple permissions', () => {
2154+
const genStateMachine = id => ({
2155+
id,
2156+
definition: {
2157+
StartAt: 'A',
2158+
States: {
2159+
A: {
2160+
Type: 'Task',
2161+
Resource: 'arn:aws:states:::events:putEvents',
2162+
Parameters: {
2163+
Entries: [
2164+
{
2165+
Source: 'source',
2166+
DetailType: 'DetailType',
2167+
EventBusName: 'default',
2168+
},
2169+
{
2170+
Source: 'source',
2171+
DetailType: 'DetailType',
2172+
EventBusName: 'custom',
2173+
},
2174+
],
2175+
},
2176+
End: true,
2177+
},
2178+
},
2179+
},
2180+
});
2181+
2182+
serverless.service.stepFunctions = {
2183+
stateMachines: {
2184+
myStateMachine1: genStateMachine('StateMachine1'),
2185+
},
2186+
};
2187+
2188+
serverlessStepFunctions.compileIamRole();
2189+
const statements = serverlessStepFunctions.serverless.service
2190+
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
2191+
.Properties.Policies[0].PolicyDocument.Statement;
2192+
2193+
const eventPermissions = statements.filter(s => _.isEqual(s.Action, ['events:PutEvents']));
2194+
expect(eventPermissions[0].Resource).to.has.lengthOf(2);
2195+
expect(eventPermissions[0].Resource).to.deep.eq([
2196+
{
2197+
'Fn::Sub': [
2198+
'arn:aws:events:${AWS::Region}:${AWS::AccountId}:event-bus/${eventBus}',
2199+
{ eventBus: 'default' },
2200+
],
2201+
},
2202+
{
2203+
'Fn::Sub': [
2204+
'arn:aws:events:${AWS::Region}:${AWS::AccountId}:event-bus/${eventBus}',
2205+
{ eventBus: 'custom' },
2206+
],
2207+
},
2208+
]);
2209+
});
21102210
});

0 commit comments

Comments
 (0)