3ec5be3f51
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>
327 lines
8.6 KiB
Go
327 lines
8.6 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
//go:build !ts_omit_kube
|
|
|
|
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"net/netip"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/peterbourgon/ff/v3/ffcli"
|
|
"k8s.io/client-go/util/homedir"
|
|
"sigs.k8s.io/yaml"
|
|
"tailscale.com/ipn"
|
|
"tailscale.com/ipn/ipnstate"
|
|
"tailscale.com/tailcfg"
|
|
"tailscale.com/types/netmap"
|
|
"tailscale.com/util/dnsname"
|
|
"tailscale.com/version"
|
|
)
|
|
|
|
var configureKubeconfigArgs struct {
|
|
http bool // Use HTTP instead of HTTPS (default) for the auth proxy.
|
|
}
|
|
|
|
func configureKubeconfigCmd() *ffcli.Command {
|
|
return &ffcli.Command{
|
|
Name: "kubeconfig",
|
|
ShortHelp: "[ALPHA] Connect to a Kubernetes cluster using a Tailscale Auth Proxy",
|
|
ShortUsage: "tailscale configure kubeconfig <hostname-or-fqdn>",
|
|
LongHelp: strings.TrimSpace(`
|
|
Run this command to configure kubectl to connect to a Kubernetes cluster over Tailscale.
|
|
|
|
The hostname argument should be set to the Tailscale hostname of the peer running as an auth proxy in the cluster.
|
|
|
|
See: https://tailscale.com/s/k8s-auth-proxy
|
|
`),
|
|
FlagSet: (func() *flag.FlagSet {
|
|
fs := newFlagSet("kubeconfig")
|
|
fs.BoolVar(&configureKubeconfigArgs.http, "http", false, "Use HTTP instead of HTTPS to connect to the auth proxy. Ignored if you include a scheme in the hostname argument.")
|
|
return fs
|
|
})(),
|
|
Exec: runConfigureKubeconfig,
|
|
}
|
|
}
|
|
|
|
// kubeconfigPath returns the path to the kubeconfig file for the current user.
|
|
func kubeconfigPath() (string, error) {
|
|
if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
|
|
if version.IsSandboxedMacOS() {
|
|
return "", errors.New("cannot read $KUBECONFIG on GUI builds of the macOS client: this requires the open-source tailscaled distribution")
|
|
}
|
|
var out string
|
|
for _, out = range filepath.SplitList(kubeconfig) {
|
|
if info, err := os.Stat(out); !os.IsNotExist(err) && !info.IsDir() {
|
|
break
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
var dir string
|
|
if version.IsSandboxedMacOS() {
|
|
// The HOME environment variable in macOS sandboxed apps is set to
|
|
// ~/Library/Containers/<app-id>/Data, but the kubeconfig file is
|
|
// located in ~/.kube/config. We rely on the "com.apple.security.temporary-exception.files.home-relative-path.read-write"
|
|
// entitlement to access the file.
|
|
containerHome := os.Getenv("HOME")
|
|
dir, _, _ = strings.Cut(containerHome, "/Library/Containers/")
|
|
} else {
|
|
dir = homedir.HomeDir()
|
|
}
|
|
return filepath.Join(dir, ".kube", "config"), nil
|
|
}
|
|
|
|
func runConfigureKubeconfig(ctx context.Context, args []string) error {
|
|
if len(args) != 1 || args[0] == "" {
|
|
return flag.ErrHelp
|
|
}
|
|
hostOrFQDNOrIP, http, err := getInputs(args[0], configureKubeconfigArgs.http)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing inputs: %w", err)
|
|
}
|
|
|
|
st, err := localClient.Status(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if st.BackendState != "Running" {
|
|
return errors.New("Tailscale is not running")
|
|
}
|
|
nm, err := getNetMap(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
targetFQDN, err := nodeOrServiceDNSNameFromArg(st, nm, hostOrFQDNOrIP)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
targetFQDN = strings.TrimSuffix(targetFQDN, ".")
|
|
var kubeconfig string
|
|
if kubeconfig, err = kubeconfigPath(); err != nil {
|
|
return err
|
|
}
|
|
scheme := "https://"
|
|
if http {
|
|
scheme = "http://"
|
|
}
|
|
if err = setKubeconfigForPeer(scheme, targetFQDN, kubeconfig); err != nil {
|
|
return err
|
|
}
|
|
printf("kubeconfig configured for %q at URL %q\n", targetFQDN, scheme+targetFQDN)
|
|
return nil
|
|
}
|
|
|
|
func getInputs(arg string, httpArg bool) (string, bool, error) {
|
|
u, err := url.Parse(arg)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
return u.Host, u.Scheme == "http", nil
|
|
default:
|
|
return arg, httpArg, nil
|
|
}
|
|
}
|
|
|
|
// appendOrSetNamed finds a map with a "name" key matching name in dst, and
|
|
// replaces it with val. If no such map is found, val is appended to dst.
|
|
func appendOrSetNamed(dst []any, name string, val map[string]any) []any {
|
|
if got := slices.IndexFunc(dst, func(m any) bool {
|
|
if m, ok := m.(map[string]any); ok {
|
|
return m["name"] == name
|
|
}
|
|
return false
|
|
}); got != -1 {
|
|
dst[got] = val
|
|
} else {
|
|
dst = append(dst, val)
|
|
}
|
|
return dst
|
|
}
|
|
|
|
var errInvalidKubeconfig = errors.New("invalid kubeconfig")
|
|
|
|
func updateKubeconfig(cfgYaml []byte, scheme, fqdn string) ([]byte, error) {
|
|
var cfg map[string]any
|
|
if len(cfgYaml) > 0 {
|
|
if err := yaml.Unmarshal(cfgYaml, &cfg); err != nil {
|
|
return nil, errInvalidKubeconfig
|
|
}
|
|
}
|
|
if cfg == nil {
|
|
cfg = map[string]any{
|
|
"apiVersion": "v1",
|
|
"kind": "Config",
|
|
}
|
|
} else if cfg["apiVersion"] != "v1" || cfg["kind"] != "Config" {
|
|
return nil, errInvalidKubeconfig
|
|
}
|
|
|
|
var clusters []any
|
|
if cm, ok := cfg["clusters"]; ok {
|
|
clusters, _ = cm.([]any)
|
|
}
|
|
cfg["clusters"] = appendOrSetNamed(clusters, fqdn, map[string]any{
|
|
"name": fqdn,
|
|
"cluster": map[string]string{
|
|
"server": scheme + fqdn,
|
|
},
|
|
})
|
|
|
|
var users []any
|
|
if um, ok := cfg["users"]; ok {
|
|
users, _ = um.([]any)
|
|
}
|
|
cfg["users"] = appendOrSetNamed(users, "tailscale-auth", map[string]any{
|
|
// We just need one of these, and can reuse it for all clusters.
|
|
"name": "tailscale-auth",
|
|
"user": map[string]string{
|
|
// We do not use the token, but if we do not set anything here
|
|
// kubectl will prompt for a username and password.
|
|
"token": "unused",
|
|
},
|
|
})
|
|
|
|
var contexts []any
|
|
if cm, ok := cfg["contexts"]; ok {
|
|
contexts, _ = cm.([]any)
|
|
}
|
|
cfg["contexts"] = appendOrSetNamed(contexts, fqdn, map[string]any{
|
|
"name": fqdn,
|
|
"context": map[string]string{
|
|
"cluster": fqdn,
|
|
"user": "tailscale-auth",
|
|
},
|
|
})
|
|
cfg["current-context"] = fqdn
|
|
return yaml.Marshal(cfg)
|
|
}
|
|
|
|
func setKubeconfigForPeer(scheme, fqdn, filePath string) error {
|
|
dir := filepath.Dir(filePath)
|
|
if _, err := os.Stat(dir); err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
if err := os.Mkdir(dir, 0755); err != nil {
|
|
if version.IsSandboxedMacOS() && errors.Is(err, os.ErrPermission) {
|
|
// macOS sandboxing prevents us from creating the .kube directory
|
|
// in the home directory.
|
|
return errors.New("unable to create .kube directory in home directory, please create it manually (e.g. mkdir ~/.kube")
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
b, err := os.ReadFile(filePath)
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("reading kubeconfig: %w", err)
|
|
}
|
|
b, err = updateKubeconfig(b, scheme, fqdn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(filePath, b, 0600)
|
|
}
|
|
|
|
// nodeOrServiceDNSNameFromArg returns the PeerStatus.DNSName value from a peer
|
|
// in st that matches the input arg which can be a base name, full DNS name, or
|
|
// an IP. If none is found, it looks for a Tailscale Service
|
|
func nodeOrServiceDNSNameFromArg(st *ipnstate.Status, nm *netmap.NetworkMap, arg string) (string, error) {
|
|
// First check for a node DNS name.
|
|
if dnsName, ok := nodeDNSNameFromArg(st, arg); ok {
|
|
return dnsName, nil
|
|
}
|
|
|
|
// If not found, check for a Tailscale Service DNS name.
|
|
rec, ok := serviceDNSRecordFromNetMap(nm, arg)
|
|
if !ok {
|
|
return "", fmt.Errorf("no peer found for %q", arg)
|
|
}
|
|
|
|
// Validate we can see a peer advertising the Tailscale Service.
|
|
ip, err := netip.ParseAddr(rec.Value)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error parsing ExtraRecord IP address %q: %w", rec.Value, err)
|
|
}
|
|
ipPrefix := netip.PrefixFrom(ip, ip.BitLen())
|
|
for _, ps := range st.Peer {
|
|
for _, allowedIP := range ps.AllowedIPs.All() {
|
|
if allowedIP == ipPrefix {
|
|
return rec.Name, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return "", fmt.Errorf("%q is in MagicDNS, but is not currently reachable on any known peer", arg)
|
|
}
|
|
|
|
func getNetMap(ctx context.Context) (*netmap.NetworkMap, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
|
|
watcher, err := localClient.WatchIPNBus(ctx, ipn.NotifyInitialNetMap)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer watcher.Close()
|
|
|
|
n, err := watcher.Next()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return n.NetMap, nil
|
|
}
|
|
|
|
func serviceDNSRecordFromNetMap(nm *netmap.NetworkMap, arg string) (rec tailcfg.DNSRecord, ok bool) {
|
|
argIP, _ := netip.ParseAddr(arg)
|
|
argFQDN, err := dnsname.ToFQDN(arg)
|
|
argFQDNValid := err == nil
|
|
if !argIP.IsValid() && !argFQDNValid {
|
|
return rec, false
|
|
}
|
|
|
|
for _, rec := range nm.DNS.ExtraRecords {
|
|
if argIP.IsValid() {
|
|
recIP, _ := netip.ParseAddr(rec.Value)
|
|
if recIP == argIP {
|
|
return rec, true
|
|
}
|
|
continue
|
|
}
|
|
|
|
if !argFQDNValid {
|
|
continue
|
|
}
|
|
|
|
recFirstLabel := dnsname.FirstLabel(rec.Name)
|
|
if strings.EqualFold(arg, recFirstLabel) {
|
|
return rec, true
|
|
}
|
|
|
|
recFQDN, err := dnsname.ToFQDN(rec.Name)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if strings.EqualFold(argFQDN.WithTrailingDot(), recFQDN.WithTrailingDot()) {
|
|
return rec, true
|
|
}
|
|
}
|
|
|
|
return tailcfg.DNSRecord{}, false
|
|
}
|