Skip to content

Commit

Permalink
Merge pull request #10 from becksteadn/build
Browse files Browse the repository at this point in the history
Add logdna logging with AWS Secrets Manager
  • Loading branch information
scriptingislife authored Feb 6, 2020
2 parents 855ed76 + bfc84a1 commit 8db4292
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 69 deletions.
127 changes: 60 additions & 67 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: python

services:
- docker

env:
global:
- PATH=$HOME/.local/bin:$PATH
Expand All @@ -11,79 +9,74 @@ env:
- TEST_FUNCTION_NAME=glimpseTest
- RUN_FUNCTION_DIR=run/function
- RUN_FUNCTION_NAME=glimpseRun
- secure: "H4K4IjdpfjJfQEtvXHx4lcHq+ru9XfyN0HSvrFKxSgA4raFSSoW07gum1EkirLXakOag8uFstAZokCf4LyFoe0AsdvE8x7TQ7OR7i2d6Lyv4P96SL16E7f5ZItA3dNiBKt9FudWGXbaTrQ71QcubzKrI9KRBM5LOGAQnhwU2cGQzcw8YAWpDxPHKJd+ZZ/zXtNhkLo5R9ZEg6yuoJpZdiPyEWknsYjDIwdIX3wB/AAeBa179IU4DQ2HxLGsPX/CBwXZ6eJWEwVL9Eb1fmiOtJG3LhVEbdZ2Jp8RpYbowjhM9uaIsLAQLJrB6tJ5f1lAhlkC/o8bs8SCNDLNIVqI1hyqk2QYn9ztRd9lXiQiQU8zJeadJ+BU1LnDln9zmY1FszRx3qX7V2nDaCMKpuRtICEL6DTeq/cX5UypaprGlo6Nqk4u28dsnFMkNwYS+ncUR4IZlvPRbXxY/9U5jbiav3Mf2OFTGpqBtd/fS7+8uGmKKm7YFNKcIXJZEk+ekUgXmAxWiB4MWcOCfvMObMJT3oQHWmjBCazH545d9XwZv4KGeiZslboTogQTat84yw5AXszwFJ+PUuvPd1MjCxIF5l16Qmmzk/KqJtSogTI1zJLLiuwgFJ474wdYouDysV3qvv91EQVzV9S1REMAtLajLAhbeXvlhZWT9fZVKkyuDXnI="
- secure: "jgHbSu95AWrI7U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RDt8Jkhr5aYhukmh1Ca5u5T+SOpNjVoOKqCjfzM/VVP5qW4WmYplgoIWMhhfZSFBU2ta6SOcf7g0NusaWnTY1YiAqRlXT+MLnyFMsDhEhUsn0+soOfXSeThFn1aFUT4jHbU2wxdm/beA67KFvkL3HTFn2f5K2T3uvs8tYDFZB9Zjk0iukCC57r0iHa/esf/ENR1we7Bkw1WLGU21eL/hCJEk5NEf+Vf/kgFY2wZagVr+lKS1WMIS5BG3jm1ByNzmFZe+N3IlYH/hQkqPVJh4klj2MNgqRtSknpNl4xRGjLKI4B++7M1h6LC8iQn7I=
- secure: Famc1ERr8UfOaU8MCMri2IXiR6rZ1+WKXu7VMJHGoU9G/CM5X/S/ScfK8uesEGRgHfDljpNMdwKkrMnM5Wn3d6r6LrontOi7DKGuBe174h6r9o448UtEaU9ceiEWKOgT/NLplcBBoCTOiYryQF20sgGFJmZeEYc2U034Ic5reqF3bdN12JaQJiK+UemFK5/9PwdYVPUSs6o4Rwb+RHmcFmh6kVeTBQWtxZYlxuJtyeRY7EjVKsKTYdUuvo7fITlNbvXxk8LWiXNxOz9Tq955ZNso23pPTBGN0zMu2QiEi1cGFnZjPfkilGDoDXWdKlAwH3D0ds+t4bCHpFoQsvU1e6u/dl8P8oC9hG6IYTN3z+9L1IVlLYcfqyq2PJtZkgAKBCNV6WQPwR0vMgXgZbXdJs/vcbgg7VeXwDR5Zpnpt3tHYFhKOVsAohh8Fpq5Ka5fF4724/h4Y6APb0LSwcKPezXHzhEIoIboEQfIUqhh/k2S/vBHAhS2QC9AOPp5wHMUC8fYWW3K+IYkyNFkJMTfZijF+7pmw7aVf8vd4PoXzW8fXTWBu9kays0pkPDazJ0zh0AkENyKIyOLYp8CRBrhwCdplYKbsCh96WQbUYKl4UUSFPl+w/rYv7QeQROikn9pwZwAuFX/GUv0noCELnjj6sVyzis0kn87ZF1REfDxH/k=
install:
- pip install awscli

- pip install awscli
before_script:
- make build

- make build
script:
- make run URL="https://google.com"
- make update URL="https://google.com"
- make update URL="1.1.1.1"
- make update URL="http://letmeoutofyour.net:8081/"
- make update URL="httpbin.org/anything/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold
-w 32 | head -n 1)"
- make update URL="httpbin.org/redirect/6"
- make update URL="https://httpbin.org/image/png"
- make update URL="https://httpbin.org/headers"
- make update URL="https://self-signed.badssl.com/"
- make update URL="https://expired.badssl.com/"
- make ua UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
- make ua UA="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0"
- make ua UA="curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)"
- make ua UA="python-requests/1.2.0"
- make ua UA="Googlebot/2.1 (+http://www.googlebot.com/bot.html)"
- make ua UA="Screw you webmaster!!!!"
#- make ua UA="Im gonna mess\n\ryou up"
- make ua UA=""
- make run URL="" || echo "SAVED - Made to fail"
- make run URL="file:///etc/passwd" || echo "SAVED - Made to fail"
- make run URL="gibberish" || echo "SAVED - Made to fail"
- make run URL="mjnjvfidrilyenbbvjwisyzdpycppftjombbxqrtvkcpsbxdnllqupptmhancjqimgcpsfuhzjpdkiaibkcigwyrmaajjszyxvyjekbobdzluepicwnelaaljhkqzmqdupjbhercywvvomwzfszzeptaeaiofiohixlzfwjnlgvilyklivymuknqybunftprnvotjviimcttlyjqfhdefapbpzvzugghzaisdyrmebjurqtzbbheomgmentccdekijireporxxmvqneebbrfzhvgbydzmiaewpqopnxzdsrt" || echo "Made to fail"

- make run URL="https://google.com"
- make update URL="https://google.com"
- make update URL="1.1.1.1"
- make update URL="http://letmeoutofyour.net:8081/"
- make update URL="httpbin.org/anything/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' |
fold -w 32 | head -n 1)"
- make update URL="httpbin.org/redirect/6"
- make update URL="https://httpbin.org/image/png"
- make update URL="https://httpbin.org/headers"
- make update URL="https://self-signed.badssl.com/"
- make update URL="https://expired.badssl.com/"
- make ua UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/70.0.3538.77 Safari/537.36"
- make ua UA="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0"
- make ua UA="curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6
enabled)"
- make ua UA="python-requests/1.2.0"
- make ua UA="Googlebot/2.1 (+http://www.googlebot.com/bot.html)"
- make ua UA="Screw you webmaster!!!!"
- make ua UA=""
- make run URL="" || echo "SAVED - Made to fail"
- make run URL="file:///etc/passwd" || echo "SAVED - Made to fail"
- make run URL="gibberish" || echo "SAVED - Made to fail"
- make run URL="mjnjvfidrilyenbbvjwisyzdpycppftjombbxqrtvkcpsbxdnllqupptmhancjqimgcpsfuhzjpdkiaibkcigwyrmaajjszyxvyjekbobdzluepicwnelaaljhkqzmqdupjbhercywvvomwzfszzeptaeaiofiohixlzfwjnlgvilyklivymuknqybunftprnvotjviimcttlyjqfhdefapbpzvzugghzaisdyrmebjurqtzbbheomgmentccdekijireporxxmvqneebbrfzhvgbydzmiaewpqopnxzdsrt"
|| echo "Made to fail"
before_deploy:
- make pack

- make pack
deploy:
- provider: s3
access_key_id: "$AWS_ACCESS_KEY_ID"
secret_access_key: "$AWS_SECRET_ACCESS_KEY"
bucket: "$BUCKET_NAME"
region: us-east-1
upload_dir: "$TEST_FUNCTION_DIR"
local_dir: dist/function
skip_cleanup: true
on:
branch: build
- provider: s3
access_key_id: "$AWS_ACCESS_KEY_ID"
secret_access_key: "$AWS_SECRET_ACCESS_KEY"
bucket: "$BUCKET_NAME"
region: us-east-1
upload_dir: "$RUN_FUNCTION_DIR"
local_dir: dist/function
skip_cleanup: true
on:
branch: master

- provider: s3
access_key_id: "$AWS_ACCESS_KEY_ID"
secret_access_key: "$AWS_SECRET_ACCESS_KEY"
bucket: "$BUCKET_NAME"
region: us-east-1
upload_dir: "$TEST_FUNCTION_DIR"
local_dir: dist/function
skip_cleanup: true
on:
branch: build
- provider: s3
access_key_id: "$AWS_ACCESS_KEY_ID"
secret_access_key: "$AWS_SECRET_ACCESS_KEY"
bucket: "$BUCKET_NAME"
region: us-east-1
upload_dir: "$RUN_FUNCTION_DIR"
local_dir: dist/function
skip_cleanup: true
on:
branch: master
after_deploy:
- if [ $TRAVIS_BRANCH == "build" ]; then
aws lambda update-function-code --function-name $TEST_FUNCTION_NAME --s3-bucket $BUCKET_NAME --s3-key $TEST_FUNCTION_DIR/build.zip;
else echo "Skipping Lambda update for $TEST_FUNCTION_NAME.";
fi
- if [ $TRAVIS_BRANCH == "master" ]; then
aws lambda update-function-code --function-name $RUN_FUNCTION_NAME --s3-bucket $BUCKET_NAME --s3-key $RUN_FUNCTION_DIR/build.zip;
else echo "Skipping Lambda update for $RUN_FUNCTION_NAME.";
fi

- if [ $TRAVIS_BRANCH == "build" ]; then aws lambda update-function-code --function-name
$TEST_FUNCTION_NAME --s3-bucket $BUCKET_NAME --s3-key $TEST_FUNCTION_DIR/build.zip;
else echo "Skipping Lambda update for $TEST_FUNCTION_NAME."; fi
- if [ $TRAVIS_BRANCH == "master" ]; then aws lambda update-function-code --function-name
$RUN_FUNCTION_NAME --s3-bucket $BUCKET_NAME --s3-key $RUN_FUNCTION_DIR/build.zip;
else echo "Skipping Lambda update for $RUN_FUNCTION_NAME."; fi
notifications:
slack:
template:
- "Build <%{build_url}|#%{build_number}> (<%{compare_url}|%{commit}>) of *%{repository_slug}* @ %{branch} %{result} in %{duration}"
- Build <%{build_url}|#%{build_number}> (<%{compare_url}|%{commit}>) of *%{repository_slug}*
@ %{branch} %{result} in %{duration}
rooms:
secure: t5ThZ20j8M5RwvJQQYyN+0MYjqefgrv7OtdUYDpzpxCCB2o9lGTYmIuQrLRZY7BIA1SZS3nIiZvwudR41ibcjfF/Np5W0FntezLR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YRLPpFBFlOVYl7xlDi8ITpdxUJvjjjbgIkTn0UamYjdG1MjXWOFgi+qQfPnQ5vYWGNOxiGOQ7uGlD8pvatyzFJkPhMBSU3QNm5OB7hTlPJDwZCMmtvV+uO4itXU6dfPO1SZdtgshUi1hUNtuu7r1JsAtq7zGXaYjLhk7feNJ6z3oq6V8DVd7HQK+TkEUN6OghAFae20=
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ pack: clean fetch-dependencies
# Update the test Lambda function with the current local code
#
deploy-test: pack
aws s3 cp ./build.zip s3://${S3_BUCKET}/${TEST_S3_KEY} --profile ${AWS_USER}
aws lambda update-function-code --function-name ${TEST_FUNCTION_NAME} --s3-bucket ${S3_BUCKET} --s3-key ${TEST_S3_KEY} --profile ${AWS_USER}
aws s3 cp ./dist/function/build.zip s3://${S3_BUCKET}/${TEST_S3_KEY} --profile ${AWS_USER}
aws --region us-east-1 lambda update-function-code --function-name ${TEST_FUNCTION_NAME} --s3-bucket ${S3_BUCKET} --s3-key ${TEST_S3_KEY} --profile ${AWS_USER}

#
# Copy the code from the test environment to
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
- PATH=/var/task/bin
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- CI
- GLIMPSE_BUCKET_NAME=glimpsefiles
- GLIMPSE_SCREENSHOT_DIR=test/screenshots/
- GLIMPSE_DB_TABLE=glimpsetest
Expand Down
4 changes: 4 additions & 0 deletions src/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import glimpse_driver as gd
from s3_help import S3
from db_help import DynamoDB
import logging_help
from selenium.common.exceptions import WebDriverException

# MD5 hash a string like the URL
Expand Down Expand Up @@ -121,6 +122,9 @@ def lambda_handler(event, context):

db.put(db_data)

print('[!] Logging Scan')
logging_help.log_scan(db_data)

return return_data

except WebDriverException as e:
Expand Down
109 changes: 109 additions & 0 deletions src/logging_help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Use this code snippet in your app.
# If you need more information about configurations or implementing the sample code, visit the AWS docs:
# https://aws.amazon.com/developers/getting-started/python/

import os
import boto3
import json
import base64
import requests
from requests.auth import HTTPBasicAuth
from botocore.exceptions import ClientError

def get_secret():

secret_name = 'LogDNAIngestionKey' # os.environ['LOGGING_KEY']
region_name = "us-east-1"


#secrets_client = boto3.client('secretsmanager')
#secret_arn = "arn:aws:secretsmanager:us-east-1:358663747217:secret:LogDNAIngestionKey-HEKYmj"
#auth_token = secrets_client.get_secret_value(SecretId=secret_arn).get('logdna-ingestion')

#return auth_token


# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)

# In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
# See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
# We rethrow the exception by default.

secret = '{}'

try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
if e.response['Error']['Code'] == 'DecryptionFailureException':
# Secrets Manager can't decrypt the protected secret text using the provided KMS key.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InternalServiceErrorException':
# An error occurred on the server side.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InvalidParameterException':
# You provided an invalid value for a parameter.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InvalidRequestException':
# You provided a parameter value that is not valid for the current state of the resource.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'ResourceNotFoundException':
# We can't find the resource that you asked for.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
else:
# Decrypts secret using the associated KMS CMK.
# Depending on whether the secret is a string or binary, one of these fields will be populated.
if 'SecretString' in get_secret_value_response:
secret = get_secret_value_response['SecretString']
else:
secret = base64.b64decode(get_secret_value_response['SecretBinary'])

return json.loads(secret).get('logdna-ingestion')

def log_scan(db_data):

log_env = "unknown"

if "CI" in os.environ and os.environ.get("CI") == "true":
log_env = "test"
else:
log_env = "production"

logdna = get_secret()

logdata = {
"lines": [
{
"line": "A new scan was initiated | {}".format(db_data["title"]),
"app": "glimpse",
"level": "INFO",
"env": log_env,
"meta": {
"urlhash": db_data["urlhash"],
"url": db_data["url"],
"effectiveurl": db_data["effectiveurl"],
"title": db_data["title"],
"timescanned": db_data["timescanned"],
"numscans": int(db_data["numscans"])
}
}
]
}

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, ''))

if submission.status_code != 200: # or submission.json['status'] != "ok":
raise ValueError('Got status {}'.format(submission.status_code))

0 comments on commit 8db4292

Please sign in to comment.