Skip to content

Commit

Permalink
fix(aws-apigateway-sqs): Remove /message path when delete method is n…
Browse files Browse the repository at this point in the history
…ot allowed (#1030)

* Fix remove unnecessary message path

* Update integration test expected results

* Fix lint issues
  • Loading branch information
joe-king-sh committed Nov 22, 2023
1 parent c350b81 commit f772200
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ export class ApiGatewayToSqs extends Construct {
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com')
});

// Setup the API Gateway resource
const apiGatewayResource = this.apiGateway.root.addResource('message');

// Create
const createRequestTemplate = props.createRequestTemplate ?? this.defaultCreateRequestTemplate;
if (props.allowCreateOperation && props.allowCreateOperation === true) {
Expand Down Expand Up @@ -281,6 +278,7 @@ export class ApiGatewayToSqs extends Construct {
// Delete
const deleteRequestTemplate = props.deleteRequestTemplate ?? this.defaultDeleteRequestTemplate;
if (props.allowDeleteOperation && props.allowDeleteOperation === true) {
const apiGatewayResource = this.apiGateway.root.addResource('message');
this.addActionToPolicy("sqs:DeleteMessage");
defaults.addProxyMethodToApiResource({
service: "sqs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ test('Test deployment for override ApiGateway deleteRequestTemplate', () => {
});
});

test('Test deployment for disallow delete operation', () => {
const stack = new Stack();

new ApiGatewayToSqs(stack, 'api-gateway-sqs', {
allowDeleteOperation: false
});
const template = Template.fromStack(stack);
const resources = template.findResources('AWS::ApiGateway::Resource', {
PathPart: "message"
});
expect(Object.keys(resources).length).toBe(0);
});

test('Test deployment for allow delete operation', () => {
const stack = new Stack();

new ApiGatewayToSqs(stack, 'api-gateway-sqs', {
allowDeleteOperation: true
});
const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::ApiGateway::Resource', {
PathPart: "message"
});
});

test('Test deployment with existing queue object', () => {
const stack = new Stack();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"Name": "RestApi"
}
},
"testapigatewaysqsdefaultRestApiDeploymentFB9688F5aa398c65c945d66dd485b50d59cc7a25": {
"testapigatewaysqsdefaultRestApiDeploymentFB9688F5ba8a42d07caa9db068377b2cabf4da74": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "Automatically created by the RestApi construct",
Expand All @@ -208,7 +208,6 @@
},
"DependsOn": [
"testapigatewaysqsdefaultRestApiGET733E6394",
"testapigatewaysqsdefaultRestApimessage41073D7F",
"testapigatewaysqsdefaultRestApiPOSTD8ACD1CB"
],
"Metadata": {
Expand All @@ -235,7 +234,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaysqsdefaultRestApiDeploymentFB9688F5aa398c65c945d66dd485b50d59cc7a25"
"Ref": "testapigatewaysqsdefaultRestApiDeploymentFB9688F5ba8a42d07caa9db068377b2cabf4da74"
},
"MethodSettings": [
{
Expand All @@ -252,21 +251,6 @@
"TracingEnabled": true
}
},
"testapigatewaysqsdefaultRestApimessage41073D7F": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"ParentId": {
"Fn::GetAtt": [
"testapigatewaysqsdefaultRestApi554243C3",
"RootResourceId"
]
},
"PathPart": "message",
"RestApiId": {
"Ref": "testapigatewaysqsdefaultRestApi554243C3"
}
}
},
"testapigatewaysqsdefaultRestApiPOSTD8ACD1CB": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"Name": "RestApi"
}
},
"testapigatewaysqsRestApiDeploymentCA19D3723284170350125cf6cf25e30c33a57efd": {
"testapigatewaysqsRestApiDeploymentCA19D37261367bad52ebfa5cbc65d48442b36029": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "Automatically created by the RestApi construct",
Expand Down Expand Up @@ -236,7 +236,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaysqsRestApiDeploymentCA19D3723284170350125cf6cf25e30c33a57efd"
"Ref": "testapigatewaysqsRestApiDeploymentCA19D37261367bad52ebfa5cbc65d48442b36029"
},
"MethodSettings": [
{
Expand All @@ -253,26 +253,11 @@
"TracingEnabled": true
}
},
"testapigatewaysqsRestApimessage6D62B7B0": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"ParentId": {
"Fn::GetAtt": [
"testapigatewaysqsRestApi557C7EDC",
"RootResourceId"
]
},
"PathPart": "message",
"RestApiId": {
"Ref": "testapigatewaysqsRestApi557C7EDC"
}
}
},
"testapigatewaysqsRestApimessageDELETE2D4539B7": {
"testapigatewaysqsRestApiPOST26D15DBA": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"AuthorizationType": "AWS_IAM",
"HttpMethod": "DELETE",
"HttpMethod": "POST",
"Integration": {
"Credentials": {
"Fn::GetAtt": [
Expand All @@ -298,7 +283,7 @@
"integration.request.header.Content-Type": "'application/x-www-form-urlencoded'"
},
"RequestTemplates": {
"application/json": "Action=DeleteMessage&ReceiptHandle=$util.urlEncode($input.params('receiptHandle'))"
"application/json": "Action=SendMessage&MessageBody=$util.urlEncode(\"$input.body\")"
},
"Type": "AWS",
"Uri": {
Expand Down Expand Up @@ -343,18 +328,21 @@
}
],
"ResourceId": {
"Ref": "testapigatewaysqsRestApimessage6D62B7B0"
"Fn::GetAtt": [
"testapigatewaysqsRestApi557C7EDC",
"RootResourceId"
]
},
"RestApiId": {
"Ref": "testapigatewaysqsRestApi557C7EDC"
}
}
},
"testapigatewaysqsRestApiPOST26D15DBA": {
"testapigatewaysqsRestApiGET4AA265C9": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"AuthorizationType": "AWS_IAM",
"HttpMethod": "POST",
"HttpMethod": "GET",
"Integration": {
"Credentials": {
"Fn::GetAtt": [
Expand All @@ -380,7 +368,7 @@
"integration.request.header.Content-Type": "'application/x-www-form-urlencoded'"
},
"RequestTemplates": {
"application/json": "Action=SendMessage&MessageBody=$util.urlEncode(\"$input.body\")"
"application/json": "Action=ReceiveMessage"
},
"Type": "AWS",
"Uri": {
Expand Down Expand Up @@ -435,11 +423,26 @@
}
}
},
"testapigatewaysqsRestApiGET4AA265C9": {
"testapigatewaysqsRestApimessage6D62B7B0": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"ParentId": {
"Fn::GetAtt": [
"testapigatewaysqsRestApi557C7EDC",
"RootResourceId"
]
},
"PathPart": "message",
"RestApiId": {
"Ref": "testapigatewaysqsRestApi557C7EDC"
}
}
},
"testapigatewaysqsRestApimessageDELETE2D4539B7": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"AuthorizationType": "AWS_IAM",
"HttpMethod": "GET",
"HttpMethod": "DELETE",
"Integration": {
"Credentials": {
"Fn::GetAtt": [
Expand All @@ -465,7 +468,7 @@
"integration.request.header.Content-Type": "'application/x-www-form-urlencoded'"
},
"RequestTemplates": {
"application/json": "Action=ReceiveMessage"
"application/json": "Action=DeleteMessage&ReceiptHandle=$util.urlEncode($input.params('receiptHandle'))"
},
"Type": "AWS",
"Uri": {
Expand Down Expand Up @@ -510,10 +513,7 @@
}
],
"ResourceId": {
"Fn::GetAtt": [
"testapigatewaysqsRestApi557C7EDC",
"RootResourceId"
]
"Ref": "testapigatewaysqsRestApimessage6D62B7B0"
},
"RestApiId": {
"Ref": "testapigatewaysqsRestApi557C7EDC"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"Name": "RestApi"
}
},
"testapigatewaysqsintegrationresponsesRestApiDeployment9749223507b4327bc896117bc13d1c395d6fa5e7": {
"testapigatewaysqsintegrationresponsesRestApiDeployment9749223547b7cf1e61211d0a1959e7fc8dd7abd0": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "Automatically created by the RestApi construct",
Expand All @@ -207,8 +207,7 @@
}
},
"DependsOn": [
"testapigatewaysqsintegrationresponsesRestApiGETD105D1F1",
"testapigatewaysqsintegrationresponsesRestApimessageC29A9FEB"
"testapigatewaysqsintegrationresponsesRestApiGETD105D1F1"
],
"Metadata": {
"cfn_nag": {
Expand All @@ -234,7 +233,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaysqsintegrationresponsesRestApiDeployment9749223507b4327bc896117bc13d1c395d6fa5e7"
"Ref": "testapigatewaysqsintegrationresponsesRestApiDeployment9749223547b7cf1e61211d0a1959e7fc8dd7abd0"
},
"MethodSettings": [
{
Expand All @@ -251,21 +250,6 @@
"TracingEnabled": true
}
},
"testapigatewaysqsintegrationresponsesRestApimessageC29A9FEB": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"ParentId": {
"Fn::GetAtt": [
"testapigatewaysqsintegrationresponsesRestApi3BE7E402",
"RootResourceId"
]
},
"PathPart": "message",
"RestApiId": {
"Ref": "testapigatewaysqsintegrationresponsesRestApi3BE7E402"
}
}
},
"testapigatewaysqsintegrationresponsesRestApiGETD105D1F1": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"Name": "RestApi"
}
},
"testapigatewaysqsexistingqueueRestApiDeploymentA3CB681F40a3b61200f0070439f29a9d27e30e73": {
"testapigatewaysqsexistingqueueRestApiDeploymentA3CB681Fb34af53058b656b91c9af037460db593": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "Automatically created by the RestApi construct",
Expand All @@ -119,8 +119,7 @@
}
},
"DependsOn": [
"testapigatewaysqsexistingqueueRestApiGET860786F8",
"testapigatewaysqsexistingqueueRestApimessageDF0AE465"
"testapigatewaysqsexistingqueueRestApiGET860786F8"
],
"Metadata": {
"cfn_nag": {
Expand All @@ -146,7 +145,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaysqsexistingqueueRestApiDeploymentA3CB681F40a3b61200f0070439f29a9d27e30e73"
"Ref": "testapigatewaysqsexistingqueueRestApiDeploymentA3CB681Fb34af53058b656b91c9af037460db593"
},
"MethodSettings": [
{
Expand All @@ -163,21 +162,6 @@
"TracingEnabled": true
}
},
"testapigatewaysqsexistingqueueRestApimessageDF0AE465": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"ParentId": {
"Fn::GetAtt": [
"testapigatewaysqsexistingqueueRestApi2E025EF5",
"RootResourceId"
]
},
"PathPart": "message",
"RestApiId": {
"Ref": "testapigatewaysqsexistingqueueRestApi2E025EF5"
}
}
},
"testapigatewaysqsexistingqueueRestApiGET860786F8": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"Name": "RestApi"
}
},
"testapigatewaysqsdefaultRestApiDeploymentFB9688F5638eca4a0d71dff702d4b2c6b1d1e2df": {
"testapigatewaysqsdefaultRestApiDeploymentFB9688F5fb9a1a72439389c4d385b8e23725ff77": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "Automatically created by the RestApi construct",
Expand All @@ -207,8 +207,7 @@
}
},
"DependsOn": [
"testapigatewaysqsdefaultRestApiGET733E6394",
"testapigatewaysqsdefaultRestApimessage41073D7F"
"testapigatewaysqsdefaultRestApiGET733E6394"
],
"Metadata": {
"cfn_nag": {
Expand All @@ -234,7 +233,7 @@
"Format": "{\"requestId\":\"$context.requestId\",\"ip\":\"$context.identity.sourceIp\",\"user\":\"$context.identity.user\",\"caller\":\"$context.identity.caller\",\"requestTime\":\"$context.requestTime\",\"httpMethod\":\"$context.httpMethod\",\"resourcePath\":\"$context.resourcePath\",\"status\":\"$context.status\",\"protocol\":\"$context.protocol\",\"responseLength\":\"$context.responseLength\"}"
},
"DeploymentId": {
"Ref": "testapigatewaysqsdefaultRestApiDeploymentFB9688F5638eca4a0d71dff702d4b2c6b1d1e2df"
"Ref": "testapigatewaysqsdefaultRestApiDeploymentFB9688F5fb9a1a72439389c4d385b8e23725ff77"
},
"MethodSettings": [
{
Expand All @@ -251,21 +250,6 @@
"TracingEnabled": true
}
},
"testapigatewaysqsdefaultRestApimessage41073D7F": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"ParentId": {
"Fn::GetAtt": [
"testapigatewaysqsdefaultRestApi554243C3",
"RootResourceId"
]
},
"PathPart": "message",
"RestApiId": {
"Ref": "testapigatewaysqsdefaultRestApi554243C3"
}
}
},
"testapigatewaysqsdefaultRestApiGET733E6394": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
Expand Down

0 comments on commit f772200

Please sign in to comment.