2023-08-16 20:20:55 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2023-08-24 22:02:42 +00:00
|
|
|
//go:build !plan9
|
|
|
|
|
2023-08-16 20:20:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-07-08 20:18:55 +00:00
|
|
|
"net/netip"
|
|
|
|
"reflect"
|
2023-08-16 20:20:55 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-11-20 15:41:18 +00:00
|
|
|
"go.uber.org/zap"
|
2023-08-16 20:20:55 +00:00
|
|
|
"tailscale.com/client/tailscale/apitype"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/util/must"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestImpersonationHeaders(t *testing.T) {
|
2023-11-20 15:41:18 +00:00
|
|
|
zl, err := zap.NewDevelopment()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-08-16 20:20:55 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
emailish string
|
|
|
|
tags []string
|
|
|
|
capMap tailcfg.PeerCapMap
|
|
|
|
|
|
|
|
wantHeaders http.Header
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "user",
|
|
|
|
emailish: "foo@example.com",
|
|
|
|
wantHeaders: http.Header{
|
|
|
|
"Impersonate-User": {"foo@example.com"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tagged",
|
|
|
|
emailish: "tagged-device",
|
|
|
|
tags: []string{"tag:foo", "tag:bar"},
|
|
|
|
wantHeaders: http.Header{
|
|
|
|
"Impersonate-User": {"node.ts.net"},
|
|
|
|
"Impersonate-Group": {"tag:foo", "tag:bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user-with-cap",
|
|
|
|
emailish: "foo@example.com",
|
|
|
|
capMap: tailcfg.PeerCapMap{
|
2024-06-04 17:31:37 +00:00
|
|
|
tailcfg.PeerCapabilityKubernetes: {
|
2023-09-18 16:40:14 +00:00
|
|
|
tailcfg.RawMessage(`{"impersonate":{"groups":["group1","group2"]}}`),
|
|
|
|
tailcfg.RawMessage(`{"impersonate":{"groups":["group1","group3"]}}`), // One group is duplicated.
|
|
|
|
tailcfg.RawMessage(`{"impersonate":{"groups":["group4"]}}`),
|
|
|
|
tailcfg.RawMessage(`{"impersonate":{"groups":["group2"]}}`), // duplicate
|
2023-08-16 20:20:55 +00:00
|
|
|
|
|
|
|
// These should be ignored, but should parse correctly.
|
2023-09-18 16:40:14 +00:00
|
|
|
tailcfg.RawMessage(`{}`),
|
|
|
|
tailcfg.RawMessage(`{"impersonate":{}}`),
|
|
|
|
tailcfg.RawMessage(`{"impersonate":{"groups":[]}}`),
|
2023-08-16 20:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantHeaders: http.Header{
|
|
|
|
"Impersonate-Group": {"group1", "group2", "group3", "group4"},
|
|
|
|
"Impersonate-User": {"foo@example.com"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tagged-with-cap",
|
|
|
|
emailish: "tagged-device",
|
|
|
|
tags: []string{"tag:foo", "tag:bar"},
|
|
|
|
capMap: tailcfg.PeerCapMap{
|
2024-06-04 17:31:37 +00:00
|
|
|
tailcfg.PeerCapabilityKubernetes: {
|
2023-09-18 16:40:14 +00:00
|
|
|
tailcfg.RawMessage(`{"impersonate":{"groups":["group1"]}}`),
|
2023-08-16 20:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantHeaders: http.Header{
|
|
|
|
"Impersonate-Group": {"group1"},
|
|
|
|
"Impersonate-User": {"node.ts.net"},
|
|
|
|
},
|
|
|
|
},
|
2024-06-10 15:36:22 +00:00
|
|
|
{
|
|
|
|
name: "mix-of-caps",
|
|
|
|
emailish: "tagged-device",
|
|
|
|
tags: []string{"tag:foo", "tag:bar"},
|
|
|
|
capMap: tailcfg.PeerCapMap{
|
|
|
|
tailcfg.PeerCapabilityKubernetes: {
|
|
|
|
tailcfg.RawMessage(`{"impersonate":{"groups":["group1"]},"recorder":["tag:foo"],"enforceRecorder":true}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantHeaders: http.Header{
|
|
|
|
"Impersonate-Group": {"group1"},
|
|
|
|
"Impersonate-User": {"node.ts.net"},
|
|
|
|
},
|
|
|
|
},
|
2023-08-16 20:20:55 +00:00
|
|
|
{
|
|
|
|
name: "bad-cap",
|
|
|
|
emailish: "tagged-device",
|
|
|
|
tags: []string{"tag:foo", "tag:bar"},
|
|
|
|
capMap: tailcfg.PeerCapMap{
|
2024-06-04 17:31:37 +00:00
|
|
|
tailcfg.PeerCapabilityKubernetes: {
|
2023-09-18 16:40:14 +00:00
|
|
|
tailcfg.RawMessage(`[]`),
|
2023-08-16 20:20:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantHeaders: http.Header{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
r := must.Get(http.NewRequest("GET", "https://op.ts.net/api/foo", nil))
|
2024-01-16 21:56:23 +00:00
|
|
|
r = r.WithContext(whoIsKey.WithValue(r.Context(), &apitype.WhoIsResponse{
|
2023-08-16 20:20:55 +00:00
|
|
|
Node: &tailcfg.Node{
|
|
|
|
Name: "node.ts.net",
|
|
|
|
Tags: tc.tags,
|
|
|
|
},
|
|
|
|
UserProfile: &tailcfg.UserProfile{
|
|
|
|
LoginName: tc.emailish,
|
|
|
|
},
|
|
|
|
CapMap: tc.capMap,
|
2024-01-16 21:56:23 +00:00
|
|
|
}))
|
2023-11-20 15:41:18 +00:00
|
|
|
addImpersonationHeaders(r, zl.Sugar())
|
2023-08-16 20:20:55 +00:00
|
|
|
|
|
|
|
if d := cmp.Diff(tc.wantHeaders, r.Header); d != "" {
|
|
|
|
t.Errorf("unexpected header (-want +got):\n%s", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-08 20:18:55 +00:00
|
|
|
|
|
|
|
func Test_determineRecorderConfig(t *testing.T) {
|
|
|
|
addr1, addr2 := netip.MustParseAddrPort("[fd7a:115c:a1e0:ab12:4843:cd96:626b:628b]:80"), netip.MustParseAddrPort("100.99.99.99:80")
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
wantFailOpen bool
|
|
|
|
wantRecorderAddresses []netip.AddrPort
|
|
|
|
who *apitype.WhoIsResponse
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "two_ips_fail_closed",
|
|
|
|
who: whoResp(map[string][]string{string(tailcfg.PeerCapabilityKubernetes): {`{"recorderAddrs":["[fd7a:115c:a1e0:ab12:4843:cd96:626b:628b]:80","100.99.99.99:80"],"enforceRecorder":true}`}}),
|
|
|
|
wantRecorderAddresses: []netip.AddrPort{addr1, addr2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two_ips_fail_open",
|
|
|
|
who: whoResp(map[string][]string{string(tailcfg.PeerCapabilityKubernetes): {`{"recorderAddrs":["[fd7a:115c:a1e0:ab12:4843:cd96:626b:628b]:80","100.99.99.99:80"]}`}}),
|
|
|
|
wantRecorderAddresses: []netip.AddrPort{addr1, addr2},
|
|
|
|
wantFailOpen: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "odd_rule_combination_fail_closed",
|
|
|
|
who: whoResp(map[string][]string{string(tailcfg.PeerCapabilityKubernetes): {`{"recorderAddrs":["100.99.99.99:80"],"enforceRecorder":false}`, `{"recorderAddrs":["[fd7a:115c:a1e0:ab12:4843:cd96:626b:628b]:80"]}`, `{"enforceRecorder":true,"impersonate":{"groups":["system:masters"]}}`}}),
|
|
|
|
wantRecorderAddresses: []netip.AddrPort{addr2, addr1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no_caps",
|
|
|
|
who: whoResp(map[string][]string{}),
|
|
|
|
wantFailOpen: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no_recorder_caps",
|
|
|
|
who: whoResp(map[string][]string{"foo": {`{"x":"y"}`}, string(tailcfg.PeerCapabilityKubernetes): {`{"impersonate":{"groups":["system:masters"]}}`}}),
|
|
|
|
wantFailOpen: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
gotFailOpen, gotRecorderAddresses, err := determineRecorderConfig(tt.who)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if gotFailOpen != tt.wantFailOpen {
|
|
|
|
t.Errorf("determineRecorderConfig() gotFailOpen = %v, want %v", gotFailOpen, tt.wantFailOpen)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotRecorderAddresses, tt.wantRecorderAddresses) {
|
|
|
|
t.Errorf("determineRecorderConfig() gotRecorderAddresses = %v, want %v", gotRecorderAddresses, tt.wantRecorderAddresses)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func whoResp(capMap map[string][]string) *apitype.WhoIsResponse {
|
|
|
|
resp := &apitype.WhoIsResponse{
|
|
|
|
CapMap: tailcfg.PeerCapMap{},
|
|
|
|
}
|
|
|
|
for cap, rules := range capMap {
|
|
|
|
resp.CapMap[tailcfg.PeerCapability(cap)] = raw(rules...)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func raw(in ...string) []tailcfg.RawMessage {
|
|
|
|
var out []tailcfg.RawMessage
|
|
|
|
for _, i := range in {
|
|
|
|
out = append(out, tailcfg.RawMessage(i))
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|