mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
tailscaled only learns its device ID, and so only writes it to its state Secret, once it gets its first netmap. If it is shut down after it registered with control but before that, there is no ID to delete its device by. tailscaled does write its machine key to the state Secret before it registers. This adds EnsureUnreportedDeviceDeleted, which looks up the device by that machine key and deletes it, for reconcilers to use when a state Secret has no device ID. Updates #18922 Signed-off-by: chaosinthecrd <[email protected]>
114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build !plan9
|
|
|
|
package tailscaled
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"go.uber.org/zap"
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
tailscaleclient "tailscale.com/client/tailscale/v2"
|
|
|
|
"tailscale.com/ipn"
|
|
"tailscale.com/k8s-operator/tsclient"
|
|
"tailscale.com/tailcfg"
|
|
"tailscale.com/types/key"
|
|
)
|
|
|
|
// ClientProvider returns a Tailscale API client for the given tailnet name. A blank name should return the
|
|
// operator's default client.
|
|
type ClientProvider interface {
|
|
For(tailnet string) (tsclient.Client, error)
|
|
}
|
|
|
|
// NewAuthKey mints a single-use, preauthorized tailnet auth key with the given tags. The key is intended for one
|
|
// tailscaled pod to consume on first startup; callers should not persist or share it.
|
|
func NewAuthKey(ctx context.Context, client tsclient.Client, tags []string) (string, error) {
|
|
var caps tailscaleclient.KeyCapabilities
|
|
caps.Devices.Create.Reusable = false
|
|
caps.Devices.Create.Preauthorized = true
|
|
caps.Devices.Create.Tags = tags
|
|
|
|
key, err := client.Keys().CreateAuthKey(ctx, tailscaleclient.CreateKeyRequest{Capabilities: caps})
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to create auth key: %w", err)
|
|
}
|
|
return key.Key, nil
|
|
}
|
|
|
|
// EnsureDeviceDeleted deletes the tailnet device with the given stable node ID from control, treating a
|
|
// not-found response as success (the device was already gone).
|
|
func EnsureDeviceDeleted(ctx context.Context, client tsclient.Client, logger *zap.SugaredLogger, id tailcfg.StableNodeID) error {
|
|
logger.Debugf("deleting device %s from control", string(id))
|
|
err := client.Devices().Delete(ctx, string(id))
|
|
switch {
|
|
case tailscaleclient.IsNotFound(err):
|
|
logger.Debugf("device %s not found, likely because it has already been deleted from control", string(id))
|
|
case err != nil:
|
|
return fmt.Errorf("error deleting device: %w", err)
|
|
default:
|
|
logger.Debugf("device %s deleted from control", string(id))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EnsureUnreportedDeviceDeleted deletes the device registered with the machine key in state Secret secret, for a
|
|
// tailscaled that registered with control but was shut down before it received a netmap, so it never learned its
|
|
// device ID to write to the state Secret.
|
|
func EnsureUnreportedDeviceDeleted(ctx context.Context, client tsclient.Client, logger *zap.SugaredLogger, secret *corev1.Secret) error {
|
|
mkText := secret.Data[string(ipn.MachineKeyStateKey)]
|
|
// tailscaled writes its machine key before registering, so no key means no device.
|
|
if len(mkText) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var mk key.MachinePrivate
|
|
if err := mk.UnmarshalText(mkText); err != nil {
|
|
// tailscaled refuses to start with an invalid machine key, so it cannot have registered a device with it.
|
|
logger.Warnf("failed to parse machine key in state Secret %q, skipping: %v", secret.Name, err)
|
|
return nil
|
|
}
|
|
|
|
mkPub := mk.Public().String()
|
|
devices, err := client.Devices().List(ctx, tailscaleclient.WithFilter("machineKey", []string{mkPub}))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to list devices: %w", err)
|
|
}
|
|
|
|
for _, dev := range devices {
|
|
// Control ignores filters it does not support, so check the key rather than trusting the filter.
|
|
if dev.MachineKey != mkPub {
|
|
continue
|
|
}
|
|
|
|
logger.Infof("deleting device %q that registered but never reported its device ID", dev.NodeID)
|
|
if err := client.Devices().Delete(ctx, dev.NodeID); err != nil && !tailscaleclient.IsNotFound(err) {
|
|
return fmt.Errorf("failed to delete device %q: %w", dev.NodeID, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// AuthKeyFromConfigSecret returns the auth key embedded in the tailscaled config file stored in secret, or nil if
|
|
// none is set. secret is expected to be a Secret produced by NewConfigSecret. The Data map may contain multiple
|
|
// versioned config files (cap-<n>.hujson); the first one to parse successfully and yield a non-empty AuthKey wins.
|
|
func AuthKeyFromConfigSecret(secret *corev1.Secret) *string {
|
|
for _, body := range secret.Data {
|
|
var conf ipn.ConfigVAlpha
|
|
if err := json.Unmarshal(body, &conf); err != nil {
|
|
continue
|
|
}
|
|
if conf.AuthKey != nil && *conf.AuthKey != "" {
|
|
return conf.AuthKey
|
|
}
|
|
}
|
|
return nil
|
|
}
|