Files
notifications-admin/app/s3_client/__init__.py
Alex Janousek 8d33f28b76 Refactored reports to use pregenerated docs instead (#2831)
* Refactored reports to use pregenerated docs instead

* Fixed e2e test

* Fixed anothr bug

* Cleanup

* Fixed timezone conversion

* Updated ref files

* Updated reference files, refreshed ui/ux for report generation. Buttons toggle on and off based on if report exists

* Fixed linting errors, removed pytz

* Fixed test failure

* e2e test fix

* Speeding up unit tests

* Removed python time library that was causing performance issues with unit tests

* Updated poetry lock

* Unit test improvements

* Made change that ken reccomended
2025-08-15 15:02:54 -04:00

99 lines
3.1 KiB
Python

import os
import botocore
from boto3 import Session
from botocore.config import Config
from flask import current_app
from notifications_utils.s3 import S3ObjectNotFound
AWS_CLIENT_CONFIG = Config(
# This config is required to enable S3 to connect to FIPS-enabled
# endpoints. See https://aws.amazon.com/compliance/fips/ for more
# information.
s3={
"addressing_style": "virtual",
},
use_fips_endpoint=True,
)
def get_s3_object(
bucket_name,
filename,
access_key,
secret_key,
region,
):
# To inspect contents: obj.get()['Body'].read().decode('utf-8')
session = Session(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
region_name=region,
)
s3 = session.resource(
"s3",
config=AWS_CLIENT_CONFIG,
)
obj = s3.Object(bucket_name, filename)
# This 'proves' that use of moto in the relevant tests in test_send.py
# mocks everything related to S3. What you will see in the logs is:
# Exception: CREATED AT <MagicMock name='resource().Bucket().creation_date' id='4665562448'>
#
# raise Exception(f"CREATED AT {_s3.Bucket(bucket_name).creation_date}")
if os.getenv("NOTIFY_ENVIRONMENT") == "test":
teststr = str(s3.Bucket(bucket_name).creation_date).lower()
if "magicmock" not in teststr:
raise Exception(
"Test is not mocked, use @mock_aws or the relevant mocker.patch to avoid accessing S3"
)
return obj
def check_s3_file_exists(obj):
try:
obj.load()
return True
except botocore.exceptions.ClientError as client_error:
if client_error.response["Error"]["Code"] in ["404", "NoSuchKey"]:
return False
current_app.logger.error(
f"Error checking S3 file {obj.bucket_name}/{obj.key}: {client_error}"
)
return False
def get_s3_metadata(obj):
try:
return obj.get()["Metadata"]
except botocore.exceptions.ClientError as client_error:
current_app.logger.error(
f"Unable to download s3 file {obj.bucket_name}/{obj.key}"
)
if client_error.response["Error"]["Code"] == "NoSuchKey":
raise S3ObjectNotFound(client_error.response, client_error.operation_name)
raise client_error
def set_s3_metadata(obj, **kwargs):
copy_from_object_result = obj.copy_from(
CopySource=f"{obj.bucket_name}/{obj.key}",
ServerSideEncryption="AES256",
Metadata={key: str(value) for key, value in kwargs.items()},
MetadataDirective="REPLACE",
)
return copy_from_object_result
def get_s3_contents(obj):
try:
response = obj.get()
return response["Body"].read().decode("utf-8")
except botocore.exceptions.ClientError as client_error:
current_app.logger.error(
f"Unable to download s3 file {obj.bucket_name}/{obj.key}"
)
if client_error.response["Error"]["Code"] == "NoSuchKey":
raise S3ObjectNotFound(client_error.response, client_error.operation_name)
raise client_error