Skip to content
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

Add support for REQUEST type Lambda Authorizers #2093

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ def test_create_api_gateway_routes_with_different_auth_methods(self):
self.assertEqual(z.credentials_arn, parsable_template["Resources"]["Authorizer"]["Properties"]["AuthorizerCredentials"])
self.assertEqual("xxx", parsable_template["Resources"]["Authorizer"]["Properties"]["IdentityValidationExpression"])

# explicit TOKEN type authorizer
explicit_authorizer = authorizer.copy()
explicit_authorizer.update({"type": "TOKEN"})
z.create_stack_template(lambda_arn, 'helloworld', False, False, authorizer)
explicit_authorizer_parsable_template = json.loads(z.cf_template.to_json())
self.assertDictEqual(parsable_template, explicit_authorizer_parsable_template)

# Authorizer without validation expression
authorizer.pop('validation_expression', None)
z.create_stack_template(lambda_arn, 'helloworld', False, False, authorizer)
Expand All @@ -340,6 +347,25 @@ def test_create_api_gateway_routes_with_different_auth_methods(self):
self.assertEqual("TOKEN", parsable_template["Resources"]["Authorizer"]["Properties"]["Type"])
with self.assertRaises(KeyError):
parsable_template["Resources"]["Authorizer"]["Properties"]["IdentityValidationExpression"]

# REQUEST authorizer
authorizer = {
"function": "runapi.authorization.gateway_authorizer.evaluate_token",
"result_ttl": 300,
"type": "REQUEST",
"validation_expression": "xxx"
}
invocations_uri = 'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/' + lambda_arn + '/invocations'
z.create_stack_template(lambda_arn, 'helloworld', False, False, authorizer)
parsable_template = json.loads(z.cf_template.to_json())
self.assertEqual("CUSTOM", parsable_template["Resources"]["GET0"]["Properties"]["AuthorizationType"])
self.assertEqual("CUSTOM", parsable_template["Resources"]["GET1"]["Properties"]["AuthorizationType"])
self.assertEqual("REQUEST", parsable_template["Resources"]["Authorizer"]["Properties"]["Type"])
self.assertEqual("ZappaAuthorizer", parsable_template["Resources"]["Authorizer"]["Properties"]["Name"])
self.assertEqual(300, parsable_template["Resources"]["Authorizer"]["Properties"]["AuthorizerResultTtlInSeconds"])
self.assertEqual(invocations_uri, parsable_template["Resources"]["Authorizer"]["Properties"]["AuthorizerUri"])
self.assertEqual(z.credentials_arn, parsable_template["Resources"]["Authorizer"]["Properties"]["AuthorizerCredentials"])
self.assertEqual("xxx", parsable_template["Resources"]["Authorizer"]["Properties"]["IdentityValidationExpression"])

# Authorizer with arn
authorizer = {
Expand Down
3 changes: 3 additions & 0 deletions tests/tests_placebo.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ def test_handler(self, session):
event = {'authorizationToken': 'hubtoken1', 'methodArn': 'arn:aws:execute-api:us-west-2:1234:xxxxx/dev/GET/v1/endpoint/param', 'type': 'TOKEN'}
self.assertEqual("AUTHORIZER_EVENT", lh.handler(event, None))

event = {'methodArn': 'arn:aws:execute-api:us-west-2:1234:xxxxx/dev/GET/v1/endpoint/param', 'type': 'REQUEST'}
self.assertEqual("AUTHORIZER_EVENT", lh.handler(event, None))

# Ensure Zappa does return 401 if no function was defined.
lh.settings.AUTHORIZER_FUNCTION = None
with self.assertRaisesRegexp(Exception, 'Unauthorized'):
Expand Down
6 changes: 4 additions & 2 deletions zappa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ def create_authorizer(self, restapi, uri, authorizer):
if identity_validation_expression:
authorizer_resource.IdentityValidationExpression = identity_validation_expression

if authorizer_type == 'TOKEN':
if authorizer_type in ['TOKEN', 'REQUEST']:
if not self.credentials_arn:
self.get_credentials_arn()
authorizer_resource.AuthorizerResultTtlInSeconds = authorizer.get('result_ttl', 300)
Expand Down Expand Up @@ -2130,7 +2130,9 @@ def create_stack_template( self,
elif iam_authorization:
auth_type = "AWS_IAM"
elif authorizer:
auth_type = authorizer.get("type", "CUSTOM")
auth_type = authorizer.get("type", "TOKEN").upper()
if auth_type in ["TOKEN", "REQUEST"]:
auth_type = "CUSTOM"

# build a fresh template
self.cf_template = troposphere.Template()
Expand Down
2 changes: 1 addition & 1 deletion zappa/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def handler(self, event, context):
return result

# This is an API Gateway authorizer event
elif event.get('type') == 'TOKEN':
elif event.get('type') in ['TOKEN', 'REQUEST']:
whole_function = self.settings.AUTHORIZER_FUNCTION
if whole_function:
app_function = self.import_module_and_get_function(whole_function)
Expand Down