Skip to content

Commit 30d92e7

Browse files
committed
Add error handling and logging for OIDC token exchange in GitHub Actions workflow
1 parent 0dbd734 commit 30d92e7

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

.github/workflows/token.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,47 @@ jobs:
2323
runs-on: ubuntu-latest
2424
steps:
2525
- name: get OIDC token from GitHub Actions
26+
id: get_oidc_token
2627
run: |
2728
JWT=$(curl -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=${{ inputs.audience }}" | jq -r '.value')
2829
echo "::add-mask::$JWT" # Mask the JWT in the logs
2930
echo "jwt=$JWT" >> $GITHUB_OUTPUT
3031
- name: perform OIDC token exchange
3132
run: |
3233
# Perform the OIDC token exchange with Tailscale
33-
RESPONSE=$(curl -X POST https://api.tailscale.com/api/v2/oauth/token-exchange \
34+
echo "Exchanging OIDC token with Tailscale..."
35+
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" -X POST https://api.tailscale.com/api/v2/oauth/token-exchange \
3436
-H "Content-Type: application/x-www-form-urlencoded" \
3537
-d "client_id=${{ inputs.client_id}}" \
3638
-d "jwt=${{ steps.get_oidc_token.outputs.jwt }}")
37-
# make API request to demonstrate access token
38-
export ACCESS_TOKEN=$(echo $RESPONSE | jq -r '.access_token')
39+
40+
# Extract HTTP status and response body
41+
HTTP_STATUS=$(echo "$RESPONSE" | tail -n 1 | cut -d: -f2)
42+
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
43+
44+
echo "HTTP Status: $HTTP_STATUS"
45+
echo "Response body: $RESPONSE_BODY"
46+
47+
# Check if the request was successful
48+
if [ "$HTTP_STATUS" != "200" ]; then
49+
echo "Error: Token exchange failed with status $HTTP_STATUS"
50+
echo "Full response: $RESPONSE_BODY"
51+
exit 1
52+
fi
53+
54+
# Extract access token
55+
ACCESS_TOKEN=$(echo "$RESPONSE_BODY" | jq -r '.access_token')
56+
57+
if [ "$ACCESS_TOKEN" == "null" ] || [ -z "$ACCESS_TOKEN" ]; then
58+
echo "Error: No access token in response"
59+
echo "Full response: $RESPONSE_BODY"
60+
exit 1
61+
fi
62+
3963
echo "::add-mask::$ACCESS_TOKEN" # Mask the access token in the logs
40-
curl https://api.tailscale.com/api/v2/tailnet/${{ inputs.tailnet }}/devices \
64+
echo "Successfully obtained access token"
65+
66+
# Make API request to demonstrate access token
67+
echo "Testing API access..."
68+
curl -s https://api.tailscale.com/api/v2/tailnet/${{ inputs.tailnet }}/devices \
4169
--header "Authorization: Bearer ${ACCESS_TOKEN}"

0 commit comments

Comments
 (0)