Files
notifications-admin/app/s3_client/__init__.py
Alex Janousek 74c1648c8d Fix report download buttons showing as disabled (#2840)
* Fix for lazy loading while checking if file exists

* Fixed linting errors

* Fixed for static scan
2025-08-19 07:06:50 -07:00

99 lines
3.0 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:
error_code = client_error.response["Error"]["Code"]
if error_code in ["404", "NoSuchKey"]:
return False
return False
except Exception:
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