Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 17 | // package pki builds upon metropolis/pkg/pki/ to provide an |
| 18 | // etcd-backed implementation of all x509 PKI Certificates/CAs required to run |
| 19 | // Kubernetes. |
| 20 | // Most elements of the PKI are 'static' long-standing certificates/credentials |
| 21 | // stored within etcd. However, this package also provides a method to generate |
| 22 | // 'volatile' (in-memory) certificates/credentials for per-node Kubelets and |
| 23 | // any client certificates. |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 24 | package pki |
| 25 | |
| 26 | import ( |
| 27 | "context" |
| 28 | "crypto/rand" |
| 29 | "crypto/rsa" |
| 30 | "crypto/x509" |
| 31 | "encoding/pem" |
| 32 | "fmt" |
| 33 | "net" |
| 34 | |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 35 | "go.etcd.io/etcd/clientv3" |
| 36 | "k8s.io/client-go/tools/clientcmd" |
| 37 | configapi "k8s.io/client-go/tools/clientcmd/api" |
| 38 | |
Serge Bazanski | 31370b0 | 2021-01-07 16:31:14 +0100 | [diff] [blame] | 39 | common "source.monogon.dev/metropolis/node" |
| 40 | "source.monogon.dev/metropolis/pkg/logtree" |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 41 | opki "source.monogon.dev/metropolis/pkg/pki" |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 42 | ) |
| 43 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 44 | // KubeCertificateName is an enum-like unique name of a static Kubernetes |
| 45 | // certificate. The value of the name is used as the unique part of an etcd |
| 46 | // path where the certificate and key are stored. |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 47 | type KubeCertificateName string |
| 48 | |
| 49 | const ( |
| 50 | // The main Kubernetes CA, used to authenticate API consumers, and servers. |
| 51 | IdCA KubeCertificateName = "id-ca" |
| 52 | |
| 53 | // Kubernetes apiserver server certificate. |
| 54 | APIServer KubeCertificateName = "apiserver" |
| 55 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 56 | // APIServer client certificate used to authenticate to kubelets. |
| 57 | APIServerKubeletClient KubeCertificateName = "apiserver-kubelet-client" |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 58 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 59 | // Kubernetes Controller manager client certificate, used to authenticate |
| 60 | // to the apiserver. |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 61 | ControllerManagerClient KubeCertificateName = "controller-manager-client" |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 62 | // Kubernetes Controller manager server certificate, used to run its HTTP |
| 63 | // server. |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 64 | ControllerManager KubeCertificateName = "controller-manager" |
| 65 | |
| 66 | // Kubernetes Scheduler client certificate, used to authenticate to the apiserver. |
| 67 | SchedulerClient KubeCertificateName = "scheduler-client" |
| 68 | // Kubernetes scheduler server certificate, used to run its HTTP server. |
| 69 | Scheduler KubeCertificateName = "scheduler" |
| 70 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 71 | // Root-on-kube (system:masters) client certificate. Used to control the |
| 72 | // apiserver (and resources) by Metropolis internally. |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 73 | Master KubeCertificateName = "master" |
| 74 | |
| 75 | // OpenAPI Kubernetes Aggregation CA. |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 76 | // https://kubernetes.io/docs/tasks/extend-kubernetes/configure-aggregation-layer/#ca-reusage-and-conflicts |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 77 | AggregationCA KubeCertificateName = "aggregation-ca" |
| 78 | FrontProxyClient KubeCertificateName = "front-proxy-client" |
| 79 | ) |
| 80 | |
| 81 | const ( |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 82 | // etcdPrefix is where all the PKI data is stored in etcd. |
| 83 | etcdPrefix = "/kube-pki/" |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 84 | // serviceAccountKeyName is the etcd path part that is used to store the |
| 85 | // ServiceAccount authentication secret. This is not a certificate, just an |
| 86 | // RSA key. |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 87 | serviceAccountKeyName = "service-account-privkey" |
| 88 | ) |
| 89 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 90 | // PKI manages all PKI resources required to run Kubernetes on Metropolis. It |
| 91 | // contains all static certificates, which can be retrieved, or be used to |
| 92 | // generate Kubeconfigs from. |
| 93 | type PKI struct { |
| 94 | namespace opki.Namespace |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 95 | logger logtree.LeveledLogger |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 96 | KV clientv3.KV |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 97 | Certificates map[KubeCertificateName]*opki.Certificate |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 100 | func New(l logtree.LeveledLogger, kv clientv3.KV) *PKI { |
| 101 | pki := PKI{ |
| 102 | namespace: opki.Namespaced(etcdPrefix), |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 103 | logger: l, |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 104 | KV: kv, |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 105 | Certificates: make(map[KubeCertificateName]*opki.Certificate), |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | make := func(i, name KubeCertificateName, template x509.Certificate) { |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 109 | pki.Certificates[name] = pki.namespace.New(pki.Certificates[i], string(name), template) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 112 | pki.Certificates[IdCA] = pki.namespace.New(opki.SelfSigned, string(IdCA), opki.CA("Metropolis Kubernetes ID CA")) |
| 113 | make(IdCA, APIServer, opki.Server( |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 114 | []string{ |
| 115 | "kubernetes", |
| 116 | "kubernetes.default", |
| 117 | "kubernetes.default.svc", |
| 118 | "kubernetes.default.svc.cluster", |
| 119 | "kubernetes.default.svc.cluster.local", |
| 120 | "localhost", |
| 121 | }, |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 122 | // TODO(q3k): add service network internal apiserver address |
| 123 | []net.IP{{10, 0, 255, 1}, {127, 0, 0, 1}}, |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 124 | )) |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 125 | make(IdCA, APIServerKubeletClient, opki.Client("metropolis:apiserver-kubelet-client", nil)) |
| 126 | make(IdCA, ControllerManagerClient, opki.Client("system:kube-controller-manager", nil)) |
| 127 | make(IdCA, ControllerManager, opki.Server([]string{"kube-controller-manager.local"}, nil)) |
| 128 | make(IdCA, SchedulerClient, opki.Client("system:kube-scheduler", nil)) |
| 129 | make(IdCA, Scheduler, opki.Server([]string{"kube-scheduler.local"}, nil)) |
| 130 | make(IdCA, Master, opki.Client("metropolis:master", []string{"system:masters"})) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 131 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 132 | pki.Certificates[AggregationCA] = pki.namespace.New(opki.SelfSigned, string(AggregationCA), opki.CA("Metropolis OpenAPI Aggregation CA")) |
| 133 | make(AggregationCA, FrontProxyClient, opki.Client("front-proxy-client", nil)) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 134 | |
| 135 | return &pki |
| 136 | } |
| 137 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 138 | // EnsureAll ensures that all static certificates (and the serviceaccount key) |
| 139 | // are present on etcd. |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 140 | func (k *PKI) EnsureAll(ctx context.Context) error { |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 141 | for n, v := range k.Certificates { |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 142 | k.logger.Infof("Ensuring %s exists", string(n)) |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 143 | _, _, err := v.Ensure(ctx, k.KV) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 144 | if err != nil { |
| 145 | return fmt.Errorf("could not ensure certificate %q exists: %w", n, err) |
| 146 | } |
| 147 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 148 | _, err := k.ServiceAccountKey(ctx) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 149 | if err != nil { |
| 150 | return fmt.Errorf("could not ensure service account key exists: %w", err) |
| 151 | } |
| 152 | return nil |
| 153 | } |
| 154 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 155 | // Kubeconfig generates a kubeconfig blob for a given certificate name. The |
| 156 | // same lifetime semantics as in .Certificate apply. |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 157 | func (k *PKI) Kubeconfig(ctx context.Context, name KubeCertificateName) ([]byte, error) { |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 158 | c, ok := k.Certificates[name] |
| 159 | if !ok { |
| 160 | return nil, fmt.Errorf("no certificate %q", name) |
| 161 | } |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 162 | return Kubeconfig(ctx, k.KV, c) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 165 | // Certificate retrieves an x509 DER-encoded (but not PEM-wrapped) key and |
| 166 | // certificate for a given certificate name. |
| 167 | // If the requested certificate is volatile, it will be created on demand. |
| 168 | // Otherwise it will be created on etcd (if not present), and retrieved from |
| 169 | // there. |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 170 | func (k *PKI) Certificate(ctx context.Context, name KubeCertificateName) (cert, key []byte, err error) { |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 171 | c, ok := k.Certificates[name] |
| 172 | if !ok { |
| 173 | return nil, nil, fmt.Errorf("no certificate %q", name) |
| 174 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 175 | return c.Ensure(ctx, k.KV) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 178 | // Kubeconfig generates a kubeconfig blob for this certificate. The same |
| 179 | // lifetime semantics as in .Ensure apply. |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 180 | func Kubeconfig(ctx context.Context, kv clientv3.KV, c *opki.Certificate) ([]byte, error) { |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 181 | |
| 182 | cert, key, err := c.Ensure(ctx, kv) |
| 183 | if err != nil { |
| 184 | return nil, fmt.Errorf("could not ensure certificate exists: %w", err) |
| 185 | } |
| 186 | |
| 187 | kubeconfig := configapi.NewConfig() |
| 188 | |
| 189 | cluster := configapi.NewCluster() |
| 190 | cluster.Server = fmt.Sprintf("https://127.0.0.1:%v", common.KubernetesAPIPort) |
| 191 | |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 192 | ca, err := c.Issuer.CACertificate(ctx, kv) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 193 | if err != nil { |
| 194 | return nil, fmt.Errorf("could not get CA certificate: %w", err) |
| 195 | } |
| 196 | if ca != nil { |
| 197 | cluster.CertificateAuthorityData = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca}) |
| 198 | } |
| 199 | kubeconfig.Clusters["default"] = cluster |
| 200 | |
| 201 | authInfo := configapi.NewAuthInfo() |
| 202 | authInfo.ClientCertificateData = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert}) |
| 203 | authInfo.ClientKeyData = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key}) |
| 204 | kubeconfig.AuthInfos["default"] = authInfo |
| 205 | |
| 206 | ct := configapi.NewContext() |
| 207 | ct.Cluster = "default" |
| 208 | ct.AuthInfo = "default" |
| 209 | kubeconfig.Contexts["default"] = ct |
| 210 | |
| 211 | kubeconfig.CurrentContext = "default" |
| 212 | return clientcmd.Write(*kubeconfig) |
| 213 | } |
| 214 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 215 | // ServiceAccountKey retrieves (and possibly generates and stores on etcd) the |
| 216 | // Kubernetes service account key. The returned data is ready to be used by |
| 217 | // Kubernetes components (in PKIX form). |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 218 | func (k *PKI) ServiceAccountKey(ctx context.Context) ([]byte, error) { |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 219 | // TODO(q3k): this should be abstracted away once we abstract away etcd |
| 220 | // access into a library with try-or-create semantics. |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 221 | path := fmt.Sprintf("%s%s.der", etcdPrefix, serviceAccountKeyName) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 222 | |
| 223 | // Try loading key from etcd. |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 224 | keyRes, err := k.KV.Get(ctx, path) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 225 | if err != nil { |
| 226 | return nil, fmt.Errorf("failed to get key from etcd: %w", err) |
| 227 | } |
| 228 | |
| 229 | if len(keyRes.Kvs) == 1 { |
| 230 | // Certificate and key exists in etcd, return that. |
| 231 | return keyRes.Kvs[0].Value, nil |
| 232 | } |
| 233 | |
| 234 | // No key found - generate one. |
| 235 | keyRaw, err := rsa.GenerateKey(rand.Reader, 2048) |
| 236 | if err != nil { |
| 237 | panic(err) |
| 238 | } |
| 239 | key, err := x509.MarshalPKCS8PrivateKey(keyRaw) |
| 240 | if err != nil { |
| 241 | panic(err) // Always a programmer error |
| 242 | } |
| 243 | |
| 244 | // Save to etcd. |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 245 | _, err = k.KV.Put(ctx, path, string(key)) |
Serge Bazanski | dbfc638 | 2020-06-19 20:35:43 +0200 | [diff] [blame] | 246 | if err != nil { |
| 247 | err = fmt.Errorf("failed to write newly generated key: %w", err) |
| 248 | } |
| 249 | return key, nil |
| 250 | } |
Serge Bazanski | 9411f7c | 2021-03-10 13:12:53 +0100 | [diff] [blame] | 251 | |
| 252 | // VolatileKubelet returns a pair of server/client ceritficates for the Kubelet |
| 253 | // to use. The certificates are volatile, meaning they are not stored in etcd, |
| 254 | // and instead are regenerated any time this function is called. |
| 255 | func (k *PKI) VolatileKubelet(ctx context.Context, name string) (server *opki.Certificate, client *opki.Certificate, err error) { |
| 256 | name = fmt.Sprintf("system:node:%s", name) |
| 257 | err = k.EnsureAll(ctx) |
| 258 | if err != nil { |
| 259 | err = fmt.Errorf("could not ensure certificates exist: %w", err) |
| 260 | } |
| 261 | kubeCA := k.Certificates[IdCA] |
| 262 | server = k.namespace.New(kubeCA, "", opki.Server([]string{name}, nil)) |
| 263 | client = k.namespace.New(kubeCA, "", opki.Client(name, []string{"system:nodes"})) |
| 264 | return |
| 265 | } |
| 266 | |
| 267 | // VolatileClient returns a client certificate for Kubernetes clients to use. |
| 268 | // The generated certificate will place the user in the given groups, and with |
| 269 | // a given identiy as the certificate's CN. |
| 270 | func (k *PKI) VolatileClient(ctx context.Context, identity string, groups []string) (*opki.Certificate, error) { |
| 271 | if err := k.EnsureAll(ctx); err != nil { |
| 272 | return nil, fmt.Errorf("could not ensure certificates exist: %w", err) |
| 273 | } |
| 274 | kubeCA := k.Certificates[IdCA] |
| 275 | return k.namespace.New(kubeCA, "", opki.Client(identity, groups)), nil |
| 276 | } |