lazy init encryption

This commit is contained in:
Kenneth Kehl
2025-10-06 12:16:34 -07:00
parent e3a465b8ba
commit 46c5cb24fe
3 changed files with 19 additions and 5 deletions

View File

@@ -96,7 +96,7 @@ aws_ses_client = None
aws_ses_stub_client = None aws_ses_stub_client = None
aws_sns_client = None aws_sns_client = None
aws_cloudwatch_client = None aws_cloudwatch_client = None
encryption = Encryption() encryption = None
zendesk_client = None zendesk_client = None
redis_store = RedisClient() redis_store = RedisClient()
document_download_client = None document_download_client = None
@@ -144,6 +144,15 @@ def get_aws_sns_client():
return aws_sns_client return aws_sns_client
def get_encryption():
global encryption
if os.environ.get("NOTIFY_ENVIRONMENT") == "test":
return Encryption()
if encryption is None:
raise RuntimeError(f"Celery not initialized encryption: {encryption}")
return encryption
def get_document_download_client(): def get_document_download_client():
global document_download_client global document_download_client
# Our unit tests mock anyway # Our unit tests mock anyway
@@ -157,7 +166,7 @@ def get_document_download_client():
def create_app(application): def create_app(application):
global zendesk_client, migrate, document_download_client, aws_ses_client, aws_ses_stub_client, aws_sns_client global zendesk_client, migrate, document_download_client, aws_ses_client, aws_ses_stub_client, aws_sns_client, encryption # noqa
from app.config import configs from app.config import configs
notify_environment = os.environ["NOTIFY_ENVIRONMENT"] notify_environment = os.environ["NOTIFY_ENVIRONMENT"]
@@ -192,6 +201,8 @@ def create_app(application):
aws_ses_stub_client.init_app(stub_url=application.config["SES_STUB_URL"]) aws_ses_stub_client.init_app(stub_url=application.config["SES_STUB_URL"])
aws_sns_client = AwsSnsClient() aws_sns_client = AwsSnsClient()
aws_sns_client.init_app(application) aws_sns_client.init_app(application)
encryption = Encryption()
encryption.init_app(application)
# end lazy initialization # end lazy initialization
@@ -206,7 +217,6 @@ def create_app(application):
) )
notify_celery.init_app(application) notify_celery.init_app(application)
encryption.init_app(application)
redis_store.init_app(application) redis_store.init_app(application)
register_blueprint(application) register_blueprint(application)

View File

@@ -3,10 +3,12 @@ import json
from flask import current_app from flask import current_app
from requests import HTTPError, RequestException, request from requests import HTTPError, RequestException, request
from app import encryption, notify_celery from app import get_encryption, notify_celery
from app.config import QueueNames from app.config import QueueNames
from app.utils import DATETIME_FORMAT from app.utils import DATETIME_FORMAT
encryption = get_encryption()
@notify_celery.task( @notify_celery.task(
bind=True, name="send-delivery-status", max_retries=5, default_retry_delay=300 bind=True, name="send-delivery-status", max_retries=5, default_retry_delay=300

View File

@@ -5,7 +5,7 @@ import pytest
import requests_mock import requests_mock
from freezegun import freeze_time from freezegun import freeze_time
from app import encryption from app import get_encryption
from app.celery.service_callback_tasks import ( from app.celery.service_callback_tasks import (
send_complaint_to_service, send_complaint_to_service,
send_delivery_status_to_service, send_delivery_status_to_service,
@@ -20,6 +20,8 @@ from tests.app.db import (
create_template, create_template,
) )
encryption = get_encryption()
@pytest.mark.parametrize( @pytest.mark.parametrize(
"notification_type", [NotificationType.EMAIL, NotificationType.SMS] "notification_type", [NotificationType.EMAIL, NotificationType.SMS]