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
21 changes: 17 additions & 4 deletions lib/deploy/stepFunctions/compileIamRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,36 @@ const path = require('path');
module.exports = {
compileIamRole() {
const customRolesProvided = [];
let functionArns = [];
this.getAllStateMachines().forEach((stateMachineName) => {
const stateMachineObj = this.getStateMachine(stateMachineName);
customRolesProvided.push('role' in stateMachineObj);

const stateMachineJson = JSON.stringify(stateMachineObj);
const regex = new RegExp(/"Resource":"([a-z:#{}_\-.]*)"/gi);
let match = regex.exec(stateMachineJson);
while (match !== null) {
functionArns.push(match[1]);
match = regex.exec(stateMachineJson);
}
});
if (_.isEqual(_.uniq(customRolesProvided), [true])) {
return BbPromise.resolve();
}
functionArns = _.uniq(functionArns);

let iamRoleStateMachineExecutionTemplate = JSON.stringify(this.serverless.utils.readFileSync(
let iamRoleStateMachineExecutionTemplate = this.serverless.utils.readFileSync(
path.join(__dirname,
'..',
'..',
'iam-role-statemachine-execution-template.json'))
'iam-role-statemachine-execution-template.txt')
);

iamRoleStateMachineExecutionTemplate =
iamRoleStateMachineExecutionTemplate.replace('[region]', this.options.region)
.replace('[PolicyName]', this.getStateMachinePolicyName());
iamRoleStateMachineExecutionTemplate
.replace('[region]', this.options.region)
.replace('[PolicyName]', this.getStateMachinePolicyName())
.replace('[functions]', JSON.stringify(functionArns));

const iamRoleStateMachineLogicalId = this.getiamRoleStateMachineLogicalId();
const newIamRoleStateMachineExecutionObject = {
Expand Down
42 changes: 42 additions & 0 deletions lib/deploy/stepFunctions/compileIamRole.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,46 @@ describe('#compileIamRole', () => {
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution.Type
).to.equal('AWS::IAM::Role');
});

it('should give invokeFunction permission for only functions referenced by state machine', () => {
const helloLambda = 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello';
const worldLambda = 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:world';
serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
name: 'stateMachineBeta1',
definition: {
StartAt: 'Hello',
States: {
Hello: {
Type: 'Task',
Resource: helloLambda,
End: true,
},
},
},
},
myStateMachine2: {
name: 'stateMachineBeta2',
definition: {
StartAt: 'World',
States: {
World: {
Type: 'Task',
Resource: worldLambda,
End: true,
},
},
},
},
},
};

serverlessStepFunctions.compileIamRole();
const policy = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
.Properties.Policies[0];
expect(policy.PolicyDocument.Statement[0].Resource)
.to.be.deep.equal([helloLambda, worldLambda]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Action": [
"lambda:InvokeFunction"
],
"Resource": "*"
"Resource": [functions]
}
]
}
Expand Down