Files
notifications-api/app/clients/cloudwatch/aws_cloudwatch.py

135 lines
5.3 KiB
Python
Raw Normal View History

2023-05-04 07:56:24 -07:00
import json
2023-08-29 13:12:18 -07:00
import os
2023-05-04 07:56:24 -07:00
import re
import time
from boto3 import client
from flask import current_app
2023-05-04 07:56:24 -07:00
from app.clients import AWS_CLIENT_CONFIG, Client
2023-05-04 07:56:24 -07:00
from app.cloudfoundry_config import cloud_config
2023-05-05 08:09:15 -07:00
class AwsCloudwatchClient(Client):
2023-05-04 07:56:24 -07:00
"""
2023-05-04 08:40:16 -07:00
This client is responsible for retrieving sms delivery receipts from cloudwatch.
2023-05-04 07:56:24 -07:00
"""
def init_app(self, current_app, *args, **kwargs):
2023-08-29 13:12:18 -07:00
if os.getenv("LOCALSTACK_ENDPOINT_URL"):
self._client = client(
"logs",
region_name=cloud_config.sns_region,
aws_access_key_id=cloud_config.sns_access_key,
aws_secret_access_key=cloud_config.sns_secret_key,
config=AWS_CLIENT_CONFIG,
endpoint_url=os.getenv("LOCALSTACK_ENDPOINT_URL"),
2023-08-29 13:12:18 -07:00
)
self._is_localstack = True
else:
self._client = client(
"logs",
region_name=cloud_config.sns_region,
aws_access_key_id=cloud_config.sns_access_key,
aws_secret_access_key=cloud_config.sns_secret_key,
config=AWS_CLIENT_CONFIG,
2023-08-29 13:12:18 -07:00
)
self._is_localstack = False
2023-05-05 08:09:15 -07:00
super(Client, self).__init__(*args, **kwargs)
2023-05-04 07:56:24 -07:00
self.current_app = current_app
self._valid_sender_regex = re.compile(r"^\+?\d{5,14}$")
@property
def name(self):
2023-08-29 14:54:30 -07:00
return "cloudwatch"
2023-05-04 07:56:24 -07:00
2023-08-29 13:12:18 -07:00
def is_localstack(self):
return self._is_localstack
2023-05-09 08:45:51 -07:00
def _get_log(self, my_filter, log_group_name, sent_at):
# Check all cloudwatch logs from the time the notification was sent (currently 5 minutes previously) until now
2023-05-04 07:56:24 -07:00
now = round(time.time() * 1000)
2023-05-09 08:45:51 -07:00
beginning = sent_at
current_app.logger.info(f"TIME RANGE TO CHECK {beginning} to {now}")
2023-05-04 07:56:24 -07:00
next_token = None
all_log_events = []
while True:
if next_token:
response = self._client.filter_log_events(
logGroupName=log_group_name,
filterPattern=my_filter,
nextToken=next_token,
startTime=beginning,
2023-08-29 14:54:30 -07:00
endTime=now,
2023-05-04 07:56:24 -07:00
)
else:
response = self._client.filter_log_events(
logGroupName=log_group_name,
filterPattern=my_filter,
startTime=beginning,
2023-08-29 14:54:30 -07:00
endTime=now,
2023-05-04 07:56:24 -07:00
)
2023-08-29 14:54:30 -07:00
log_events = response.get("events", [])
2023-05-04 07:56:24 -07:00
all_log_events.extend(log_events)
2023-05-09 08:45:51 -07:00
if len(log_events) > 0:
# We found it
current_app.logger.info(
f"WE FOUND THE EVENT WE WERE LOOKING FOR? {log_events}"
)
2023-05-09 08:45:51 -07:00
break
2023-08-29 14:54:30 -07:00
next_token = response.get("nextToken")
2023-05-04 07:56:24 -07:00
if not next_token:
break
if len(all_log_events) == 0:
print(f"WE FOUND NO LOG EVENTS OVER TIME RANGE {beginning} to {now}")
2023-05-04 07:56:24 -07:00
return all_log_events
2023-05-09 08:45:51 -07:00
def check_sms(self, message_id, notification_id, created_at):
if os.getenv("LOCALSTACK_ENDPOINT_URL"):
current_app.logger.info("GADZOOKS WE ARE RUNNING WITH LOCALSTACK")
2023-09-21 14:48:10 -07:00
region = cloud_config.sns_region
2023-05-05 08:09:15 -07:00
# TODO this clumsy approach to getting the account number will be fixed as part of notify-api #258
account_number = cloud_config.ses_domain_arn
2023-09-21 14:48:10 -07:00
account_number = account_number.replace(f"arn:aws:ses:{region}:", "")
2023-05-04 07:56:24 -07:00
account_number = account_number.split(":")
account_number = account_number[0]
2023-09-21 14:48:10 -07:00
log_group_name = f"sns/{region}/{account_number}/DirectPublishToPhoneNumber"
current_app.logger.info(
f"LOG GROUP NAME: {log_group_name} MESSAGE ID: {message_id}"
)
2023-05-04 07:56:24 -07:00
filter_pattern = '{$.notification.messageId="XXXXX"}'
filter_pattern = filter_pattern.replace("XXXXX", message_id)
2023-05-09 08:45:51 -07:00
all_log_events = self._get_log(filter_pattern, log_group_name, created_at)
current_app.logger.info(f"NUMBER OF ALL LOG EVENTS {len(all_log_events)}")
2023-05-04 07:56:24 -07:00
if all_log_events and len(all_log_events) > 0:
current_app.logger.info(
"SHOULD RETURN SUCCESS BECAUSE WE FOUND A SUCCESS MESSAGE FOR MESSAGE ID"
)
2023-05-04 07:56:24 -07:00
event = all_log_events[0]
2023-08-29 14:54:30 -07:00
message = json.loads(event["message"])
current_app.logger.info(f"MESSAGE {message}")
2023-08-29 14:54:30 -07:00
return "success", message["delivery"]["providerResponse"]
2023-05-04 07:56:24 -07:00
2023-08-29 14:54:30 -07:00
log_group_name = (
2023-09-21 14:48:10 -07:00
f"sns/{region}/{account_number}/DirectPublishToPhoneNumber/Failure"
2023-08-29 14:54:30 -07:00
)
current_app.logger.info(f"FAILURE LOG GROUP NAME {log_group_name}")
2023-05-09 08:45:51 -07:00
all_failed_events = self._get_log(filter_pattern, log_group_name, created_at)
current_app.logger.info(
f"NUMBER OF ALL FAILED LOG EVENTS {len(all_failed_events)}"
)
2023-05-04 07:56:24 -07:00
if all_failed_events and len(all_failed_events) > 0:
current_app.logger.info("SHOULD RETURN FAILED BECAUSE WE FOUND A FAILURE")
2023-05-04 07:56:24 -07:00
event = all_failed_events[0]
2023-08-29 14:54:30 -07:00
message = json.loads(event["message"])
current_app.logger.info(f"MESSAGE {message}")
2023-08-29 14:54:30 -07:00
return "failure", message["delivery"]["providerResponse"]
2023-05-04 07:56:24 -07:00
print(f"RAISING EXCEPTION FOR MESSAGE_ID {message_id}")
2023-08-29 14:54:30 -07:00
raise Exception(
f"No event found for message_id {message_id} notification_id {notification_id}"
)