mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-12 03:01:28 +01:00
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
241 lines
7.2 KiB
Go
241 lines
7.2 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package setting
|
|
|
|
import (
|
|
"errors"
|
|
"iter"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
jsonv2 "github.com/go-json-experiment/json"
|
|
"github.com/go-json-experiment/json/jsontext"
|
|
xmaps "golang.org/x/exp/maps"
|
|
"tailscale.com/util/deephash"
|
|
"tailscale.com/util/syspolicy/pkey"
|
|
)
|
|
|
|
// Snapshot is an immutable collection of ([Key], [RawItem]) pairs, representing
|
|
// a set of policy settings applied at a specific moment in time.
|
|
// A nil pointer to [Snapshot] is valid.
|
|
type Snapshot struct {
|
|
m map[pkey.Key]RawItem
|
|
sig deephash.Sum // of m
|
|
summary Summary
|
|
}
|
|
|
|
// NewSnapshot returns a new [Snapshot] with the specified items and options.
|
|
func NewSnapshot(items map[pkey.Key]RawItem, opts ...SummaryOption) *Snapshot {
|
|
return &Snapshot{m: xmaps.Clone(items), sig: deephash.Hash(&items), summary: SummaryWith(opts...)}
|
|
}
|
|
|
|
// All returns an iterator over policy settings in s. The iteration order is not
|
|
// specified and is not guaranteed to be the same from one call to the next.
|
|
func (s *Snapshot) All() iter.Seq2[pkey.Key, RawItem] {
|
|
if s == nil {
|
|
return func(yield func(pkey.Key, RawItem) bool) {}
|
|
}
|
|
return maps.All(s.m)
|
|
}
|
|
|
|
// Get returns the value of the policy setting with the specified key
|
|
// or nil if it is not configured or has an error.
|
|
func (s *Snapshot) Get(k pkey.Key) any {
|
|
v, _ := s.GetErr(k)
|
|
return v
|
|
}
|
|
|
|
// GetErr returns the value of the policy setting with the specified key,
|
|
// [ErrNotConfigured] if it is not configured, or an error returned by
|
|
// the policy Store if the policy setting could not be read.
|
|
func (s *Snapshot) GetErr(k pkey.Key) (any, error) {
|
|
if s != nil {
|
|
if s, ok := s.m[k]; ok {
|
|
return s.Value(), s.Error()
|
|
}
|
|
}
|
|
return nil, ErrNotConfigured
|
|
}
|
|
|
|
// GetSetting returns the untyped policy setting with the specified key and true
|
|
// if a policy setting with such key has been configured;
|
|
// otherwise, it returns zero, false.
|
|
func (s *Snapshot) GetSetting(k pkey.Key) (setting RawItem, ok bool) {
|
|
setting, ok = s.m[k]
|
|
return setting, ok
|
|
}
|
|
|
|
// Equal reports whether s and s2 are equal.
|
|
func (s *Snapshot) Equal(s2 *Snapshot) bool {
|
|
if s == s2 {
|
|
return true
|
|
}
|
|
if !s.EqualItems(s2) {
|
|
return false
|
|
}
|
|
return s.Summary() == s2.Summary()
|
|
}
|
|
|
|
// EqualItems reports whether items in s and s2 are equal.
|
|
func (s *Snapshot) EqualItems(s2 *Snapshot) bool {
|
|
if s == s2 {
|
|
return true
|
|
}
|
|
if s.Len() != s2.Len() {
|
|
return false
|
|
}
|
|
if s.Len() == 0 {
|
|
return true
|
|
}
|
|
return s.sig == s2.sig
|
|
}
|
|
|
|
// Keys return an iterator over keys in s. The iteration order is not specified
|
|
// and is not guaranteed to be the same from one call to the next.
|
|
func (s *Snapshot) Keys() iter.Seq[pkey.Key] {
|
|
if s.m == nil {
|
|
return func(yield func(pkey.Key) bool) {}
|
|
}
|
|
return maps.Keys(s.m)
|
|
}
|
|
|
|
// Len reports the number of [RawItem]s in s.
|
|
func (s *Snapshot) Len() int {
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
return len(s.m)
|
|
}
|
|
|
|
// Summary returns information about s as a whole rather than about specific [RawItem]s in it.
|
|
func (s *Snapshot) Summary() Summary {
|
|
if s == nil {
|
|
return Summary{}
|
|
}
|
|
return s.summary
|
|
}
|
|
|
|
// String implements [fmt.Stringer]
|
|
func (s *Snapshot) String() string {
|
|
if s.Len() == 0 && s.Summary().IsEmpty() {
|
|
return "{Empty}"
|
|
}
|
|
var sb strings.Builder
|
|
if !s.summary.IsEmpty() {
|
|
sb.WriteRune('{')
|
|
if s.Len() == 0 {
|
|
sb.WriteString("Empty, ")
|
|
}
|
|
sb.WriteString(s.summary.String())
|
|
sb.WriteRune('}')
|
|
}
|
|
for _, k := range slices.Sorted(s.Keys()) {
|
|
if sb.Len() != 0 {
|
|
sb.WriteRune('\n')
|
|
}
|
|
sb.WriteString(string(k))
|
|
sb.WriteString(" = ")
|
|
sb.WriteString(s.m[k].String())
|
|
}
|
|
return sb.String()
|
|
}
|
|
|
|
// snapshotJSON holds JSON-marshallable data for [Snapshot].
|
|
type snapshotJSON struct {
|
|
Summary Summary `json:",omitzero"`
|
|
Settings map[pkey.Key]RawItem `json:",omitempty"`
|
|
}
|
|
|
|
var (
|
|
_ jsonv2.MarshalerTo = (*Snapshot)(nil)
|
|
_ jsonv2.UnmarshalerFrom = (*Snapshot)(nil)
|
|
)
|
|
|
|
// As of 2025-07-28, jsonv2 no longer has a default representation for [time.Duration],
|
|
// so we need to provide a custom marshaler.
|
|
//
|
|
// This is temporary until the decision on the default representation is made
|
|
// (see https://github.com/golang/go/issues/71631#issuecomment-2981670799).
|
|
//
|
|
// In the future, we might either use the default representation (if compatible with
|
|
// [time.Duration.String]) or specify something like json.WithFormat[time.Duration]("units")
|
|
// when golang/go#71664 is implemented.
|
|
//
|
|
// TODO(nickkhyl): revisit this when the decision on the default [time.Duration]
|
|
// representation is made in golang/go#71631 and/or golang/go#71664 is implemented.
|
|
var formatDurationAsUnits = jsonv2.JoinOptions(
|
|
jsonv2.WithMarshalers(jsonv2.MarshalToFunc(func(e *jsontext.Encoder, t time.Duration) error {
|
|
return e.WriteToken(jsontext.String(t.String()))
|
|
})),
|
|
)
|
|
|
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
|
func (s *Snapshot) MarshalJSONTo(out *jsontext.Encoder) error {
|
|
data := &snapshotJSON{}
|
|
if s != nil {
|
|
data.Summary = s.summary
|
|
data.Settings = s.m
|
|
}
|
|
return jsonv2.MarshalEncode(out, data, formatDurationAsUnits)
|
|
}
|
|
|
|
// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
|
|
func (s *Snapshot) UnmarshalJSONFrom(in *jsontext.Decoder) error {
|
|
if s == nil {
|
|
return errors.New("s must not be nil")
|
|
}
|
|
data := &snapshotJSON{}
|
|
if err := jsonv2.UnmarshalDecode(in, data); err != nil {
|
|
return err
|
|
}
|
|
*s = Snapshot{m: data.Settings, sig: deephash.Hash(&data.Settings), summary: data.Summary}
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON implements [json.Marshaler].
|
|
func (s *Snapshot) MarshalJSON() ([]byte, error) {
|
|
return jsonv2.Marshal(s) // uses MarshalJSONTo
|
|
}
|
|
|
|
// UnmarshalJSON implements [json.Unmarshaler].
|
|
func (s *Snapshot) UnmarshalJSON(b []byte) error {
|
|
return jsonv2.Unmarshal(b, s) // uses UnmarshalJSONFrom
|
|
}
|
|
|
|
// MergeSnapshots returns a [Snapshot] that contains all [RawItem]s
|
|
// from snapshot1 and snapshot2 and the [Summary] with the narrower [PolicyScope].
|
|
// If there's a conflict between policy settings in the two snapshots,
|
|
// the policy settings from the snapshot with the broader scope take precedence.
|
|
// In other words, policy settings configured for the [DeviceScope] win
|
|
// over policy settings configured for a user scope.
|
|
func MergeSnapshots(snapshot1, snapshot2 *Snapshot) *Snapshot {
|
|
scope1, ok1 := snapshot1.Summary().Scope().GetOk()
|
|
scope2, ok2 := snapshot2.Summary().Scope().GetOk()
|
|
if ok1 && ok2 && scope1.StrictlyContains(scope2) {
|
|
// Swap snapshots if snapshot1 has higher precedence than snapshot2.
|
|
snapshot1, snapshot2 = snapshot2, snapshot1
|
|
}
|
|
if snapshot2.Len() == 0 {
|
|
return snapshot1
|
|
}
|
|
summaryOpts := make([]SummaryOption, 0, 2)
|
|
if scope, ok := snapshot1.Summary().Scope().GetOk(); ok {
|
|
// Use the scope from snapshot1, if present, which is the more specific snapshot.
|
|
summaryOpts = append(summaryOpts, scope)
|
|
}
|
|
if snapshot1.Len() == 0 {
|
|
if origin, ok := snapshot2.Summary().Origin().GetOk(); ok {
|
|
// Use the origin from snapshot2 if snapshot1 is empty.
|
|
summaryOpts = append(summaryOpts, origin)
|
|
}
|
|
return &Snapshot{snapshot2.m, snapshot2.sig, SummaryWith(summaryOpts...)}
|
|
}
|
|
m := make(map[pkey.Key]RawItem, snapshot1.Len()+snapshot2.Len())
|
|
xmaps.Copy(m, snapshot1.m)
|
|
xmaps.Copy(m, snapshot2.m) // snapshot2 has higher precedence
|
|
return &Snapshot{m, deephash.Hash(&m), SummaryWith(summaryOpts...)}
|
|
}
|