Fix unserializable datetime objects

This fixes the issue where publishing non JSON serializable objects to MozDef causes errors. This fix converts datetime objects to iso formatted dates.
This commit is contained in:
Gene Wood 2017-06-26 16:44:37 -07:00
Родитель 4285803970
Коммит 430e321b77
1 изменённых файлов: 11 добавлений и 2 удалений

Просмотреть файл

@ -11,14 +11,23 @@
from security_monkey import app
import botocore.exceptions, botocore.parsers
import mozdef_client
from datetime import datetime
import json
def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, datetime):
serial = obj.isoformat()
return serial
raise TypeError("Type not serializable")
def publish_to_mozdef(summary='',
details={}):
msg = mozdef_client.MozDefEvent('')
msg.summary = summary
msg.tags = ['asap']
msg.details = details
msg.details = json.loads(json.dumps(details, default=json_serial))
region, account_id, queue_name = app.config.get(
'SQS_QUEUE_ARN').split(':')[3:]
@ -36,4 +45,4 @@ def publish_to_mozdef(summary='',
app.logger.critical(
"Alerter: Attempt to send message to SQS queue {} in account {} "
"in region {} failed with error {}".format(
queue_name, account_id, region, e))
queue_name, account_id, region, e))