2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-10-25 21:12:54 +01:00
|
|
|
|
|
|
|
//go:build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-12-07 01:03:53 +00:00
|
|
|
|
2023-03-02 16:10:36 +00:00
|
|
|
"tailscale.com/kube"
|
2022-12-07 20:29:45 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-10-25 21:12:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// findKeyInKubeSecret inspects the kube secret secretName for a data
|
|
|
|
// field called "authkey", and returns its value if present.
|
|
|
|
func findKeyInKubeSecret(ctx context.Context, secretName string) (string, error) {
|
2023-03-02 16:10:36 +00:00
|
|
|
s, err := kc.GetSecret(ctx, secretName)
|
2022-10-25 21:12:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-03-02 16:10:36 +00:00
|
|
|
ak, ok := s.Data["authkey"]
|
|
|
|
if !ok {
|
|
|
|
return "", nil
|
2022-10-25 21:12:54 +01:00
|
|
|
}
|
2023-03-02 16:10:36 +00:00
|
|
|
return string(ak), nil
|
2022-10-25 21:12:54 +01:00
|
|
|
}
|
|
|
|
|
2022-12-07 20:29:45 +00:00
|
|
|
// storeDeviceInfo writes deviceID into the "device_id" data field of the kube
|
|
|
|
// secret secretName.
|
|
|
|
func storeDeviceInfo(ctx context.Context, secretName string, deviceID tailcfg.StableNodeID, fqdn string) error {
|
2022-11-07 17:24:42 +00:00
|
|
|
// First check if the secret exists at all. Even if running on
|
|
|
|
// kubernetes, we do not necessarily store state in a k8s secret.
|
2023-03-02 16:10:36 +00:00
|
|
|
if _, err := kc.GetSecret(ctx, secretName); err != nil {
|
|
|
|
if s, ok := err.(*kube.Status); ok {
|
|
|
|
if s.Code >= 400 && s.Code <= 499 {
|
|
|
|
// Assume the secret doesn't exist, or we don't have
|
|
|
|
// permission to access it.
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-07 17:24:42 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:10:36 +00:00
|
|
|
m := &kube.Secret{
|
|
|
|
Data: map[string][]byte{
|
|
|
|
"device_id": []byte(deviceID),
|
|
|
|
"device_fqdn": []byte(fqdn),
|
2022-10-25 21:12:54 +01:00
|
|
|
},
|
|
|
|
}
|
2023-03-02 16:10:36 +00:00
|
|
|
return kc.StrategicMergePatchSecret(ctx, secretName, m, "tailscale-container")
|
2022-10-25 21:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteAuthKey deletes the 'authkey' field of the given kube
|
|
|
|
// secret. No-op if there is no authkey in the secret.
|
|
|
|
func deleteAuthKey(ctx context.Context, secretName string) error {
|
|
|
|
// m is a JSON Patch data structure, see https://jsonpatch.com/ or RFC 6902.
|
2023-03-02 16:10:36 +00:00
|
|
|
m := []kube.JSONPatch{
|
2022-10-25 21:12:54 +01:00
|
|
|
{
|
|
|
|
Op: "remove",
|
|
|
|
Path: "/data/authkey",
|
|
|
|
},
|
|
|
|
}
|
2023-03-02 16:10:36 +00:00
|
|
|
if err := kc.JSONPatchSecret(ctx, secretName, m); err != nil {
|
|
|
|
if s, ok := err.(*kube.Status); ok && s.Code == http.StatusUnprocessableEntity {
|
2022-10-25 21:12:54 +01:00
|
|
|
// This is kubernetes-ese for "the field you asked to
|
|
|
|
// delete already doesn't exist", aka no-op.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:10:36 +00:00
|
|
|
var kc *kube.Client
|
2022-10-25 21:12:54 +01:00
|
|
|
|
2022-11-10 06:01:34 +00:00
|
|
|
func initKube(root string) {
|
2023-03-02 16:10:36 +00:00
|
|
|
if root != "/" {
|
|
|
|
// If we are running in a test, we need to set the root path to the fake
|
|
|
|
// service account directory.
|
|
|
|
kube.SetRootPathForTesting(root)
|
2022-10-25 21:12:54 +01:00
|
|
|
}
|
2023-03-02 16:10:36 +00:00
|
|
|
var err error
|
|
|
|
kc, err = kube.New()
|
2022-10-25 21:12:54 +01:00
|
|
|
if err != nil {
|
2023-03-02 16:10:36 +00:00
|
|
|
log.Fatalf("Error creating kube client: %v", err)
|
2022-10-25 21:12:54 +01:00
|
|
|
}
|
2023-03-02 16:10:36 +00:00
|
|
|
if root != "/" {
|
|
|
|
// If we are running in a test, we need to set the URL to the
|
|
|
|
// httptest server.
|
|
|
|
kc.SetURL(fmt.Sprintf("https://%s:%s", os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT_HTTPS")))
|
2022-10-25 21:12:54 +01:00
|
|
|
}
|
|
|
|
}
|