Skip to content

Commit e0e21db

Browse files
author
Yu Hu
committed
docker auth for gcp artifact registry. be a little smarter about detect the region
1 parent ee00d0e commit e0e21db

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
# Script to extract Artifact Registry regions from image URLs in files and authenticate with docker
3+
# Usage: ./docker-auth.sh [path] [default-regions]
4+
# path: optional subdirectory under /home/core/devcontainer
5+
# default-regions: comma-separated list of regions to always include (defaults to us-central1)
6+
7+
set -o errexit
8+
set -o nounset
9+
set -o pipefail
10+
set -o xtrace
11+
12+
# Use /home/core/devcontainer as prefix for the input
13+
DEVCONTAINER_PATH="/home/core/devcontainer${1:+/$1}"
14+
15+
# Default regions to always include (comma-separated)
16+
DEFAULT_REGIONS="${2:-us-central1}"
17+
18+
# Check if path exists and search for registries
19+
REGISTRIES=""
20+
if [ ! -d "$DEVCONTAINER_PATH" ]; then
21+
echo "Warning: Directory $DEVCONTAINER_PATH does not exist, skipping file search" >&2
22+
else
23+
echo "Searching for Artifact Registry URLs in: $DEVCONTAINER_PATH" >&2
24+
25+
# Find all image URLs matching *-docker.pkg.dev pattern
26+
# Extract unique registry hostnames and then get locations
27+
REGISTRIES=$(grep -r -h -o -E '[a-z0-9-]+-docker\.pkg\.dev/[^[:space:]"'\'']*' "$DEVCONTAINER_PATH" 2>/dev/null | \
28+
cut -d'/' -f1 | \
29+
sort -u)
30+
31+
if [ -n "$REGISTRIES" ]; then
32+
echo "Found registries:" >&2
33+
echo "$REGISTRIES" >&2
34+
else
35+
echo "No Artifact Registry URLs found in $DEVCONTAINER_PATH" >&2
36+
fi
37+
fi
38+
39+
# Extract locations from registries (remove -docker.pkg.dev suffix)
40+
if [ -n "$REGISTRIES" ]; then
41+
LOCATIONS=$(echo "$REGISTRIES" | sed 's/-docker\.pkg\.dev$//' | sort -u)
42+
else
43+
LOCATIONS=""
44+
fi
45+
46+
# Append default regions (convert comma-separated to newline-separated)
47+
DEFAULT_REGIONS_NEWLINE=$(echo "$DEFAULT_REGIONS" | tr ',' '\n')
48+
LOCATIONS=$(echo -e "${LOCATIONS}\n${DEFAULT_REGIONS_NEWLINE}" | sort -u)
49+
50+
# Get access token from metadata server
51+
echo "Getting access token..." >&2
52+
53+
# Temporarily disable xtrace to avoid logging sensitive credentials
54+
set +o xtrace
55+
56+
TOKEN=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" -H "Metadata-Flavor: Google")
57+
ACCESS=$(echo ${TOKEN} | grep -oP '(?<="access_token":")[^"]*')
58+
59+
# Login to each registry
60+
echo "Logging into artifact registries..." >&2
61+
echo "$LOCATIONS" | while read -r location; do
62+
[ -z "$location" ] && continue
63+
echo " Logging into: ${location}-docker.pkg.dev" >&2
64+
docker login -u oauth2accesstoken -p "${ACCESS}" "https://${location}-docker.pkg.dev" > /dev/null 2>&1
65+
done
66+
67+
# Re-enable xtrace
68+
set -o xtrace
69+
70+
echo "Done!" >&2

0 commit comments

Comments
 (0)