This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
/
root.go
executable file
·713 lines (571 loc) · 17.3 KB
/
root.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package apiserver
/*
Copyright 2019 Crunchy Data Solutions, Inc.
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.
*/
import (
"bufio"
"bytes"
"crypto/rsa"
"crypto/x509"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
"github.com/crunchydata/postgres-operator/config"
"github.com/crunchydata/postgres-operator/kubeapi"
"github.com/crunchydata/postgres-operator/tlsutil"
"github.com/crunchydata/postgres-operator/util"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const rsaKeySize = 2048
const duration365d = time.Hour * 24 * 365
const PGOSecretName = "pgo.tls"
// pgouserPath ...
const pgouserPath = "/default-pgo-config/pgouser"
const pgouserFile = "pgouser"
const VERSION_MISMATCH_ERROR = "pgo client and server version mismatch"
// RESTClient ...
var RESTClient *rest.RESTClient
// Clientset ...
var Clientset *kubernetes.Clientset
var RESTConfig *rest.Config
// MetricsFlag if set to true will cause crunchy-collect to be added into new clusters
var MetricsFlag, BadgerFlag bool
// AuditFlag if set to true will cause auditing to occur in the logs
var AuditFlag bool
// DebugFlag is the debug flag value
var DebugFlag bool
// BasicAuth comes from the apiserver config
var BasicAuth bool
// Namespace comes from the apiserver config in this version
var PgoNamespace string
var Namespace string
var CRUNCHY_DEBUG bool
// TreeTrunk is for debugging only in this context
const TreeTrunk = "└── "
// TreeBranch is for debugging only in this context
const TreeBranch = "├── "
type CredentialDetail struct {
Username string
Password string
Role string
Namespaces []string
}
// Credentials holds the BasicAuth credentials found in the config
var Credentials map[string]CredentialDetail
var Pgo config.PgoConfig
type containerResourcesTemplateFields struct {
RequestsMemory, RequestsCPU string
LimitsMemory, LimitsCPU string
}
func Initialize() {
PgoNamespace = os.Getenv("PGO_OPERATOR_NAMESPACE")
if PgoNamespace == "" {
log.Info("PGO_OPERATOR_NAMESPACE environment variable is not set and is required, this is the namespace that the Operator is to run within.")
os.Exit(2)
}
log.Info("Pgo Namespace is [" + PgoNamespace + "]")
namespaceList := util.GetNamespaces()
log.Debugf("watching the following namespaces: [%v]", namespaceList)
Namespace = os.Getenv("NAMESPACE")
if Namespace == "" {
log.Error("NAMESPACE environment variable is set to empty string which pgo will interpret as watch 'all' namespaces")
}
tmp := os.Getenv("CRUNCHY_DEBUG")
CRUNCHY_DEBUG = false
if tmp == "true" {
CRUNCHY_DEBUG = true
}
log.Info("Namespace is [" + Namespace + "]")
BasicAuth = true
MetricsFlag = false
BadgerFlag = false
AuditFlag = false
log.Infoln("apiserver starts")
ConnectToKube()
getCredentials()
InitializePerms()
err := Pgo.GetConfig(Clientset, PgoNamespace)
if err != nil {
log.Error("error in Pgo configuration")
os.Exit(2)
}
initConfig()
validateWithKube()
validateUserCredentials()
}
// ConnectToKube ...
func ConnectToKube() {
kubeconfig := flag.String("kubeconfig", "", "Path to a kube config. Only required if out-of-cluster.")
flag.Parse()
var err error
RESTConfig, err = buildConfig(*kubeconfig)
if err != nil {
panic(err)
}
Clientset, err = kubernetes.NewForConfig(RESTConfig)
if err != nil {
panic(err)
}
RESTClient, _, err = util.NewClient(RESTConfig)
if err != nil {
panic(err)
}
}
// buildConfig ...
func buildConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func initConfig() {
AuditFlag = Pgo.Pgo.Audit
if AuditFlag {
log.Info("audit flag is set to true")
}
MetricsFlag = Pgo.Cluster.Metrics
if MetricsFlag {
log.Info("metrics flag is set to true")
}
BadgerFlag = Pgo.Cluster.Badger
if BadgerFlag {
log.Info("badger flag is set to true")
}
tmp := Pgo.BasicAuth
if tmp == "" {
BasicAuth = true
} else {
var err error
BasicAuth, err = strconv.ParseBool(tmp)
if err != nil {
log.Error("BasicAuth config value is not valid")
os.Exit(2)
}
}
log.Infof("BasicAuth is %v", BasicAuth)
if !validStorageSettings() {
log.Error("Storage Settings are not defined correctly, can't continue")
os.Exit(2)
}
if !validContainerResourcesSettings() {
log.Error("Container Resources settings are not defined correctly, can't continue")
os.Exit(2)
}
}
func file2lines(filePath string) ([]string, error) {
f, err := os.Open(filePath)
if err != nil {
log.Error(err)
os.Exit(2)
}
defer f.Close()
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func parseUserMap(dat string) CredentialDetail {
creds := CredentialDetail{}
fields := strings.Split(strings.TrimSpace(dat), ":")
creds.Username = strings.TrimSpace(fields[0])
creds.Password = strings.TrimSpace(fields[1])
creds.Role = strings.TrimSpace(fields[2])
creds.Namespaces = strings.Split(strings.TrimSpace(fields[3]), ",")
return creds
}
// getCredentials ...
func getCredentials() {
var lines []string
var err error
Credentials = make(map[string]CredentialDetail)
log.Infof("getCredentials with PgoNamespace=%s", PgoNamespace)
cm, found := kubeapi.GetConfigMap(Clientset, config.CustomConfigMapName, PgoNamespace)
if found {
log.Infof("Config: %s ConfigMap found in ns %s, using config files from the configmap", config.CustomConfigMapName, PgoNamespace)
val := cm.Data[pgouserFile]
if val == "" {
log.Infof("could not find %s in ConfigMap", pgouserFile)
log.Error(err)
os.Exit(2)
}
scanner := bufio.NewScanner(strings.NewReader(val))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
err = scanner.Err()
} else {
log.Infof("No custom %s file found in configmap, using defaults", pgouserFile)
lines, err = file2lines(pgouserPath)
}
if err != nil {
log.Error(err)
os.Exit(2)
}
for _, v := range lines {
creds := parseUserMap(v)
Credentials[creds.Username] = creds
}
log.Debugf("pgouser has %v", Credentials)
}
// validateUserCredentials ...
func validateUserCredentials() {
for _, v := range Credentials {
log.Infof("validating user %s and role %s", v.Username, v.Role)
if RoleMap[v.Role] == nil {
errMsg := fmt.Sprintf("role not found on pgouser user [%s], invalid role was [%s]", v.Username, v.Role)
log.Error(errMsg)
os.Exit(2)
}
}
//validate the pgouser server config file has valid namespaces
for _, v := range Credentials {
log.Infof("validating user %s namespaces %v", v.Username, v.Namespaces)
for i := 0; i < len(v.Namespaces); i++ {
if v.Namespaces[i] == "" {
} else {
watching := util.WatchingNamespace(Clientset, v.Namespaces[i])
if !watching {
errMsg := fmt.Sprintf("namespace %s found on pgouser user [%s], but this namespace is not being watched", v.Namespaces[i], v.Username, v.Role)
log.Error(errMsg)
os.Exit(2)
}
}
}
}
}
func BasicAuthCheck(username, password string) bool {
if BasicAuth == false {
return true
}
var value CredentialDetail
var ok bool
if value, ok = Credentials[username]; ok {
} else {
return false
}
if value.Password != password {
return false
}
return true
}
func BasicAuthzCheck(username, perm string) bool {
creds := Credentials[username]
log.Debugf("BasicAuthzCheck %s %s %v", creds.Role, perm, HasPerm(creds.Role, perm))
return HasPerm(creds.Role, perm)
}
//GetNamespace determines if a user has permission for
//a namespace they are requesting
//a valid requested namespace is required
func GetNamespace(clientset *kubernetes.Clientset, username, requestedNS string) (string, error) {
log.Debugf("GetNamespace username [%s] ns [%s]", username, requestedNS)
if requestedNS == "" {
return requestedNS, errors.New("empty namespace is not valid from pgo clients")
}
if !UserIsPermittedInNamespace(username, requestedNS) {
errMsg := fmt.Sprintf("user [%s] is not allowed access to namespace [%s]", username, requestedNS)
return requestedNS, errors.New(errMsg)
}
if util.WatchingNamespace(clientset, requestedNS) {
return requestedNS, nil
}
log.Debugf("GetNamespace did not find the requested namespace %s", requestedNS)
return requestedNS, errors.New("requested Namespace was not found to be in the list of Namespaces being watched.")
}
func Authn(perm string, w http.ResponseWriter, r *http.Request) (string, error) {
var err error
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
username, password, authOK := r.BasicAuth()
if AuditFlag {
log.Infof("[audit] %s username=[%s] method=[%s] ip=[%s]", perm, username, r.Method, r.RemoteAddr)
}
log.Debugf("Authentication Attempt %s username=[%s]", perm, username)
if authOK == false {
http.Error(w, "Not authorized", 401)
return "", errors.New("Not Authorized")
}
if !BasicAuthCheck(username, password) {
log.Errorf("Authentication Failed %s username=[%s]", perm, username)
http.Error(w, "Not authenticated in apiserver", 401)
return "", errors.New("Not Authenticated")
}
if !BasicAuthzCheck(username, perm) {
log.Errorf("Authentication Failed %s username=[%s]", perm, username)
http.Error(w, "Not authorized for this apiserver action", 401)
return "", errors.New("Not authorized for this apiserver action")
}
log.Debug("Authentication Success")
return username, err
}
func validContainerResourcesSettings() bool {
log.Infof("ContainerResources has %d definitions", len(Pgo.ContainerResources))
//validate any Container Resources in pgo.yaml for correct formats
if !IsValidContainerResourceValues() {
return false
}
drs := Pgo.DefaultContainerResources
if drs == "" {
log.Info("DefaultContainerResources was not specified in pgo.yaml, so no container resources will be specified")
return true
}
//validate the DefaultContainerResource value
if IsValidContainerResource(drs) {
log.Info(drs + " is valid")
} else {
log.Error(drs + " is NOT valid")
return false
}
return true
}
func validStorageSettings() bool {
log.Infof("Storage has %d definitions", len(Pgo.Storage))
ps := Pgo.PrimaryStorage
if IsValidStorageName(ps) {
log.Info(ps + " is valid")
} else {
log.Error(ps + " is NOT valid")
return false
}
rs := Pgo.ReplicaStorage
if IsValidStorageName(rs) {
log.Info(rs + " is valid")
} else {
log.Error(rs + " is NOT valid")
return false
}
bs := Pgo.BackupStorage
if IsValidStorageName(bs) {
log.Info(bs + " is valid")
} else {
log.Error(bs + " is NOT valid")
return false
}
return true
}
func IsValidContainerResource(name string) bool {
_, ok := Pgo.ContainerResources[name]
return ok
}
func IsValidStorageName(name string) bool {
_, ok := Pgo.Storage[name]
return ok
}
// ValidateNodeLabel
// returns error if node label is invalid
func ValidateNodeLabel(nodeLabel string) error {
parts := strings.Split(nodeLabel, "=")
if len(parts) != 2 {
return errors.New(nodeLabel + " node label does not follow key=value format")
}
keyValid, valueValid, err := IsValidNodeLabel(parts[0], parts[1])
if err != nil {
return err
}
if !keyValid {
return errors.New(nodeLabel + " key was not valid .. check node labels for correct values to specify")
}
if !valueValid {
return errors.New(nodeLabel + " node label value was not valid .. check node labels for correct values to specify")
}
return nil
}
// IsValidNodeLabel
// returns bool for key validity
// returns bool for value validity
// returns error
func IsValidNodeLabel(key, value string) (bool, bool, error) {
var err error
keyValid := false
valueValid := false
nodes, err := kubeapi.GetAllNodes(Clientset)
if err != nil {
return false, false, err
}
for _, node := range nodes.Items {
if val, exists := node.ObjectMeta.Labels[key]; exists {
keyValid = true
if val == value {
valueValid = true
}
}
}
return keyValid, valueValid, err
}
func IsValidContainerResourceValues() bool {
var err error
for k, v := range Pgo.ContainerResources {
log.Infof("Container Resources %s [%v]", k, v)
resources, _ := Pgo.GetContainerResource(k)
_, err = resource.ParseQuantity(resources.RequestsMemory)
if err != nil {
log.Errorf("%s.RequestsMemory value invalid format", k)
return false
}
_, err = resource.ParseQuantity(resources.RequestsCPU)
if err != nil {
log.Errorf("%s.RequestsCPU value invalid format", k)
return false
}
_, err = resource.ParseQuantity(resources.LimitsMemory)
if err != nil {
log.Errorf("%s.LimitsMemory value invalid format", k)
return false
}
_, err = resource.ParseQuantity(resources.LimitsCPU)
if err != nil {
log.Errorf("%s.LimitsCPU value invalid format", k)
return false
}
}
return true
}
func validateWithKube() {
log.Debug("validateWithKube called")
configNodeLabels := make([]string, 2)
configNodeLabels[0] = Pgo.Cluster.PrimaryNodeLabel
configNodeLabels[1] = Pgo.Cluster.ReplicaNodeLabel
for _, n := range configNodeLabels {
//parse & validate pgo.yaml node labels if set
if n != "" {
if err := ValidateNodeLabel(n); err != nil {
log.Error(n + " node label specified in pgo.yaml is invalid")
log.Error(err)
os.Exit(2)
}
log.Debugf("%s is a valid pgo.yaml node label default", n)
}
}
err := util.ValidateNamespaces(Clientset)
if err != nil {
log.Error(err)
os.Exit(2)
}
}
// GetContainerResources ...
func GetContainerResourcesJSON(resources *crv1.PgContainerResources) string {
//test for the case where no container resources are specified
if resources.RequestsMemory == "" || resources.RequestsCPU == "" ||
resources.LimitsMemory == "" || resources.LimitsCPU == "" {
return ""
}
fields := containerResourcesTemplateFields{}
fields.RequestsMemory = resources.RequestsMemory
fields.RequestsCPU = resources.RequestsCPU
fields.LimitsMemory = resources.LimitsMemory
fields.LimitsCPU = resources.LimitsCPU
doc := bytes.Buffer{}
err := config.ContainerResourcesTemplate.Execute(&doc, fields)
if err != nil {
log.Error(err.Error())
return ""
}
if log.GetLevel() == log.DebugLevel {
config.ContainerResourcesTemplate.Execute(os.Stdout, fields)
}
return doc.String()
}
func UserIsPermittedInNamespace(username, requestedNS string) bool {
detail := Credentials[username]
//handle the case of a user in pgouser with "" (all) namespaces
if len(detail.Namespaces) == 1 {
if detail.Namespaces[0] == "" {
return true
}
}
for i := 0; i < len(detail.Namespaces); i++ {
if detail.Namespaces[i] == requestedNS {
return true
}
}
return false
}
//validate or generate the TLS keys
func GetTLS(certPath, keyPath string) error {
var pgoSecret *v1.Secret
var found bool
var err error
pgoSecret, found, err = kubeapi.GetSecret(Clientset, PGOSecretName, PgoNamespace)
if found {
log.Infof("%s Secret found in namespace %s", PGOSecretName, PgoNamespace)
log.Infof("cert key data len is %d", len(pgoSecret.Data[v1.TLSCertKey]))
if err := ioutil.WriteFile(certPath, pgoSecret.Data[v1.TLSCertKey], 0644); err != nil {
return err
}
log.Infof("private key data len is %d", len(pgoSecret.Data[v1.TLSPrivateKeyKey]))
if err := ioutil.WriteFile(keyPath, pgoSecret.Data[v1.TLSPrivateKeyKey], 0644); err != nil {
return err
}
} else {
log.Infof("%s Secret NOT found in namespace %s", PGOSecretName, PgoNamespace)
err = generateTLS(certPath, keyPath)
if err != nil {
log.Error("error generating pgo.tls Secret")
return err
}
}
return nil
}
// generate a self signed cert and the pgo.tls Secret to hold it
func generateTLS(certPath, keyPath string) error {
var err error
//generate private key
var privateKey *rsa.PrivateKey
privateKey, err = tlsutil.NewPrivateKey()
if err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
privateKeyBytes := tlsutil.EncodePrivateKeyPEM(privateKey)
log.Debugf("generated privateKeyBytes len %d", len(privateKeyBytes))
var caCert *x509.Certificate
caCert, err = tlsutil.NewSelfSignedCACertificate(privateKey)
if err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
caCertBytes := tlsutil.EncodeCertificatePEM(caCert)
log.Debugf("generated caCertBytes len %d", len(caCertBytes))
// CreateSecret
newSecret := v1.Secret{}
newSecret.Name = PGOSecretName
newSecret.ObjectMeta.Labels = make(map[string]string)
newSecret.ObjectMeta.Labels[config.LABEL_VENDOR] = "crunchydata"
newSecret.Data = make(map[string][]byte)
newSecret.Data[v1.TLSCertKey] = caCertBytes
newSecret.Data[v1.TLSPrivateKeyKey] = privateKeyBytes
newSecret.Type = v1.SecretTypeTLS
err = kubeapi.CreateSecret(Clientset, &newSecret, PgoNamespace)
if err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
if err := ioutil.WriteFile(certPath, newSecret.Data[v1.TLSCertKey], 0644); err != nil {
return err
}
if err := ioutil.WriteFile(keyPath, newSecret.Data[v1.TLSPrivateKeyKey], 0644); err != nil {
return err
}
return err
}