Skip to content

Commit 772f265

Browse files
authored
Merge pull request #105 from udx/auth-gcp-improvement
Improve GCP Authentication with Application Default Credentials
2 parents 0e2ed2b + 9e92061 commit 772f265

File tree

6 files changed

+44
-36
lines changed

6 files changed

+44
-36
lines changed

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
jq -r '.packages[] | select(.versionInfo != null) | "\(.name) | \(.versionInfo)"' sbom.json | sort | uniq | head -n 20 | column -t -s '|'
101101
102102
- name: Upload SBOM Artifact
103-
uses: actions/upload-artifact@v4
103+
uses: actions/upload-artifact@v5
104104
with:
105105
name: sbom
106106
path: sbom.json

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ jobs:
115115
fi
116116
117117
- name: Upload SBOM Artifact
118-
uses: actions/upload-artifact@v4
118+
uses: actions/upload-artifact@v5
119119
with:
120120
name: sbom
121121
path: sbom.json
@@ -141,7 +141,7 @@ jobs:
141141
git config --global user.name "UDX Worker"
142142
143143
- name: Download SBOM Artifact
144-
uses: actions/download-artifact@v5
144+
uses: actions/download-artifact@v6
145145
with:
146146
name: sbom
147147

lib/auth.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ authenticate_actors() {
7979

8080
mapfile -t actors_array < "$actors_file"
8181
rm -f "$actors_file"
82+
83+
# Create local creds dir
84+
log_info "Pre-creating local creds dir"
85+
mkdir -p "$LOCAL_CREDS_DIR"
8286

8387
for actor in "${actors_array[@]}"; do
8488
local type provider creds auth_script auth_function

lib/auth/gcp.sh

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
# shellcheck source=${WORKER_LIB_DIR}/utils.sh disable=SC1091
44
source "${WORKER_LIB_DIR}/utils.sh"
55

6-
# Function to authenticate GCP service accounts
6+
# Function to set ADC credentials
77
#
88
# Example usage of the function
99
# gcp_authenticate "/path/to/your/gcp_creds.json"
10+
# gcp_authenticate "${GCP_CREDS}"
1011
#
1112

12-
# Function to authenticate GCP service accounts
13+
# Function to set ADC credentials
1314
gcp_authenticate() {
1415
local creds_json="$1"
1516

@@ -22,6 +23,12 @@ gcp_authenticate() {
2223
return 1
2324
fi
2425

26+
# If GOOGLE_APPLICATION_CREDENTIALS already set, do not override
27+
if [ -n "$GOOGLE_APPLICATION_CREDENTIALS" ]; then
28+
log_info "GCP Authentication" "GOOGLE_APPLICATION_CREDENTIALS already set, skipping authentication."
29+
return 0
30+
fi
31+
2532
# Extract necessary fields from the JSON credentials
2633
local clientEmail privateKey projectId
2734

@@ -34,44 +41,31 @@ gcp_authenticate() {
3441
return 1
3542
fi
3643

37-
# Adjust privateKey formatting
38-
# Replace "\\n" with actual new line, handle BEGIN and END markers
39-
privateKey=$(echo "$privateKey" | sed 's/\\n/\n/g' | sed 's/- /\n-/g' | sed 's/ -/-\n/g')
40-
41-
# Create a temporary credentials file for gcloud authentication
42-
local temp_creds_file="/tmp/gcp_creds.json"
43-
# Use jq to create a valid JSON with the modified privateKey
44-
jq -n --arg clientEmail "$clientEmail" --arg privateKey "$privateKey" --arg projectId "$projectId" \
45-
'{client_email: $clientEmail, private_key: $privateKey, project_id: $projectId}' > "$temp_creds_file"
46-
47-
# Set GOOGLE_APPLICATION_CREDENTIALS only if ACTORS_CLEANUP is disabled
48-
if [ "$ACTORS_CLEANUP" = false ]; then
49-
if [ -f "$GCP_CREDS" ]; then
50-
# If GCP_CREDS is a file path and exists, use it directly
51-
export GOOGLE_APPLICATION_CREDENTIALS="$GCP_CREDS"
52-
else
53-
# Otherwise create and use a local copy
54-
mkdir -p "$HOME/creds"
55-
cat "$creds_json" > "$HOME/creds/gcp_creds.json"
56-
export GOOGLE_APPLICATION_CREDENTIALS="$HOME/creds/gcp_creds.json"
57-
fi
44+
if [ -f "$GCP_CREDS" ]; then
45+
# If GCP_CREDS is a file path and exists, use it directly
46+
export GOOGLE_APPLICATION_CREDENTIALS="$GCP_CREDS"
47+
else
48+
49+
# Adjust privateKey formatting
50+
# Replace "\\n" with actual new line, handle BEGIN and END markers
51+
privateKey=$(echo "$privateKey" | sed 's/\\n/\n/g' | sed 's/- /\n-/g' | sed 's/ -/-\n/g')
52+
53+
jq -n --arg clientEmail "$clientEmail" --arg privateKey "$privateKey" --arg projectId "$projectId" \
54+
'{type: "service_account", client_email: $clientEmail, private_key: $privateKey, project_id: $projectId}' > "$LOCAL_CREDS_DIR/gcp_creds.json"
55+
56+
export GOOGLE_APPLICATION_CREDENTIALS="$LOCAL_CREDS_DIR/gcp_creds.json"
5857
fi
5958

60-
log_info "GCP Authentication" "Authenticating GCP service account..."
61-
if ! gcloud auth activate-service-account "$clientEmail" --key-file="$temp_creds_file" >/dev/null 2>&1; then
62-
log_error "GCP Authentication" "GCP service account authentication failed."
63-
rm -f "$temp_creds_file"
64-
return 1
59+
# If GOOGLE_APPLICATION_CREDENTIALS is set, authorize environment with provided credentials
60+
if [ -n "$GOOGLE_APPLICATION_CREDENTIALS" ]; then
61+
log_info "GCP Authentication" "Authorizing environment with provided credentials."
62+
gcloud auth login --cred-file="$GOOGLE_APPLICATION_CREDENTIALS" > /dev/null 2>&1
6563
fi
6664

6765
if ! gcloud config set project "$projectId" >/dev/null 2>&1; then
6866
log_error "GCP Authentication" "Failed to set GCP project."
69-
rm -f "$temp_creds_file"
7067
return 1
7168
fi
7269

7370
log_success "GCP Authentication" "GCP service account authenticated and project set."
74-
75-
# Clean up temporary credentials file
76-
rm -f "$temp_creds_file"
7771
}

lib/cleanup.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ cleanup_actors() {
122122
fi
123123
;;
124124
gcp)
125-
if cleanup_provider "gcloud" "gcloud auth revoke --all" "gcloud auth list" "GCP"; then
125+
if cleanup_provider "gcloud" "gcloud auth revoke --all && unset GOOGLE_APPLICATION_CREDENTIALS" "gcloud auth list" "GCP"; then
126126
any_cleanup=true
127127
fi
128128
;;
@@ -146,6 +146,12 @@ cleanup_actors() {
146146
if [[ "$any_cleanup" == false ]]; then
147147
log_info "No active sessions found for any configured providers."
148148
fi
149+
150+
# Remove local copy creds dir
151+
if [ -d "$LOCAL_CREDS_DIR" ]; then
152+
log_info "Removing local copy creds dir"
153+
rm -rf "$LOCAL_CREDS_DIR"
154+
fi
149155

150156
# Clear the configured providers array
151157
configured_providers=()

lib/environment.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ configure_environment() {
3636
export ACTORS_CLEANUP=true
3737
fi
3838

39+
if [[ -z "${LOCAL_CREDS_DIR:-}" ]]; then
40+
export LOCAL_CREDS_DIR="$HOME/.config/worker/creds"
41+
fi
42+
3943
# Extract and authenticate actors
4044
local actors
4145
actors=$(get_config_section "$resolved_config" "actors")

0 commit comments

Comments
 (0)