mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-11 18:51:41 +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>
174 lines
6.0 KiB
Go
174 lines
6.0 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package rsop facilitates [source.Store] registration via [RegisterStore]
|
|
// and provides access to the effective policy merged from all registered sources
|
|
// via [PolicyFor].
|
|
package rsop
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"tailscale.com/syncs"
|
|
"tailscale.com/util/slicesx"
|
|
"tailscale.com/util/syspolicy/internal"
|
|
"tailscale.com/util/syspolicy/setting"
|
|
"tailscale.com/util/syspolicy/source"
|
|
)
|
|
|
|
var (
|
|
policyMu syncs.Mutex // protects [policySources] and [effectivePolicies]
|
|
policySources []*source.Source // all registered policy sources
|
|
effectivePolicies []*Policy // all active (non-closed) effective policies returned by [PolicyFor]
|
|
|
|
// effectivePolicyLRU is an LRU cache of [Policy] by [setting.Scope].
|
|
// Although there could be multiple [setting.PolicyScope] instances with the same [setting.Scope],
|
|
// such as two user scopes for different users, there is only one [setting.DeviceScope], only one
|
|
// [setting.CurrentProfileScope], and in most cases, only one active user scope.
|
|
// Therefore, cache misses that require falling back to [effectivePolicies] are extremely rare.
|
|
// It's a fixed-size array of atomic values and can be accessed without [policyMu] held.
|
|
effectivePolicyLRU [setting.NumScopes]syncs.AtomicValue[*Policy]
|
|
)
|
|
|
|
// PolicyFor returns the [Policy] for the specified scope,
|
|
// creating it from the registered [source.Store]s if it doesn't already exist.
|
|
func PolicyFor(scope setting.PolicyScope) (*Policy, error) {
|
|
if err := internal.Init.Do(); err != nil {
|
|
return nil, err
|
|
}
|
|
policy := effectivePolicyLRU[scope.Kind()].Load()
|
|
if policy != nil && policy.Scope() == scope && policy.IsValid() {
|
|
return policy, nil
|
|
}
|
|
return policyForSlow(scope)
|
|
}
|
|
|
|
func policyForSlow(scope setting.PolicyScope) (policy *Policy, err error) {
|
|
defer func() {
|
|
// Always update the LRU cache on exit if we found (or created)
|
|
// a policy for the specified scope.
|
|
if policy != nil {
|
|
effectivePolicyLRU[scope.Kind()].Store(policy)
|
|
}
|
|
}()
|
|
|
|
policyMu.Lock()
|
|
defer policyMu.Unlock()
|
|
if policy, ok := findPolicyByScopeLocked(scope); ok {
|
|
return policy, nil
|
|
}
|
|
|
|
// If there is no existing effective policy for the specified scope,
|
|
// we need to create one using the policy sources registered for that scope.
|
|
sources := slicesx.Filter(nil, policySources, func(source *source.Source) bool {
|
|
return source.Scope().Contains(scope)
|
|
})
|
|
policy, err = newPolicy(scope, sources...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
effectivePolicies = append(effectivePolicies, policy)
|
|
return policy, nil
|
|
}
|
|
|
|
// findPolicyByScopeLocked returns a policy with the specified scope and true if
|
|
// one exists in the [effectivePolicies] list, otherwise it returns nil, false.
|
|
// [policyMu] must be held.
|
|
func findPolicyByScopeLocked(target setting.PolicyScope) (policy *Policy, ok bool) {
|
|
for _, policy := range effectivePolicies {
|
|
if policy.Scope() == target && policy.IsValid() {
|
|
return policy, true
|
|
}
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
// deletePolicy deletes the specified effective policy from [effectivePolicies]
|
|
// and [effectivePolicyLRU].
|
|
func deletePolicy(policy *Policy) {
|
|
policyMu.Lock()
|
|
defer policyMu.Unlock()
|
|
if i := slices.Index(effectivePolicies, policy); i != -1 {
|
|
effectivePolicies = slices.Delete(effectivePolicies, i, i+1)
|
|
}
|
|
effectivePolicyLRU[policy.Scope().Kind()].CompareAndSwap(policy, nil)
|
|
}
|
|
|
|
// registerSource registers the specified [source.Source] to be used by the package.
|
|
// It updates existing [Policy]s returned by [PolicyFor] to use this source if
|
|
// they are within the source's [setting.PolicyScope].
|
|
func registerSource(source *source.Source) error {
|
|
policyMu.Lock()
|
|
defer policyMu.Unlock()
|
|
if slices.Contains(policySources, source) {
|
|
// already registered
|
|
return nil
|
|
}
|
|
policySources = append(policySources, source)
|
|
return forEachEffectivePolicyLocked(func(policy *Policy) error {
|
|
if !source.Scope().Contains(policy.Scope()) {
|
|
// Policy settings in the specified source do not apply
|
|
// to the scope of this effective policy.
|
|
// For example, a user policy source is being registered
|
|
// while the effective policy is for the device (or another user).
|
|
return nil
|
|
}
|
|
return policy.addSource(source)
|
|
})
|
|
}
|
|
|
|
// replaceSource is like [unregisterSource](old) followed by [registerSource](new),
|
|
// but performed atomically: the effective policy will contain settings
|
|
// either from the old source or the new source, never both and never neither.
|
|
func replaceSource(old, new *source.Source) error {
|
|
policyMu.Lock()
|
|
defer policyMu.Unlock()
|
|
oldIndex := slices.Index(policySources, old)
|
|
if oldIndex == -1 {
|
|
return fmt.Errorf("the source is not registered: %v", old)
|
|
}
|
|
policySources[oldIndex] = new
|
|
return forEachEffectivePolicyLocked(func(policy *Policy) error {
|
|
if !old.Scope().Contains(policy.Scope()) || !new.Scope().Contains(policy.Scope()) {
|
|
return nil
|
|
}
|
|
return policy.replaceSource(old, new)
|
|
})
|
|
}
|
|
|
|
// unregisterSource unregisters the specified [source.Source],
|
|
// so that it won't be used by any new or existing [Policy].
|
|
func unregisterSource(source *source.Source) error {
|
|
policyMu.Lock()
|
|
defer policyMu.Unlock()
|
|
index := slices.Index(policySources, source)
|
|
if index == -1 {
|
|
return nil
|
|
}
|
|
policySources = slices.Delete(policySources, index, index+1)
|
|
return forEachEffectivePolicyLocked(func(policy *Policy) error {
|
|
if !source.Scope().Contains(policy.Scope()) {
|
|
return nil
|
|
}
|
|
return policy.removeSource(source)
|
|
})
|
|
}
|
|
|
|
// forEachEffectivePolicyLocked calls fn for every non-closed [Policy] in [effectivePolicies].
|
|
// It accumulates the returned errors and returns an error that wraps all errors returned by fn.
|
|
// The [policyMu] mutex must be held while this function is executed.
|
|
func forEachEffectivePolicyLocked(fn func(p *Policy) error) error {
|
|
var errs []error
|
|
for _, policy := range effectivePolicies {
|
|
if policy.IsValid() {
|
|
err := fn(policy)
|
|
if err != nil && !errors.Is(err, ErrPolicyClosed) {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|