Skip to content
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

Production deploy 9/22/23 #496

Merged
merged 18 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions app/celery/provider_tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from datetime import datetime, timedelta
from time import time

Expand Down Expand Up @@ -39,9 +40,15 @@ def check_sms_delivery_receipt(self, message_id, notification_id, sent_at):
failure appears in the cloudwatch logs, so this should keep retrying until the log appears, or until
we run out of retries.
"""
status, provider_response = aws_cloudwatch_client.check_sms(
message_id, notification_id, sent_at
)
# TODO the localstack cloudwatch doesn't currently have our log groups. Possibly create them with awslocal?
if aws_cloudwatch_client.is_localstack():
status = "success"
provider_response = "this is a fake successful localstack sms message"
else:
status, provider_response = aws_cloudwatch_client.check_sms(
message_id, notification_id, sent_at
)

if status == "success":
status = NOTIFICATION_DELIVERED
elif status == "failure":
Expand Down Expand Up @@ -73,8 +80,19 @@ def deliver_sms(self, notification_id):
"Start sending SMS for notification id: {}".format(notification_id)
)
notification = notifications_dao.get_notification_by_id(notification_id)
ansi_green = "\033[32m"
ansi_reset = "\033[0m"

if not notification:
raise NoResultFound()
if (
os.getenv("NOTIFY_ENVIRONMENT") == "development"
and "authentication code" in notification.content
):
current_app.logger.warning(
ansi_green + f"AUTHENTICATION CODE: {notification.content}" + ansi_reset
)

message_id = send_to_providers.send_sms_to_provider(notification)
# We have to put it in UTC. For other timezones, the delay
# will be ignored and it will fire immediately (although this probably only affects developer testing)
Expand Down
38 changes: 28 additions & 10 deletions app/clients/cloudwatch/aws_cloudwatch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import re
import time

Expand All @@ -14,13 +15,26 @@ class AwsCloudwatchClient(Client):
"""

def init_app(self, current_app, *args, **kwargs):
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,
)
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"),
)
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,
)
self._is_localstack = False

super(Client, self).__init__(*args, **kwargs)
self.current_app = current_app
self._valid_sender_regex = re.compile(r"^\+?\d{5,14}$")
Expand All @@ -29,6 +43,9 @@ def init_app(self, current_app, *args, **kwargs):
def name(self):
return "cloudwatch"

def is_localstack(self):
return self._is_localstack

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
now = round(time.time() * 1000)
Expand Down Expand Up @@ -62,13 +79,14 @@ def _get_log(self, my_filter, log_group_name, sent_at):
return all_log_events

def check_sms(self, message_id, notification_id, created_at):
region = cloud_config.sns_region
# 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
account_number = account_number.replace("arn:aws:ses:us-west-2:", "")
account_number = account_number.replace(f"arn:aws:ses:{region}:", "")
account_number = account_number.split(":")
account_number = account_number[0]

log_group_name = f"sns/us-west-2/{account_number}/DirectPublishToPhoneNumber"
log_group_name = f"sns/{region}/{account_number}/DirectPublishToPhoneNumber"
filter_pattern = '{$.notification.messageId="XXXXX"}'
filter_pattern = filter_pattern.replace("XXXXX", message_id)
all_log_events = self._get_log(filter_pattern, log_group_name, created_at)
Expand All @@ -79,7 +97,7 @@ def check_sms(self, message_id, notification_id, created_at):
return "success", message["delivery"]["providerResponse"]

log_group_name = (
f"sns/us-west-2/{account_number}/DirectPublishToPhoneNumber/Failure"
f"sns/{region}/{account_number}/DirectPublishToPhoneNumber/Failure"
)
all_failed_events = self._get_log(filter_pattern, log_group_name, created_at)
if all_failed_events and len(all_failed_events) > 0:
Expand Down
26 changes: 19 additions & 7 deletions app/clients/sms/aws_sns.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
from time import monotonic

Expand All @@ -16,13 +17,24 @@ class AwsSnsClient(SmsClient):
"""

def init_app(self, current_app, *args, **kwargs):
self._client = client(
"sns",
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,
)
if os.getenv("LOCALSTACK_ENDPOINT_URL"):
self._client = client(
"sns",
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"),
)
else:
self._client = client(
"sns",
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,
)

super(SmsClient, self).__init__(*args, **kwargs)
self.current_app = current_app
self._valid_sender_regex = re.compile(r"^\+?\d{5,14}$")
Expand Down
3 changes: 3 additions & 0 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def bulk_invite_user_to_service(file_name, service_id, user_id, auth_type, permi
# "send_texts,send_emails,view_activity"
from app.service_invite.rest import create_invited_user

current_app.logger.info("ENTER")
file = open(file_name)
for email_address in file:
data = {
Expand All @@ -273,6 +274,7 @@ def bulk_invite_user_to_service(file_name, service_id, user_id, auth_type, permi
"auth_type": auth_type,
"invite_link_host": current_app.config["ADMIN_BASE_URL"],
}
current_app.logger.info(f"DATA = {data}")
with current_app.test_request_context(
path="/service/{}/invite/".format(service_id),
method="POST",
Expand All @@ -281,6 +283,7 @@ def bulk_invite_user_to_service(file_name, service_id, user_id, auth_type, permi
):
try:
response = create_invited_user(service_id)
current_app.logger.info(f"RESPONSE {response[1]}")
if response[1] != 201:
print(
"*** ERROR occurred for email address: {}".format(
Expand Down
44 changes: 44 additions & 0 deletions docs/localstack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
How to Use Localstack in Your Development Work
==================================



### Install Docker Desktop (One-Time)

* https://docs.docker.com/desktop/install/mac-install/


### Install Localstack (One-Time)

* >pip install --upgrade localstack
* >localstack --version # should be 2.2.0 or later


### Add LOCALSTACK_ENDPOINT_URL to Your .env File (One-Time)

* Find the value in the sample.env file (# LOCALSTACK_ENDPOINT_URL=http://localhost:4566).
* Copy and uncomment it into your .env file

### Run with Localstack (Recurring)

#### Start Docker Desktop and localstack image

* Open Docker Desktop from Finder
* Images->Local->localstack/localstack click on the start button on the right hand side to get the localstack
docker image going


#### Start Localstack

* From your project directory in a separate terminal window, either:
* >localstack start
* >pipenv run localstack start

#### Proceed With Your Usual Development Activities

Assuming you followed all these steps and nothing went wrong, you should be running with localstack for SNS now.
You should be able to send an SMS message in the UI and observe it in the dashboard moving from Pending to Delivered
over a period of five minutes. And you should not receive a text message.

NOTE: You will still be prompted for a 2FA code when you log in. To get the code, look in the notification-api
logs for "AUTHENTICATION_CODE:".
4 changes: 2 additions & 2 deletions migrations/versions/0402_total_message_limit_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import sqlalchemy as sa


revision = '0402_total_message_limit_default'
down_revision = '0401_add_e2e_test_user'
revision = "0402_total_message_limit_default"
down_revision = "0401_add_e2e_test_user"


def upgrade():
Expand Down
Loading