Skip to content

Commit 63f5e29

Browse files
committed
Add automatic tailnet detection for OIDC workload federation
- Try using '-' as tailnet which represents the current/default tailnet for the token - Fall back to extracting tailnet from client_id if that fails - This should work with OIDC tokens that are scoped to a specific tailnet - Add dedicated step to determine the correct tailnet before making API calls
1 parent 8ad3159 commit 63f5e29

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

.github/workflows/delete-inactive-tailnet-nodes.yml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,36 @@ jobs:
5656
echo "access_token=$ACCESS_TOKEN" >> $GITHUB_OUTPUT
5757
echo "Successfully obtained access token"
5858
59+
- name: Determine tailnet from token
60+
id: get_tailnet
61+
run: |
62+
# Try to get tailnet info using the token
63+
echo "Determining tailnet associated with OIDC token..."
64+
65+
# First, try to list tailnets or get current tailnet info
66+
# The OIDC token should be scoped to a specific tailnet
67+
# Try using "-" as tailnet which means "current tailnet"
68+
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
69+
https://api.tailscale.com/api/v2/tailnet/-/devices \
70+
--header "Authorization: Bearer ${{ steps.get_access_token.outputs.access_token }}")
71+
72+
HTTP_STATUS=$(echo "$RESPONSE" | tail -n 1 | cut -d: -f2)
73+
74+
if [ "$HTTP_STATUS" = "200" ]; then
75+
echo "Using current tailnet (-)..."
76+
echo "tailnet=-" >> $GITHUB_OUTPUT
77+
else
78+
# Fall back to extracting from client_id
79+
TAILNET=$(echo "${{ inputs.client_id }}" | cut -d'/' -f2)
80+
echo "Using tailnet from client_id: $TAILNET"
81+
echo "tailnet=$TAILNET" >> $GITHUB_OUTPUT
82+
fi
83+
5984
- name: Get all devices in tailnet
6085
id: get_devices
6186
run: |
62-
# Extract tailnet from the client_id (format: clientid/tailnet)
63-
TAILNET=$(echo "${{ inputs.client_id }}" | cut -d'/' -f2)
87+
TAILNET="${{ steps.get_tailnet.outputs.tailnet }}"
6488
echo "Fetching devices from tailnet: $TAILNET"
65-
echo "tailnet=$TAILNET" >> $GITHUB_OUTPUT
6689
6790
# Make API call with proper error handling
6891
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
@@ -243,5 +266,5 @@ jobs:
243266
if: inputs.dry_run == false && steps.filter_devices.outputs.devices_count != '0'
244267
run: |
245268
echo "Fetching updated device list..."
246-
TAILNET=$(echo "${{ inputs.client_id }}" | cut -d'/' -f2)
269+
TAILNET="${{ steps.get_tailnet.outputs.tailnet }}"
247270
./tailscale/scripts/list-devices.sh "${{ steps.get_access_token.outputs.access_token }}" "$TAILNET" table || true

0 commit comments

Comments
 (0)