From 54ea9934b43cf6bca1aa65114b5882e8c994d175 Mon Sep 17 00:00:00 2001 From: Stanislas Bruhiere Date: Fri, 13 Oct 2023 19:15:47 +0200 Subject: [PATCH] feat: only use 1 env var --- .github/workflows/builds.yml | 2 -- .github/workflows/tests.yml | 1 - README.md | 3 +-- docker-compose-dev.yml | 2 -- docker-compose.yml | 1 - src/app/config.py | 4 +--- src/app/services/bucket/s3.py | 9 ++++----- src/app/services/services.py | 3 +-- 8 files changed, 7 insertions(+), 18 deletions(-) diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml index 4a4830e5..c6c292e6 100644 --- a/.github/workflows/builds.yml +++ b/.github/workflows/builds.yml @@ -30,8 +30,6 @@ jobs: S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }} S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} S3_REGION: ${{ secrets.S3_REGION }} - S3_USE_PROXY: "false" - S3_PROXY_URL: ${{ secrets.S3_PROXY_URL }} run: docker-compose up -d --build - name: Docker sanity check run: sleep 20 && nc -vz localhost 8080 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a90c9003..62975e3f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -29,7 +29,6 @@ jobs: S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }} S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} S3_REGION: ${{ secrets.S3_REGION }} - S3_USE_PROXY: "false" TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }} TELEGRAM_TEST_CHAT_ID: ${{ secrets.TELEGRAM_TEST_CHAT_ID }} run: | diff --git a/README.md b/README.md index 4ac2b1b3..3f9bfcfa 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,7 @@ This file will have to hold the following information: - `S3_REGION`: your S3 bucket is geographically identified by its location's region - `S3_ENDPOINT_URL`: the URL providing a S3 endpoint by your cloud provider - `BUCKET_NAME`: the name of the storage bucket -- `S3_USE_PROXY`: enables the use of a proxy for public urls to hide the real s3 url -- `S3_PROXY_URL`: the url of the proxy to hide the real s3 url behind +- `S3_PROXY_URL`: the url of the proxy to hide the real s3 url behind, do not use proxy if "" Optionally, the following information can be added: - `SENTRY_DSN`: the URL of the [Sentry](https://sentry.io/) project, which monitors back-end errors and report them back. - `SENTRY_SERVER_NAME`: the server tag to apply to events. diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index f1e6bccc..5223a770 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -25,8 +25,6 @@ services: - S3_ACCESS_KEY=${S3_ACCESS_KEY} - S3_SECRET_KEY=${S3_SECRET_KEY} - S3_REGION=${S3_REGION} - - S3_USE_PROXY=false - - S3_PROXY_URL=${S3_PROXY_URL} - TELEGRAM_TOKEN=${TELEGRAM_TOKEN} - TELEGRAM_TEST_CHAT_ID=${TELEGRAM_TEST_CHAT_ID} depends_on: diff --git a/docker-compose.yml b/docker-compose.yml index c5163e0f..bc8eeea1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,7 +22,6 @@ services: - S3_ACCESS_KEY=${S3_ACCESS_KEY} - S3_SECRET_KEY=${S3_SECRET_KEY} - S3_REGION=${S3_REGION} - - S3_USE_PROXY=false - S3_PROXY_URL=${S3_PROXY_URL} - SENTRY_DSN=${SENTRY_DSN} - SENTRY_SERVER_NAME=${SENTRY_SERVER_NAME} diff --git a/src/app/config.py b/src/app/config.py index fa923aa5..45f2bec9 100644 --- a/src/app/config.py +++ b/src/app/config.py @@ -47,9 +47,7 @@ S3_SECRET_KEY: str = os.environ["S3_SECRET_KEY"] S3_REGION: str = os.environ["S3_REGION"] S3_ENDPOINT_URL: str = os.environ["S3_ENDPOINT_URL"] -S3_USE_PROXY: bool = os.environ.get("S3_USE_PROXY", "false").lower() != "false" -S3_PROXY_URL: Union[str, - None] = os.environ["S3_PROXY_URL"] if S3_USE_PROXY else None +S3_PROXY_URL: str = os.environ.get("S3_PROXY_URL", "") DUMMY_BUCKET_FILE = ( "https://ec.europa.eu/jrc/sites/jrcsh/files/styles/normal-responsive/" "public/growing-risk-future-wildfires_adobestock_199370851.jpeg" diff --git a/src/app/services/bucket/s3.py b/src/app/services/bucket/s3.py index 514799f2..37289ee0 100644 --- a/src/app/services/bucket/s3.py +++ b/src/app/services/bucket/s3.py @@ -24,15 +24,13 @@ class S3Bucket: access_key: the S3 access key secret_key: the S3 secret key bucket_name: the bucket name - use_proxy: whether to use a proxy for public url generation proxy_url: the proxy url """ - def __init__(self, region: str, endpoint_url: str, access_key: str, secret_key: str, bucket_name: str, use_proxy: bool = False, proxy_url: Union[str, None] = None) -> None: + def __init__(self, region: str, endpoint_url: str, access_key: str, secret_key: str, bucket_name: str, proxy_url: str) -> None: _session = boto3.Session(access_key, secret_key, region_name=region) self._s3 = _session.client("s3", endpoint_url=endpoint_url) self.bucket_name = bucket_name - self.use_proxy = use_proxy self.proxy_url = proxy_url async def get_file_metadata(self, bucket_key: str) -> Dict[str, Any]: @@ -52,14 +50,15 @@ async def check_file_existence(self, bucket_key: str) -> bool: async def get_public_url(self, bucket_key: str, url_expiration: int = 3600) -> str: """Generate a temporary public URL for a bucket file""" if not (await self.check_file_existence(bucket_key)): - raise HTTPException(status_code=404, detail="File cannot be found on the bucket storage") + raise HTTPException( + status_code=404, detail="File cannot be found on the bucket storage") # Point to the bucket file file_params = {"Bucket": self.bucket_name, "Key": bucket_key} # Generate a public URL for it using boto3 presign URL generation\ presigned_url = self._s3.generate_presigned_url( "get_object", Params=file_params, ExpiresIn=url_expiration) - if self.use_proxy: + if len(self.proxy_url) > 0: return presigned_url.replace(self.bucket_name, self.proxy_url) return presigned_url diff --git a/src/app/services/services.py b/src/app/services/services.py index cad0e970..4d1153c3 100644 --- a/src/app/services/services.py +++ b/src/app/services/services.py @@ -11,5 +11,4 @@ bucket_service = S3Bucket(cfg.S3_REGION, cfg.S3_ENDPOINT_URL, cfg.S3_ACCESS_KEY, cfg.S3_SECRET_KEY, - cfg.BUCKET_NAME, cfg.S3_USE_PROXY, - cfg.S3_PROXY_URL) + cfg.BUCKET_NAME, cfg.S3_PROXY_URL)