mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
352 lines
10 KiB
Go
352 lines
10 KiB
Go
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
3 years ago
|
|
||
3 months ago
|
// Package kubeclient provides a client to interact with Kubernetes.
|
||
3 years ago
|
// This package is Tailscale-internal and not meant for external consumption.
|
||
|
// Further, the API should not be considered stable.
|
||
3 months ago
|
// Client is split into a separate package for consumption of
|
||
|
// non-Kubernetes shared libraries and binaries. Be mindful of not increasing
|
||
|
// dependency size for those consumers when adding anything new here.
|
||
3 months ago
|
package kubeclient
|
||
3 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"context"
|
||
|
"crypto/tls"
|
||
|
"crypto/x509"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
2 years ago
|
"log"
|
||
2 years ago
|
"net"
|
||
3 years ago
|
"net/http"
|
||
2 years ago
|
"net/url"
|
||
3 years ago
|
"os"
|
||
|
"path/filepath"
|
||
|
"sync"
|
||
|
"time"
|
||
2 years ago
|
|
||
3 months ago
|
"tailscale.com/kube/kubeapi"
|
||
2 years ago
|
"tailscale.com/util/multierr"
|
||
3 years ago
|
)
|
||
|
|
||
|
const (
|
||
|
saPath = "/var/run/secrets/kubernetes.io/serviceaccount"
|
||
|
defaultURL = "https://kubernetes.default.svc"
|
||
|
)
|
||
|
|
||
2 years ago
|
// rootPathForTests is set by tests to override the root path to the
|
||
|
// service account directory.
|
||
|
var rootPathForTests string
|
||
|
|
||
|
// SetRootPathForTesting sets the path to the service account directory.
|
||
|
func SetRootPathForTesting(p string) {
|
||
|
rootPathForTests = p
|
||
|
}
|
||
|
|
||
3 years ago
|
func readFile(n string) ([]byte, error) {
|
||
2 years ago
|
if rootPathForTests != "" {
|
||
|
return os.ReadFile(filepath.Join(rootPathForTests, saPath, n))
|
||
|
}
|
||
3 years ago
|
return os.ReadFile(filepath.Join(saPath, n))
|
||
|
}
|
||
|
|
||
|
// Client handles connections to Kubernetes.
|
||
|
// It expects to be run inside a cluster.
|
||
7 months ago
|
type Client interface {
|
||
3 months ago
|
GetSecret(context.Context, string) (*kubeapi.Secret, error)
|
||
|
UpdateSecret(context.Context, *kubeapi.Secret) error
|
||
|
CreateSecret(context.Context, *kubeapi.Secret) error
|
||
|
StrategicMergePatchSecret(context.Context, string, *kubeapi.Secret, string) error
|
||
7 months ago
|
JSONPatchSecret(context.Context, string, []JSONPatch) error
|
||
|
CheckSecretPermissions(context.Context, string) (bool, bool, error)
|
||
|
SetDialer(dialer func(context.Context, string, string) (net.Conn, error))
|
||
|
SetURL(string)
|
||
|
}
|
||
|
|
||
|
type client struct {
|
||
3 years ago
|
mu sync.Mutex
|
||
|
url string
|
||
|
ns string
|
||
|
client *http.Client
|
||
|
token string
|
||
|
tokenExpiry time.Time
|
||
|
}
|
||
|
|
||
|
// New returns a new client
|
||
7 months ago
|
func New() (Client, error) {
|
||
3 years ago
|
ns, err := readFile("namespace")
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
caCert, err := readFile("ca.crt")
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
cp := x509.NewCertPool()
|
||
|
if ok := cp.AppendCertsFromPEM(caCert); !ok {
|
||
|
return nil, fmt.Errorf("kube: error in creating root cert pool")
|
||
|
}
|
||
7 months ago
|
return &client{
|
||
3 years ago
|
url: defaultURL,
|
||
|
ns: string(ns),
|
||
|
client: &http.Client{
|
||
|
Transport: &http.Transport{
|
||
|
TLSClientConfig: &tls.Config{
|
||
|
RootCAs: cp,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
// SetURL sets the URL to use for the Kubernetes API.
|
||
|
// This is used only for testing.
|
||
7 months ago
|
func (c *client) SetURL(url string) {
|
||
2 years ago
|
c.url = url
|
||
|
}
|
||
|
|
||
2 years ago
|
// SetDialer sets the dialer to use when establishing a connection
|
||
|
// to the Kubernetes API server.
|
||
7 months ago
|
func (c *client) SetDialer(dialer func(ctx context.Context, network, addr string) (net.Conn, error)) {
|
||
2 years ago
|
c.client.Transport.(*http.Transport).DialContext = dialer
|
||
|
}
|
||
|
|
||
7 months ago
|
func (c *client) expireToken() {
|
||
3 years ago
|
c.mu.Lock()
|
||
|
defer c.mu.Unlock()
|
||
|
c.tokenExpiry = time.Now()
|
||
|
}
|
||
|
|
||
7 months ago
|
func (c *client) getOrRenewToken() (string, error) {
|
||
3 years ago
|
c.mu.Lock()
|
||
|
defer c.mu.Unlock()
|
||
|
tk, te := c.token, c.tokenExpiry
|
||
|
if time.Now().Before(te) {
|
||
|
return tk, nil
|
||
|
}
|
||
|
|
||
|
tkb, err := readFile("token")
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
c.token = string(tkb)
|
||
|
c.tokenExpiry = time.Now().Add(30 * time.Minute)
|
||
|
return c.token, nil
|
||
|
}
|
||
|
|
||
7 months ago
|
func (c *client) secretURL(name string) string {
|
||
3 years ago
|
if name == "" {
|
||
|
return fmt.Sprintf("%s/api/v1/namespaces/%s/secrets", c.url, c.ns)
|
||
|
}
|
||
|
return fmt.Sprintf("%s/api/v1/namespaces/%s/secrets/%s", c.url, c.ns, name)
|
||
|
}
|
||
|
|
||
|
func getError(resp *http.Response) error {
|
||
2 years ago
|
if resp.StatusCode == 200 || resp.StatusCode == 201 {
|
||
|
// These are the only success codes returned by the Kubernetes API.
|
||
|
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#http-status-codes
|
||
3 years ago
|
return nil
|
||
|
}
|
||
3 months ago
|
st := &kubeapi.Status{}
|
||
3 years ago
|
if err := json.NewDecoder(resp.Body).Decode(st); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return st
|
||
|
}
|
||
|
|
||
2 years ago
|
func setHeader(key, value string) func(*http.Request) {
|
||
|
return func(req *http.Request) {
|
||
|
req.Header.Set(key, value)
|
||
3 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
// doRequest performs an HTTP request to the Kubernetes API.
|
||
|
// If in is not nil, it is expected to be a JSON-encodable object and will be
|
||
|
// sent as the request body.
|
||
|
// If out is not nil, it is expected to be a pointer to an object that can be
|
||
|
// decoded from JSON.
|
||
|
// If the request fails with a 401, the token is expired and a new one is
|
||
|
// requested.
|
||
7 months ago
|
func (c *client) doRequest(ctx context.Context, method, url string, in, out any, opts ...func(*http.Request)) error {
|
||
2 years ago
|
req, err := c.newRequest(ctx, method, url, in)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
2 years ago
|
for _, opt := range opts {
|
||
|
opt(req)
|
||
3 years ago
|
}
|
||
|
resp, err := c.client.Do(req)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
if err := getError(resp); err != nil {
|
||
3 months ago
|
if st, ok := err.(*kubeapi.Status); ok && st.Code == 401 {
|
||
3 years ago
|
c.expireToken()
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
if out != nil {
|
||
|
return json.NewDecoder(resp.Body).Decode(out)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
7 months ago
|
func (c *client) newRequest(ctx context.Context, method, url string, in any) (*http.Request, error) {
|
||
2 years ago
|
tk, err := c.getOrRenewToken()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
var body io.Reader
|
||
|
if in != nil {
|
||
|
switch in := in.(type) {
|
||
|
case []byte:
|
||
|
body = bytes.NewReader(in)
|
||
|
default:
|
||
|
var b bytes.Buffer
|
||
|
if err := json.NewEncoder(&b).Encode(in); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
body = &b
|
||
|
}
|
||
|
}
|
||
|
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if body != nil {
|
||
|
req.Header.Add("Content-Type", "application/json")
|
||
|
}
|
||
|
req.Header.Add("Accept", "application/json")
|
||
|
req.Header.Add("Authorization", "Bearer "+tk)
|
||
|
return req, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetSecret fetches the secret from the Kubernetes API.
|
||
3 months ago
|
func (c *client) GetSecret(ctx context.Context, name string) (*kubeapi.Secret, error) {
|
||
|
s := &kubeapi.Secret{Data: make(map[string][]byte)}
|
||
3 years ago
|
if err := c.doRequest(ctx, "GET", c.secretURL(name), nil, s); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return s, nil
|
||
|
}
|
||
|
|
||
|
// CreateSecret creates a secret in the Kubernetes API.
|
||
3 months ago
|
func (c *client) CreateSecret(ctx context.Context, s *kubeapi.Secret) error {
|
||
3 years ago
|
s.Namespace = c.ns
|
||
|
return c.doRequest(ctx, "POST", c.secretURL(""), s, nil)
|
||
|
}
|
||
|
|
||
|
// UpdateSecret updates a secret in the Kubernetes API.
|
||
3 months ago
|
func (c *client) UpdateSecret(ctx context.Context, s *kubeapi.Secret) error {
|
||
3 years ago
|
return c.doRequest(ctx, "PUT", c.secretURL(s.Name), s, nil)
|
||
|
}
|
||
2 years ago
|
|
||
|
// JSONPatch is a JSON patch operation.
|
||
7 months ago
|
// It currently (2023-03-02) only supports "add" and "remove" operations.
|
||
2 years ago
|
//
|
||
|
// https://tools.ietf.org/html/rfc6902
|
||
|
type JSONPatch struct {
|
||
1 year ago
|
Op string `json:"op"`
|
||
|
Path string `json:"path"`
|
||
|
Value any `json:"value,omitempty"`
|
||
2 years ago
|
}
|
||
|
|
||
|
// JSONPatchSecret updates a secret in the Kubernetes API using a JSON patch.
|
||
7 months ago
|
// It currently (2023-03-02) only supports "add" and "remove" operations.
|
||
|
func (c *client) JSONPatchSecret(ctx context.Context, name string, patch []JSONPatch) error {
|
||
2 years ago
|
for _, p := range patch {
|
||
1 year ago
|
if p.Op != "remove" && p.Op != "add" {
|
||
2 years ago
|
panic(fmt.Errorf("unsupported JSON patch operation: %q", p.Op))
|
||
|
}
|
||
|
}
|
||
|
return c.doRequest(ctx, "PATCH", c.secretURL(name), patch, nil, setHeader("Content-Type", "application/json-patch+json"))
|
||
|
}
|
||
|
|
||
|
// StrategicMergePatchSecret updates a secret in the Kubernetes API using a
|
||
|
// strategic merge patch.
|
||
|
// If a fieldManager is provided, it will be used to track the patch.
|
||
3 months ago
|
func (c *client) StrategicMergePatchSecret(ctx context.Context, name string, s *kubeapi.Secret, fieldManager string) error {
|
||
2 years ago
|
surl := c.secretURL(name)
|
||
|
if fieldManager != "" {
|
||
|
uv := url.Values{
|
||
|
"fieldManager": {fieldManager},
|
||
|
}
|
||
|
surl += "?" + uv.Encode()
|
||
|
}
|
||
|
s.Namespace = c.ns
|
||
|
s.Name = name
|
||
|
return c.doRequest(ctx, "PATCH", surl, s, nil, setHeader("Content-Type", "application/strategic-merge-patch+json"))
|
||
|
}
|
||
|
|
||
|
// CheckSecretPermissions checks the secret access permissions of the current
|
||
|
// pod. It returns an error if the basic permissions tailscale needs are
|
||
7 months ago
|
// missing, and reports whether the patch and create permissions are additionally present.
|
||
2 years ago
|
//
|
||
|
// Errors encountered during the access checking process are logged, but ignored
|
||
|
// so that the pod tries to fail alive if the permissions exist and there's just
|
||
|
// something wrong with SelfSubjectAccessReviews. There shouldn't be, pods
|
||
|
// should always be able to use SSARs to assess their own permissions, but since
|
||
|
// we didn't use to check permissions this way we'll be cautious in case some
|
||
|
// old version of k8s deviates from the current behavior.
|
||
7 months ago
|
func (c *client) CheckSecretPermissions(ctx context.Context, secretName string) (canPatch, canCreate bool, err error) {
|
||
2 years ago
|
var errs []error
|
||
|
for _, verb := range []string{"get", "update"} {
|
||
|
ok, err := c.checkPermission(ctx, verb, secretName)
|
||
|
if err != nil {
|
||
|
log.Printf("error checking %s permission on secret %s: %v", verb, secretName, err)
|
||
|
} else if !ok {
|
||
|
errs = append(errs, fmt.Errorf("missing %s permission on secret %q", verb, secretName))
|
||
|
}
|
||
|
}
|
||
|
if len(errs) > 0 {
|
||
7 months ago
|
return false, false, multierr.New(errs...)
|
||
2 years ago
|
}
|
||
7 months ago
|
canPatch, err = c.checkPermission(ctx, "patch", secretName)
|
||
2 years ago
|
if err != nil {
|
||
|
log.Printf("error checking patch permission on secret %s: %v", secretName, err)
|
||
7 months ago
|
return false, false, nil
|
||
|
}
|
||
|
canCreate, err = c.checkPermission(ctx, "create", secretName)
|
||
|
if err != nil {
|
||
|
log.Printf("error checking create permission on secret %s: %v", secretName, err)
|
||
|
return false, false, nil
|
||
2 years ago
|
}
|
||
7 months ago
|
return canPatch, canCreate, nil
|
||
2 years ago
|
}
|
||
|
|
||
|
// checkPermission reports whether the current pod has permission to use the
|
||
7 months ago
|
// given verb (e.g. get, update, patch, create) on secretName.
|
||
|
func (c *client) checkPermission(ctx context.Context, verb, secretName string) (bool, error) {
|
||
2 years ago
|
sar := map[string]any{
|
||
|
"apiVersion": "authorization.k8s.io/v1",
|
||
|
"kind": "SelfSubjectAccessReview",
|
||
|
"spec": map[string]any{
|
||
|
"resourceAttributes": map[string]any{
|
||
|
"namespace": c.ns,
|
||
|
"verb": verb,
|
||
|
"resource": "secrets",
|
||
|
"name": secretName,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
var res struct {
|
||
|
Status struct {
|
||
|
Allowed bool `json:"allowed"`
|
||
|
} `json:"status"`
|
||
|
}
|
||
|
url := c.url + "/apis/authorization.k8s.io/v1/selfsubjectaccessreviews"
|
||
|
if err := c.doRequest(ctx, "POST", url, sar, &res); err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
return res.Status.Allowed, nil
|
||
|
}
|
||
7 months ago
|
|
||
|
func IsNotFoundErr(err error) bool {
|
||
3 months ago
|
if st, ok := err.(*kubeapi.Status); ok && st.Code == 404 {
|
||
7 months ago
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|