Skip to content
Merged
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
17 changes: 10 additions & 7 deletions lib/deploy/stepFunctions/compileIamRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,24 +636,25 @@ function getHttpInvokePermissions(state) {
];
}

function getEventBridgeSchedulerPermissions(state) {
const scheduleGroupName = state.Parameters.GroupName;
const scheduleTargetRoleArn = state.Parameters.Target.RoleArn;
function getEventBridgeSchedulerPermissions(action, state) {
const scheduleGroupName = state.Parameters.GroupName || 'default';
const scheduleTargetRoleArn = state.Parameters.Target && state.Parameters.Target.RoleArn;

return [
{
action: 'scheduler:CreateSchedule',
action,
resource: {
'Fn::Sub': [
'arn:${AWS::Partition}:scheduler:${AWS::Region}:${AWS::AccountId}:schedule/${scheduleGroupName}/*',
{ scheduleGroupName },
],
},
},
{
// createSchedule needs a target role; deleteSchedule does not
...(scheduleTargetRoleArn ? [{
action: 'iam:PassRole',
resource: scheduleTargetRoleArn,
},
}] : []),
];
}

Expand Down Expand Up @@ -851,7 +852,9 @@ function getIamPermissions(taskStates) {
return getEventBridgePermissions(state);

case 'arn:aws:states:::aws-sdk:scheduler:createSchedule':
return getEventBridgeSchedulerPermissions(state);
return getEventBridgeSchedulerPermissions('scheduler:CreateSchedule', state);
case 'arn:aws:states:::aws-sdk:scheduler:deleteSchedule':
return getEventBridgeSchedulerPermissions('scheduler:DeleteSchedule', state);

case 'arn:aws:states:::http:invoke':
return getHttpInvokePermissions(state);
Expand Down
81 changes: 81 additions & 0 deletions lib/deploy/stepFunctions/compileIamRole.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4476,6 +4476,87 @@ describe('#compileIamRole', () => {
expect(rolePermissions[0].Resource).to.deep.eq(['arn:aws:iam::${AWS::AccountId}:role/MyIAMRole']);
});

it('should give event bridge scheduler deleteSchedule permissions', () => {
const genStateMachine = id => ({
id,
definition: {
StartAt: 'A',
States: {
A: {
Type: 'Task',
Resource: 'arn:aws:states:::aws-sdk:scheduler:deleteSchedule',
Parameters: {
'Name.$': '$$.Execution.Name',
GroupName: 'MyScheduleGroup',
},
End: true,
},
},
},
});

serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: genStateMachine('StateMachine1'),
},
};

serverlessStepFunctions.compileIamRole();
const statements = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
.Properties.Policies[0].PolicyDocument.Statement;

const schedulerPermissions = statements.filter(s => _.isEqual(s.Action, ['scheduler:DeleteSchedule']));
expect(schedulerPermissions[0].Resource).to.has.lengthOf(1);
expect(schedulerPermissions[0].Resource).to.deep.eq([{
'Fn::Sub': [
'arn:${AWS::Partition}:scheduler:${AWS::Region}:${AWS::AccountId}:schedule/${scheduleGroupName}/*',
{ scheduleGroupName: 'MyScheduleGroup' },
],
}]);
const rolePermissions = statements.filter(s => _.isEqual(s.Action, ['iam:PassRole']));
expect(rolePermissions).to.have.lengthOf(0);
});

it('should give event bridge scheduler deleteSchedule permissions with default group', () => {
const genStateMachine = id => ({
id,
definition: {
StartAt: 'A',
States: {
A: {
Type: 'Task',
Resource: 'arn:aws:states:::aws-sdk:scheduler:deleteSchedule',
Parameters: {
'Name.$': '$$.Execution.Name',
},
End: true,
},
},
},
});

serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: genStateMachine('StateMachine1'),
},
};

serverlessStepFunctions.compileIamRole();
const statements = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
.Properties.Policies[0].PolicyDocument.Statement;

const schedulerPermissions = statements.filter(s => _.isEqual(s.Action, ['scheduler:DeleteSchedule']));
expect(schedulerPermissions[0].Resource).to.has.lengthOf(1);
expect(schedulerPermissions[0].Resource).to.deep.eq([{
'Fn::Sub': [
'arn:${AWS::Partition}:scheduler:${AWS::Region}:${AWS::AccountId}:schedule/${scheduleGroupName}/*',
{ scheduleGroupName: 'default' },
],
}]);
});

it('should give http:invoke permissions with static ConnectionArn', () => {
serverless.service.stepFunctions = {
stateMachines: {
Expand Down
Loading