diff --git a/ipn/localapi/syspolicy_api.go b/ipn/localapi/syspolicy_api.go index 9962f342b..222980c66 100644 --- a/ipn/localapi/syspolicy_api.go +++ b/ipn/localapi/syspolicy_api.go @@ -34,7 +34,12 @@ func (h *Handler) servePolicy(w http.ResponseWriter, r *http.Request) { var scope setting.PolicyScope if suffix == "" { - scope = setting.DefaultScope() + policyUID := string(h.Actor.UserID()) + if policyUID == "" { + scope = setting.DefaultScope() + } else { + scope = setting.UserScopeOf(policyUID) + } } else if err := scope.UnmarshalText([]byte(suffix)); err != nil { http.Error(w, fmt.Sprintf("%q is not a valid scope", suffix), http.StatusBadRequest) return diff --git a/ipn/localapi/syspolicy_api_test.go b/ipn/localapi/syspolicy_api_test.go new file mode 100644 index 000000000..8224b7048 --- /dev/null +++ b/ipn/localapi/syspolicy_api_test.go @@ -0,0 +1,100 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !ts_omit_syspolicy + +package localapi + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "tailscale.com/ipn/ipnauth" + "tailscale.com/util/httpm" + "tailscale.com/util/syspolicy/pkey" + "tailscale.com/util/syspolicy/rsop" + "tailscale.com/util/syspolicy/setting" + "tailscale.com/util/syspolicy/source" +) + +func TestPolicyUsesCallerUserScope(t *testing.T) { + setting.SetDefinitionsForTest(t, + setting.NewDefinition( + pkey.ManagedByOrganizationName, + setting.UserSetting, + setting.StringValue, + ), + ) + + const uid = "S-1-5-21-1001" + + store := source.NewTestStore(t) + store.SetStrings( + source.TestSettingOf(pkey.ManagedByOrganizationName, "UserCorp"), + ) + rsop.RegisterStoreForTest(t, "UserStore", setting.UserScopeOf(uid), store) + + h := &Handler{ + PermitRead: true, + Actor: &ipnauth.TestActor{UID: uid}, + } + + req := httptest.NewRequest(httpm.GET, "/localapi/v0/policy/", nil) + rec := httptest.NewRecorder() + + h.servePolicy(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf( + "status = %d; want %d; body = %s", + rec.Code, + http.StatusOK, + rec.Body.String(), + ) + } + + if got := rec.Body.String(); !strings.Contains(got, "UserCorp") { + t.Fatalf("response does not contain user-scoped policy: %s", got) + } +} + +func TestPolicyUsesDefaultScopeForEmptyUserID(t *testing.T) { + setting.SetDefinitionsForTest(t, + setting.NewDefinition( + pkey.ManagedByOrganizationName, + setting.DeviceSetting, + setting.StringValue, + ), + ) + + store := source.NewTestStore(t) + store.SetStrings( + source.TestSettingOf(pkey.ManagedByOrganizationName, "DeviceCorp"), + ) + rsop.RegisterStoreForTest(t, "DeviceStore", setting.DefaultScope(), store) + + h := &Handler{ + PermitRead: true, + Actor: &ipnauth.TestActor{}, + } + + req := httptest.NewRequest(httpm.GET, "/localapi/v0/policy/", nil) + rec := httptest.NewRecorder() + + h.servePolicy(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf( + "status = %d; want %d; body = %s", + rec.Code, + http.StatusOK, + rec.Body.String(), + ) + } + + if got := rec.Body.String(); !strings.Contains(got, "DeviceCorp") { + t.Fatalf("response does not contain default-scope policy: %s", got) + } +}