blob: dc48b96583a099dab13872b0f727142d95038b82 [file] [log] [blame]
Lorenz Brun6e8f69c2019-11-18 10:44:24 +01001// 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
17package kubernetes
18
19import (
20 "context"
21 "encoding/pem"
22 "errors"
23 "fmt"
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020024 "io"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010025 "net"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010026 "os/exec"
27 "path"
28
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010029 "go.etcd.io/etcd/clientv3"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010030
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020031 "git.monogon.dev/source/nexantic.git/core/internal/common/supervisor"
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010032 "git.monogon.dev/source/nexantic.git/core/pkg/fileargs"
Lorenz Brun6e8f69c2019-11-18 10:44:24 +010033)
34
35type apiserverConfig struct {
36 advertiseAddress net.IP
37 serviceIPRange net.IPNet
38 // All PKI-related things are in DER
39 idCA []byte
40 kubeletClientCert []byte
41 kubeletClientKey []byte
42 aggregationCA []byte
43 aggregationClientCert []byte
44 aggregationClientKey []byte
45 serviceAccountPrivKey []byte // In PKIX form
46 serverCert []byte
47 serverKey []byte
48}
49
50func getPKIApiserverConfig(consensusKV clientv3.KV) (*apiserverConfig, error) {
51 var config apiserverConfig
52 var err error
53 config.idCA, _, err = getCert(consensusKV, "id-ca")
54 config.kubeletClientCert, config.kubeletClientKey, err = getCert(consensusKV, "kubelet-client")
55 config.aggregationCA, _, err = getCert(consensusKV, "aggregation-ca")
56 config.aggregationClientCert, config.aggregationClientKey, err = getCert(consensusKV, "front-proxy-client")
57 config.serverCert, config.serverKey, err = getCert(consensusKV, "apiserver")
58 saPrivkey, err := consensusKV.Get(context.Background(), path.Join(etcdPath, "service-account-privkey.der"))
59 if err != nil {
60 return nil, fmt.Errorf("failed to get serviceaccount privkey: %w", err)
61 }
62 if len(saPrivkey.Kvs) != 1 {
63 return nil, errors.New("failed to get serviceaccount privkey: not found")
64 }
65 config.serviceAccountPrivKey = saPrivkey.Kvs[0].Value
66 return &config, nil
67}
68
Lorenz Brun8e3b8fc2020-05-19 14:29:40 +020069func runAPIServer(config apiserverConfig, output io.Writer) supervisor.Runnable {
70 return func(ctx context.Context) error {
71 args, err := fileargs.New()
72 if err != nil {
73 panic(err) // If this fails, something is very wrong. Just crash.
74 }
75 defer args.Close()
76 cmd := exec.CommandContext(ctx, "/kubernetes/bin/kube", "kube-apiserver",
77 fmt.Sprintf("--advertise-address=%v", config.advertiseAddress.String()),
78 "--authorization-mode=Node,RBAC",
79 args.FileOpt("--client-ca-file", "client-ca.pem",
80 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.idCA})),
81 "--enable-admission-plugins=NodeRestriction,PodSecurityPolicy",
82 "--enable-aggregator-routing=true",
83 "--insecure-port=0",
84 // Due to the magic of GRPC this really needs four slashes and a :0
85 fmt.Sprintf("--etcd-servers=%v", "unix:////consensus/listener.sock:0"),
86 args.FileOpt("--kubelet-client-certificate", "kubelet-client-cert.pem",
87 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.kubeletClientCert})),
88 args.FileOpt("--kubelet-client-key", "kubelet-client-key.pem",
89 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.kubeletClientKey})),
90 "--kubelet-preferred-address-types=Hostname",
91 args.FileOpt("--proxy-client-cert-file", "aggregation-client-cert.pem",
92 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.aggregationClientCert})),
93 args.FileOpt("--proxy-client-key-file", "aggregation-client-key.pem",
94 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.aggregationClientKey})),
95 "--requestheader-allowed-names=front-proxy-client",
96 args.FileOpt("--requestheader-client-ca-file", "aggregation-ca.pem",
97 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.aggregationCA})),
98 "--requestheader-extra-headers-prefix=X-Remote-Extra-",
99 "--requestheader-group-headers=X-Remote-Group",
100 "--requestheader-username-headers=X-Remote-User",
101 args.FileOpt("--service-account-key-file", "service-account-pubkey.pem",
102 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serviceAccountPrivKey})),
103 fmt.Sprintf("--service-cluster-ip-range=%v", config.serviceIPRange.String()),
104 args.FileOpt("--tls-cert-file", "server-cert.pem",
105 pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: config.serverCert})),
106 args.FileOpt("--tls-private-key-file", "server-key.pem",
107 pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: config.serverKey})),
108 )
109 if args.Error() != nil {
110 return err
111 }
112 cmd.Stdout = output
113 cmd.Stderr = output
114 supervisor.Signal(ctx, supervisor.SignalHealthy)
115 err = cmd.Run()
116 fmt.Fprintf(output, "apiserver stopped: %v\n", err)
Lorenz Brun6e8f69c2019-11-18 10:44:24 +0100117 return err
118 }
Lorenz Brun6e8f69c2019-11-18 10:44:24 +0100119}