Skip to content

Commit 6499b50

Browse files
authored
fix: normalize the open api version (#3551)
1 parent e302b20 commit 6499b50

7 files changed

+1161
-7
lines changed

samtranslator/model/api/api_generator.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ def __init__( # noqa: PLR0913
251251
self.resource_attributes = resource_attributes
252252
self.passthrough_resource_attributes = passthrough_resource_attributes
253253
self.open_api_version = open_api_version
254-
self.remove_extra_stage = open_api_version
255254
self.models = models
256255
self.domain = domain
257256
self.fail_on_warnings = fail_on_warnings
@@ -400,7 +399,7 @@ def _construct_deployment(self, rest_api: ApiGatewayRestApi) -> ApiGatewayDeploy
400399
self.logical_id + "Deployment", attributes=self.passthrough_resource_attributes
401400
)
402401
deployment.RestApiId = rest_api.get_runtime_attr("rest_api_id")
403-
if not self.remove_extra_stage:
402+
if not self.open_api_version:
404403
deployment.StageName = "Stage"
405404

406405
return deployment
@@ -438,7 +437,7 @@ def _construct_stage(
438437
if swagger is not None:
439438
deployment.make_auto_deployable(
440439
stage,
441-
self.remove_extra_stage,
440+
self.open_api_version,
442441
swagger,
443442
self.domain,
444443
redeploy_restapi_parameters,
@@ -1125,11 +1124,10 @@ def _openapi_postprocess(self, definition_body: Dict[str, Any]) -> Dict[str, Any
11251124
if definition_body.get("swagger") is not None:
11261125
return definition_body
11271126

1128-
if definition_body.get("openapi") is not None and self.open_api_version is None:
1129-
self.open_api_version = definition_body.get("openapi")
1127+
normalized_open_api_version = definition_body.get("openapi", self.open_api_version)
11301128

1131-
if self.open_api_version and SwaggerEditor.safe_compare_regex_with_string(
1132-
SwaggerEditor._OPENAPI_VERSION_3_REGEX, self.open_api_version
1129+
if normalized_open_api_version and SwaggerEditor.safe_compare_regex_with_string(
1130+
SwaggerEditor._OPENAPI_VERSION_3_REGEX, normalized_open_api_version
11331131
):
11341132
if definition_body.get("securityDefinitions"):
11351133
components = definition_body.get("components", Py27Dict())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
Transform:
2+
- AWS::Serverless-2016-10-31
3+
Resources:
4+
ApiGatewayCognitoExecutionRole4F7CB5C8:
5+
Type: AWS::IAM::Role
6+
Properties:
7+
AssumeRolePolicyDocument:
8+
Statement:
9+
- Action: sts:AssumeRole
10+
Effect: Allow
11+
Principal:
12+
Service: apigateway.amazonaws.com
13+
Version: '2012-10-17'
14+
Policies:
15+
- PolicyDocument:
16+
Statement:
17+
- Action: lambda:Invoke*
18+
Effect: Allow
19+
Resource:
20+
Fn::GetAtt:
21+
- LambdaFunction7804BD21
22+
- Arn
23+
Version: '2012-10-17'
24+
PolicyName: apigInvokeLambda
25+
LambdaFunctionServiceRoleD6E423C9:
26+
Type: AWS::IAM::Role
27+
Properties:
28+
AssumeRolePolicyDocument:
29+
Statement:
30+
- Action: sts:AssumeRole
31+
Effect: Allow
32+
Principal:
33+
Service: lambda.amazonaws.com
34+
Version: '2012-10-17'
35+
ManagedPolicyArns:
36+
- Fn::Join:
37+
- ''
38+
- - 'arn:'
39+
- Ref: AWS::Partition
40+
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
41+
LambdaFunctionServiceRoleDefaultPolicyF01A7EDC:
42+
Type: AWS::IAM::Policy
43+
Properties:
44+
PolicyDocument:
45+
Statement:
46+
- Action: sns:Publish
47+
Effect: Allow
48+
Resource: '*'
49+
Version: '2012-10-17'
50+
PolicyName: LambdaFunctionServiceRoleDefaultPolicyF01A7EDC
51+
Roles:
52+
- Ref: LambdaFunctionServiceRoleD6E423C9
53+
LambdaFunction7804BD21:
54+
Type: AWS::Lambda::Function
55+
Properties:
56+
Code:
57+
ZipFile: |
58+
exports.handler = async (event, context, callback) => {
59+
const auth = event.queryStringParameters.authorization
60+
const policyDocument = {
61+
Version: '2012-10-17',
62+
Statement: [{
63+
Action: 'execute-api:Invoke',
64+
Effect: auth && auth.toLowerCase() === 'allow' ? 'Allow' : 'Deny',
65+
Resource: event.methodArn
66+
}]
67+
}
68+
69+
return {
70+
principalId: 'user',
71+
context: {},
72+
policyDocument
73+
}
74+
}
75+
Role:
76+
Fn::GetAtt:
77+
- LambdaFunctionServiceRoleD6E423C9
78+
- Arn
79+
Handler: index.handler
80+
Runtime: nodejs16.x
81+
MyCognitoUserPool:
82+
Type: AWS::Cognito::UserPool
83+
Properties:
84+
UserPoolName: MyCognitoUserPool
85+
ApiGatewayCognitoService15108F0B:
86+
Type: AWS::Serverless::Api
87+
Properties:
88+
StageName: prod
89+
Auth:
90+
AddDefaultAuthorizerToCorsPreflight: false
91+
Authorizers:
92+
CognitoAuthorizer:
93+
UserPoolArn:
94+
Fn::GetAtt: MyCognitoUserPool.Arn
95+
DefaultAuthorizer: CognitoAuthorizer
96+
DefinitionBody:
97+
openapi: 3.0.2
98+
info:
99+
title: RxtHofApprovalServiceLambdaCognito
100+
version: '2018-05-10'
101+
paths:
102+
/reviews:
103+
post:
104+
operationId: CreateReview
105+
requestBody:
106+
content:
107+
application/json:
108+
schema:
109+
$ref: '#/components/schemas/CreateReviewRequestContent'
110+
required: true
111+
responses:
112+
'200':
113+
description: CreateReview 200 response
114+
headers:
115+
Access-Control-Allow-Origin:
116+
schema:
117+
type: string
118+
Access-Control-Expose-Headers:
119+
schema:
120+
type: string
121+
content:
122+
application/json:
123+
schema:
124+
$ref: '#/components/schemas/CreateReviewResponseContent'
125+
x-amazon-apigateway-integration:
126+
type: aws_proxy
127+
httpMethod: POST
128+
uri:
129+
Fn::Sub: arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction7804BD21.Arn}/invocations
130+
credentials:
131+
Fn::Sub: ${ApiGatewayCognitoExecutionRole4F7CB5C8.Arn}
132+
responses:
133+
default:
134+
statusCode: '200'
135+
responseParameters:
136+
method.response.header.Access-Control-Allow-Origin: "'*'"
137+
method.response.header.Access-Control-Expose-Headers: "'Content-Length,Content-Type,X-Amzn-Errortype,X-Amzn-Requestid'"
138+
components:
139+
schemas:
140+
CreateReviewRequestContent:
141+
type: object
142+
properties:
143+
reviewId:
144+
type: string
145+
CreateReviewResponseContent:
146+
type: object
147+
properties:
148+
reviewId:
149+
type: string
150+
securitySchemes:
151+
aws.auth.sigv4:
152+
type: apiKey
153+
description: AWS Signature Version 4 authentication
154+
name: Authorization
155+
in: header
156+
x-amazon-apigateway-authtype: awsSigv4
157+
security:
158+
- aws.auth.sigv4: []
159+
x-amazon-apigateway-gateway-responses:
160+
DEFAULT_5XX:
161+
responseTemplates:
162+
application/json: '{"message":$context.error.messageString}'
163+
responseParameters:
164+
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
165+
OpenApiVersion: '2.0'
166+
TracingEnabled: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
Transform:
2+
- AWS::Serverless-2016-10-31
3+
Resources:
4+
ApiGatewayCognitoExecutionRole4F7CB5C8:
5+
Type: AWS::IAM::Role
6+
Properties:
7+
AssumeRolePolicyDocument:
8+
Statement:
9+
- Action: sts:AssumeRole
10+
Effect: Allow
11+
Principal:
12+
Service: apigateway.amazonaws.com
13+
Version: '2012-10-17'
14+
Policies:
15+
- PolicyDocument:
16+
Statement:
17+
- Action: lambda:Invoke*
18+
Effect: Allow
19+
Resource:
20+
Fn::GetAtt:
21+
- LambdaFunction7804BD21
22+
- Arn
23+
Version: '2012-10-17'
24+
PolicyName: apigInvokeLambda
25+
LambdaFunctionServiceRoleD6E423C9:
26+
Type: AWS::IAM::Role
27+
Properties:
28+
AssumeRolePolicyDocument:
29+
Statement:
30+
- Action: sts:AssumeRole
31+
Effect: Allow
32+
Principal:
33+
Service: lambda.amazonaws.com
34+
Version: '2012-10-17'
35+
ManagedPolicyArns:
36+
- Fn::Join:
37+
- ''
38+
- - 'arn:'
39+
- Ref: AWS::Partition
40+
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
41+
LambdaFunctionServiceRoleDefaultPolicyF01A7EDC:
42+
Type: AWS::IAM::Policy
43+
Properties:
44+
PolicyDocument:
45+
Statement:
46+
- Action: sns:Publish
47+
Effect: Allow
48+
Resource: '*'
49+
Version: '2012-10-17'
50+
PolicyName: LambdaFunctionServiceRoleDefaultPolicyF01A7EDC
51+
Roles:
52+
- Ref: LambdaFunctionServiceRoleD6E423C9
53+
LambdaFunction7804BD21:
54+
Type: AWS::Lambda::Function
55+
Properties:
56+
Code:
57+
ZipFile: |
58+
exports.handler = async (event, context, callback) => {
59+
const auth = event.queryStringParameters.authorization
60+
const policyDocument = {
61+
Version: '2012-10-17',
62+
Statement: [{
63+
Action: 'execute-api:Invoke',
64+
Effect: auth && auth.toLowerCase() === 'allow' ? 'Allow' : 'Deny',
65+
Resource: event.methodArn
66+
}]
67+
}
68+
69+
return {
70+
principalId: 'user',
71+
context: {},
72+
policyDocument
73+
}
74+
}
75+
Role:
76+
Fn::GetAtt:
77+
- LambdaFunctionServiceRoleD6E423C9
78+
- Arn
79+
Handler: index.handler
80+
Runtime: nodejs16.x
81+
MyCognitoUserPool:
82+
Type: AWS::Cognito::UserPool
83+
Properties:
84+
UserPoolName: MyCognitoUserPool
85+
ApiGatewayCognitoService15108F0B:
86+
Type: AWS::Serverless::Api
87+
Properties:
88+
StageName: prod
89+
Auth:
90+
AddDefaultAuthorizerToCorsPreflight: false
91+
Authorizers:
92+
CognitoAuthorizer:
93+
UserPoolArn:
94+
Fn::GetAtt: MyCognitoUserPool.Arn
95+
DefaultAuthorizer: CognitoAuthorizer
96+
DefinitionBody:
97+
info:
98+
title: RxtHofApprovalServiceLambdaCognito
99+
version: '2018-05-10'
100+
paths:
101+
/reviews:
102+
post:
103+
operationId: CreateReview
104+
requestBody:
105+
content:
106+
application/json:
107+
schema:
108+
$ref: '#/components/schemas/CreateReviewRequestContent'
109+
required: true
110+
responses:
111+
'200':
112+
description: CreateReview 200 response
113+
headers:
114+
Access-Control-Allow-Origin:
115+
schema:
116+
type: string
117+
Access-Control-Expose-Headers:
118+
schema:
119+
type: string
120+
content:
121+
application/json:
122+
schema:
123+
$ref: '#/components/schemas/CreateReviewResponseContent'
124+
x-amazon-apigateway-integration:
125+
type: aws_proxy
126+
httpMethod: POST
127+
uri:
128+
Fn::Sub: arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction7804BD21.Arn}/invocations
129+
credentials:
130+
Fn::Sub: ${ApiGatewayCognitoExecutionRole4F7CB5C8.Arn}
131+
responses:
132+
default:
133+
statusCode: '200'
134+
responseParameters:
135+
method.response.header.Access-Control-Allow-Origin: "'*'"
136+
method.response.header.Access-Control-Expose-Headers: "'Content-Length,Content-Type,X-Amzn-Errortype,X-Amzn-Requestid'"
137+
components:
138+
schemas:
139+
CreateReviewRequestContent:
140+
type: object
141+
properties:
142+
reviewId:
143+
type: string
144+
CreateReviewResponseContent:
145+
type: object
146+
properties:
147+
reviewId:
148+
type: string
149+
securitySchemes:
150+
aws.auth.sigv4:
151+
type: apiKey
152+
description: AWS Signature Version 4 authentication
153+
name: Authorization
154+
in: header
155+
x-amazon-apigateway-authtype: awsSigv4
156+
security:
157+
- aws.auth.sigv4: []
158+
x-amazon-apigateway-gateway-responses:
159+
DEFAULT_5XX:
160+
responseTemplates:
161+
application/json: '{"message":$context.error.messageString}'
162+
responseParameters:
163+
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
164+
OpenApiVersion: '3.0'
165+
TracingEnabled: true

0 commit comments

Comments
 (0)