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