From 7bad04a57d17bd7a06f61e328d9c0af2f953f9b3 Mon Sep 17 00:00:00 2001 From: Caio Rondon Date: Thu, 24 Sep 2026 22:01:03 -0300 Subject: [PATCH] Reuse the Tailscale local client across authentication requests Signed-off-by: Caio Rondon --- auth.go | 11 +++++---- auth_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 auth_test.go diff --git a/auth.go b/auth.go index c84ac09..ee12729 100644 --- a/auth.go +++ b/auth.go @@ -11,6 +11,7 @@ import ( "net/http" "reflect" "strings" + "sync" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig" @@ -33,6 +34,7 @@ func init() { // Otherwise, it will attempt to find and use the local tailscaled daemon running on the system. type Auth struct { localclient *local.Client + clientMu sync.Mutex } func (Auth) CaddyModule() caddy.ModuleInfo { @@ -94,12 +96,13 @@ type wrappedListener interface { // If the LocalClient has not already been configured, the provided request will be used to // lookup the tailscale node that serviced the request, and get the associated LocalClient. func (ta *Auth) client(r *http.Request) (*local.Client, error) { + ta.clientMu.Lock() + defer ta.clientMu.Unlock() if ta.localclient != nil { return ta.localclient, nil } - // if request was made through a tsnet listener, set up the client for the associated tsnet - // server. + // If the request was made through a tsnet listener, use its local client. server := r.Context().Value(caddyhttp.ServerCtxKey).(*caddyhttp.Server) for _, listener := range server.Listeners() { if tsl, ok := findTsnetListener(listener); ok { @@ -112,7 +115,7 @@ func (ta *Auth) client(r *http.Request) (*local.Client, error) { } if ta.localclient == nil { - // default to empty client that will talk to local tailscaled + // Default to a client that talks to the local tailscaled. ta.localclient = new(local.Client) } @@ -132,7 +135,7 @@ type tsnetListener interface { // - tailscale_name: the user's display name // - tailscale_profile_picture: the user's profile picture URL // - tailscale_tailnet: the user's tailnet name (if the user is not connecting to a shared node) -func (ta Auth) Authenticate(w http.ResponseWriter, r *http.Request) (caddyauth.User, bool, error) { +func (ta *Auth) Authenticate(w http.ResponseWriter, r *http.Request) (caddyauth.User, bool, error) { user := caddyauth.User{} client, err := ta.client(r) diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 0000000..716fdfb --- /dev/null +++ b/auth_test.go @@ -0,0 +1,67 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: Apache-2.0 + +package tscaddy + +import ( + "context" + "net/http" + "net/http/httptest" + "sync" + "testing" + + "github.com/caddyserver/caddy/v2/modules/caddyhttp" + "tailscale.com/client/local" +) + +func authTestRequest() *http.Request { + request := httptest.NewRequest("GET", "http://example.com/", nil) + request.RemoteAddr = "100.64.0.1:1234" + return request.WithContext(context.WithValue(request.Context(), caddyhttp.ServerCtxKey, &caddyhttp.Server{})) +} + +func TestAuthenticateReusesLocalClient(t *testing.T) { + auth := new(Auth) + request := authTestRequest() + ctx, cancel := context.WithCancel(request.Context()) + cancel() + request = request.WithContext(ctx) + + // A canceled WhoIs call must still leave the initialized client available. + _, _, _ = auth.Authenticate(httptest.NewRecorder(), request) + first := auth.localclient + if first == nil { + t.Fatal("Authenticate did not retain its local client") + } + + _, _, _ = auth.Authenticate(httptest.NewRecorder(), request) + if auth.localclient != first { + t.Fatal("Authenticate replaced its local client") + } +} + +func TestAuthClientConcurrent(t *testing.T) { + auth := new(Auth) + request := authTestRequest() + const count = 32 + clients := make([]*local.Client, count) + errs := make([]error, count) + var wg sync.WaitGroup + for i := range clients { + wg.Add(1) + go func() { + defer wg.Done() + clients[i], errs[i] = auth.client(request) + }() + } + wg.Wait() + + for i, client := range clients { + if errs[i] != nil { + t.Fatalf("client %d: %v", i, errs[i]) + } + if client != clients[0] { + t.Fatalf("client %d differs from the first client", i) + } + } +}