Skip to content

Commit 5c6bd1e

Browse files
authored
Merge pull request #79 from Dstack-TEE/phala-cloud-prelaunch-script-v0.0.13
feat(prelaunch): v0.0.13 - registry-aware login & GHCR pull verification
2 parents 4583be4 + 6fa4bb2 commit 5c6bd1e

1 file changed

Lines changed: 54 additions & 13 deletions

File tree

phala-cloud-prelaunch-script/prelaunch.sh

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#!/bin/bash
21
echo "----------------------------------------------"
3-
echo "Running Phala Cloud Pre-Launch Script v0.0.12"
2+
echo "Running Phala Cloud Pre-Launch Script v0.0.13"
43
echo "----------------------------------------------"
54
set -e
65

@@ -33,7 +32,19 @@ perform_cleanup() {
3332

3433
# Function: Check Docker login status without exposing credentials
3534
check_docker_login() {
36-
# Try to verify login status without exposing credentials
35+
local registry="$1"
36+
37+
# When registry is specified, check auth entry for that registry in Docker config
38+
if [[ -n "$registry" ]]; then
39+
local docker_config_path="${DOCKER_CONFIG:-$HOME/.docker}/config.json"
40+
if [[ -f "$docker_config_path" ]] && grep -q "$registry" "$docker_config_path"; then
41+
return 0
42+
else
43+
return 1
44+
fi
45+
fi
46+
47+
# Fallback check when no explicit registry is provided
3748
if docker info 2>/dev/null | grep -q "Username"; then
3849
return 0
3950
else
@@ -47,31 +58,33 @@ echo "Starting login process..."
4758
# Check if Docker credentials exist
4859
if [[ -n "$DSTACK_DOCKER_USERNAME" && -n "$DSTACK_DOCKER_PASSWORD" ]]; then
4960
echo "Docker credentials found"
50-
61+
DOCKER_REGISTRY_TARGET="${DSTACK_DOCKER_REGISTRY:-docker.io}"
62+
echo "Target Docker registry: $DOCKER_REGISTRY_TARGET"
63+
5164
# Check if already logged in
52-
if check_docker_login; then
53-
echo "Already logged in to Docker registry"
65+
if check_docker_login "$DSTACK_DOCKER_REGISTRY"; then
66+
echo "Already logged in to Docker registry: $DOCKER_REGISTRY_TARGET"
5467
else
55-
echo "Logging in to Docker registry..."
68+
echo "Logging in to Docker registry: $DOCKER_REGISTRY_TARGET"
5669
# Login without exposing password in process list
5770
if [[ -n "$DSTACK_DOCKER_REGISTRY" ]]; then
5871
echo "$DSTACK_DOCKER_PASSWORD" | docker login -u "$DSTACK_DOCKER_USERNAME" --password-stdin "$DSTACK_DOCKER_REGISTRY"
5972
else
6073
echo "$DSTACK_DOCKER_PASSWORD" | docker login -u "$DSTACK_DOCKER_USERNAME" --password-stdin
6174
fi
62-
75+
6376
if [ $? -eq 0 ]; then
64-
echo "Docker login successful"
77+
echo "Docker login successful: $DOCKER_REGISTRY_TARGET"
6578
else
66-
echo "Docker login failed"
79+
echo "Docker login failed: $DOCKER_REGISTRY_TARGET"
6780
notify_host_hoot_error "docker login failed"
6881
exit 1
6982
fi
7083
fi
7184
# Check if AWS ECR credentials exist
7285
elif [[ -n "$DSTACK_AWS_ACCESS_KEY_ID" && -n "$DSTACK_AWS_SECRET_ACCESS_KEY" && -n "$DSTACK_AWS_REGION" && -n "$DSTACK_AWS_ECR_REGISTRY" ]]; then
7386
echo "AWS ECR credentials found"
74-
87+
7588
# Check if AWS CLI is installed
7689
if [ ! -f "./aws/dist/aws" ]; then
7790
notify_host_hoot_info "awscli not installed, installing..."
@@ -92,13 +105,13 @@ elif [[ -n "$DSTACK_AWS_ACCESS_KEY_ID" && -n "$DSTACK_AWS_SECRET_ACCESS_KEY" &&
92105
export AWS_ACCESS_KEY_ID="$DSTACK_AWS_ACCESS_KEY_ID"
93106
export AWS_SECRET_ACCESS_KEY="$DSTACK_AWS_SECRET_ACCESS_KEY"
94107
export AWS_DEFAULT_REGION="$DSTACK_AWS_REGION"
95-
108+
96109
# Set session token if provided (for temporary credentials)
97110
if [[ -n "$DSTACK_AWS_SESSION_TOKEN" ]]; then
98111
echo "AWS session token found, using temporary credentials"
99112
export AWS_SESSION_TOKEN="$DSTACK_AWS_SESSION_TOKEN"
100113
fi
101-
114+
102115
# Test AWS credentials before attempting ECR login
103116
echo "Testing AWS credentials..."
104117
if ! ./aws/dist/aws sts get-caller-identity &> /dev/null; then
@@ -135,6 +148,34 @@ fi
135148

136149
perform_cleanup
137150

151+
#
152+
# GHCR image pull access verification (pure HTTP, no docker daemon)
153+
#
154+
if [[ "$DOCKER_REGISTRY_TARGET" == "ghcr.io" && -n "$DSTACK_DOCKER_USERNAME" && -n "$DSTACK_DOCKER_PASSWORD" ]]; then
155+
COMPOSE_IMAGES=$(grep 'image:' /dstack/docker-compose.yaml 2>/dev/null | awk '{print $2}' | tr -d '"'"'" || true)
156+
for img in $COMPOSE_IMAGES; do
157+
[[ "$img" != ghcr.io/* ]] && continue
158+
repo="${img#ghcr.io/}"; repo="${repo%%:*}"
159+
tag="${img##*:}"; [[ "$tag" == "$img" || "$tag" == "$repo" ]] && tag="latest"
160+
echo "Verifying GHCR pull access: $img"
161+
token=$(curl -sf -u "$DSTACK_DOCKER_USERNAME:$DSTACK_DOCKER_PASSWORD" \
162+
"https://ghcr.io/token?service=ghcr.io&scope=repository:${repo}:pull" | jq -r '.token // empty' || true)
163+
if [[ -z "$token" ]]; then
164+
echo "ERROR: GHCR token exchange failed for $img"
165+
notify_host_hoot_error "GHCR token exchange failed: $img"
166+
exit 1
167+
fi
168+
http_code=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $token" \
169+
"https://ghcr.io/v2/${repo}/manifests/${tag}")
170+
if [[ "$http_code" != "200" ]]; then
171+
echo "ERROR: GHCR pull access denied for $img (HTTP $http_code)"
172+
notify_host_hoot_error "GHCR pull access denied: $img (HTTP $http_code)"
173+
exit 1
174+
fi
175+
echo "GHCR pull access OK: $img"
176+
done
177+
fi
178+
138179
#
139180
# Set root password.
140181
#

0 commit comments

Comments
 (0)