Skip to content

Commit

Permalink
Add top-level JSON type checks (#6)
Browse files Browse the repository at this point in the history
* Add top-level JSON type checks

* Fix logging
  • Loading branch information
prog893 authored Jan 7, 2020
1 parent 0f45ad8 commit 0f9ce4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions lambda/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,31 @@ def append_to_dict(dictionary: dict, log_type: str, log_data: object, log_timest
dictionary[log_type]['records'].append(log_data)


def normalize_kinesis_payload(payload: dict):
def normalize_kinesis_payload(p: dict):
# Normalize messages from CloudWatch (subscription filters) and pass through anything else
# https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/logs/SubscriptionFilters.html

logger.debug(f"normalizer input: {payload}")
logger.debug(f"normalizer input: {p}")

payloads = []

if len(payload) < 1:
logger.error(f"Got weird record: \"{payload}\", skipping")
if len(p) < 1:
logger.error(f"Got weird record: \"{p}\", skipping")
return payloads

# check if data is JSON and parse
try:
payload = json.loads(payload)
payload = json.loads(p)
if type(payload) is not dict:
logger.error(f"Top-level JSON data is not an object, giving up: {payload}")
return payloads

except JSONDecodeError:
logger.error(f"Non-JSON data found: {payload}, skipping")
logger.error(f"Non-JSON data found: {p}, giving up")
return payloads

if 'messageType' in payload:
logger.debug(f"Got payload looking like CloudWatch Logs via subscription filters: "
f"{payload}")
logger.debug(f"Got payload looking like CloudWatch Logs via subscription filters: {payload}")

if payload['messageType'] == "DATA_MESSAGE":
if 'logEvents' in payload:
Expand All @@ -117,7 +119,13 @@ def normalize_kinesis_payload(payload: dict):
payload_parsed = json.loads(event['message'])
logger.debug(f"parsed payload: {payload_parsed}")

except JSONDecodeError:
if type(payload_parsed) is not dict:
logger.error(f"Top-level JSON data in CWL payload is not an object, giving up: "
f"{payload_parsed}")
continue

except JSONDecodeError as e:
logger.debug(e)
logger.debug(f"Non-JSON data found inside CWL message: {event}, giving up")
continue

Expand All @@ -136,7 +144,7 @@ def normalize_kinesis_payload(payload: dict):
raise ValueError(f"Unknown messageType: {payload}")
else:
payloads.append(payload)

return payloads


Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ resource "aws_iam_role_policy_attachment" "xray_access" {

module "iam" {
source = "baikonur-oss/iam-nofile/aws"
version = "1.0.1"
version = "v2.0.0"

type = "lambda"
name = var.name
Expand Down

0 comments on commit 0f9ce4a

Please sign in to comment.