-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathsecret.go
116 lines (96 loc) · 3.46 KB
/
secret.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
/*
Copyright 2021 RadonDB.
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 syncer
import (
"fmt"
"github.com/presslabs/controller-util/pkg/rand"
"github.com/presslabs/controller-util/pkg/syncer"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/radondb/radondb-mysql-kubernetes/mysqlcluster"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)
const (
// The length of the secret string.
rStrLen = 12
)
// NewSecretSyncer returns secret syncer.
func NewSecretSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.Interface {
secret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: c.GetNameForResource(utils.Secret),
Namespace: c.Namespace,
},
}
return syncer.NewObjectSyncer("Secret", c.Unwrap(), secret, cli, func() error {
if secret.Data == nil {
secret.Data = make(map[string][]byte)
}
secret.Data["operator-user"] = []byte(utils.OperatorUser)
if err := addRandomPassword(secret.Data, "operator-password"); err != nil {
return err
}
secret.Data["donor-user"] = []byte(utils.DonorCloneUser)
if err := addRandomPassword(secret.Data, "donor-password"); err != nil {
return err
}
// xtrabackup http server user and password
secret.Data["backup-user"] = []byte(utils.BackupUser)
if err := addRandomPassword(secret.Data, "backup-password"); err != nil {
return err
}
secret.Data["metrics-user"] = []byte(utils.MetricsUser)
if err := addRandomPassword(secret.Data, "metrics-password"); err != nil {
return err
}
if c.Spec.MetricsOpts.Enabled {
dataSource := fmt.Sprintf("%s:%s@(localhost:3306)/", utils.MetricsUser, utils.BytesToString(secret.Data["metrics-password"]))
secret.Data["data-source"] = []byte(dataSource)
}
secret.Data["replication-user"] = []byte(utils.ReplicationUser)
if err := addRandomPassword(secret.Data, "replication-password"); err != nil {
return err
}
if err := addRandomPassword(secret.Data, "internal-root-password"); err != nil {
return err
}
if c.Spec.MysqlOpts.RootHost != "localhost" && c.Spec.MysqlOpts.RootPassword == "" {
if err := addRandomPassword(secret.Data, "root-password"); err != nil {
return err
}
} else {
secret.Data["root-password"] = []byte(c.Spec.MysqlOpts.RootPassword)
}
secret.Data["mysql-user"] = []byte(c.Spec.MysqlOpts.User)
secret.Data["mysql-password"] = []byte(c.Spec.MysqlOpts.Password)
secret.Data["mysql-database"] = []byte(c.Spec.MysqlOpts.Database)
return nil
})
}
// addRandomPassword checks if a key exists and if not registers a random string for that key
func addRandomPassword(data map[string][]byte, key string) error {
if len(data[key]) == 0 {
// NOTE: use only alpha-numeric string, this strings are used unescaped in MySQL queries.
random, err := rand.AlphaNumericString(rStrLen)
if err != nil {
return err
}
data[key] = []byte(random)
}
return nil
}