def test_parse_event_not_api_gw(): event = {"a": 1} new_event = EventParser.parse_event(event=event, handlers=[ExceptionHandler()]) assert new_event == event
def create_span(cls, event=None, context=None, force=False) -> "SpansContainer": """ This function creates a span out of a given AWS context. The force flag delete any existing span-container (to handle with warm execution of lambdas). Note that if lambda will be executed directly (regular pythonic function call and not invoked), it will override the container. """ if cls._span and not force: return cls._span additional_info = {} if Configuration.verbose: additional_info.update({ "event": prepare_large_data(EventParser.parse_event(omit_keys(event))), "envs": prepare_large_data(omit_keys(dict(os.environ))), }) trace_root, transaction_id, suffix = parse_trace_id( os.environ.get("_X_AMZN_TRACE_ID", "")) remaining_time = getattr(context, "get_remaining_time_in_millis", lambda: MAX_LAMBDA_TIME)() cls._span = SpansContainer( started=int(time.time() * 1000), name=os.environ.get("AWS_LAMBDA_FUNCTION_NAME"), runtime=os.environ.get("AWS_EXECUTION_ENV"), region=os.environ.get("AWS_REGION"), memory_allocated=os.environ.get("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"), log_stream_name=os.environ.get("AWS_LAMBDA_LOG_STREAM_NAME"), log_group_name=os.environ.get("AWS_LAMBDA_LOG_GROUP_NAME"), trace_root=trace_root, transaction_id=transaction_id, trace_id_suffix=suffix, request_id=getattr(context, "aws_request_id", ""), account=safe_split_get( getattr(context, "invoked_function_arn", ""), ":", 4, ""), trigger_by=parse_triggered_by(event), max_finish_time=int(time.time() * 1000) + remaining_time, **additional_info, ) return cls._span
def test_parse_event_sqs(): not_order_sns_event = { "Records": [ { "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d", "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...", "body": "Test message1", "attributes": { "ApproximateReceiveCount": "1", "SentTimestamp": "1545082649183", "SenderId": "AIDAIENQZJOLO23YVJ4VO", "ApproximateFirstReceiveTimestamp": "1545082649185", }, "messageAttributes": { "a": 1 }, "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3", "eventSource": "aws:sqs", "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue", "awsRegion": "us-east-2", }, { "messageId": "2e1424d4-f796-459a-8184-9c92662be6da", "receiptHandle": "AQEBzWwaftRI0KuVm4tP+/7q1rGgNqicHq...", "body": "Test message2", "attributes": { "ApproximateReceiveCount": "1", "SentTimestamp": "1545082650636", "SenderId": "AIDAIENQZJOLO23YVJ4VO", "ApproximateFirstReceiveTimestamp": "1545082650649", }, "messageAttributes": { "b": 2 }, "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3", "eventSource": "aws:sqs", "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue", "awsRegion": "us-east-2", }, ] } order_sns_event = EventParser.parse_event(event=not_order_sns_event) assert json.dumps(order_sns_event) == json.dumps( OrderedDict({ "Records": [ { "body": "Test message1", "messageAttributes": { "a": 1 }, "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d", }, { "body": "Test message2", "messageAttributes": { "b": 2 }, "messageId": "2e1424d4-f796-459a-8184-9c92662be6da", }, ] }))
def test_parse_event_api_gw_v1(): not_order_api_gw_event = { "resource": "/add-user", "path": "/add-user", "httpMethod": "POST", "headers": { "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7", "Authorization": "auth", "CloudFront-Forwarded-Proto": "https", "CloudFront-Is-Desktop-Viewer": "true", "CloudFront-Is-Mobile-Viewer": "false", "CloudFront-Is-SmartTV-Viewer": "false", "CloudFront-Is-Tablet-Viewer": "false", "CloudFront-Viewer-Country": "IL", "content-type": "application/json;charset=UTF-8", "customer_id": "c_1111", "Host": "aaaa.execute-api.us-west-2.amazonaws.com", "origin": "https://aaa.io", "Referer": "https://aaa.io/users", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "cross-site", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36", "Via": "2.0 59574f77a7cf2d23d64904db278e5711.cloudfront.net (CloudFront)", "X-Amz-Cf-Id": "J4KbOEUrZCnUQSLsDq1PyYXmfpVy8x634huSeBX0HCbscgH-N2AtVA==", "X-Amzn-Trace-Id": "Root=1-5e9bf868-1c53a38cfe070266db0bfbd9", "X-Forwarded-For": "5.102.206.161, 54.182.243.106", "X-Forwarded-Port": "443", "X-Forwarded-Proto": "https", }, "multiValueHeaders": { "Accept": ["application/json, text/plain, */*"], "Accept-Encoding": ["gzip, deflate, br"], "Accept-Language": ["he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7"], "Authorization": ["auth"], "CloudFront-Forwarded-Proto": ["https"], "CloudFront-Is-Desktop-Viewer": ["true"], "CloudFront-Is-Mobile-Viewer": ["false"], "CloudFront-Is-SmartTV-Viewer": ["false"], "CloudFront-Is-Tablet-Viewer": ["false"], "CloudFront-Viewer-Country": ["IL"], "content-type": ["application/json;charset=UTF-8"], "customer_id": ["c_1111"], "Host": ["a.execute-api.us-west-2.amazonaws.com"], "origin": ["https://aaa.io"], "Referer": ["https://aaa.io/users"], "sec-fetch-dest": ["empty"], "sec-fetch-mode": ["cors"], "sec-fetch-site": ["cross-site"], "User-Agent": [ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36" ], "Via": [ "2.0 59574f77a7cf2d23d64904db278e5711.cloudfront.net (CloudFront)" ], "X-Amz-Cf-Id": ["J4KbOEUrZCnUQSLsDq1PyYXmfpVy8x634huSeBX0HCbscgH-N2AtVA=="], "X-Amzn-Trace-Id": ["Root=1-5e9bf868-1c53a38cfe070266db0bfbd9"], "X-Forwarded-For": ["5.102.206.161, 54.182.243.106"], "X-Forwarded-Port": ["443"], "X-Forwarded-Proto": ["https"], }, "queryStringParameters": "1", "multiValueQueryStringParameters": "1", "pathParameters": "1", "stageVariables": None, "requestContext": { "resourceId": "ua33sn", "authorizer": { "claims": { "sub": "a87005bb-3030-4962-bae8-48cd629ba20b", "custom:customer": "c_1111", "iss": "https://cognito-idp.us-west-2.amazonaws.com/us-west-2", "custom:customer-name": "a", "cognito:username": "******", "aud": "4lidcnek50hi18996gadaop8j0", "event_id": "9fe80735-f265-41d5-a7ca-04b88c2a4a4c", "token_use": "id", "auth_time": "1587038744", "exp": "Sun Apr 19 08:06:14 UTC 2020", "custom:role": "admin", "iat": "Sun Apr 19 07:06:14 UTC 2020", "email": "*****@*****.**", } }, "resourcePath": "/add-user", "httpMethod": "POST", "extendedRequestId": "LOPAXFcuvHcFUKg=", "requestTime": "19/Apr/2020:07:06:16 +0000", "path": "/prod/add-user", "accountId": "114300393969", "protocol": "HTTP/1.1", "stage": "prod", "domainPrefix": "psqn7b0ev2", "requestTimeEpoch": 1587279976628, "requestId": "78542821-ca17-4e83-94ec-96993a9d451d", "identity": { "cognitoIdentityPoolId": None, "accountId": None, "cognitoIdentityId": None, "caller": None, "sourceIp": "5.102.206.161", "principalOrgId": None, "accessKey": None, "cognitoAuthenticationType": None, "cognitoAuthenticationProvider": None, "userArn": None, "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36", "user": None, }, "domainName": "psqn7b0ev2.execute-api.us-west-2.amazonaws.com", "apiId": "psqn7b0ev2", }, "body": '{"email":"*****@*****.**"}', "isBase64Encoded": False, } order_api_gw_event = EventParser.parse_event(event=not_order_api_gw_event) assert json.dumps(order_api_gw_event) == json.dumps( OrderedDict({ "resource": "/add-user", "path": "/add-user", "httpMethod": "POST", "queryStringParameters": "1", "pathParameters": "1", "body": '{"email":"*****@*****.**"}', "requestContext": { "authorizer": { "claims": { "sub": "a87005bb-3030-4962-bae8-48cd629ba20b", "custom:customer": "c_1111", "iss": "https://cognito-idp.us-west-2.amazonaws.com/us-west-2", "custom:customer-name": "a", "cognito:username": "******", "aud": "4lidcnek50hi18996gadaop8j0", "event_id": "9fe80735-f265-41d5-a7ca-04b88c2a4a4c", "token_use": "id", "auth_time": "1587038744", "exp": "Sun Apr 19 08:06:14 UTC 2020", "custom:role": "admin", "iat": "Sun Apr 19 07:06:14 UTC 2020", "email": "*****@*****.**", } } }, "headers": { "Authorization": "auth", "content-type": "application/json;charset=UTF-8", "customer_id": "c_1111", "Host": "aaaa.execute-api.us-west-2.amazonaws.com", "origin": "https://aaa.io", "Referer": "https://aaa.io/users", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36", }, "stageVariables": None, "isBase64Encoded": False, }))
def test_parse_event_sns(): not_order_sns_event = { "Records": [ { "EventVersion": "1.0", "EventSubscriptionArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", "EventSource": "aws:sns", "Sns": { "SignatureVersion": "1", "Timestamp": "2019-01-02T12:45:07.000Z", "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==", "SigningCertUrl": "https://sns.us-east-2.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem", "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", "Message": "Hello from SNS1!", "MessageAttributes": { "Test": { "Type": "String", "Value": "TestString" }, "TestBinary": { "Type": "Binary", "Value": "TestBinary" }, }, "Type": "Notification", "UnsubscribeUrl": "https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", "TopicArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda", "Subject": "TestInvoke", }, }, { "EventVersion": "1.0", "EventSubscriptionArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", "EventSource": "aws:sns", "Sns": { "SignatureVersion": "1", "Timestamp": "2019-01-02T12:45:07.000Z", "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==", "SigningCertUrl": "https://sns.us-east-2.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem", "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", "Message": "Hello from SNS2!", "MessageAttributes": { "Test": { "Type": "String", "Value": "TestString" }, "TestBinary": { "Type": "Binary", "Value": "TestBinary" }, }, "Type": "Notification", "UnsubscribeUrl": "https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", "TopicArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda", "Subject": "TestInvoke", }, }, ] } order_sns_event = EventParser.parse_event(event=not_order_sns_event) assert json.dumps(order_sns_event) == json.dumps( OrderedDict({ "Records": [ { "Sns": { "Message": "Hello from SNS1!", "MessageAttributes": { "Test": { "Type": "String", "Value": "TestString" }, "TestBinary": { "Type": "Binary", "Value": "TestBinary" }, }, "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", } }, { "Sns": { "Message": "Hello from SNS2!", "MessageAttributes": { "Test": { "Type": "String", "Value": "TestString" }, "TestBinary": { "Type": "Binary", "Value": "TestBinary" }, }, "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", } }, ] }))
def test_parse_event_api_gw_v2(): not_order_api_gw_event = { "version": "2.0", "routeKey": "ANY /nodejs-apig-function-1G3XMPLZXVXYI", "rawPath": "/default/nodejs-apig-function-1G3XMPLZXVXYI", "rawQueryString": "", "cookies": ["s_fid=7AABXMPL1AFD9BBF-0643XMPL09956DE2", "regStatus=pre-register"], "headers": { "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9", "content-length": "0", "host": "r3pmxmplak.execute-api.us-east-2.amazonaws.com", "sec-fetch-dest": "document", "sec-fetch-mode": "navigate", "sec-fetch-site": "cross-site", "sec-fetch-user": "******", "upgrade-insecure-requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36", "x-amzn-trace-id": "Root=1-5e6722a7-cc56xmpl46db7ae02d4da47e", "x-forwarded-for": "205.255.255.176", "x-forwarded-port": "443", "x-forwarded-proto": "https", }, "requestContext": { "accountId": "123456789012", "apiId": "r3pmxmplak", "domainName": "r3pmxmplak.execute-api.us-east-2.amazonaws.com", "domainPrefix": "r3pmxmplak", "http": { "method": "GET", "path": "/default/nodejs-apig-function-1G3XMPLZXVXYI", "protocol": "HTTP/1.1", "sourceIp": "205.255.255.176", "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36", }, "requestId": "JKJaXmPLvHcESHA=", "routeKey": "ANY /nodejs-apig-function-1G3XMPLZXVXYI", "stage": "default", "time": "10/Mar/2020:05:16:23 +0000", "timeEpoch": 1583817383220, }, "isBase64Encoded": True, } order_api_gw_event = EventParser.parse_event(event=not_order_api_gw_event) assert json.dumps(order_api_gw_event) == json.dumps( OrderedDict({ "version": "2.0", "routeKey": "ANY /nodejs-apig-function-1G3XMPLZXVXYI", "rawPath": "/default/nodejs-apig-function-1G3XMPLZXVXYI", "requestContext": { "http": { "method": "GET", "path": "/default/nodejs-apig-function-1G3XMPLZXVXYI", "protocol": "HTTP/1.1", "sourceIp": "205.255.255.176", "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36", } }, "headers": { "content-length": "0", "host": "r3pmxmplak.execute-api.us-east-2.amazonaws.com", "upgrade-insecure-requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36", }, "cookies": [ "s_fid=7AABXMPL1AFD9BBF-0643XMPL09956DE2", "regStatus=pre-register" ], "isBase64Encoded": True, }))
def test_parse_event_not_api_gw_none_check(): new_event = EventParser.parse_event(event=None) assert new_event is None