-
Notifications
You must be signed in to change notification settings - Fork 458
/
operator.go
311 lines (285 loc) · 11.3 KB
/
operator.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// This file is part of MinIO Operator
// Copyright (c) 2020 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"net"
"net/http"
"slices"
"strings"
"time"
"github.com/minio/operator/pkg/certs"
miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
xcerts "github.com/minio/pkg/certs"
"github.com/minio/pkg/env"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
)
const (
// CertPasswordEnv Env variable is used to decrypt the private key in the TLS certificate for operator if need it
CertPasswordEnv = "OPERATOR_CERT_PASSWD"
// OperatorDeploymentNameEnv Env variable to specify a custom deployment name for Operator
OperatorDeploymentNameEnv = "MINIO_OPERATOR_DEPLOYMENT_NAME"
// OperatorCATLSSecretName is the name of the secret for the operator CA
OperatorCATLSSecretName = "operator-ca-tls"
// DefaultDeploymentName is the default name of the operator deployment
DefaultDeploymentName = "minio-operator"
)
var serverCertsManager *xcerts.Manager
func (c *Controller) fetchUserCredentials(ctx context.Context, tenant *miniov2.Tenant) []*v1.Secret {
var userCredentials []*v1.Secret
for _, credential := range tenant.Spec.Users {
credentialSecret, err := c.kubeClientSet.CoreV1().Secrets(tenant.Namespace).Get(ctx, credential.Name, metav1.GetOptions{})
if err == nil && credentialSecret != nil {
userCredentials = append(userCredentials, credentialSecret)
}
}
return userCredentials
}
// getTransport returns a *http.Transport with the collection of the trusted CA certificates
// returns a cached transport if already available
func (c *Controller) getTransport() *http.Transport {
if c.transport != nil {
return c.transport
}
c.transport = c.createTransport()
return c.transport
}
// createTransport returns a *http.Transport with the collection of the trusted CA certificates
func (c *Controller) createTransport() *http.Transport {
rootCAs := c.fetchTransportCACertificates()
dialer := &net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 15 * time.Second,
}
c.transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
MaxIdleConnsPerHost: 1024,
IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: 15 * time.Minute,
TLSHandshakeTimeout: 15 * time.Second,
ExpectContinueTimeout: 15 * time.Second,
// Go net/http automatically unzip if content-type is
// gzip disable this feature, as we are always interested
// in raw stream.
DisableCompression: true,
TLSClientConfig: &tls.Config{
// Can't use SSLv3 because of POODLE and BEAST
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
// Can't use TLSv1.1 because of RC4 cipher usage
MinVersion: tls.VersionTLS12,
RootCAs: rootCAs,
},
}
return c.transport
}
// fetchTransportCACertificates retrieves a *x509.CertPool with all CA that operator will trust
func (c *Controller) fetchTransportCACertificates() (pool *x509.CertPool) {
rootCAs := miniov2.MustGetSystemCertPool()
// Default kubernetes CA certificate
rootCAs.AppendCertsFromPEM(miniov2.GetPodCAFromFile())
// Append all external Certificate Authorities added to Operator, secrets with prefix "operator-ca-tls"
secretsAvailableAtOperatorNS, _ := c.kubeClientSet.CoreV1().Secrets(miniov2.GetNSFromFile()).List(context.Background(), metav1.ListOptions{})
for _, secret := range secretsAvailableAtOperatorNS.Items {
// Check if secret starts with "operator-ca-tls-"
if strings.HasPrefix(secret.Name, OperatorCATLSSecretName) {
operatorCATLSCert, err := c.kubeClientSet.CoreV1().Secrets(miniov2.GetNSFromFile()).Get(context.Background(), secret.Name, metav1.GetOptions{})
if err == nil && operatorCATLSCert != nil {
if newPublicCert, err := getFileFromSecretDataField(operatorCATLSCert.Data, certs.PublicCertFile); err == nil {
rootCAs.AppendCertsFromPEM(newPublicCert)
}
if newTLSCert, err := getFileFromSecretDataField(operatorCATLSCert.Data, certs.TLSCertFile); err == nil {
rootCAs.AppendCertsFromPEM(newTLSCert)
}
if newCACert, err := getFileFromSecretDataField(operatorCATLSCert.Data, certs.CAPublicCertFile); err == nil {
rootCAs.AppendCertsFromPEM(newCACert)
}
}
}
}
return rootCAs
}
// getFileFromSecretDataField Get the value of a secret field
// limiting the field key name to public TLS certificate file names
func getFileFromSecretDataField(secretData map[string][]byte, key string) ([]byte, error) {
keys := []string{
certs.TLSCertFile,
certs.CAPublicCertFile,
certs.PublicCertFile,
}
if slices.Contains(keys, key) {
data, ok := secretData[key]
if ok {
return data, nil
}
} else {
return nil, fmt.Errorf("unknow TLS key '%s'", key)
}
return nil, fmt.Errorf("key '%s' not found in secret", key)
}
// TrustTLSCertificatesInSecretIfChanged Compares old and new secret content and trusts TLS certificates if field
// content is different, looks for the fields public.crt, tls.crt and ca.crt
func (c *Controller) TrustTLSCertificatesInSecretIfChanged(newSecret *corev1.Secret, oldSecret *corev1.Secret) bool {
added := false
if oldSecret == nil {
// secret did not exist before, we trust all certs in it
if c.trustPEMInSecretField(newSecret, certs.PublicCertFile) {
added = true
}
if c.trustPEMInSecretField(newSecret, certs.TLSCertFile) {
added = true
}
if c.trustPEMInSecretField(newSecret, certs.CAPublicCertFile) {
added = true
}
} else {
// compare to add to trust only certs that changed
if c.trustIfChanged(newSecret, oldSecret, certs.PublicCertFile) {
added = true
}
if c.trustIfChanged(newSecret, oldSecret, certs.TLSCertFile) {
added = true
}
if c.trustIfChanged(newSecret, oldSecret, certs.CAPublicCertFile) {
added = true
}
}
return added
}
func (c *Controller) trustIfChanged(newSecret *corev1.Secret, oldSecret *corev1.Secret, fieldToCompare string) bool {
if newPublicCert, err := getFileFromSecretDataField(newSecret.Data, fieldToCompare); err == nil {
if oldPublicCert, err := getFileFromSecretDataField(oldSecret.Data, fieldToCompare); err == nil {
newPublicCert = bytes.TrimSpace(newPublicCert)
oldPublicCert = bytes.TrimSpace(oldPublicCert)
// add to trust only if cert changed
if !bytes.Equal(oldPublicCert, newPublicCert) {
if err := c.addTLSCertificatesToTrustInTransport(newPublicCert); err == nil {
klog.Infof("Added certificates in field '%s' of '%s/%s' secret to trusted RootCA's", fieldToCompare, newSecret.Namespace, newSecret.Name)
return true
}
klog.Errorf("Failed adding certs in field '%s' of '%s/%s' secret: %v", fieldToCompare, newSecret.Namespace, newSecret.Name, err)
}
} else {
// If field was not present in old secret but is in new secret then is an addition, we trust it
if err := c.addTLSCertificatesToTrustInTransport(newPublicCert); err == nil {
klog.Infof("Added certificates in field '%s' of '%s/%s' secret to trusted RootCA's", fieldToCompare, newSecret.Namespace, newSecret.Name)
return true
}
klog.Errorf("Failed adding certificates in field %s of '%s/%s' secret: %v", fieldToCompare, newSecret.Namespace, newSecret.Name, err)
}
}
return false
}
func (c *Controller) trustPEMInSecretField(secret *corev1.Secret, fieldToCompare string) bool {
newPublicCert, err := getFileFromSecretDataField(secret.Data, fieldToCompare)
if err == nil {
if err := c.addTLSCertificatesToTrustInTransport(newPublicCert); err == nil {
klog.Infof("Added certificates in field '%s' of '%s/%s' secret to trusted RootCA's", fieldToCompare, secret.Namespace, secret.Name)
return true
}
klog.Errorf("Failed adding certificates in field '%s' of '%s/%s' secret: %v", fieldToCompare, secret.Namespace, secret.Name, err)
}
return false
}
func (c *Controller) addTLSCertificatesToTrustInTransport(certificateData []byte) error {
var x509Certs []*x509.Certificate
current := certificateData
// A single PEM file could contain more than one certificates, keeping track of the index to help debugging
certIndex := 1
for len(current) > 0 {
var pemBlock *pem.Block
if pemBlock, current = pem.Decode(current); pemBlock == nil {
return fmt.Errorf("invalid PEM in file in index %d", certIndex)
}
x509Cert, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
return fmt.Errorf("error parsing x509 certificate from PEM in index, %d: %v", certIndex, err)
}
x509Certs = append(x509Certs, x509Cert)
certIndex++
}
for _, cert := range x509Certs {
c.getTransport().TLSClientConfig.RootCAs.AddCert(cert)
}
return nil
}
func (c *Controller) createUsers(ctx context.Context, tenant *miniov2.Tenant, tenantConfiguration map[string][]byte) (err error) {
defer func() {
if err == nil {
if _, err = c.updateProvisionedUsersStatus(ctx, tenant, true); err != nil {
klog.V(2).Infof(err.Error())
}
}
}()
userCredentials := c.fetchUserCredentials(ctx, tenant)
if len(userCredentials) == 0 {
return nil
}
if _, err = c.updateTenantStatus(ctx, tenant, StatusProvisioningInitialUsers, 0); err != nil {
return err
}
// get a new admin client
adminClient, err := tenant.NewMinIOAdmin(tenantConfiguration, c.getTransport())
if err != nil {
klog.Errorf("Error instantiating adminClnt: %v", err)
return err
}
// configuration that means MinIO is running with LDAP enabled
// and we need to skip the console user creation
if err = tenant.CreateUsers(adminClient, userCredentials, tenantConfiguration); err != nil {
klog.V(2).Infof("Unable to create MinIO users: %v", err)
}
return err
}
func (c *Controller) createBuckets(ctx context.Context, tenant *miniov2.Tenant, tenantConfiguration map[string][]byte) (created bool, err error) {
tenantBuckets := tenant.Spec.Buckets
if len(tenantBuckets) == 0 {
return false, nil
}
// get a new admin client
minioClient, err := tenant.NewMinIOUser(tenantConfiguration, c.getTransport())
if err != nil {
// show the error and continue
klog.Errorf("Error instantiating minio Client: %v ", err)
return false, err
}
created, err = tenant.CreateBuckets(minioClient, tenantBuckets...)
if err != nil {
klog.V(2).Infof("Unable to create MinIO buckets: %v", err)
if _, terr := c.updateTenantStatus(ctx, tenant, StatusProvisioningDefaultBuckets, 0); terr != nil {
return false, err
}
return false, err
}
if created {
if _, err = c.updateProvisionedBucketStatus(ctx, tenant, true); err != nil {
klog.V(2).Infof(err.Error())
}
}
return created, err
}
// getOperatorDeploymentName Internal func returns the Operator deployment name from MINIO_OPERATOR_DEPLOYMENT_NAME ENV variable or the default name
func getOperatorDeploymentName() string {
return env.Get(OperatorDeploymentNameEnv, DefaultDeploymentName)
}