Skip to content

Commit

Permalink
Merge pull request #493 from wri/develop
Browse files Browse the repository at this point in the history
GTC-2449 Catch access denied error in get_aws_files() [merge to master]
  • Loading branch information
danscales authored Apr 2, 2024
2 parents bb838ec + 315b2da commit 1a46756
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/routes/datasets/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def _verify_source_file_access(sources: List[str]) -> None:
raise HTTPException(
status_code=400,
detail=(
"Cannot access all of the source files. "
"Cannot access all of the source files (non-existent or access denied). "
f"Invalid sources: {invalid_sources}"
),
)
8 changes: 7 additions & 1 deletion app/utils/aws.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Any, Dict, List, Optional, Sequence

import boto3
import botocore
import httpx
from httpx_auth import AWS4Auth
from fastapi.logger import logger

from ..settings.globals import (
AWS_REGION,
Expand Down Expand Up @@ -108,7 +110,11 @@ def get_aws_files(
if exit_after_max and num_matches >= exit_after_max:
break

except s3_client.exceptions.NoSuchBucket:
# Strangely, s3_client.exceptions has NoSuchBucket, but doesn't have
# AccessDenied, even though you can get that error, so we just catch all botocore
# exceptions.
except botocore.exceptions.ClientError as error:
logger.warning(f"get_aws_file: {error}")
matches = list()

return matches
Expand Down
4 changes: 2 additions & 2 deletions tests/routes/datasets/test_versions.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ async def test_invalid_source_uri(async_client: AsyncClient):
assert response.json()["status"] == "failed"
assert (
response.json()["message"]
== f"Cannot access all of the source files. Invalid sources: ['{bad_uri}']"
== f"Cannot access all of the source files (non-existent or access denied). Invalid sources: ['{bad_uri}']"
)

# Create a version with a valid source_uri so we have something to append to
Expand All @@ -332,7 +332,7 @@ async def test_invalid_source_uri(async_client: AsyncClient):
assert response.json()["status"] == "failed"
assert (
response.json()["message"]
== f"Cannot access all of the source files. Invalid sources: ['{bad_uri}']"
== f"Cannot access all of the source files (non-existent or access denied). Invalid sources: ['{bad_uri}']"
)

# Test appending to a version that DOESN'T exist
Expand Down

0 comments on commit 1a46756

Please sign in to comment.