-
-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: make it possible to use ssm parameters as slack_webhook_url #107
Changes from all commits
cf22653
300aef8
1fbebfe
e28f062
0838bdc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,15 @@ def decrypt(encrypted_url): | |
except Exception: | ||
logging.exception("Failed to decrypt URL with KMS") | ||
|
||
def get_ssm_param(ssm_param): | ||
try: | ||
ssm = boto3.client("ssm") | ||
return ssm.get_parameter( | ||
Name=ssm_param, | ||
WithDecryption=True | ||
).get("Parameter").get("Value") | ||
except Exception: | ||
logging.exception("Failed to get URL from SSM") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably need to raise an exception here and possible change the message to |
||
|
||
def cloudwatch_notification(message, region): | ||
states = {'OK': 'good', 'INSUFFICIENT_DATA': 'warning', 'ALARM': 'danger'} | ||
|
@@ -47,7 +56,9 @@ def default_notification(subject, message): | |
# Send a message to a slack channel | ||
def notify_slack(subject, message, region): | ||
slack_url = os.environ['SLACK_WEBHOOK_URL'] | ||
if not slack_url.startswith("http"): | ||
if 'SLACK_WEBHOOK_URL_IS_SSM_PARAM' in os.environ and os.environ['SLACK_WEBHOOK_URL_IS_SSM_PARAM'] == 'True': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would propose a slightly more "pythontic" way (less chained conditionals): slack_webhook_url = os.environ.get('SLACK_WEBHOOK_URL')
slack_webhook_ssm_path = os.environ.get('SLACK_WEBHOOK_URL_SSM_PATH')
if slack_webhook_url:
slack_url = decrypt(slack_url)
elif slack_webhook_ssm_path:
slack_url = get_ssm_param(slack_url)
else:
raise Exception('Either `SLACK_WEBHOOK_URL` or `SLACK_WEBHOOK_URL_SSM_PATH` value must be provided') adding the |
||
slack_url = get_ssm_param(slack_url) | ||
elif not slack_url.startswith("http"): | ||
slack_url = decrypt(slack_url) | ||
|
||
slack_channel = os.environ['SLACK_CHANNEL'] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would move the client init outside of the function to cache it and open connection each time :D