-
Notifications
You must be signed in to change notification settings - Fork 2k
/
init.go
273 lines (246 loc) · 7.3 KB
/
init.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
/*
Copyright 2023.
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 util
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"os"
"time"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
)
const MaxRetryForConnectDB = 5
type RegisterRequest struct {
UID string `json:"uid"`
}
type K8sUser struct {
Name string `bson:"name" json:"name"`
}
// the following code is the struct of user stored in mongoDB.
type User struct {
UID string `bson:"uid" json:"uid"`
Name string `bson:"name" json:"name"`
PasswordUser string `bson:"password_user" json:"password_user"`
Password string `bson:"password" json:"password"`
CreatedTime string `bson:"created_time" json:"created_time"`
K8sUsers []K8sUser `bson:"k8s_users" json:"k8s_users"`
}
func newUser(uid, name, passwordUser, password, k8sUser string) User {
return User{
UID: uid,
Name: name,
PasswordUser: passwordUser,
Password: password,
// to iso string
CreatedTime: time.Now().Format(time.RFC3339),
K8sUsers: []K8sUser{
{
Name: k8sUser,
},
},
}
}
// The following code is used to implement the instance of sub-task which is used to
// initialize the cluster uuid and preset root user for mongoDB
type initTask struct {
options Options
}
func (t initTask) initWork(instance *TaskInstance) error {
err := t.register(instance)
if err != nil {
instance.logger.Error(err, "failed to register")
return err
}
return nil
}
// 1. preset root user
// 2. check if the cluster has been registered
// 3. register to cloud (if the monitor is enabled)
// 4. store cluster-info to k8s
const maxRetry = 5
func (t initTask) register(instance *TaskInstance) error {
var err error
// step 1
for cnt := 0; cnt < maxRetry; cnt++ {
err = t.presetRootUser(instance)
if err != nil {
instance.logger.Error(err, "failed to preset root user")
time.Sleep(time.Minute)
continue
}
break
}
if err != nil {
return err
}
ClusterInfo := createClusterInfo()
// step 2
// check if the cluster has been registered
registered, err := t.checkRegister(instance)
if err != nil {
instance.logger.Error(err, "failed to check register")
return err
}
if registered {
instance.logger.Info("cluster has been registered")
return nil
}
// step 3
// if the monitor is enabled, info will be sent to the cloud.
if t.options.GetEnvOptions().MonitorConfiguration == "true" {
err := t.registerToCloud(*ClusterInfo, instance)
if err != nil {
instance.logger.Error(err, "failed to register to cloud")
return err
}
}
// step 4
// only after register to cloud, the store-behavior will be executed.
err = instance.Create(instance.ctx, ClusterInfo)
if err != nil {
instance.logger.Error(err, "failed to create cluster-info")
}
return err
}
// check if the cluster has been registered.
func (t initTask) checkRegister(instance *TaskInstance) (bool, error) {
info := &corev1.Secret{}
err := instance.Get(instance.ctx, types.NamespacedName{
Name: ClusterInfo,
Namespace: GetOptions().GetEnvOptions().Namespace,
}, info)
if err != nil && apierrors.IsNotFound(err) {
return false, nil
}
return true, err
}
// registerToCloud is used to send info to the cloud.
func (t initTask) registerToCloud(ClusterInfo corev1.Secret, instance *TaskInstance) error {
urlMap, err := GetURL(instance.ctx, instance.Client)
if err != nil {
return fmt.Errorf("failed to get url: %w", err)
}
rr := RegisterRequest{
UID: string(ClusterInfo.Data["uuid"]),
}
// send info to cloud
err = Push(urlMap[RegisterURL], rr)
if err != nil {
return fmt.Errorf("failed to write to cloud: %w", err)
}
return nil
}
func createClusterInfo() *corev1.Secret {
uuid := uuid.New().String()
secret := &corev1.Secret{}
secret.Name = ClusterInfo
secret.Namespace = GetOptions().GetEnvOptions().Namespace
secret.Data = map[string][]byte{
"uuid": []byte(uuid),
}
return secret
}
// presetRootUser is used to preset root user for mongoDB.
func (t initTask) presetRootUser(instance *TaskInstance) error {
// init mongoDB client
client, err := t.initMongoDB(instance)
if err != nil {
return fmt.Errorf("failed to init mongoDB: %w", err)
}
defer func() {
err := client.Disconnect(instance.ctx)
if err != nil {
instance.logger.Error(err, "failed to disconnect mongoDB")
}
}()
// preset root user
uuid := uuid.New().String()
passwd := hashPassword(defaultPassword, t.options.GetEnvOptions().SaltKey)
user := newUser(uuid, defaultuser, defaultuser, passwd, defaultK8sUser)
collection := client.Database(defaultDB).Collection(defaultCollection)
// check if the user already exists
isExists := preCheck(instance.ctx, collection)
if isExists {
instance.logger.Info("root user already exists")
return nil
}
// insert root user
insertResult, err := collection.InsertOne(context.Background(), user)
if err != nil {
instance.logger.Info("insert root user failed")
return err
}
instance.logger.Info("insert root user successfully", "insertedID", insertResult.InsertedID)
return nil
}
// init mongoDB client to preset root user.
func (t initTask) initMongoDB(instance *TaskInstance) (*mongo.Client, error) {
var client *mongo.Client
var err error
MongoURI := t.options.GetEnvOptions().MongoURI
clientOptions := mongoOptions.Client().ApplyURI(MongoURI)
for i := 0; i < MaxRetryForConnectDB; i++ {
client, err = mongo.Connect(instance.ctx, clientOptions)
if err != nil {
instance.logger.Error(err, "failed to connect to mongo")
time.Sleep(5 * time.Second)
continue
}
err = client.Ping(instance.ctx, nil)
if err != nil {
instance.logger.Error(err, "failed to ping to mongo")
time.Sleep(5 * time.Second)
continue
}
instance.logger.Info("connect to mongo successfully")
break
}
if err != nil {
return nil, fmt.Errorf("failed to connect to mongo: %w", err)
}
return client, nil
}
// makesure the user does not exist
func preCheck(ctx context.Context, collection *mongo.Collection) bool {
filter := bson.M{"password_user": defaultuser}
var existingUser User
err := collection.FindOne(ctx, filter).Decode(&existingUser)
return err == nil
}
// the following code is consistent with the front-end login logic
func hashPassword(password string, key string) string {
hash := sha256.New()
validSalt, err := decodeBase64(key)
if err != nil {
fmt.Println("Error decoding salt:", err.Error())
os.Exit(1)
}
hash.Write([]byte(password + string(validSalt)))
return hex.EncodeToString(hash.Sum(nil))
}
func decodeBase64(s string) ([]byte, error) {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
fmt.Println("Error decoding string:", err.Error())
return nil, err
}
return data, nil
}