forked from kubeflow/katib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
292 lines (264 loc) · 9.47 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Copyright 2022 The Kubeflow Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package certgenerator
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"math/big"
"os"
"path/filepath"
"strings"
"time"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
configv1beta1 "github.com/kubeflow/katib/pkg/apis/config/v1beta1"
"github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
)
var (
log = logf.Log.WithName("cert-generator")
errServiceNotFound = errors.New("unable to locate controller service")
errCertCheckFail = errors.New("failed to check if certs already exist")
errCreateCertFail = errors.New("failed to create certs")
errCreateCertSecretFail = errors.New("failed to create secret embedded certs")
errInjectCertError = errors.New("failed to inject certs into WebhookConfigurations")
)
// CertGenerator is the manager to generate certs.
type CertGenerator struct {
namespace string
webhookServiceName string
webhookSecretName string
fullServiceDomain string
kubeClient client.Client
certsReady chan struct{}
certs *certificates
}
var _ manager.Runnable = &CertGenerator{}
var _ manager.LeaderElectionRunnable = &CertGenerator{}
func (c *CertGenerator) Start(ctx context.Context) error {
if err := c.generate(ctx); err != nil {
return err
}
log.Info("Waiting for certs to get ready.")
if err := wait.ExponentialBackoffWithContext(ctx, wait.Backoff{
Duration: time.Second,
Factor: 2,
Jitter: 1,
Steps: 10,
Cap: time.Minute * 5,
}, ensureCertMounted(time.Now())); err != nil {
return err
}
// Sending an empty data to a certsReady means it starts to register controllers to the manager.
c.certsReady <- struct{}{}
return nil
}
// ensureCertMounted ensures that the generated certs are mounted inside the container.
func ensureCertMounted(start time.Time) func(context.Context) (bool, error) {
return func(ctx context.Context) (bool, error) {
now := time.Now()
outputLog := false
if now.Sub(start) >= 15*time.Second {
start = now
outputLog = true
}
certFile := filepath.Join(consts.CertDir, serverCertName)
if _, err := os.Stat(certFile); err != nil {
if outputLog {
log.Info("Public key file doesn't exist in the container yet.", "publicKeyFile", certFile)
}
return false, nil
}
keyFile := filepath.Join(consts.CertDir, serverKeyName)
if _, err := os.Stat(keyFile); err != nil {
if outputLog {
log.Info("Private key file doesn't exist in the container yet.", "privateKeyFile", keyFile)
}
return false, nil
}
log.Info("Succeeded to be mounted certs inside the container.")
return true, nil
}
}
func (c *CertGenerator) NeedLeaderElection() bool {
return false
}
// AddToManager adds the cert-generator to the manager.
func AddToManager(mgr manager.Manager, config configv1beta1.CertGeneratorConfig, certsReady chan struct{}) error {
return mgr.Add(&CertGenerator{
namespace: consts.DefaultKatibNamespace,
webhookServiceName: config.WebhookServiceName,
webhookSecretName: config.WebhookSecretName,
fullServiceDomain: strings.Join([]string{
config.WebhookServiceName,
consts.DefaultKatibNamespace,
"svc",
}, "."),
kubeClient: mgr.GetClient(),
certsReady: certsReady,
})
}
// generate generates certificates for the admission webhooks.
func (c *CertGenerator) generate(ctx context.Context) error {
controllerService := &corev1.Service{}
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: c.webhookServiceName, Namespace: c.namespace}, controllerService); err != nil {
return fmt.Errorf("%w: %v", errServiceNotFound, err)
}
certExist, err := c.isCertExist(ctx)
if err != nil {
return fmt.Errorf("%w: %v", errCertCheckFail, err)
}
if !certExist {
if err = c.createCert(); err != nil {
return fmt.Errorf("%w: %v", errCreateCertFail, err)
}
if err = c.updateCertSecret(ctx); err != nil {
return fmt.Errorf("%w: %v", errCreateCertSecretFail, err)
}
}
if err = c.injectCert(ctx); err != nil {
return fmt.Errorf("%w: %v", errInjectCertError, err)
}
return nil
}
// isCertExist checks if a secret embedded certs already exists.
// For example, it will return true if the katib-controller is created with enabled leader-election
// since another controller pod will create the secret.
func (c *CertGenerator) isCertExist(ctx context.Context) (bool, error) {
secret := &corev1.Secret{}
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: c.webhookSecretName, Namespace: c.namespace}, secret); err != nil {
return false, err
}
key := secret.Data[serverKeyName]
cert := secret.Data[serverCertName]
if len(key) != 0 && len(cert) != 0 {
c.certs = &certificates{
keyPem: key,
certPem: cert,
}
return true, nil
}
return false, nil
}
// createCert creates the self-signed certificate and private key.
func (c *CertGenerator) createCert() error {
now := time.Now()
template := &x509.Certificate{
SerialNumber: big.NewInt(0),
Subject: pkix.Name{
CommonName: c.fullServiceDomain,
},
DNSNames: []string{
c.fullServiceDomain,
},
NotBefore: now,
NotAfter: now.Add(24 * time.Hour * 365 * 10),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
log.Info("Generating self-signed public certificate and private key.")
rawKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
der, err := x509.CreateCertificate(rand.Reader, template, template, rawKey.Public(), rawKey)
if err != nil {
return err
}
if c.certs, err = encode(rawKey, der); err != nil {
return err
}
return nil
}
// updateCertSecret updates Secret embedded tls.key and tls.crt.
func (c *CertGenerator) updateCertSecret(ctx context.Context) error {
secret := &corev1.Secret{}
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: c.webhookSecretName, Namespace: c.namespace}, secret); err != nil {
return err
}
newSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
UID: secret.UID,
Name: secret.Name,
Namespace: secret.Namespace,
},
TypeMeta: secret.TypeMeta,
}
newSecret.Data = map[string][]byte{
serverKeyName: c.certs.keyPem,
serverCertName: c.certs.certPem,
}
return c.kubeClient.Patch(ctx, newSecret, client.Apply, client.FieldOwner(ssaFieldOwnerName), client.ForceOwnership)
}
// injectCert applies patch to ValidatingWebhookConfiguration and MutatingWebhookConfiguration.
func (c *CertGenerator) injectCert(ctx context.Context) error {
vWebhookConfig := &admissionregistrationv1.ValidatingWebhookConfiguration{}
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: Webhook}, vWebhookConfig); err != nil {
return err
}
if !bytes.Equal(vWebhookConfig.Webhooks[0].ClientConfig.CABundle, c.certs.certPem) {
newVWebhookConfig := &admissionregistrationv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
UID: vWebhookConfig.UID,
Name: vWebhookConfig.Name,
Namespace: vWebhookConfig.Namespace,
Generation: vWebhookConfig.Generation,
},
TypeMeta: vWebhookConfig.TypeMeta,
}
newVWebhookConfig.Webhooks = vWebhookConfig.Webhooks
newVWebhookConfig.Webhooks[0].ClientConfig.CABundle = c.certs.certPem
log.Info("Trying to patch ValidatingWebhookConfiguration adding the caBundle.")
err := c.kubeClient.Patch(ctx, newVWebhookConfig, client.Apply, client.FieldOwner(ssaFieldOwnerName), client.ForceOwnership)
if err != nil {
log.Error(err, "Unable to patch ValidatingWebhookConfiguration", "ValidatingWebhookConfiguration", Webhook)
return err
}
}
mWebhookConfig := &admissionregistrationv1.MutatingWebhookConfiguration{}
if err := c.kubeClient.Get(ctx, client.ObjectKey{Name: Webhook}, mWebhookConfig); err != nil {
return err
}
if !bytes.Equal(mWebhookConfig.Webhooks[0].ClientConfig.CABundle, c.certs.certPem) ||
!bytes.Equal(mWebhookConfig.Webhooks[1].ClientConfig.CABundle, c.certs.certPem) {
newMWebhookConfig := &admissionregistrationv1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
UID: mWebhookConfig.UID,
Name: mWebhookConfig.Name,
Namespace: mWebhookConfig.Namespace,
Generation: mWebhookConfig.Generation,
},
TypeMeta: mWebhookConfig.TypeMeta,
}
newMWebhookConfig.Webhooks = mWebhookConfig.Webhooks
newMWebhookConfig.Webhooks[0].ClientConfig.CABundle = c.certs.certPem
newMWebhookConfig.Webhooks[1].ClientConfig.CABundle = c.certs.certPem
log.Info("Trying to patch MutatingWebhookConfiguration adding the caBundle.")
err := c.kubeClient.Patch(ctx, newMWebhookConfig, client.Apply, client.FieldOwner(ssaFieldOwnerName), client.ForceOwnership)
if err != nil {
log.Error(err, "Unable to patch MutatingWebhookConfiguration", "MutatingWebhookConfiguration", Webhook)
return err
}
}
return nil
}