Skip to content

Instantly share code, notes, and snippets.

@deploytoprod
Created April 24, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deploytoprod/548fd6f8282c6c605df79246412052bc to your computer and use it in GitHub Desktop.
Save deploytoprod/548fd6f8282c6c605df79246412052bc to your computer and use it in GitHub Desktop.
import json
import boto3
def crc(event, context):
""" Returns a CRC (Challenge Response Check) to keep this webhook
secure. https://goo.gl/kFdJgV for more details. """
# Short circuit ping from CloudWatch Events
if event.get('source', None) == 'aws.events':
print('ping')
return
import base64
import hmac
import hashlib
print('Calculating CRC')
ssm = boto3.client('ssm')
try:
response = ssm.get_parameters(
Names=[
'sls.dev.twitter.translator.consumer.secret'
],
WithDecryption=True
)
except ClientError as error:
print('Problem getting keys from SSM: {}'.format(error))
return {
'statusCode': 501,
'body': 'Problem getting consumer key'
}
else:
params = response['Parameters']
crc = event['queryStringParameters']['crc_token']
sha256_hash_digest = hmac.new(
params[0]['Value'].encode('utf-8'), msg=crc.encode('utf-8'),
digestmod=hashlib.sha256).digest()
body = json.dumps({'response_token': 'sha256=' +
base64.b64encode(sha256_hash_digest).decode('utf-8')})
print('Body response: {}'.format(body))
response = {
'statusCode': 200,
'body': body
}
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment