Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"reflect"
"strings"
"sync"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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)
Expand Down
67 changes: 67 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}