-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Defining CORS when ApiKeyRequired is true results in an OPTIONS …
…method that requires an API key (#2981) Co-authored-by: Christoffer Rehn <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,812 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
integration/resources/expected/combination/api_with_cors_and_apikey.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[ | ||
{ | ||
"LogicalResourceId": "MyApi", | ||
"ResourceType": "AWS::ApiGateway::RestApi" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyApiDeployment", | ||
"ResourceType": "AWS::ApiGateway::Deployment" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyApidevStage", | ||
"ResourceType": "AWS::ApiGateway::Stage" | ||
}, | ||
{ | ||
"LogicalResourceId": "ApiGatewayLambdaRole", | ||
"ResourceType": "AWS::IAM::Role" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyFunction", | ||
"ResourceType": "AWS::Lambda::Function" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyFunctionRole", | ||
"ResourceType": "AWS::IAM::Role" | ||
} | ||
] |
87 changes: 87 additions & 0 deletions
87
integration/resources/templates/combination/api_with_cors_and_apikey.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
|
||
Transform: | ||
- AWS::Serverless-2016-10-31 | ||
|
||
Globals: | ||
Api: | ||
Auth: | ||
ApiKeyRequired: true | ||
AddApiKeyRequiredToCorsPreflight: false | ||
|
||
Resources: | ||
|
||
MyFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: index.handler | ||
InlineCode: | | ||
exports.handler = async function (event) { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: "Hello, SAM!" }), | ||
} | ||
} | ||
Runtime: nodejs16.x | ||
|
||
ApiGatewayLambdaRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Principal: {Service: apigateway.amazonaws.com} | ||
Action: sts:AssumeRole | ||
Policies: | ||
- PolicyName: AllowInvokeLambdaFunctions | ||
PolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Action: lambda:InvokeFunction | ||
Resource: '*' | ||
|
||
MyApi: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
Cors: | ||
AllowMethods: "'methods'" | ||
AllowHeaders: "'headers'" | ||
AllowOrigin: "'origins'" | ||
MaxAge: "'600'" | ||
Auth: | ||
ApiKeyRequired: true | ||
StageName: dev | ||
DefinitionBody: | ||
openapi: 3.0.1 | ||
paths: | ||
/apione: | ||
get: | ||
x-amazon-apigateway-integration: | ||
credentials: | ||
Fn::Sub: ${ApiGatewayLambdaRole.Arn} | ||
uri: | ||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations | ||
passthroughBehavior: when_no_match | ||
httpMethod: POST | ||
type: aws_proxy | ||
/apitwo: | ||
get: | ||
x-amazon-apigateway-integration: | ||
credentials: | ||
Fn::Sub: ${ApiGatewayLambdaRole.Arn} | ||
uri: | ||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations | ||
passthroughBehavior: when_no_match | ||
httpMethod: POST | ||
type: aws_proxy | ||
|
||
|
||
|
||
Outputs: | ||
ApiUrl: | ||
Description: URL of your API endpoint | ||
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/dev/" | ||
Metadata: | ||
SamTransformTest: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
|
||
Transform: | ||
- AWS::Serverless-2016-10-31 | ||
|
||
Globals: | ||
Api: | ||
Auth: | ||
ApiKeyRequired: true | ||
AddApiKeyRequiredToCorsPreflight: false | ||
|
||
Resources: | ||
|
||
MyFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: index.handler | ||
InlineCode: | | ||
exports.handler = async function (event) { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: "Hello, SAM!" }), | ||
} | ||
} | ||
Runtime: nodejs16.x | ||
|
||
ApiGatewayLambdaRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Principal: {Service: apigateway.amazonaws.com} | ||
Action: sts:AssumeRole | ||
Policies: | ||
- PolicyName: AllowInvokeLambdaFunctions | ||
PolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Action: lambda:InvokeFunction | ||
Resource: '*' | ||
|
||
MyApi: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
Cors: | ||
AllowMethods: "'methods'" | ||
AllowHeaders: "'headers'" | ||
AllowOrigin: "'origins'" | ||
MaxAge: "'600'" | ||
Auth: | ||
ApiKeyRequired: true | ||
StageName: dev | ||
DefinitionBody: | ||
openapi: 3.0.1 | ||
paths: | ||
/apione: | ||
get: | ||
x-amazon-apigateway-integration: | ||
credentials: | ||
Fn::Sub: ${ApiGatewayLambdaRole.Arn} | ||
uri: | ||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations | ||
passthroughBehavior: when_no_match | ||
httpMethod: POST | ||
type: aws_proxy | ||
/apitwo: | ||
get: | ||
x-amazon-apigateway-integration: | ||
credentials: | ||
Fn::Sub: ${ApiGatewayLambdaRole.Arn} | ||
uri: | ||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations | ||
passthroughBehavior: when_no_match | ||
httpMethod: POST | ||
type: aws_proxy | ||
|
||
|
||
|
||
Outputs: | ||
ApiUrl: | ||
Description: URL of your API endpoint | ||
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/dev/" | ||
Metadata: | ||
SamTransformTest: true |
66 changes: 66 additions & 0 deletions
66
tests/translator/input/api_with_cors_and_apikey_defined_at_api_level.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
|
||
Transform: | ||
- AWS::Serverless-2016-10-31 | ||
|
||
Resources: | ||
|
||
MyFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: index.handler | ||
InlineCode: | | ||
exports.handler = async function (event) { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: "Hello, SAM!" }), | ||
} | ||
} | ||
Runtime: nodejs16.x | ||
|
||
ApiGatewayLambdaRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Principal: {Service: apigateway.amazonaws.com} | ||
Action: sts:AssumeRole | ||
Policies: | ||
- PolicyName: AllowInvokeLambdaFunctions | ||
PolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Action: lambda:InvokeFunction | ||
Resource: '*' | ||
|
||
MyApi: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
Cors: "'*'" | ||
Auth: | ||
ApiKeyRequired: true | ||
AddApiKeyRequiredToCorsPreflight: false | ||
StageName: dev | ||
DefinitionBody: | ||
openapi: 3.0.1 | ||
paths: | ||
/hello: | ||
get: | ||
x-amazon-apigateway-integration: | ||
credentials: | ||
Fn::Sub: ${ApiGatewayLambdaRole.Arn} | ||
uri: | ||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations | ||
passthroughBehavior: when_no_match | ||
httpMethod: POST | ||
type: aws_proxy | ||
|
||
|
||
|
||
Outputs: | ||
WebEndpoint: | ||
Description: API Gateway endpoint URL | ||
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/dev/hello" |
Oops, something went wrong.