Files
tailscale/ipn/localapi/syspolicy_api_test.go
Nick Khyl d19e414f0d util/syspolicy: make exit node, DNS and subnet settings per-profile
In this PR, we make ExitNodeID, ExitNodeIP, ExitNode.AllowOverride,
AllowedSuggestedExitNodes, ExitNodeAllowLANAccess, EnableDNSRegistration,
UseTailscaleDNSSettings and UseTailscaleSubnets profile settings,
so they can be configured by a policy source in the current profile's
scope as well as for the device.

We also make the current profile's scope the default one, so that tailscaled
uses settings from such sources. Device settings still take precedence,
and nothing changes until a profile-scoped source is registered.

Updates tailscale/corp#51945

Change-Id: I472595eb723cd3419003a02de8c66b0d8ebf9417
Signed-off-by: Nick Khyl <[email protected]>
2026-11-20 14:43:21 -08:00

101 lines
2.2 KiB
Go

// 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.DeviceScope, 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)
}
}