From d9833a67fec03732106865b1e80776cb7da02a2b Mon Sep 17 00:00:00 2001 From: Nathaniel Beckstead Date: Wed, 5 Feb 2020 22:00:34 -0500 Subject: [PATCH 1/2] log general messages --- src/lambda_function.py | 3 +++ src/logging_help.py | 47 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/lambda_function.py b/src/lambda_function.py index 808e067..7e77aa9 100644 --- a/src/lambda_function.py +++ b/src/lambda_function.py @@ -35,6 +35,9 @@ def lambda_handler(event, context): # Decode the url argument and fix if no protocol url = urllib.parse.unquote(event['url']) + + logging_help.log_msg('Scan requested for URL: {}'.format(url)) + # Filter for potentially malicious or invalid URLs filter(url) diff --git a/src/logging_help.py b/src/logging_help.py index ffadb85..335ba6a 100644 --- a/src/logging_help.py +++ b/src/logging_help.py @@ -5,11 +5,24 @@ import os import boto3 import json +import time import base64 import requests from requests.auth import HTTPBasicAuth from botocore.exceptions import ClientError +h_data = {"Content-Type": "application/json; charset=UTF-8"} + +def get_env(): + log_env = "unknown" + + if "CI" in os.environ and os.environ.get("CI") == "true": + log_env = "test" + else: + log_env = "production" + + return log_env + def get_secret(): secret_name = 'LogDNAIngestionKey' # os.environ['LOGGING_KEY'] @@ -71,17 +84,35 @@ def get_secret(): return json.loads(secret).get('logdna-ingestion') -def log_scan(db_data): +def log_msg(message): + log_env = get_env() - log_env = "unknown" + logdna = get_secret() - if "CI" in os.environ and os.environ.get("CI") == "true": - log_env = "test" - else: - log_env = "production" + logdata = { + "lines": [ + { + "line": message, + "app": "glimpse", + "level": "INFO", + "env": log_env + } + ] + } + + submission = requests.post('https://logs.logdna.com/logs/ingest?hostname=GLIMPSE&now={}'.format(int(time.time())), json=logdata, headers=h_data, auth=HTTPBasicAuth(logdna, '')) + + if submission.status_code != 200: # or submission.json['status'] != "ok": + raise ValueError('Got status {}'.format(submission.status_code)) + + + +def log_scan(db_data): + log_env = get_env() logdna = get_secret() + logdata = { "lines": [ { @@ -101,9 +132,7 @@ def log_scan(db_data): ] } - h_data = {"Content-Type": "application/json; charset=UTF-8"} - - submission = requests.post('https://logs.logdna.com/logs/ingest?hostname=GLIMPSE', json=logdata, headers=h_data, auth=HTTPBasicAuth(logdna, '')) + submission = requests.post('https://logs.logdna.com/logs/ingest?hostname=GLIMPSE&now={}'.format(int(time.time())), json=logdata, headers=h_data, auth=HTTPBasicAuth(logdna, '')) if submission.status_code != 200: # or submission.json['status'] != "ok": raise ValueError('Got status {}'.format(submission.status_code)) From c4af4a39c424dfff314a7df1bdf8f8ce9c26ee75 Mon Sep 17 00:00:00 2001 From: Nathaniel Beckstead Date: Wed, 5 Feb 2020 22:06:03 -0500 Subject: [PATCH 2/2] log message for existing scan --- src/lambda_function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lambda_function.py b/src/lambda_function.py index 7e77aa9..bbe989a 100644 --- a/src/lambda_function.py +++ b/src/lambda_function.py @@ -84,7 +84,7 @@ def lambda_handler(event, context): pp.pprint(return_data) print('[!] Logging Scan') - logging_help.log_scan(db_data) + logging_help.log_msg('Existing data returned for hash {}'.format(url_hash)) return return_data