mirror of
https://github.com/tailscale/tailscale.git
synced 2026-07-15 01:53:08 -04:00
Tailscaled had no way to seed device-scope syspolicy settings short of environment variables or a custom store wired up out of tree. Add a --syspolicy-file flag whose default points at a well-known JSON file that, when present, is parsed as a map[string]any and registered as a device-scope policy source. The default path is /etc/tailscale/syspolicy.json on every non-Windows platform (Linux, the BSDs, illumos/Solaris, and tailscaled-without-the-GUI on macOS) and %ProgramData%\Tailscale\syspolicy.json on Windows. The flag lets users running tailscaled by hand (development, custom installs) point it at an alternate file, and "" disables the load entirely. JSON values map to setting types as expected: strings to StringValue/PreferenceOptionValue/VisibilityValue/DurationValue (e.g. "24h" parsed by time.ParseDuration), booleans to BooleanValue, numbers to IntegerValue, and string arrays to StringListValue. The file is validated against the registered setting definitions at load time so unknown keys and value/type mismatches fail startup loudly rather than producing surprising defaults at first read. When HuJSON support is linked into the build (default; opt out with ts_omit_hujsonconf), the file may use HuJSON (comments, trailing commas). With ts_omit_hujsonconf it must be pure standard JSON. This mirrors the pattern used by ipn/conffile. On Windows the JSON file and the existing HKLM registry store both register at DeviceScope. rsop merges later-registered same-scope sources over earlier ones, so per-key values in the file override the registry while keys absent from the file fall back to the registry. The loader is registered via a feature.Hook from a file gated by !ts_omit_syspolicy, and called from main after flag parsing. tsnet still does not depend on the root syspolicy package, so embedders don't pick this up implicitly. Fixes #20305 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Change-Id: Ie6326461c14efb226979ac162998a9c6373ce493
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package syspolicy
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
|
|
"tailscale.com/util/syspolicy/rsop"
|
|
"tailscale.com/util/syspolicy/setting"
|
|
"tailscale.com/util/syspolicy/source"
|
|
)
|
|
|
|
// LoadJSONPolicyFile loads policy settings from the JSON file at path and
|
|
// registers them as a [setting.DeviceScope] policy source under sourceName.
|
|
//
|
|
// If path does not exist, no source is registered and the function returns
|
|
// nil. Malformed JSON, unknown setting keys, or values that cannot be
|
|
// decoded as the registered type for their key all surface as errors here
|
|
// rather than at first use, and nothing is registered.
|
|
//
|
|
// LoadJSONPolicyFile is intended to be called once, early in process
|
|
// startup, after command-line flags are parsed but before any policy
|
|
// setting is read.
|
|
func LoadJSONPolicyFile(sourceName, path string) error {
|
|
store, err := source.NewJSONPolicyStoreFromFile(path)
|
|
if err != nil {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("syspolicy: loading %s: %w", path, err)
|
|
}
|
|
if err := store.Validate(); err != nil {
|
|
return fmt.Errorf("syspolicy: invalid %s:\n%w", path, err)
|
|
}
|
|
if _, err := rsop.RegisterStore(sourceName, setting.DeviceScope, store); err != nil {
|
|
return fmt.Errorf("syspolicy: registering %s: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|