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
|
2023-09-25 13:22:21 -07:00
|
|
|
from flask import current_app
|
2023-05-04 07:56:24 -07:00
|
|
|
|
2023-08-10 18:02:45 -04: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,
|
2023-08-30 09:29:08 -07:00
|
|
|
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,
|
2023-08-30 09:29:08 -07:00
|
|
|
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
|
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
|
2023-09-26 08:11:47 -07:00
|
|
|
|
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
|
|
|
|
|
return all_log_events
|
|
|
|
|
|
2023-09-26 08:11:47 -07:00
|
|
|
def _extract_account_number(self, ses_domain_arn, region):
|
|
|
|
|
account_number = ses_domain_arn
|
|
|
|
|
# handle cloud.gov case
|
|
|
|
|
if "aws-us-gov" in account_number:
|
|
|
|
|
account_number = account_number.replace(f"arn:aws-us-gov:ses:{region}:", "")
|
|
|
|
|
account_number = account_number.split(":")
|
|
|
|
|
account_number = account_number[0]
|
|
|
|
|
# handle staging case
|
|
|
|
|
else:
|
|
|
|
|
account_number = account_number.replace(f"arn:aws:ses:{region}:", "")
|
|
|
|
|
account_number = account_number.split(":")
|
|
|
|
|
account_number = account_number[0]
|
|
|
|
|
return account_number
|
|
|
|
|
|
2023-05-09 08:45:51 -07:00
|
|
|
def check_sms(self, message_id, notification_id, created_at):
|
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
|
2023-09-26 08:11:47 -07:00
|
|
|
account_number = self._extract_account_number(
|
|
|
|
|
cloud_config.ses_domain_arn, region
|
|
|
|
|
)
|
2023-05-04 07:56:24 -07:00
|
|
|
|
2023-09-21 14:48:10 -07:00
|
|
|
log_group_name = f"sns/{region}/{account_number}/DirectPublishToPhoneNumber"
|
2023-09-25 13:22:21 -07:00
|
|
|
current_app.logger.info(
|
2023-09-26 08:11:47 -07:00
|
|
|
f"Log group name: {log_group_name} message id: {message_id}"
|
2023-09-25 13:22:21 -07:00
|
|
|
)
|
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)
|
2023-05-04 07:56:24 -07:00
|
|
|
if all_log_events and len(all_log_events) > 0:
|
|
|
|
|
event = all_log_events[0]
|
2023-08-29 14:54:30 -07:00
|
|
|
message = json.loads(event["message"])
|
2023-09-25 13:22:21 -07:00
|
|
|
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
|
|
|
)
|
2023-09-26 08:11:47 -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)
|
2023-05-04 07:56:24 -07:00
|
|
|
if all_failed_events and len(all_failed_events) > 0:
|
2023-09-25 13:22:21 -07:00
|
|
|
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"])
|
2023-09-25 13:22:21 -07:00
|
|
|
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
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
raise Exception(
|
|
|
|
|
f"No event found for message_id {message_id} notification_id {notification_id}"
|
|
|
|
|
)
|