Skip to content

Commit 5dd8e54

Browse files
Merge pull request #261 from purple-technology/master
Fix SQS QueueUrl transformation to ARN when intrinsic function being passed in
2 parents 279f5c2 + 90e9c64 commit 5dd8e54

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

lib/deploy/stepFunctions/compileIamRole.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ function sqsQueueUrlToArn(serverless, queueUrl) {
3535
const accountId = match[2];
3636
const queueName = match[3];
3737
return `arn:aws:sqs:${region}:${accountId}:${queueName}`;
38-
} if (isIntrinsic(queueUrl) && queueUrl.Ref) {
39-
// most likely we'll see a { Ref: LogicalId }, which we need to map to
40-
// { Fn::GetAtt: [ LogicalId, Arn ] } to get the ARN
41-
return {
42-
'Fn::GetAtt': [queueUrl.Ref, 'Arn'],
43-
};
38+
}
39+
if (isIntrinsic(queueUrl)) {
40+
if (queueUrl.Ref) {
41+
// most likely we'll see a { Ref: LogicalId }, which we need to map to
42+
// { Fn::GetAtt: [ LogicalId, Arn ] } to get the ARN
43+
return {
44+
'Fn::GetAtt': [queueUrl.Ref, 'Arn'],
45+
};
46+
}
47+
// in case of for example { Fn::ImportValue: sharedValueToImport }
48+
// we need to use "*" as ARN
49+
return '*';
4450
}
4551
serverless.cli.consoleLog(`Unable to parse SQS queue url [${queueUrl}]`);
4652
return [];

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,56 @@ describe('#compileIamRole', () => {
362362
expect(policy.PolicyDocument.Statement[0].Resource).to.equal('*');
363363
});
364364

365+
it('should give sqs:SendMessage permission to * whenever QueueUrl is some intrinsic function except Ref', () => {
366+
const helloQueue = 'https://sqs.#{AWS::Region}.amazonaws.com/#{AWS::AccountId}/hello';
367+
const worldQueue = 'https://sqs.us-east-1.amazonaws.com/#{AWS::AccountId}/world';
368+
369+
const genStateMachine = (name, queueUrl) => ({
370+
name,
371+
definition: {
372+
StartAt: 'A',
373+
States: {
374+
A: {
375+
Type: 'Task',
376+
Resource: 'arn:aws:states:::sqs:sendMessage',
377+
Parameters: {
378+
QueueUrl: queueUrl,
379+
Message: '42',
380+
},
381+
Next: 'B',
382+
},
383+
B: {
384+
Type: 'Task',
385+
Resource: 'arn:aws:states:::sqs:sendMessage',
386+
Parameters: {
387+
QueueUrl: {
388+
'Fn::ImportValue': 'some-shared-value-here',
389+
},
390+
Message: '42',
391+
},
392+
End: true,
393+
},
394+
},
395+
},
396+
});
397+
398+
serverless.service.stepFunctions = {
399+
stateMachines: {
400+
myStateMachine1: genStateMachine('stateMachineBeta1', helloQueue),
401+
myStateMachine2: genStateMachine('stateMachineBeta2', worldQueue),
402+
},
403+
};
404+
405+
serverlessStepFunctions.compileIamRole();
406+
const policy = serverlessStepFunctions.serverless.service
407+
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
408+
.Properties.Policies[0];
409+
410+
// when using instrinct functions other than Ref to define QueueUrl
411+
// we can't recontruct ARN from it, so we need to give broad permissions
412+
expect(policy.PolicyDocument.Statement[0].Resource).to.equal('*');
413+
});
414+
365415
it('should not give sqs:SendMessage permission if QueueUrl and QueueUrl.$ are missing', () => {
366416
const genStateMachine = name => ({
367417
name,

0 commit comments

Comments
 (0)