Skip to content

Commit dd9d79d

Browse files
committed
add tailscaled-auth-setup script for post-creation authentication
1 parent 4430789 commit dd9d79d

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

src/tailscale/devcontainer-feature.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"documentationURL": "https://tailscale.com/kb/1160/github-codespaces/",
77
"licenseURL": "https://github.com/tailscale/codespace/blob/main/LICENSE",
88
"entrypoint": "/usr/local/sbin/tailscaled-entrypoint",
9+
"postCreateCommand": "/usr/local/sbin/tailscaled-auth-setup",
910
"capAdd": ["NET_ADMIN", "NET_RAW", "MKNOD"],
1011
"options": {
1112
"version": {

src/tailscale/install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ install -D -m 755 "$scratch_dir/tailscale" /usr/local/bin/tailscale
6767
install -D -m 755 "$scratch_dir/tailscaled" /usr/local/sbin/tailscaled
6868
install -D -m 755 "$script_dir/tailscaled-entrypoint.sh" /usr/local/sbin/tailscaled-entrypoint
6969
install -D -m 755 "$script_dir/tailscaled-devcontainer-start.sh" /usr/local/sbin/tailscaled-devcontainer-start
70+
install -D -m 755 "$script_dir/tailscaled-auth-setup.sh" /usr/local/sbin/tailscaled-auth-setup
7071

7172
mkdir -p /var/lib/tailscale /var/run/tailscale /var/log
7273
touch /var/log/tailscaled.log
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2025 Tailscale Inc & AUTHORS All rights reserved.
3+
# Use of this source code is governed by a BSD-style
4+
# license that can be found in the LICENSE file.
5+
6+
# This script handles Tailscale authentication during postCreateCommand
7+
# when GitHub Codespaces secrets are guaranteed to be available
8+
9+
if [[ $(id -u) -ne 0 ]]; then
10+
if ! command -v sudo > /dev/null; then
11+
>&2 echo "tailscale auth setup could not run as root."
12+
exit 1
13+
fi
14+
exec sudo --non-interactive -E "$0" "$@"
15+
fi
16+
17+
# Move the auth key to a non-exported variable so it is not leaking into child
18+
# process environments.
19+
auth_key="$TS_AUTH_KEY"
20+
unset TS_AUTH_KEY
21+
22+
TAILSCALED_SOCK=/var/run/tailscale/tailscaled.sock
23+
24+
# Wait for tailscaled to be ready (it should be running from entrypoint)
25+
count=100
26+
while ((count--)); do
27+
[[ -S $TAILSCALED_SOCK ]] && break
28+
sleep 0.1
29+
30+
if ((count == 0)); then
31+
>&2 echo "ERROR: tailscaled socket not found. Is tailscaled running?"
32+
exit 1
33+
fi
34+
done
35+
36+
# Check if already authenticated
37+
if /usr/local/bin/tailscale status --json >/dev/null 2>&1; then
38+
# Already authenticated, check if it's working
39+
if /usr/local/bin/tailscale status --json | grep -q '"BackendState":"Running"'; then
40+
echo "Tailscale is already running and authenticated"
41+
exit 0
42+
fi
43+
fi
44+
45+
# Authenticate with auth key if available
46+
if [[ -n "$auth_key" ]]; then
47+
if [[ "$auth_key" == "test-auth-key" ]]; then
48+
# Special test case
49+
touch /tmp/test-auth-key-seen
50+
echo "Test auth key detected"
51+
else
52+
echo "Authenticating Tailscale with auth key..."
53+
hostnamearg=""
54+
if [[ -n "${CODESPACE_NAME}" ]]; then
55+
hostnamearg="--hostname=${CODESPACE_NAME}"
56+
fi
57+
58+
if /usr/local/bin/tailscale up --accept-routes --authkey="$auth_key" $hostnamearg; then
59+
echo "Tailscale authentication successful"
60+
else
61+
>&2 echo "ERROR: Tailscale authentication failed"
62+
exit 1
63+
fi
64+
fi
65+
else
66+
echo "Tailscale is running. To authenticate, run: sudo tailscale up --accept-routes"
67+
fi

src/tailscale/tailscaled-devcontainer-start.sh

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,3 @@ if [[ -n "$TAILSCALED_PID" ]]; then
6363
fi
6464
done
6565
fi
66-
67-
if [[ -n "$auth_key" ]]; then
68-
if [[ "$auth_key" == "test-auth-key" ]]; then
69-
touch /tmp/test-auth-key-seen
70-
else
71-
hostnamearg=""
72-
if [[ -n "${CODESPACE_NAME}" ]]; then
73-
hostnamearg="--hostname=${CODESPACE_NAME}"
74-
fi
75-
/usr/local/bin/tailscale up --accept-routes --authkey="$auth_key" $hostnamearg
76-
fi
77-
fi

test/tailscale/tailscale_auth_key.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)