|
6 | 6 | package apiproxy |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "encoding/json" |
9 | 10 | "net/http" |
| 11 | + "net/http/httptest" |
10 | 12 | "net/netip" |
11 | 13 | "reflect" |
12 | 14 | "testing" |
13 | 15 |
|
14 | 16 | "github.com/google/go-cmp/cmp" |
15 | 17 | "go.uber.org/zap" |
| 18 | + "tailscale.com/client/local" |
16 | 19 | "tailscale.com/client/tailscale/apitype" |
| 20 | + "tailscale.com/net/memnet" |
17 | 21 | "tailscale.com/tailcfg" |
18 | 22 | "tailscale.com/tailcfg/peercap" |
19 | 23 | "tailscale.com/util/must" |
@@ -181,6 +185,94 @@ func Test_determineRecorderConfig(t *testing.T) { |
181 | 185 | } |
182 | 186 | } |
183 | 187 |
|
| 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 | + |
| 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 | + |
| 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 | + |
184 | 276 | func whoResp(capMap map[string][]string) *apitype.WhoIsResponse { |
185 | 277 | resp := &apitype.WhoIsResponse{ |
186 | 278 | CapMap: tailcfg.PeerCapMap{}, |
|
0 commit comments