Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
u"apiId": u"1234567890",
u"resourcePath": u"/{proxy+}",
u"httpMethod": u"POST",
u"requestId": u"c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
u"accountId": u"123456789012",
u"identity": {
u"userAgent": u"Custom User Agent String",
u"cognitoIdentityPoolId": u"userpoolID",
u"cognitoIdentityId": u"myCognitoID",
u"sourceIp": u"127.0.0.1",
},
"stage": "prod"
},
u'query': {}
}
environ = create_wsgi_request(event, script_name='http://zappa.com/',
trailing_slash=False)
self.assertEqual(environ['QUERY_STRING'], u'')
u"apiId": u"1234567890",
u"resourcePath": u"/{proxy+}",
u"httpMethod": u"POST",
u"requestId": u"c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
u"accountId": u"123456789012",
u"identity": {
u"userAgent": u"Custom User Agent String",
u"cognitoIdentityPoolId": u"userpoolID",
u"cognitoIdentityId": u"myCognitoID",
u"sourceIp": u"127.0.0.1",
},
"stage": "prod"
},
u'query': {}
}
environ = create_wsgi_request(event, script_name='http://zappa.com/',
trailing_slash=False)
self.assertEqual(environ['QUERY_STRING'], u'foo=1&foo=2')
u"resourcePath": u"/{proxy+}",
u"httpMethod": u"POST",
u"requestId": u"c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
u"accountId": u"123456789012",
u"identity": {
u"userAgent": u"Custom User Agent String",
u"cognitoIdentityPoolId": u"userpoolID",
u"cognitoIdentityId": u"myCognitoID",
u"sourceIp": u"127.0.0.1",
},
"stage": "prod"
},
u'query': {}
}
environ = create_wsgi_request(event, script_name='http://zappa.com/',
trailing_slash=False,
context_header_mappings={'CognitoIdentityID': 'identity.cognitoIdentityId',
'APIStage': 'stage',
'InvalidValue': 'identity.cognitoAuthenticationType',
'OtherInvalid': 'nothinghere'})
self.assertEqual(environ['HTTP_COGNITOIDENTITYID'], u'myCognitoID')
self.assertEqual(environ['HTTP_APISTAGE'], u'prod')
self.assertNotIn('HTTP_INVALIDVALUE', environ)
self.assertNotIn('HTTP_OTHERINVALID', environ)
u'headers': {
u'Content-Type': u'application/json'
},
u'pathParameters': {
u'proxy': 'v1/runs'
},
u'requestContext': {
u'authorizer': {
u'principalId': u'user1'
},
},
u'query': {}
}
environ = create_wsgi_request(event, script_name='http://zappa.com/',
trailing_slash=False,
context_header_mappings={'PrincipalId': 'authorizer.principalId'})
self.assertEqual(environ['HTTP_PRINCIPALID'], u'user1')
# Validate multiple mappings with an invalid mapping
# Invalid mapping should be ignored
event = {
u'httpMethod': u'GET',
u'queryStringParameters': None,
u'path': u'/v1/runs',
u'params': {},
u'body': {},
u'headers': {
u'Content-Type': u'application/json'
},
u'pathParameters': {
u'path': u'/v1/runs',
u'params': {},
u'body': {},
u'headers': {
u'Content-Type': u'application/json'
},
u'pathParameters': {
u'proxy': 'v1/runs'
},
u'requestContext': {
u'authorizer': {}
},
u'query': {}
}
environ = create_wsgi_request(event, script_name='http://zappa.com/',
trailing_slash=False)
user = environ.get('REMOTE_USER', u'no_user')
self.assertEqual(user, u'no_user')
u'body': {},
u'headers': {
u'Content-Type': u'application/json'
},
u'pathParameters': {
u'proxy': 'v1/runs'
},
u'requestContext': {
u'authorizer': {
u'principalId': u'user1'
}
},
u'query': {}
}
environ = create_wsgi_request(event, script_name='http://zappa.com/',
trailing_slash=False)
self.assertEqual(environ['REMOTE_USER'], u'user1')
# With empty authorizer, should not include REMOTE_USER
event = {
u'httpMethod': u'GET',
u'queryStringParameters': None,
u'path': u'/v1/runs',
u'params': {},
u'body': {},
u'headers': {
u'Content-Type': u'application/json'
},
u'pathParameters': {
u'proxy': 'v1/runs'
},
An AWS Lambda function which parses specific API Gateway input into a WSGI request.
The request get fed it to Django, processes the Django response, and returns that
back to the API Gateway.
"""
time_start = datetime.datetime.now()
# If in DEBUG mode, log all raw incoming events.
if settings.DEBUG:
logger.info('Zappa Event: {}'.format(event))
# This is a normal HTTP request
if event.get('method', None):
# Create the environment for WSGI and handle the request
environ = create_wsgi_request(event, script_name=settings.SCRIPT_NAME)
# We are always on https on Lambda, so tell our wsgi app that.
environ['HTTPS'] = 'on'
environ['wsgi.url_scheme'] = 'https'
wrap_me = get_wsgi_application()
app = ZappaWSGIMiddleware(wrap_me)
# Execute the application
response = Response.from_app(app, environ)
response.content = response.data
# Prepare the special dictionary which will be returned to the API GW.
returnme = {'Content': response.data}
# Pack the WSGI response into our special dictionary.
# event_echo = getattr(settings, "EVENT_ECHO", True)
# if event_echo and 'event_echo' in event['params'].values():
# return {'Content': str(event) + '\n' + str(context), 'Status': 200}
if settings.DOMAIN:
# If we're on a domain, we operate normally
script_name = ''
else:
# But if we're not, then our base URL
# will be something like
# https://blahblahblah.execute-api.us-east-1.amazonaws.com/dev
# So, we need to make sure the WSGI app knows this.
script_name = '/' + settings.API_STAGE
# Create the environment for WSGI and handle the request
environ = create_wsgi_request(
event,
script_name=script_name,
trailing_slash=self.trailing_slash
)
# We are always on https on Lambda, so tell our wsgi app that.
environ['HTTPS'] = 'on'
environ['wsgi.url_scheme'] = 'https'
environ['lambda.context'] = context
# Execute the application
response = Response.from_app(self.wsgi_app, environ)
# This is the object we're going to return.
# Pack the WSGI response into our special dictionary.
zappa_returndict = dict()
# TODO: Enable Let's Encrypt
# # If Let's Encrypt is defined in the settings,
# # and the path is your.domain.com/.well-known/acme-challenge/{{lets_encrypt_challenge_content}},
# # return a 200 of lets_encrypt_challenge_content.
# lets_encrypt_challenge_path = getattr(settings, "LETS_ENCRYPT_CHALLENGE_PATH", None)
# lets_encrypt_challenge_content = getattr(settings, "LETS_ENCRYPT_CHALLENGE_CONTENT", None)
# if lets_encrypt_challenge_path:
# if len(event['params']) == 3:
# if event['params']['parameter_1'] == '.well-known' and \
# event['params']['parameter_2'] == 'acme-challenge' and \
# event['params']['parameter_3'] == lets_encrypt_challenge_path:
# return {'Content': lets_encrypt_challenge_content, 'Status': 200}
# Create the environment for WSGI and handle the request
environ = create_wsgi_request(event, script_name=settings.SCRIPT_NAME,
trailing_slash=False)
# We are always on https on Lambda, so tell our wsgi app that.
environ['wsgi.url_scheme'] = 'https'
response = Response.from_app(app, environ)
# This doesn't work. It should probably be set right after creation, not
# at such a late stage.
# response.autocorrect_location_header = False
zappa_returndict = dict()
if response.data:
zappa_returndict['Content'] = response.data