-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathcustom_users.go
More file actions
516 lines (419 loc) · 13.3 KB
/
custom_users.go
File metadata and controls
516 lines (419 loc) · 13.3 KB
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
package perconaservermongodb
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo"
s "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/secret"
)
func (r *ReconcilePerconaServerMongoDB) reconcileCustomUsers(ctx context.Context, cr *api.PerconaServerMongoDB) error {
if cr.Spec.Users == nil && len(cr.Spec.Users) == 0 && cr.Spec.Roles == nil && len(cr.Spec.Roles) == 0 {
return nil
}
if cr.Status.State != api.AppStateReady {
return nil
}
log := logf.FromContext(ctx)
var err error
var mongoCli mongo.Client
if cr.Spec.Sharding.Enabled {
mongoCli, err = r.mongosClientWithRole(ctx, cr, api.RoleUserAdmin)
} else {
mongoCli, err = r.mongoClientWithRole(ctx, cr, cr.Spec.Replsets[0], api.RoleUserAdmin)
}
if err != nil {
return errors.Wrap(err, "failed to get mongo client")
}
defer func() {
err := mongoCli.Disconnect(ctx)
if err != nil {
log.Error(err, "failed to close mongo connection")
}
}()
handleRoles(ctx, cr, mongoCli)
err = handleUsers(ctx, cr, mongoCli, r.client)
if err != nil {
return errors.Wrap(err, "handle users")
}
return nil
}
func handleUsers(ctx context.Context, cr *api.PerconaServerMongoDB, mongoCli mongo.Client, client client.Client) error {
log := logf.FromContext(ctx)
if len(cr.Spec.Users) == 0 {
return nil
}
systemUserNames, err := fetchSystemUserNames(ctx, cr, client)
if err != nil {
return err
}
uniqueUserNames := make(map[string]struct{}, len(cr.Spec.Users))
for _, user := range cr.Spec.Users {
err := validateUser(&user, systemUserNames, uniqueUserNames)
if err != nil {
log.Error(err, "invalid user", "user", user)
continue
}
userInfo, err := mongoCli.GetUserInfo(ctx, user.Name, user.DB)
if err != nil {
log.Error(err, "get user info")
continue
}
if user.IsExternalDB() && userInfo == nil {
err = createExternalUser(ctx, mongoCli, &user)
if err != nil {
return errors.Wrapf(err, "create user %s", user.Name)
}
continue
}
userSecretPassKey := user.Name
if user.PasswordSecretRef != nil {
userSecretPassKey = user.PasswordSecretRef.Key
}
sec, err := getCustomUserSecret(ctx, client, cr, &user, userSecretPassKey)
if err != nil {
log.Error(err, "failed to get user secret", "user", user)
continue
}
// Kubernetes annotation keys have a limit of 63 characters for the name part
// Format: "percona.com/<name>" where it must be less than or equal to 63 characters
// We need to keep the "-hash" suffix (5 chars), so we have 58 chars for the prefix
annotationKeyBase := fmt.Sprintf("percona.com/%s-%s", cr.Name, user.Name)
const hashSuffix = "-hash"
const maxAnnotationNameLength = 63
const maxPrefixLength = maxAnnotationNameLength - len(hashSuffix)
if len(annotationKeyBase) > maxPrefixLength {
annotationKeyBase = annotationKeyBase[:maxPrefixLength]
}
annotationKey := fmt.Sprintf("%s%s", annotationKeyBase, hashSuffix)
if userInfo == nil && !user.IsExternalDB() {
err = createUser(ctx, client, mongoCli, &user, sec, annotationKey, userSecretPassKey)
if err != nil {
return errors.Wrapf(err, "create user %s", user.Name)
}
continue
}
err = updatePass(ctx, client, mongoCli, &user, userInfo, sec, annotationKey, userSecretPassKey)
if err != nil {
log.Error(err, "update user pass", "user", user.Name)
continue
}
err = updateRoles(ctx, mongoCli, &user, userInfo)
if err != nil {
log.Error(err, "update user roles", "user", user.Name)
continue
}
}
return nil
}
func validateUser(user *api.User, sysUserNames, uniqueUserNames map[string]struct{}) error {
if sysUserNames == nil || uniqueUserNames == nil {
return errors.New("invalid sys or unique usernames config")
}
if _, reserved := sysUserNames[user.Name]; reserved {
return fmt.Errorf("creating user with reserved user name %s is forbidden", user.Name)
}
if _, exists := uniqueUserNames[user.Name]; exists {
return fmt.Errorf("username %s should be unique", user.Name)
}
uniqueUserNames[user.Name] = struct{}{}
if len(user.Roles) == 0 {
return fmt.Errorf("user %s must have at least one role", user.Name)
}
if user.DB == "" {
user.DB = "admin"
}
if user.PasswordSecretRef != nil && user.PasswordSecretRef.Key == "" {
user.PasswordSecretRef.Key = "password"
}
return nil
}
func fetchSystemUserNames(ctx context.Context, cr *api.PerconaServerMongoDB, client client.Client) (map[string]struct{}, error) {
sysUsersSecret := corev1.Secret{}
err := client.Get(ctx, types.NamespacedName{
Namespace: cr.Namespace,
Name: api.InternalUserSecretName(cr),
}, &sysUsersSecret)
if err != nil && !k8serrors.IsNotFound(err) {
return nil, errors.Wrap(err, "get internal sys users secret")
}
return sysUserNames(sysUsersSecret), nil
}
func handleRoles(ctx context.Context, cr *api.PerconaServerMongoDB, cli mongo.Client) {
log := logf.FromContext(ctx)
if len(cr.Spec.Roles) == 0 {
return
}
for _, role := range cr.Spec.Roles {
roleInfo, err := cli.GetRole(ctx, role.DB, role.Role)
if err != nil {
log.Error(err, "get role info", "role", role.Role)
continue
}
mr, err := toMongoRoleModel(role)
if err != nil {
log.Error(err, "to mongo role model", "role", role.Role)
continue
}
if roleInfo == nil {
log.Info("Creating role", "role", role.Role)
err := cli.CreateRole(ctx, role.DB, *mr)
if err != nil {
log.Error(err, "create role", "role", role.Role)
continue
}
log.Info("Role created", "role", role.Role)
continue
}
if rolesChanged(mr, roleInfo) {
log.Info("Updating role", "role", role.Role)
err := cli.UpdateRole(ctx, role.DB, *mr)
if err != nil {
log.Error(err, "update role %s", role.Role)
continue
}
log.Info("Role updated", "role", role.Role)
}
}
}
func rolesChanged(r1, r2 *mongo.Role) bool {
if len(r1.Privileges) != len(r2.Privileges) {
return true
}
if len(r1.AuthenticationRestrictions) != len(r2.AuthenticationRestrictions) {
return true
}
if len(r1.Roles) != len(r2.Roles) {
return true
}
opts := cmp.Options{
cmpopts.SortSlices(func(x, y string) bool { return x < y }),
cmpopts.EquateEmpty(),
}
if !cmp.Equal(r1.Privileges, r2.Privileges, opts) {
return true
}
if !cmp.Equal(r1.AuthenticationRestrictions, r2.AuthenticationRestrictions, opts) {
return true
}
if !cmp.Equal(r1.Roles, r2.Roles, opts) {
return true
}
return false
}
func toMongoRoleModel(role api.Role) (*mongo.Role, error) {
mr := &mongo.Role{
Role: role.Role,
DB: role.DB,
}
for _, r := range role.Roles {
mr.Roles = append(mr.Roles, mongo.InheritenceRole{
Role: r.Role,
DB: r.DB,
})
}
for _, p := range role.Privileges {
if p.Resource.Cluster != nil && (p.Resource.DB != "" || p.Resource.Collection != "") {
return nil, errors.New("field role.privilege.resource must have exactly db and collection set, or have only cluster set")
}
rp := mongo.RolePrivilege{
Actions: p.Actions,
Resource: make(map[string]interface{}, 3),
}
if p.Resource.Cluster != nil {
rp.Resource["cluster"] = *p.Resource.Cluster
} else {
rp.Resource["db"] = p.Resource.DB
rp.Resource["collection"] = p.Resource.Collection
}
mr.Privileges = append(mr.Privileges, rp)
}
if role.AuthenticationRestrictions != nil {
for _, ar := range role.AuthenticationRestrictions {
mr.AuthenticationRestrictions = append(mr.AuthenticationRestrictions, mongo.RoleAuthenticationRestriction{
ClientSource: ar.ClientSource,
ServerAddress: ar.ServerAddress,
})
}
} else {
mr.AuthenticationRestrictions = nil
}
return mr, nil
}
// sysUserNames returns a set of system usernames from the sysUsersSecret.
func sysUserNames(sysUsersSecret corev1.Secret) map[string]struct{} {
names := make(map[string]struct{}, len(sysUsersSecret.Data))
for k, v := range sysUsersSecret.Data {
if strings.Contains(k, "_USER") {
names[string(v)] = struct{}{}
}
}
return names
}
func updatePass(
ctx context.Context,
cli client.Client,
mongoCli mongo.Client,
user *api.User,
userInfo *mongo.User,
secret *corev1.Secret,
annotationKey, passKey string) error {
log := logf.FromContext(ctx)
if userInfo == nil || user.IsExternalDB() {
return nil
}
newHash := sha256Hash(secret.Data[passKey])
hash, ok := secret.Annotations[annotationKey]
if ok && hash == newHash {
return nil
}
if secret.Annotations == nil {
secret.Annotations = make(map[string]string)
}
log.Info("User password changed, updating it.", "user", user.UserID())
err := mongoCli.UpdateUserPass(ctx, user.DB, user.Name, string(secret.Data[passKey]))
if err != nil {
return errors.Wrapf(err, "update user %s password", user.Name)
}
secret.Annotations[annotationKey] = string(newHash)
if err := cli.Update(ctx, secret); err != nil {
return errors.Wrapf(err, "update secret %s", secret.Name)
}
log.Info("User updated", "user", user.UserID())
return nil
}
func updateRoles(ctx context.Context, mongoCli mongo.Client, user *api.User, userInfo *mongo.User) error {
log := logf.FromContext(ctx)
if userInfo == nil {
return nil
}
roles := make([]mongo.Role, 0)
for _, role := range user.Roles {
roles = append(roles, mongo.Role{DB: role.DB, Role: role.Name})
}
if reflect.DeepEqual(userInfo.Roles, roles) {
return nil
}
log.Info("User roles changed, updating them.", "user", user.UserID())
err := mongoCli.UpdateUserRoles(ctx, user.DB, user.Name, roles)
if err != nil {
return err
}
return nil
}
// createExternalUser creates a user with $external database authentication method.
func createExternalUser(ctx context.Context, mongoCli mongo.Client, user *api.User) error {
log := logf.FromContext(ctx)
roles := make([]mongo.Role, 0)
for _, role := range user.Roles {
roles = append(roles, mongo.Role{DB: role.DB, Role: role.Name})
}
log.Info("Creating user", "user", user.UserID())
err := mongoCli.CreateUser(ctx, user.DB, user.Name, "", roles...)
if err != nil {
return err
}
log.Info("User created", "user", user.UserID())
return nil
}
func createUser(
ctx context.Context,
cli client.Client,
mongoCli mongo.Client,
user *api.User,
secret *corev1.Secret,
annotationKey, passKey string) error {
log := logf.FromContext(ctx)
roles := make([]mongo.Role, 0)
for _, role := range user.Roles {
roles = append(roles, mongo.Role{DB: role.DB, Role: role.Name})
}
log.Info("Creating user", "user", user.UserID())
err := mongoCli.CreateUser(ctx, user.DB, user.Name, string(secret.Data[passKey]), roles...)
if err != nil {
return err
}
if secret.Annotations == nil {
secret.Annotations = make(map[string]string)
}
secret.Annotations[annotationKey] = string(sha256Hash(secret.Data[passKey]))
if err := cli.Update(ctx, secret); err != nil {
return err
}
log.Info("User created", "user", user.UserID())
return nil
}
// getCustomUserSecret gets secret by name defined by `user.PasswordSecretRef.Name` or returns a secret
// with newly generated password if name matches defaultName
func getCustomUserSecret(ctx context.Context, cl client.Client, cr *api.PerconaServerMongoDB, user *api.User, passKey string) (*corev1.Secret, error) {
log := logf.FromContext(ctx)
if user.IsExternalDB() {
return nil, nil
}
defaultSecretName := fmt.Sprintf("%s-custom-user-secret", cr.Name)
secretName := defaultSecretName
if user.PasswordSecretRef != nil {
secretName = user.PasswordSecretRef.Name
}
secret := &corev1.Secret{}
err := cl.Get(ctx, types.NamespacedName{Name: secretName, Namespace: cr.Namespace}, secret)
if err != nil && secretName != defaultSecretName {
return nil, errors.Wrap(err, "failed to get user secret")
}
if err != nil && !k8serrors.IsNotFound(err) && secretName == defaultSecretName {
return nil, errors.Wrap(err, "failed to get user secret")
}
if err != nil && k8serrors.IsNotFound(err) {
secret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: cr.Namespace,
},
}
pass, err := s.GeneratePassword()
if err != nil {
return nil, errors.Wrap(err, "generate custom user password")
}
if secret.Data == nil {
secret.Data = make(map[string][]byte)
}
secret.Data[passKey] = pass
err = cl.Create(ctx, secret)
if err != nil {
return nil, errors.Wrap(err, "create custom users secret")
}
log.Info("Created custom user secrets", "secrets", secret.Name)
}
_, hasPass := secret.Data[passKey]
if !hasPass && secretName == defaultSecretName {
pass, err := s.GeneratePassword()
if err != nil {
return nil, errors.Wrap(err, "generate custom user password")
}
if secret.Data == nil {
secret.Data = make(map[string][]byte)
}
secret.Data[passKey] = pass
err = cl.Update(ctx, secret)
if err != nil {
return nil, errors.Wrap(err, "failed to update user secret")
}
// given that the secret was updated, the password now exists
hasPass = true
}
// pass key should be present in the user provided secret
if !hasPass {
return nil, errors.New("password key not found in secret")
}
return secret, nil
}