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