-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: Py27hash fix #2182
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
fix: Py27hash fix #2182
Changes from 10 commits
4cdc686
cc06ac0
52d0e7a
e066921
71e6fbd
94cf884
687fd60
754922c
7503e2f
be860a2
e21ed03
18ecddb
d866388
ff9ef73
61e3d99
9cfc04a
ab54931
3aa24c6
80451e4
fe74979
dd6c992
621df32
1defc47
46e0421
8e09c99
59c4062
bb58ec5
41f7857
b9dc3ea
c79cb44
596f93d
62b4953
9181397
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from samtranslator.model.intrinsics import ref, fnSub | ||
from samtranslator.translator import logical_id_generator | ||
from samtranslator.translator.arn_generator import ArnGenerator | ||
from samtranslator.utils.py27hash_fix import Py27Dict, Py27UniStr | ||
|
||
|
||
class ApiGatewayRestApi(Resource): | ||
|
@@ -133,10 +134,10 @@ def __init__(self, api_logical_id=None, response_parameters=None, response_templ | |
self.status_code = status_code_str | ||
|
||
def generate_swagger(self): | ||
swagger = { | ||
"responseParameters": self._add_prefixes(self.response_parameters), | ||
"responseTemplates": self.response_templates, | ||
} | ||
# Applying Py27Dict here as this goes into swagger | ||
swagger = Py27Dict() | ||
swagger["responseParameters"] = self._add_prefixes(self.response_parameters) | ||
swagger["responseTemplates"] = self.response_templates | ||
|
||
# Prevent "null" being written. | ||
if self.status_code: | ||
|
@@ -146,13 +147,26 @@ def generate_swagger(self): | |
|
||
def _add_prefixes(self, response_parameters): | ||
GATEWAY_RESPONSE_PREFIX = "gatewayresponse." | ||
prefixed_parameters = {} | ||
# applying Py27Dict as this is part of swagger | ||
prefixed_parameters = Py27Dict() | ||
for key, value in response_parameters.get("Headers", {}).items(): | ||
prefixed_parameters[GATEWAY_RESPONSE_PREFIX + "header." + key] = value | ||
param_key = GATEWAY_RESPONSE_PREFIX + "header." + key | ||
if isinstance(key, Py27UniStr): | ||
# if key is from template, we need to convert param_key to Py27UniStr | ||
param_key = Py27UniStr(param_key) | ||
prefixed_parameters[param_key] = value | ||
|
||
for key, value in response_parameters.get("Paths", {}).items(): | ||
prefixed_parameters[GATEWAY_RESPONSE_PREFIX + "path." + key] = value | ||
param_key = GATEWAY_RESPONSE_PREFIX + "path." + key | ||
if isinstance(key, Py27UniStr): | ||
param_key = Py27UniStr(param_key) | ||
prefixed_parameters[param_key] = value | ||
|
||
for key, value in response_parameters.get("QueryStrings", {}).items(): | ||
prefixed_parameters[GATEWAY_RESPONSE_PREFIX + "querystring." + key] = value | ||
param_key = GATEWAY_RESPONSE_PREFIX + "querystring." + key | ||
if isinstance(key, Py27UniStr): | ||
param_key = Py27UniStr(param_key) | ||
prefixed_parameters[param_key] = value | ||
|
||
return prefixed_parameters | ||
|
||
|
@@ -287,21 +301,20 @@ def _is_missing_identity_source(self, identity): | |
def generate_swagger(self): | ||
authorizer_type = self._get_type() | ||
APIGATEWAY_AUTHORIZER_KEY = "x-amazon-apigateway-authorizer" | ||
swagger = { | ||
"type": "apiKey", | ||
"name": self._get_swagger_header_name(), | ||
"in": "header", | ||
"x-amazon-apigateway-authtype": self._get_swagger_authtype(), | ||
} | ||
swagger = Py27Dict() | ||
swagger["type"] = "apiKey" | ||
swagger["name"] = self._get_swagger_header_name() | ||
swagger["in"] = "header" | ||
swagger["x-amazon-apigateway-authtype"] = self._get_swagger_authtype() | ||
|
||
if authorizer_type == "COGNITO_USER_POOLS": | ||
swagger[APIGATEWAY_AUTHORIZER_KEY] = { | ||
"type": self._get_swagger_authorizer_type(), | ||
"providerARNs": self._get_user_pool_arn_array(), | ||
} | ||
authorizer_dict = Py27Dict() | ||
authorizer_dict["type"] = self._get_swagger_authorizer_type() | ||
authorizer_dict["providerARNs"] = self._get_user_pool_arn_array() | ||
swagger[APIGATEWAY_AUTHORIZER_KEY] = authorizer_dict | ||
|
||
elif authorizer_type == "LAMBDA": | ||
swagger[APIGATEWAY_AUTHORIZER_KEY] = {"type": self._get_swagger_authorizer_type()} | ||
swagger[APIGATEWAY_AUTHORIZER_KEY] = Py27Dict({"type": self._get_swagger_authorizer_type()}) | ||
partition = ArnGenerator.get_partition_name() | ||
resource = "lambda:path/2015-03-31/functions/${__FunctionArn__}/invocations" | ||
authorizer_uri = fnSub( | ||
|
@@ -347,20 +360,36 @@ def _get_identity_source(self): | |
identity_source_context = [] | ||
|
||
if self.identity.get("Headers"): | ||
identity_source_headers = list(map(lambda h: "method.request.header." + h, self.identity.get("Headers"))) | ||
identity_source_headers = [] | ||
for header in self.identity.get("Headers"): | ||
item = "method.request.header." + header | ||
if isinstance(header, Py27UniStr): | ||
item = Py27UniStr(item) | ||
identity_source_headers.append(item) | ||
|
||
if self.identity.get("QueryStrings"): | ||
identity_source_query_strings = list( | ||
map(lambda qs: "method.request.querystring." + qs, self.identity.get("QueryStrings")) | ||
) | ||
identity_source_query_strings = [] | ||
for qs in self.identity.get("QueryStrings"): | ||
item = "method.request.querystring." + qs | ||
if isinstance(qs, Py27UniStr): | ||
item = Py27UniStr(item) | ||
identity_source_query_strings.append(item) | ||
|
||
if self.identity.get("StageVariables"): | ||
identity_source_stage_variables = list( | ||
map(lambda sv: "stageVariables." + sv, self.identity.get("StageVariables")) | ||
) | ||
identity_source_stage_variables = [] | ||
for sv in self.identity.get("StageVariables"): | ||
item = "stageVariables." + sv | ||
if isinstance(sv, Py27UniStr): | ||
item = Py27UniStr(item) | ||
identity_source_stage_variables.append(item) | ||
|
||
if self.identity.get("Context"): | ||
identity_source_context = list(map(lambda c: "context." + c, self.identity.get("Context"))) | ||
identity_source_context = [] | ||
for c in self.identity.get("Context"): | ||
|
||
item = "context." + c | ||
if isinstance(c, Py27UniStr): | ||
item = Py27UniStr(item) | ||
identity_source_context.append(item) | ||
|
||
identity_source_array = ( | ||
identity_source_headers | ||
|
@@ -369,6 +398,9 @@ def _get_identity_source(self): | |
+ identity_source_context | ||
) | ||
identity_source = ", ".join(identity_source_array) | ||
if any(isinstance(i, Py27UniStr) for i in identity_source_array): | ||
# Convert identity_source to Py27UniStr if any part of it is Py27UniStr | ||
identity_source = Py27UniStr(identity_source) | ||
|
||
return identity_source | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
from samtranslator.model.exceptions import InvalidEventException, InvalidResourceException | ||
from samtranslator.swagger.swagger import SwaggerEditor | ||
from samtranslator.open_api.open_api import OpenApiEditor | ||
from samtranslator.utils.py27hash_fix import Py27Dict, Py27UniStr | ||
|
||
CONDITION = "Condition" | ||
|
||
|
@@ -665,13 +666,16 @@ def _add_swagger_integration(self, api, function, intrinsics_resolver): | |
|
||
function_arn = function.get_runtime_attr("arn") | ||
partition = ArnGenerator.get_partition_name() | ||
uri = fnSub( | ||
arn = ( | ||
"arn:" | ||
+ partition | ||
+ ":apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/" | ||
+ make_shorthand(function_arn) | ||
+ "/invocations" | ||
) | ||
if function_arn.get("Fn::GetAtt") and isinstance(function_arn["Fn::GetAtt"][0], Py27UniStr): | ||
|
||
arn = Py27UniStr(arn) | ||
uri = Py27Dict(fnSub(arn)) | ||
|
||
editor = SwaggerEditor(swagger_body) | ||
|
||
|
@@ -1163,11 +1167,14 @@ def _add_openapi_integration(self, api, function, manage_swagger=False): | |
return | ||
|
||
function_arn = function.get_runtime_attr("arn") | ||
uri = fnSub( | ||
arn = ( | ||
"arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/" | ||
+ make_shorthand(function_arn) | ||
+ "/invocations" | ||
) | ||
if function_arn.get("Fn::GetAtt") and isinstance(function_arn["Fn::GetAtt"][0], Py27UniStr): | ||
|
||
arn = Py27UniStr(arn) | ||
uri = Py27Dict(fnSub(arn)) | ||
|
||
editor = OpenApiEditor(open_api_body) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're deprecating python 2, and this PR removes tests from running in python 2, I decided to also remove all other references to it. Let me know if I should undo the changes to documentation in case we want to do them more formally later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update our
setup.py
to constraint to specific py versions too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did remove python 2.7 from
setup.py
in this PR, but I wasn't sure aboutProgramming Language :: Python
so that one is still in the file. Should I also remove it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more like this: https://github.com/aws/aws-sam-cli/blob/develop/setup.py#L57 Maybe outside of this PR, but we don't actually constrain what python version are supported. So we can make the code none compatible but it could still be install-able through pip in python2.7