Skip to content

Commit 9eeac15

Browse files
committed
Fetch authkey in two phases
Initially see if the provisioned server or global key exist in the app usage pool. If not then fallback to the already existing environment variables Signed-off-by: Connor Kelly <connor.r.kelly@gmail.com>
1 parent 8972cd8 commit 9eeac15

3 files changed

Lines changed: 87 additions & 6 deletions

File tree

caddyfile.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ func init() {
1010
httpcaddyfile.RegisterGlobalOption("tailscale", parseApp)
1111
}
1212

13+
const (
14+
authUsageKey = "auth_key"
15+
ephemeralKey = "ephemeral"
16+
)
17+
1318
func parseApp(d *caddyfile.Dispenser, _ any) (any, error) {
1419
app := &TSApp{
1520
Servers: make(map[string]TSServer),

module.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ func getServer(_, addr string) (*tsnetServerDestructor, error) {
106106
}
107107

108108
if host != "" {
109-
// Set authkey to "TS_AUTHKEY_<HOST>". If empty,
110-
// fall back to "TS_AUTHKEY".
111-
s.AuthKey = os.Getenv("TS_AUTHKEY_" + strings.ToUpper(host))
112-
if s.AuthKey == "" {
113-
s.AuthKey = os.Getenv("TS_AUTHKEY")
114-
}
109+
s.AuthKey = getAuthKey(host)
110+
log.Println("auth_key", s.AuthKey)
115111

116112
// Set config directory for tsnet. By default, tsnet will use the name of the
117113
// running program, but we include the hostname as well so that a single
@@ -137,6 +133,21 @@ func getServer(_, addr string) (*tsnetServerDestructor, error) {
137133
return s.(*tsnetServerDestructor), nil
138134
}
139135

136+
func getAuthKey(host string) string {
137+
storedAuthKey, loaded := app.LoadOrStore(authUsageKey, "")
138+
if loaded {
139+
return storedAuthKey.(string)
140+
}
141+
142+
// Set authkey to "TS_AUTHKEY_<HOST>". If empty,
143+
// fall back to "TS_AUTHKEY".
144+
authKey := os.Getenv("TS_AUTHKEY_" + strings.ToUpper(host))
145+
if authKey == "" {
146+
authKey = os.Getenv("TS_AUTHKEY")
147+
}
148+
return authKey
149+
}
150+
140151
type TailscaleAuth struct {
141152
localclient *tailscale.LocalClient
142153
}

module_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package tscaddy
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
9+
"github.com/caddyserver/caddy/v2"
10+
)
11+
12+
func Test_GetAuthKey(t *testing.T) {
13+
14+
const testkey = "abcdefghijklmnopqrstuvwxyz"
15+
const testHostKey = "1234567890"
16+
const testenvkey = "zyxwvutsrqponmlkjihgfedca"
17+
const testHost = "unittest"
18+
19+
tests := map[string]struct {
20+
host string
21+
skipApp bool
22+
want string
23+
}{
24+
"default key from module": {
25+
want: testkey,
26+
host: "testhost",
27+
},
28+
"default key from environment": {
29+
want: testenvkey,
30+
skipApp: true,
31+
host: "testhost",
32+
},
33+
"host key from module": {
34+
want: testHostKey,
35+
host: testHost,
36+
},
37+
"host key from environment": {
38+
want: testHostKey,
39+
host: testHost,
40+
},
41+
}
42+
for name, tt := range tests {
43+
t.Run(name, func(t *testing.T) {
44+
app = caddy.NewUsagePool()
45+
if !tt.skipApp {
46+
app.LoadOrStore(authUsageKey, testkey)
47+
app.LoadOrStore(testHost, TSServer{
48+
AuthKey: testHostKey,
49+
})
50+
}
51+
os.Setenv("TS_AUTHKEY", testenvkey)
52+
hostKey := fmt.Sprintf("TS_AUTHKEY_%s", strings.ToUpper(testHost))
53+
os.Setenv(hostKey, testHostKey)
54+
t.Cleanup(func() {
55+
os.Unsetenv("TS_AUTHKEY")
56+
os.Unsetenv(hostKey)
57+
})
58+
59+
got := getAuthKey(tt.host)
60+
if got != tt.want {
61+
t.Errorf("GetAuthKey() = %v, want %v", got, tt.want)
62+
}
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)