Skip to content

Commit

Permalink
cloud: Make URL cache bucket private
Browse files Browse the repository at this point in the history
  • Loading branch information
spbnick committed Feb 14, 2024
1 parent 45c7d8b commit 843b1e0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions kcidb/cloud/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ function functions_deploy() {
env_yaml \
-- "$@")"
eval "$params"

# Allow the App Engine service account to create its own tokens
# so it can sign Google Cloud Storage URLs
mute gcloud iam service-accounts add-iam-policy-binding \
"$project@appspot.gserviceaccount.com" \
--role=roles/iam.serviceAccountTokenCreator \
--member="serviceAccount:$project@appspot.gserviceaccount.com"

# Create empty environment YAML
declare env_yaml_file
env_yaml_file=`mktemp --tmpdir kcidb_cloud_env.XXXXXXXX`
Expand Down
3 changes: 2 additions & 1 deletion kcidb/cloud/storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function storage_deploy() {
TMPDIR="$TMPDIR_ORIG" gsutil -q mb -p "$project" -c STANDARD \
-l "us-central1" -b on "gs://$bucket"
fi
TMPDIR="$TMPDIR_ORIG" gsutil -q iam ch allUsers:objectViewer "gs://$bucket/"
# Revoke public read access from the bucket
TMPDIR="$TMPDIR_ORIG" gsutil -q iam ch -d allUsers:objectViewer "gs://$bucket/"
}

# Remove a Google Cloud Storage Bucket and its contents
Expand Down
7 changes: 6 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ def kcidb_cache_urls(event, context):
cache.store(url)


# The expiration time (a timedelta) of the URLs returned by the cache
# redirect, or None to return permanent URLs pointing to the public bucket.
CACHE_REDIRECT_TTL = datetime.timedelta(seconds=10)


@functions_framework.http
def kcidb_cache_redirect(request):
"""
Expand Down Expand Up @@ -567,7 +572,7 @@ def kcidb_cache_redirect(request):

# Check if the URL is in the cache
cache_client = get_cache_client()
cache = cache_client.map(url_to_fetch)
cache = cache_client.map(url_to_fetch, ttl=CACHE_REDIRECT_TTL)
if cache:
LOGGER.info("Redirecting to the cache at %r", cache)
# Redirect to the cached URL if it exists
Expand Down

0 comments on commit 843b1e0

Please sign in to comment.