Skip to content

Commit 2128f18

Browse files
committed
tailscale: add support for auth key secrets
The secrets support here is intended for codespaces where the TS_AUTH_KEY environment variable is protected by github secrets. Setting an auth key via the environment in other devcontainer runtimes may pose additonal security risks associated with secrets in environment variables.
1 parent 30bd8d7 commit 2128f18

File tree

6 files changed

+84
-12
lines changed

6 files changed

+84
-12
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,29 @@ to your `devcontainer.json`:
1616
}
1717
```
1818

19-
Then launch your Codespace. After it starts up, run [`tailscale up`](https://tailscale.com/kb/1080/cli/#up):
19+
## Starting Tailscale
20+
21+
The Tailscale daemon starts automatically as part of the devcontainer entrypoint.
22+
23+
### Manual Log in
2024

2125
```shell
2226
sudo tailscale up --accept-routes
2327
```
2428

25-
You'll only need to run `tailscale up` once per Codespace.
26-
The Tailscale state will be saved between rebuilds.
29+
More info: [`tailscale up`](https://tailscale.com/kb/1080/cli/#up)
30+
31+
### Automatic login
32+
33+
Create an [auth key](https://tailscale.com/kb/1085/auth-keys) in the Tailscale
34+
[admin panel](https://login.tailscale.com/admin/settings/keys).
35+
36+
Create a codespace secret called `TS_AUTH_KEY` in your
37+
[codespaces configuration](https://github.com/settings/codespaces) containing
38+
the auth key you made above.
39+
40+
Now whenever you launch a devcontainer with access to this secret, it will
41+
automatically perform a `tailscale up --accept-routes --auth-key=$TS_AUTH_KEY`.
2742

2843
## Details
2944

src/tailscale/tailscaled-devcontainer-start.sh

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ if [[ $(id -u) -ne 0 ]]; then
1111
exec sudo --non-interactive -E "$0" "$@"
1212
fi
1313

14+
# Move the auth key to a non-exported variable so it is not leaking into child
15+
# process environments.
16+
auth_key="$TS_AUTH_KEY"
17+
unset TS_AUTH_KEY
18+
1419
if [[ ! -c /dev/net/tun ]]; then
1520
mkdir -p /dev/net
1621
mknod /dev/net/tun c 10 200
@@ -31,11 +36,18 @@ TAILSCALED_PID=""
3136
TAILSCALED_SOCK=/var/run/tailscale/tailscaled.sock
3237
TAILSCALED_LOG=/var/log/tailscaled.log
3338

34-
# Note: TS_DEBUG_FIREWALL_MODE: it is not recommended that users copy this
35-
# setting into other environments, the feature is in test and will be formally
36-
# released in the future, debug flags may later be recycled for other purposes
37-
# leading to unexpected behavior.
38-
>$TAILSCALED_LOG 2>&1 TS_DEBUG_FIREWALL_MODE=auto /usr/local/sbin/tailscaled &
39+
(
40+
exec 1>$TAILSCALED_LOG 2>&1
41+
cd /
42+
umask 0
43+
# Note: TS_DEBUG_FIREWALL_MODE: it is not recommended that users copy this
44+
# setting into other environments, the feature is in test and will be formally
45+
# released in the future, debug flags may later be recycled for other purposes
46+
# leading to unexpected behavior.
47+
unset TAILSCALED_PID TAILSCALED_SOCK TAILSCALED_LOG
48+
export TS_DEBUG_FIREWALL_MODE=auto
49+
exec setsid /usr/local/sbin/tailscaled
50+
) &
3951
TAILSCALED_PID=$!
4052

4153
if [[ -n "$TAILSCALED_PID" ]]; then
@@ -50,4 +62,16 @@ if [[ -n "$TAILSCALED_PID" ]]; then
5062
break
5163
fi
5264
done
65+
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/sbin/tailscale up --accept-routes --authkey="$auth_key" $hostnamearg
76+
fi
5377
fi

src/tailscale/tailscaled-entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55

66
/usr/local/sbin/tailscaled-devcontainer-start
77

8+
unset TS_AUTH_KEY
9+
810
exec "$@"

test/tailscale/scenarios.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@
1818
"version": "1.80.2"
1919
}
2020
}
21+
},
22+
"tailscale_auth_key": {
23+
"image": "ubuntu:latest",
24+
"containerEnv": {
25+
"TS_AUTH_KEY": "test-auth-key"
26+
},
27+
"features": {
28+
"tailscale": {}
29+
}
2130
}
2231
}

test/tailscale/tailscale_auth_key.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
set -e
7+
8+
source dev-container-features-test-lib
9+
10+
# Wait for the auth key to be seen by the start script.
11+
count=100
12+
while ((count--)); do
13+
[[ -f /tmp/test-auth-key-seen ]] && break
14+
sleep 0.1
15+
done
16+
17+
check "/tmp/test-auth-key-seen" ls /tmp/test-auth-key-seen
18+
19+
# It would be nice to directly test that the entrypoint is doing unset
20+
# TS_AUTH_KEY, however that isn't visible to the test execution.
21+
22+
reportResults

test/tailscale/test.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ set -e
77

88
source dev-container-features-test-lib
99

10-
if [[ "$VERSION" == latest ]]; then
11-
check "Daemon: " tailscale version --daemon
12-
else
13-
check "$VERSION" tailscale version --daemon
10+
check "daemon is running" tailscale version --daemon
11+
12+
if [[ -n "$VERSION" ]]; then
13+
check "version is correct" bash -c "tailscale version --daemon | grep -q $VERSION"
1414
fi
1515

1616
reportResults

0 commit comments

Comments
 (0)