Skip to content

Fix SQS QueueUrl transformation to ARN when intrinsic function being passed in #261

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

Merged
merged 1 commit into from
Oct 1, 2019
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
18 changes: 12 additions & 6 deletions lib/deploy/stepFunctions/compileIamRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ function sqsQueueUrlToArn(serverless, queueUrl) {
const accountId = match[2];
const queueName = match[3];
return `arn:aws:sqs:${region}:${accountId}:${queueName}`;
} if (isIntrinsic(queueUrl) && queueUrl.Ref) {
// most likely we'll see a { Ref: LogicalId }, which we need to map to
// { Fn::GetAtt: [ LogicalId, Arn ] } to get the ARN
return {
'Fn::GetAtt': [queueUrl.Ref, 'Arn'],
};
}
if (isIntrinsic(queueUrl)) {
if (queueUrl.Ref) {
// most likely we'll see a { Ref: LogicalId }, which we need to map to
// { Fn::GetAtt: [ LogicalId, Arn ] } to get the ARN
return {
'Fn::GetAtt': [queueUrl.Ref, 'Arn'],
};
}
// in case of for example { Fn::ImportValue: sharedValueToImport }
// we need to use "*" as ARN
return '*';
Copy link
Collaborator

Choose a reason for hiding this comment

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

ah, now I see why you want to add both QueueUrl as well as QueueArn, so you can ref the Arn in the IAM instead of QueueUrl. It'd be doable for sure, but it'd be a bit odd though, and hard to explain to people. Let's leave it as it is for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's what I meant. I agree with you. 🙂 👍

}
serverless.cli.consoleLog(`Unable to parse SQS queue url [${queueUrl}]`);
return [];
Expand Down
50 changes: 50 additions & 0 deletions lib/deploy/stepFunctions/compileIamRole.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,56 @@ describe('#compileIamRole', () => {
expect(policy.PolicyDocument.Statement[0].Resource).to.equal('*');
});

it('should give sqs:SendMessage permission to * whenever QueueUrl is some intrinsic function except Ref', () => {
const helloQueue = 'https://sqs.#{AWS::Region}.amazonaws.com/#{AWS::AccountId}/hello';
const worldQueue = 'https://sqs.us-east-1.amazonaws.com/#{AWS::AccountId}/world';

const genStateMachine = (name, queueUrl) => ({
name,
definition: {
StartAt: 'A',
States: {
A: {
Type: 'Task',
Resource: 'arn:aws:states:::sqs:sendMessage',
Parameters: {
QueueUrl: queueUrl,
Message: '42',
},
Next: 'B',
},
B: {
Type: 'Task',
Resource: 'arn:aws:states:::sqs:sendMessage',
Parameters: {
QueueUrl: {
'Fn::ImportValue': 'some-shared-value-here',
},
Message: '42',
},
End: true,
},
},
},
});

serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: genStateMachine('stateMachineBeta1', helloQueue),
myStateMachine2: genStateMachine('stateMachineBeta2', worldQueue),
},
};

serverlessStepFunctions.compileIamRole();
const policy = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
.Properties.Policies[0];

// when using instrinct functions other than Ref to define QueueUrl
// we can't recontruct ARN from it, so we need to give broad permissions
expect(policy.PolicyDocument.Statement[0].Resource).to.equal('*');
});

it('should not give sqs:SendMessage permission if QueueUrl and QueueUrl.$ are missing', () => {
const genStateMachine = name => ({
name,
Expand Down