mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-09 17:52:57 +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>
344 lines
9.9 KiB
Go
344 lines
9.9 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package store provides various implementation of ipn.StateStore.
|
|
package store
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"iter"
|
|
"maps"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
|
|
"tailscale.com/atomicfile"
|
|
"tailscale.com/ipn"
|
|
"tailscale.com/ipn/store/mem"
|
|
"tailscale.com/paths"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/util/mak"
|
|
"tailscale.com/util/testenv"
|
|
)
|
|
|
|
// Provider returns a StateStore for the provided path.
|
|
// The arg is of the form "prefix:rest", where prefix was previously registered with Register.
|
|
type Provider func(logf logger.Logf, arg string) (ipn.StateStore, error)
|
|
|
|
func init() {
|
|
Register("mem:", mem.New)
|
|
}
|
|
|
|
var knownStores map[string]Provider
|
|
|
|
// TPMPrefix is the path prefix used for TPM-encrypted StateStore.
|
|
const TPMPrefix = "tpmseal:"
|
|
|
|
// New returns a StateStore based on the provided arg
|
|
// and registered stores.
|
|
// The arg is of the form "prefix:rest", where prefix was previously
|
|
// registered with Register.
|
|
//
|
|
// By default the following stores are registered:
|
|
//
|
|
// - if the string begins with "mem:", the suffix
|
|
// is ignored and an in-memory store is used.
|
|
// - (Linux-only) if the string begins with "arn:",
|
|
// the suffix an AWS ARN for an SSM.
|
|
// - (Linux-only) if the string begins with "kube:",
|
|
// the suffix is a Kubernetes secret name
|
|
// - (Linux or Windows) if the string begins with "tpmseal:", the suffix is
|
|
// filepath that is sealed with the local TPM device.
|
|
// - In all other cases, the path is treated as a filepath.
|
|
func New(logf logger.Logf, path string) (ipn.StateStore, error) {
|
|
for prefix, sf := range knownStores {
|
|
if strings.HasPrefix(path, prefix) {
|
|
// We can't strip the prefix here as some NewStoreFunc (like arn:)
|
|
// expect the prefix.
|
|
if prefix == TPMPrefix {
|
|
if runtime.GOOS == "windows" {
|
|
path = TPMPrefix + TryWindowsAppDataMigration(logf, strings.TrimPrefix(path, TPMPrefix))
|
|
}
|
|
if err := maybeMigrateLocalStateFile(logf, path); err != nil {
|
|
return nil, fmt.Errorf("failed to migrate existing state file to TPM-sealed format: %w", err)
|
|
}
|
|
}
|
|
return sf(logf, path)
|
|
}
|
|
}
|
|
if runtime.GOOS == "windows" {
|
|
path = TryWindowsAppDataMigration(logf, path)
|
|
}
|
|
if err := maybeMigrateLocalStateFile(logf, path); err != nil {
|
|
return nil, fmt.Errorf("failed to migrate existing TPM-sealed state file to plaintext format: %w", err)
|
|
}
|
|
return NewFileStore(logf, path)
|
|
}
|
|
|
|
// Register registers a prefix to be used for
|
|
// NewStore. It panics if the prefix is empty, or if the
|
|
// prefix is already registered.
|
|
// The provided fn is called with the path passed to NewStore;
|
|
// the prefix is not stripped.
|
|
func Register(prefix string, fn Provider) {
|
|
if len(prefix) == 0 {
|
|
panic("prefix is empty")
|
|
}
|
|
if _, ok := knownStores[prefix]; ok {
|
|
panic(fmt.Sprintf("%q already registered", prefix))
|
|
}
|
|
mak.Set(&knownStores, prefix, fn)
|
|
}
|
|
|
|
// RegisterForTest registers a prefix to be used for NewStore in tests. An
|
|
// existing registered prefix will be replaced.
|
|
func RegisterForTest(t testenv.TB, prefix string, fn Provider) {
|
|
if len(prefix) == 0 {
|
|
panic("prefix is empty")
|
|
}
|
|
old := maps.Clone(knownStores)
|
|
t.Cleanup(func() { knownStores = old })
|
|
|
|
mak.Set(&knownStores, prefix, fn)
|
|
}
|
|
|
|
// HasKnownProviderPrefix reports whether path uses one of the registered
|
|
// Provider prefixes.
|
|
func HasKnownProviderPrefix(path string) bool {
|
|
for prefix := range knownStores {
|
|
if strings.HasPrefix(path, prefix) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// TryWindowsAppDataMigration attempts to copy the Windows state file
|
|
// from its old location to the new location. (Issue 2856)
|
|
//
|
|
// Tailscale 1.14 and before stored state under %LocalAppData%
|
|
// (usually "C:\WINDOWS\system32\config\systemprofile\AppData\Local"
|
|
// when tailscaled.exe is running as a non-user system service).
|
|
// However it is frequently cleared for almost any reason: Windows
|
|
// updates, System Restore, even various System Cleaner utilities.
|
|
//
|
|
// Returns a string of the path to use for the state file.
|
|
// This will be a fallback %LocalAppData% path if migration fails,
|
|
// a %ProgramData% path otherwise.
|
|
func TryWindowsAppDataMigration(logf logger.Logf, path string) string {
|
|
if path != paths.DefaultTailscaledStateFile() {
|
|
// If they're specifying a non-default path, just trust that they know
|
|
// what they are doing.
|
|
return path
|
|
}
|
|
oldFile := paths.LegacyStateFilePath()
|
|
return paths.TryConfigFileMigration(logf, oldFile, path)
|
|
}
|
|
|
|
// FileStore is a StateStore that uses a JSON file for persistence.
|
|
type FileStore struct {
|
|
path string
|
|
|
|
mu sync.RWMutex
|
|
cache map[ipn.StateKey][]byte
|
|
}
|
|
|
|
// Path returns the path that NewFileStore was called with.
|
|
func (s *FileStore) Path() string { return s.path }
|
|
|
|
func (s *FileStore) String() string { return fmt.Sprintf("FileStore(%q)", s.path) }
|
|
|
|
// NewFileStore returns a new file store that persists to path.
|
|
func NewFileStore(logf logger.Logf, path string) (ipn.StateStore, error) {
|
|
// We unconditionally call this to ensure that our perms are correct
|
|
if err := paths.MkStateDir(filepath.Dir(path)); err != nil {
|
|
return nil, fmt.Errorf("creating state directory: %w", err)
|
|
}
|
|
|
|
bs, err := os.ReadFile(path)
|
|
|
|
// Treat an empty file as a missing file.
|
|
// (https://github.com/tailscale/tailscale/issues/895#issuecomment-723255589)
|
|
if err == nil && len(bs) == 0 {
|
|
logf("store.NewFileStore(%q): file empty; treating it like a missing file [warning]", path)
|
|
err = os.ErrNotExist
|
|
}
|
|
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
// Write out an initial file, to verify that we can write
|
|
// to the path.
|
|
if err = atomicfile.WriteFile(path, []byte("{}"), 0600); err != nil {
|
|
return nil, err
|
|
}
|
|
return &FileStore{
|
|
path: path,
|
|
cache: map[ipn.StateKey][]byte{},
|
|
}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
ret := &FileStore{
|
|
path: path,
|
|
cache: map[ipn.StateKey][]byte{},
|
|
}
|
|
if err := json.Unmarshal(bs, &ret.cache); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
// ReadState implements the StateStore interface.
|
|
func (s *FileStore) ReadState(id ipn.StateKey) ([]byte, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
bs, ok := s.cache[id]
|
|
if !ok {
|
|
return nil, ipn.ErrStateNotExist
|
|
}
|
|
return bs, nil
|
|
}
|
|
|
|
// WriteState implements the StateStore interface.
|
|
func (s *FileStore) WriteState(id ipn.StateKey, bs []byte) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if bytes.Equal(s.cache[id], bs) {
|
|
return nil
|
|
}
|
|
s.cache[id] = bytes.Clone(bs)
|
|
bs, err := json.MarshalIndent(s.cache, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return atomicfile.WriteFile(s.path, bs, 0600)
|
|
}
|
|
|
|
func (s *FileStore) All() iter.Seq2[ipn.StateKey, []byte] {
|
|
return func(yield func(ipn.StateKey, []byte) bool) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
for k, v := range s.cache {
|
|
if !yield(k, v) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure FileStore implements ExportableStore for migration to/from
|
|
// tpm.tpmStore.
|
|
var _ ExportableStore = (*FileStore)(nil)
|
|
|
|
// ExportableStore is an ipn.StateStore that can export all of its contents.
|
|
// This interface is optional to implement, and used for migrating the state
|
|
// between different store implementations.
|
|
type ExportableStore interface {
|
|
ipn.StateStore
|
|
|
|
// All returns an iterator over all store keys. Using ReadState or
|
|
// WriteState is not safe while iterating and can lead to a deadlock. The
|
|
// order of keys in the iterator is not specified and may change between
|
|
// runs.
|
|
All() iter.Seq2[ipn.StateKey, []byte]
|
|
}
|
|
|
|
func maybeMigrateLocalStateFile(logf logger.Logf, path string) error {
|
|
path, toTPM := strings.CutPrefix(path, TPMPrefix)
|
|
|
|
// Extract JSON keys from the file on disk and guess what kind it is.
|
|
bs, err := os.ReadFile(path)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
var content map[string]any
|
|
if err := json.Unmarshal(bs, &content); err != nil {
|
|
return fmt.Errorf("failed to unmarshal %q: %w", path, err)
|
|
}
|
|
keys := slices.Sorted(maps.Keys(content))
|
|
tpmKeys := []string{"key", "nonce", "data"}
|
|
slices.Sort(tpmKeys)
|
|
// TPM-sealed files will have exactly these keys.
|
|
existingFileSealed := slices.Equal(keys, tpmKeys)
|
|
// Plaintext files for nodes that registered at least once will have this
|
|
// key, plus other dynamic ones.
|
|
_, existingFilePlaintext := content["_machinekey"]
|
|
isTPM := existingFileSealed && !existingFilePlaintext
|
|
|
|
if isTPM == toTPM {
|
|
// No migration needed.
|
|
return nil
|
|
}
|
|
|
|
newTPMStore, ok := knownStores[TPMPrefix]
|
|
if !ok {
|
|
return errors.New("this build does not support TPM integration")
|
|
}
|
|
|
|
// Open from (old format) and to (new format) stores for migration. The
|
|
// "to" store will be at tmpPath.
|
|
var from, to ipn.StateStore
|
|
tmpPath := path + ".tmp"
|
|
if toTPM {
|
|
// Migrate plaintext file to be TPM-sealed.
|
|
from, err = NewFileStore(logf, path)
|
|
if err != nil {
|
|
return fmt.Errorf("NewFileStore(%q): %w", path, err)
|
|
}
|
|
to, err = newTPMStore(logf, TPMPrefix+tmpPath)
|
|
if err != nil {
|
|
return fmt.Errorf("newTPMStore(%q): %w", tmpPath, err)
|
|
}
|
|
} else {
|
|
// Migrate TPM-selaed file to plaintext.
|
|
from, err = newTPMStore(logf, TPMPrefix+path)
|
|
if err != nil {
|
|
return fmt.Errorf("newTPMStore(%q): %w", path, err)
|
|
}
|
|
to, err = NewFileStore(logf, tmpPath)
|
|
if err != nil {
|
|
return fmt.Errorf("NewFileStore(%q): %w", tmpPath, err)
|
|
}
|
|
}
|
|
defer os.Remove(tmpPath)
|
|
|
|
fromExp, ok := from.(ExportableStore)
|
|
if !ok {
|
|
return fmt.Errorf("%T does not implement the exportableStore interface", from)
|
|
}
|
|
|
|
// Copy all the items. This is pretty inefficient, because both stores
|
|
// write the file to disk for each WriteState, but that's ok for a one-time
|
|
// migration.
|
|
for k, v := range fromExp.All() {
|
|
if err := to.WriteState(k, v); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Finally, overwrite the state file with the new one we created at
|
|
// tmpPath.
|
|
if err := atomicfile.Rename(tmpPath, path); err != nil {
|
|
return err
|
|
}
|
|
|
|
if toTPM {
|
|
logf("migrated %q from plaintext to TPM-sealed format", path)
|
|
} else {
|
|
logf("migrated %q from TPM-sealed to plaintext format", path)
|
|
}
|
|
return nil
|
|
}
|