|
| 1 | +# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: MIT-0 |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 5 | +# software and associated documentation files (the "Software"), to deal in the Software |
| 6 | +# without restriction, including without limitation the rights to use, copy, modify, |
| 7 | +# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | +# permit persons to whom the Software is furnished to do so. |
| 9 | +# |
| 10 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 11 | +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 12 | +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 13 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 14 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 15 | +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + |
| 17 | +import json |
| 18 | +import urllib3 |
| 19 | +import boto3 |
| 20 | +import os |
| 21 | +from datetime import datetime, timedelta, timezone |
| 22 | + |
| 23 | +# ------------------------------------------- |
| 24 | +# setup global data |
| 25 | +# ------------------------------------------- |
| 26 | +http = urllib3.PoolManager() |
| 27 | +utc = timezone.utc |
| 28 | +date = datetime.now().date() |
| 29 | +today = datetime.now().replace(tzinfo=utc) |
| 30 | +AWS_REGION = 'eu-west-2' |
| 31 | + |
| 32 | +if os.environ.get('EXPIRY_DAYS') is None: |
| 33 | + expiry_days = 45 |
| 34 | +else: |
| 35 | + expiry_days = int(os.environ['EXPIRY_DAYS']) |
| 36 | + |
| 37 | +if os.environ.get('CRITICAL_EXPIRY_DAYS') is None: |
| 38 | + critical_expiry_days = 15 |
| 39 | +else: |
| 40 | + critical_expiry_days = int(os.environ['CRITICAL_EXPIRY_DAYS']) |
| 41 | + |
| 42 | +expiry_window = date + timedelta(days=expiry_days) |
| 43 | +critical_expiry_window = today + timedelta(days=critical_expiry_days) |
| 44 | + |
| 45 | + |
| 46 | +def lambda_handler(event, context): |
| 47 | + # if this is coming from the ACM event, its for a single certificate |
| 48 | + if event['detail-type'] == "ACM Certificate Approaching Expiration": |
| 49 | + response = handle_single_cert(event) |
| 50 | + return { |
| 51 | + 'statusCode': 200, |
| 52 | + 'body': response |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def handle_single_cert(event): |
| 57 | + cert_client = boto3.client('acm') |
| 58 | + cert_details = cert_client.describe_certificate(CertificateArn=event['resources'][0]) |
| 59 | + result = check_cert(cert_details) |
| 60 | + return result |
| 61 | + |
| 62 | + |
| 63 | +def check_cert(cert_details): |
| 64 | + result = 'The following certificate is expiring within ' |
| 65 | + |
| 66 | + if cert_details['Certificate']['NotAfter'].date() == expiry_window: |
| 67 | + result = result + str(expiry_days) + ' days: ' + \ |
| 68 | + cert_details['Certificate']['DomainName'] + \ |
| 69 | + ' (' + cert_details['Certificate']['CertificateArn'] + ') ' |
| 70 | + send_msg_slack(result) |
| 71 | + send_email(result) |
| 72 | + elif cert_details['Certificate']['NotAfter'] < critical_expiry_window: |
| 73 | + result = ':bomb: CRITICAL:' + result + str(critical_expiry_days) + ' days: ' + \ |
| 74 | + cert_details['Certificate']['DomainName'] + \ |
| 75 | + ' (' + cert_details['Certificate']['CertificateArn'] + ') ' |
| 76 | + send_msg_slack(result) |
| 77 | + send_email(result) |
| 78 | + |
| 79 | + return result |
| 80 | + |
| 81 | + |
| 82 | +def send_email(result): |
| 83 | + sns_client = boto3.client('sns') |
| 84 | + sns_client.publish(TopicArn=os.environ['SNS_TOPIC_ARN'], Message=result.replace(":bomb: ", ""), |
| 85 | + Subject='Certificate Expiration Notification') |
| 86 | + |
| 87 | + |
| 88 | +def send_msg_slack(result): |
| 89 | + |
| 90 | + url= get_slack_url() |
| 91 | + if(url is None): |
| 92 | + print ('unable to send slack message, slack url not set') |
| 93 | + else: |
| 94 | + msg = { |
| 95 | + "channel": "<INSERT SLACK CHANNEL>", |
| 96 | + "username": "ACM_Expiry", |
| 97 | + "text": result, |
| 98 | + "icon_emoji": "" |
| 99 | + } |
| 100 | + |
| 101 | + encoded_msg = json.dumps(msg).encode('utf-8') |
| 102 | + resp = http.request('POST', url, body=encoded_msg) |
| 103 | + print({ |
| 104 | + "message": result, |
| 105 | + "status_code": resp.status, |
| 106 | + "response": resp.data |
| 107 | + }) |
| 108 | + |
| 109 | +def get_slack_url(): |
| 110 | + slack_url = None |
| 111 | + pw_response = boto3.client('secretsmanager', region_name=AWS_REGION).get_secret_value(SecretId='<INSERT SECRET ID>') |
| 112 | + secrets = pw_response['SecretString'] |
| 113 | + secret_dict= json.loads(secrets) |
| 114 | + slack_url_key = 'slack_hook_url' |
| 115 | + if slack_url_key in secret_dict: |
| 116 | + slack_url = secret_dict[slack_url_key] |
| 117 | + print('slack hook url'+slack_url) |
| 118 | + else: |
| 119 | + print(slack_url_key+' not declared in AWS Secrets') |
| 120 | + |
| 121 | + return slack_url |
0 commit comments