Skip to content

Commit 071bf94

Browse files
committed
k8s-operator/api-proxy: add test for whoIs
Adds a test that checks whoIs gets the identity from the request's remote address, and returns an error instead of using the X-Forwarded-For header when the remote address is not a tailnet node. Updates tailscale/corp#51000 Signed-off-by: chaosinthecrd <[email protected]>
1 parent 85cbfee commit 071bf94

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

‎k8s-operator/api-proxy/proxy_test.go‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
package apiproxy
77

88
import (
9+
"encoding/json"
910
"net/http"
11+
"net/http/httptest"
1012
"net/netip"
1113
"reflect"
1214
"testing"
1315

1416
"github.com/google/go-cmp/cmp"
1517
"go.uber.org/zap"
18+
"tailscale.com/client/local"
1619
"tailscale.com/client/tailscale/apitype"
20+
"tailscale.com/net/memnet"
1721
"tailscale.com/tailcfg"
1822
"tailscale.com/tailcfg/peercap"
1923
"tailscale.com/util/must"
@@ -181,6 +185,94 @@ func Test_determineRecorderConfig(t *testing.T) {
181185
}
182186
}
183187

188+
// TestWhoIs checks that whoIs resolves the caller from the request's remote
189+
// address and ignores the X-Forwarded-For header.
190+
func TestWhoIs(t *testing.T) {
191+
zl, err := zap.NewDevelopment()
192+
if err != nil {
193+
t.Fatal(err)
194+
}
195+
196+
nodes := map[string]*apitype.WhoIsResponse{
197+
"100.64.0.1:1234": {
198+
Node: &tailcfg.Node{Name: "user-node.ts.net."},
199+
UserProfile: &tailcfg.UserProfile{LoginName: "[email protected]"},
200+
},
201+
"100.64.0.2": {
202+
Node: &tailcfg.Node{Name: "admin-node.ts.net."},
203+
UserProfile: &tailcfg.UserProfile{LoginName: "[email protected]"},
204+
},
205+
}
206+
lal := memnet.Listen("local-tailscaled.sock:80")
207+
defer lal.Close()
208+
localAPI := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
209+
who, ok := nodes[r.URL.Query().Get("addr")]
210+
if !ok {
211+
http.Error(w, "no match for IP:port", http.StatusNotFound)
212+
return
213+
}
214+
json.NewEncoder(w).Encode(who)
215+
})}
216+
defer localAPI.Close()
217+
go localAPI.Serve(lal)
218+
219+
ap := &APIServerProxy{
220+
log: zl.Sugar(),
221+
lc: &local.Client{Dial: lal.Dial},
222+
}
223+
224+
tests := []struct {
225+
name string
226+
remoteAddr string
227+
xForwardedFor string
228+
wantLogin string // empty if whoIs should fail
229+
}{
230+
{
231+
name: "tailnet_node",
232+
remoteAddr: "100.64.0.1:1234",
233+
wantLogin: "[email protected]",
234+
},
235+
{
236+
name: "not_a_tailnet_node",
237+
remoteAddr: "10.0.0.5:1234",
238+
},
239+
{
240+
name: "x_forwarded_for_ignored",
241+
remoteAddr: "10.0.0.5:1234",
242+
xForwardedFor: "100.64.0.2",
243+
},
244+
{
245+
name: "x_forwarded_for_ignored_for_tailnet_node",
246+
remoteAddr: "100.64.0.1:1234",
247+
xForwardedFor: "100.64.0.2",
248+
wantLogin: "[email protected]",
249+
},
250+
}
251+
for _, tt := range tests {
252+
t.Run(tt.name, func(t *testing.T) {
253+
r := httptest.NewRequest("GET", "/api/v1/namespaces/kube-system/secrets", nil)
254+
r.RemoteAddr = tt.remoteAddr
255+
if tt.xForwardedFor != "" {
256+
r.Header.Set("X-Forwarded-For", tt.xForwardedFor)
257+
}
258+
259+
who, err := ap.whoIs(r)
260+
if tt.wantLogin == "" {
261+
if err == nil {
262+
t.Fatalf("whoIs() = %q, want error", who.UserProfile.LoginName)
263+
}
264+
return
265+
}
266+
if err != nil {
267+
t.Fatalf("whoIs() unexpected error: %v", err)
268+
}
269+
if got := who.UserProfile.LoginName; got != tt.wantLogin {
270+
t.Errorf("whoIs() = %q, want %q", got, tt.wantLogin)
271+
}
272+
})
273+
}
274+
}
275+
184276
func whoResp(capMap map[string][]string) *apitype.WhoIsResponse {
185277
resp := &apitype.WhoIsResponse{
186278
CapMap: tailcfg.PeerCapMap{},

0 commit comments

Comments
 (0)