blob: ef046a2d9d139d7f7055fae64a890a2c086f75c4 [file] [log] [blame]
Serge Bazanskidbfc6382020-06-19 20:35:43 +02001// 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 Bazanski9411f7c2021-03-10 13:12:53 +010017// 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 Bazanskidbfc6382020-06-19 20:35:43 +020024package pki
25
26import (
27 "context"
28 "crypto/rand"
29 "crypto/rsa"
30 "crypto/x509"
31 "encoding/pem"
32 "fmt"
33 "net"
34
Lorenz Brund13c1c62022-03-30 19:58:58 +020035 clientv3 "go.etcd.io/etcd/client/v3"
Serge Bazanskidbfc6382020-06-19 20:35:43 +020036 "k8s.io/client-go/tools/clientcmd"
37 configapi "k8s.io/client-go/tools/clientcmd/api"
38
Serge Bazanski31370b02021-01-07 16:31:14 +010039 common "source.monogon.dev/metropolis/node"
40 "source.monogon.dev/metropolis/pkg/logtree"
Serge Bazanski9411f7c2021-03-10 13:12:53 +010041 opki "source.monogon.dev/metropolis/pkg/pki"
Serge Bazanskidbfc6382020-06-19 20:35:43 +020042)
43
Serge Bazanski9411f7c2021-03-10 13:12:53 +010044// 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 Bazanskidbfc6382020-06-19 20:35:43 +020047type KubeCertificateName string
48
49const (
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 Bazanski9411f7c2021-03-10 13:12:53 +010056 // APIServer client certificate used to authenticate to kubelets.
57 APIServerKubeletClient KubeCertificateName = "apiserver-kubelet-client"
Serge Bazanskidbfc6382020-06-19 20:35:43 +020058
Serge Bazanski216fe7b2021-05-21 18:36:16 +020059 // Kubernetes Controller manager client certificate, used to authenticate
60 // to the apiserver.
Serge Bazanskidbfc6382020-06-19 20:35:43 +020061 ControllerManagerClient KubeCertificateName = "controller-manager-client"
Serge Bazanski216fe7b2021-05-21 18:36:16 +020062 // Kubernetes Controller manager server certificate, used to run its HTTP
63 // server.
Serge Bazanskidbfc6382020-06-19 20:35:43 +020064 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 Bazanski216fe7b2021-05-21 18:36:16 +020071 // Root-on-kube (system:masters) client certificate. Used to control the
72 // apiserver (and resources) by Metropolis internally.
Serge Bazanskidbfc6382020-06-19 20:35:43 +020073 Master KubeCertificateName = "master"
74
75 // OpenAPI Kubernetes Aggregation CA.
Serge Bazanski216fe7b2021-05-21 18:36:16 +020076 // https://kubernetes.io/docs/tasks/extend-kubernetes/configure-aggregation-layer/#ca-reusage-and-conflicts
Serge Bazanskidbfc6382020-06-19 20:35:43 +020077 AggregationCA KubeCertificateName = "aggregation-ca"
78 FrontProxyClient KubeCertificateName = "front-proxy-client"
Lorenz Bruncc078df2021-12-23 11:51:55 +010079 // The Metropolis authentication proxy needs to be able to proxy requests
80 // and assert the established identity to the Kubernetes API server.
81 MetropolisAuthProxyClient KubeCertificateName = "metropolis-auth-proxy-client"
Serge Bazanskidbfc6382020-06-19 20:35:43 +020082)
83
84const (
Serge Bazanski9411f7c2021-03-10 13:12:53 +010085 // etcdPrefix is where all the PKI data is stored in etcd.
86 etcdPrefix = "/kube-pki/"
Serge Bazanski216fe7b2021-05-21 18:36:16 +020087 // serviceAccountKeyName is the etcd path part that is used to store the
88 // ServiceAccount authentication secret. This is not a certificate, just an
89 // RSA key.
Serge Bazanskidbfc6382020-06-19 20:35:43 +020090 serviceAccountKeyName = "service-account-privkey"
91)
92
Serge Bazanski9411f7c2021-03-10 13:12:53 +010093// PKI manages all PKI resources required to run Kubernetes on Metropolis. It
94// contains all static certificates, which can be retrieved, or be used to
95// generate Kubeconfigs from.
96type PKI struct {
97 namespace opki.Namespace
Serge Bazanskic7359672020-10-30 16:38:57 +010098 logger logtree.LeveledLogger
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020099 KV clientv3.KV
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100100 Certificates map[KubeCertificateName]*opki.Certificate
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200101}
102
Lorenz Brun78cefca2022-06-20 12:59:55 +0000103func New(l logtree.LeveledLogger, kv clientv3.KV, clusterDomain string) *PKI {
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100104 pki := PKI{
105 namespace: opki.Namespaced(etcdPrefix),
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200106 logger: l,
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200107 KV: kv,
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100108 Certificates: make(map[KubeCertificateName]*opki.Certificate),
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200109 }
110
111 make := func(i, name KubeCertificateName, template x509.Certificate) {
Serge Bazanski52538842021-08-11 16:22:41 +0200112 pki.Certificates[name] = &opki.Certificate{
113 Namespace: &pki.namespace,
114 Issuer: pki.Certificates[i],
115 Name: string(name),
116 Template: template,
117 Mode: opki.CertificateManaged,
118 }
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200119 }
120
Serge Bazanski52538842021-08-11 16:22:41 +0200121 pki.Certificates[IdCA] = &opki.Certificate{
122 Namespace: &pki.namespace,
123 Issuer: opki.SelfSigned,
124 Name: string(IdCA),
125 Template: opki.CA("Metropolis Kubernetes ID CA"),
126 Mode: opki.CertificateManaged,
127 }
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100128 make(IdCA, APIServer, opki.Server(
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200129 []string{
130 "kubernetes",
131 "kubernetes.default",
132 "kubernetes.default.svc",
Lorenz Brun78cefca2022-06-20 12:59:55 +0000133 "kubernetes.default.svc." + clusterDomain,
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200134 "localhost",
135 },
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200136 // TODO(q3k): add service network internal apiserver address
137 []net.IP{{10, 0, 255, 1}, {127, 0, 0, 1}},
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200138 ))
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100139 make(IdCA, APIServerKubeletClient, opki.Client("metropolis:apiserver-kubelet-client", nil))
140 make(IdCA, ControllerManagerClient, opki.Client("system:kube-controller-manager", nil))
141 make(IdCA, ControllerManager, opki.Server([]string{"kube-controller-manager.local"}, nil))
142 make(IdCA, SchedulerClient, opki.Client("system:kube-scheduler", nil))
143 make(IdCA, Scheduler, opki.Server([]string{"kube-scheduler.local"}, nil))
144 make(IdCA, Master, opki.Client("metropolis:master", []string{"system:masters"}))
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200145
Serge Bazanski52538842021-08-11 16:22:41 +0200146 pki.Certificates[AggregationCA] = &opki.Certificate{
147 Namespace: &pki.namespace,
148 Issuer: opki.SelfSigned,
149 Name: string(AggregationCA),
150 Template: opki.CA("Metropolis OpenAPI Aggregation CA"),
151 Mode: opki.CertificateManaged,
152 }
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100153 make(AggregationCA, FrontProxyClient, opki.Client("front-proxy-client", nil))
Lorenz Bruncc078df2021-12-23 11:51:55 +0100154 make(AggregationCA, MetropolisAuthProxyClient, opki.Client("metropolis-auth-proxy-client", nil))
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200155
156 return &pki
157}
158
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200159// EnsureAll ensures that all static certificates (and the serviceaccount key)
160// are present on etcd.
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100161func (k *PKI) EnsureAll(ctx context.Context) error {
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200162 for n, v := range k.Certificates {
Serge Bazanski52538842021-08-11 16:22:41 +0200163 _, err := v.Ensure(ctx, k.KV)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200164 if err != nil {
165 return fmt.Errorf("could not ensure certificate %q exists: %w", n, err)
166 }
167 }
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200168 _, err := k.ServiceAccountKey(ctx)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200169 if err != nil {
170 return fmt.Errorf("could not ensure service account key exists: %w", err)
171 }
172 return nil
173}
174
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200175// Kubeconfig generates a kubeconfig blob for a given certificate name. The
176// same lifetime semantics as in .Certificate apply.
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100177func (k *PKI) Kubeconfig(ctx context.Context, name KubeCertificateName) ([]byte, error) {
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200178 c, ok := k.Certificates[name]
179 if !ok {
180 return nil, fmt.Errorf("no certificate %q", name)
181 }
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100182 return Kubeconfig(ctx, k.KV, c)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200183}
184
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200185// Certificate retrieves an x509 DER-encoded (but not PEM-wrapped) key and
186// certificate for a given certificate name.
187// If the requested certificate is volatile, it will be created on demand.
188// Otherwise it will be created on etcd (if not present), and retrieved from
189// there.
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100190func (k *PKI) Certificate(ctx context.Context, name KubeCertificateName) (cert, key []byte, err error) {
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200191 c, ok := k.Certificates[name]
192 if !ok {
193 return nil, nil, fmt.Errorf("no certificate %q", name)
194 }
Serge Bazanski52538842021-08-11 16:22:41 +0200195 cert, err = c.Ensure(ctx, k.KV)
196 if err != nil {
197 return
198 }
199 key, err = c.PrivateKeyX509()
200 return
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200201}
202
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200203// Kubeconfig generates a kubeconfig blob for this certificate. The same
204// lifetime semantics as in .Ensure apply.
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100205func Kubeconfig(ctx context.Context, kv clientv3.KV, c *opki.Certificate) ([]byte, error) {
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200206
Serge Bazanski52538842021-08-11 16:22:41 +0200207 cert, err := c.Ensure(ctx, kv)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200208 if err != nil {
209 return nil, fmt.Errorf("could not ensure certificate exists: %w", err)
210 }
Serge Bazanski52538842021-08-11 16:22:41 +0200211 key, err := c.PrivateKeyX509()
212 if err != nil {
213 return nil, fmt.Errorf("could not get certificate's private key: %w", err)
214 }
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200215
216 kubeconfig := configapi.NewConfig()
217
218 cluster := configapi.NewCluster()
Serge Bazanski52304a82021-10-29 16:56:18 +0200219 cluster.Server = fmt.Sprintf("https://127.0.0.1:%d", common.KubernetesAPIPort)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200220
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100221 ca, err := c.Issuer.CACertificate(ctx, kv)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200222 if err != nil {
223 return nil, fmt.Errorf("could not get CA certificate: %w", err)
224 }
225 if ca != nil {
226 cluster.CertificateAuthorityData = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca})
227 }
228 kubeconfig.Clusters["default"] = cluster
229
230 authInfo := configapi.NewAuthInfo()
231 authInfo.ClientCertificateData = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert})
232 authInfo.ClientKeyData = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key})
233 kubeconfig.AuthInfos["default"] = authInfo
234
235 ct := configapi.NewContext()
236 ct.Cluster = "default"
237 ct.AuthInfo = "default"
238 kubeconfig.Contexts["default"] = ct
239
240 kubeconfig.CurrentContext = "default"
241 return clientcmd.Write(*kubeconfig)
242}
243
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200244// ServiceAccountKey retrieves (and possibly generates and stores on etcd) the
245// Kubernetes service account key. The returned data is ready to be used by
246// Kubernetes components (in PKIX form).
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100247func (k *PKI) ServiceAccountKey(ctx context.Context) ([]byte, error) {
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200248 // TODO(q3k): this should be abstracted away once we abstract away etcd
249 // access into a library with try-or-create semantics.
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100250 path := fmt.Sprintf("%s%s.der", etcdPrefix, serviceAccountKeyName)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200251
252 // Try loading key from etcd.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200253 keyRes, err := k.KV.Get(ctx, path)
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200254 if err != nil {
255 return nil, fmt.Errorf("failed to get key from etcd: %w", err)
256 }
257
258 if len(keyRes.Kvs) == 1 {
259 // Certificate and key exists in etcd, return that.
260 return keyRes.Kvs[0].Value, nil
261 }
262
263 // No key found - generate one.
264 keyRaw, err := rsa.GenerateKey(rand.Reader, 2048)
265 if err != nil {
266 panic(err)
267 }
268 key, err := x509.MarshalPKCS8PrivateKey(keyRaw)
269 if err != nil {
270 panic(err) // Always a programmer error
271 }
272
273 // Save to etcd.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200274 _, err = k.KV.Put(ctx, path, string(key))
Serge Bazanskidbfc6382020-06-19 20:35:43 +0200275 if err != nil {
276 err = fmt.Errorf("failed to write newly generated key: %w", err)
277 }
278 return key, nil
279}
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100280
281// VolatileKubelet returns a pair of server/client ceritficates for the Kubelet
Serge Bazanski52538842021-08-11 16:22:41 +0200282// to use. The certificates are ephemeral, meaning they are not stored in etcd,
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100283// and instead are regenerated any time this function is called.
284func (k *PKI) VolatileKubelet(ctx context.Context, name string) (server *opki.Certificate, client *opki.Certificate, err error) {
285 name = fmt.Sprintf("system:node:%s", name)
286 err = k.EnsureAll(ctx)
287 if err != nil {
Serge Bazanski52538842021-08-11 16:22:41 +0200288 return nil, nil, fmt.Errorf("could not ensure certificates exist: %w", err)
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100289 }
290 kubeCA := k.Certificates[IdCA]
Serge Bazanski52538842021-08-11 16:22:41 +0200291 server = &opki.Certificate{
292 Namespace: &k.namespace,
293 Issuer: kubeCA,
294 Template: opki.Server([]string{name}, nil),
295 Mode: opki.CertificateEphemeral,
296 }
297 client = &opki.Certificate{
298 Namespace: &k.namespace,
299 Issuer: kubeCA,
300 Template: opki.Client(name, []string{"system:nodes"}),
301 Mode: opki.CertificateEphemeral,
302 }
303 return server, client, nil
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100304}
305
306// VolatileClient returns a client certificate for Kubernetes clients to use.
307// The generated certificate will place the user in the given groups, and with
308// a given identiy as the certificate's CN.
309func (k *PKI) VolatileClient(ctx context.Context, identity string, groups []string) (*opki.Certificate, error) {
310 if err := k.EnsureAll(ctx); err != nil {
311 return nil, fmt.Errorf("could not ensure certificates exist: %w", err)
312 }
Serge Bazanski52538842021-08-11 16:22:41 +0200313 return &opki.Certificate{
314 Namespace: &k.namespace,
315 Issuer: k.Certificates[IdCA],
316 Template: opki.Client(identity, groups),
317 Mode: opki.CertificateEphemeral,
318 }, nil
Serge Bazanski9411f7c2021-03-10 13:12:53 +0100319}