-
Notifications
You must be signed in to change notification settings - Fork 20
/
user_management.go
652 lines (555 loc) · 18.3 KB
/
user_management.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
// Copyright Contributors to the Open Cluster Management project
package common
import (
"context"
cryptoRand "crypto/rand"
"encoding/hex"
"fmt"
"math/rand"
"os"
"os/exec"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)
const ocpConfigNs = "openshift-config"
// k8sJSONPatch represents a Kubernetes patch of type JSON (i.e. types.JSONPatchType).
type k8sJSONPatch struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
// OCPUser represents an OpenShift user to be created on a cluster.
type OCPUser struct {
// If a namespace is not provided, a cluster role binding is created instead of a role binding.
ClusterRoles []types.NamespacedName
ClusterRoleBindings []string
Password string
Username string
Kubeconfig string
}
// GenerateInsecurePassword is a random password generator from 15-30 bytes. It is insecure
// since the characters are limited to just hex values (i.e. 1-9,a-f) from the random bytes. An
// error is returned if the random bytes cannot be read.
func GenerateInsecurePassword() (string, error) {
// A password ranging from 15-30 bytes
pwSize := rand.Intn(15) + 15
bytes := make([]byte, pwSize)
if _, err := cryptoRand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
// GetKubeConfig will generate a kubeconfig file based on an OpenShift user. The path of the
// kubeconfig file is returned. It is the responsibility of the caller to delete the kubeconfig file
// after use.
func GetKubeConfig(server, username, password string) (string, error) {
// Create a temporary file for the kubeconfig that the `oc login` command will generate
f, err := os.CreateTemp("", "e2e-kubeconfig")
if err != nil {
return "", fmt.Errorf("failed to create the temporary kubeconfig")
}
kubeconfigPath := f.Name()
// Close the file immediately so that the `oc login` command can use the file
err = f.Close()
if err != nil {
return "", fmt.Errorf("failed to close the temporary kubeconfig")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
cmd := exec.CommandContext(
ctx,
"oc",
"--kubeconfig="+kubeconfigPath,
"login",
"--server="+server,
"-u",
username,
"-p",
password,
"--insecure-skip-tls-verify=true",
)
// In some environments, `--kubeconfig` doesn't seem to be enough.
cmd.Env = append(os.Environ(), "KUBECONFIG="+kubeconfigPath)
output, err := cmd.CombinedOutput()
if err != nil {
os.Remove(kubeconfigPath)
return "", fmt.Errorf("failed to login with user '%s': %s", username, string(output))
}
return kubeconfigPath, nil
}
// Runs the given oc/kubectl command using the given OCPUser.
// Prints and returns the stdout from the command.
// If the command fails (non-zero exit code) and stderr was populated, that
// content will be returned in the error.
func OcUser(user OCPUser, args ...string) (string, error) {
args = append([]string{"--kubeconfig=" + user.Kubeconfig}, args...)
return oc(args...)
}
// CreateOCPUser will create an OpenShift user on a cluster, configure the identity provider for
// that user, and add the desired roles to the user. This function is idempotent.
func CreateOCPUser(
client kubernetes.Interface, dynamicClient dynamic.Interface, user OCPUser,
) error {
// Hash the password in the format expected by an htpasswd file.
passwordBytes, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf(
"failed to generate a bcrypt password hash for the user %s: %w", user.Username, err,
)
}
// Create a secret to hold the generated htpasswd file with the user's credentials.
htpasswd := []byte(fmt.Sprintf("%s:%s\n", user.Username, string(passwordBytes)))
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: user.Username},
Data: map[string][]byte{"htpasswd": htpasswd},
Type: corev1.SecretTypeOpaque,
}
_, err = client.CoreV1().Secrets(ocpConfigNs).Create(
context.TODO(), &secret, metav1.CreateOptions{},
)
if err != nil {
return fmt.Errorf("failed to create the secret %s/%s: %w", ocpConfigNs, user.Username, err)
}
// Configure the identity provider with this new htpasswd.
err = addHtPasswd(dynamicClient, user.Username)
if err != nil {
return fmt.Errorf(
"failed to configure the OpenShift identity provider for these users: %w", err,
)
}
// Add the desired roles to the new user.
err = addClusterRoleBindings(client, user)
if err != nil {
return err
}
return addClusterRoles(client, user)
}
// addHtPasswd will add the generated htpasswd in the input secret. The authentication name will be
// of the same name as the secret. If an identity provider of the same name is found, no action is
// taken.
func addHtPasswd(dynamicClient dynamic.Interface, secretName string) error {
const oAuthName = "cluster"
clusterOAuth, err := getClusterOAuthConfig(dynamicClient)
if err != nil {
return err
}
oauthRsrc := dynamicClient.Resource(GvrOAuth)
// The type was already validated in getClusterOAuthConfig
spec, _ := clusterOAuth.Object["spec"].(map[string]interface{})
// If spec.identityProviders is not set, it needs to first be set to an empty array for the
// patch below to work.
if _, ok := spec["identityProviders"]; !ok {
emptyArrayPatchObj := []k8sJSONPatch{
{
Op: "add",
Path: "/spec/identityProviders",
Value: []interface{}{},
},
}
emptyArrayPatch, err := json.Marshal(emptyArrayPatchObj)
if err != nil {
return fmt.Errorf("failed to marshal the empty array patch to JSON: %w", err)
}
_, err = oauthRsrc.Patch(
context.TODO(), oAuthName, types.JSONPatchType, emptyArrayPatch, metav1.PatchOptions{},
)
if err != nil {
return fmt.Errorf(`failed to patch the "%s" OAuth object: %w`, oAuthName, err)
}
} else {
idps, ok := spec["identityProviders"].([]interface{})
if !ok {
return fmt.Errorf(
`the "%s" OAuth object has an invalid spec.identityProviders field`, oAuthName,
)
}
for i, idp := range idps {
idp, ok := idp.(map[string]interface{})
if !ok {
return fmt.Errorf(
`the "%s" OAuth object has an invalid spec.identityProviders[%d] field`,
oAuthName,
i,
)
}
if name, _, _ := unstructured.NestedString(idp, "name"); name == secretName {
// An identity provider of the same name already exists, so assume it is correct.
return nil
}
}
}
identityPatchObj := []k8sJSONPatch{
{
Op: "add",
Path: "/spec/identityProviders/-",
Value: map[string]interface{}{
"name": secretName,
"mappingMethod": "claim",
"type": "HTPasswd",
"htpasswd": map[string]interface{}{
"fileData": map[string]interface{}{
"name": secretName,
},
},
},
},
}
identityPatch, err := json.Marshal(identityPatchObj)
if err != nil {
return fmt.Errorf("failed to marshal the identity provider patch to JSON: %w", err)
}
_, err = oauthRsrc.Patch(context.TODO(), oAuthName, types.JSONPatchType, identityPatch, metav1.PatchOptions{})
if err != nil {
return fmt.Errorf(
`failed to patch the "%s" OAuth object spec.identityProviders field: %w`,
oAuthName,
err,
)
}
return nil
}
// getClusterOAuthConfig gets the "cluster" OAuth object, which is used for identity provider
// configuration.
func getClusterOAuthConfig(dynamicClient dynamic.Interface) (*unstructured.Unstructured, error) {
oauthRsrc := dynamicClient.Resource(GvrOAuth)
const oAuthName = "cluster"
clusterOAuth, err := oauthRsrc.Get(context.TODO(), oAuthName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf(`failed to get the "%s" OAuth object: %w`, oAuthName, err)
}
_, ok := clusterOAuth.Object["spec"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf(`the "%s" OAuth object has an invalid spec`, oAuthName)
}
return clusterOAuth, nil
}
// AddClusterRoleBindings will add the user to the desired cluster role bindings without removing
// existing subjects. If the bindings are already set, nothing will occur.
func addClusterRoleBindings(client kubernetes.Interface, user OCPUser) error {
for _, binding := range user.ClusterRoleBindings {
bindingObj, err := client.RbacV1().ClusterRoleBindings().Get(
context.TODO(), binding, metav1.GetOptions{},
)
if err != nil {
return fmt.Errorf(
"failed to get the cluster role binding %s: %w", binding, err,
)
}
alreadySet := false
for _, subject := range bindingObj.Subjects {
if subject.APIGroup != "rbac.authorization.k8s.io" || subject.Kind != "User" {
continue
}
if subject.Name == user.Username {
alreadySet = true
break
}
}
if alreadySet {
continue
}
subject := map[string]interface{}{
"apiGroup": "rbac.authorization.k8s.io",
"kind": "User",
"name": user.Username,
}
var patchObj interface{}
var patchType types.PatchType
// A strategic merge patch is required when there are no subjects since the Kubernetes API
// rejects a JSON patch when there are no subjects set. Setting it first to an empty array
// also does not work since Kubernetes sinces to discard the empty array. The strategic
// merge patch works in this case, however, it does not work for the case when subjects
// is already set to one or more values. The Kubernetes API will just overwrite the
// entire subjects array in this case. This is why both patch types must be used.
if len(bindingObj.Subjects) == 0 {
patchType = types.StrategicMergePatchType
patchObj = map[string]interface{}{
"subjects": []map[string]interface{}{subject},
}
} else {
patchType = types.JSONPatchType
patchObj = []k8sJSONPatch{
{
Op: "add",
Path: "/subjects/-",
Value: subject,
},
}
}
patch, err := json.Marshal(patchObj)
if err != nil {
return fmt.Errorf(
"failed to marshal the cluster role binding patch to JSON for %s: %w",
binding,
err,
)
}
_, err = client.RbacV1().ClusterRoleBindings().Patch(
context.TODO(), binding, patchType, patch, metav1.PatchOptions{},
)
if err != nil {
return fmt.Errorf(
"failed to patch the cluster role binding %s: %w", binding, err,
)
}
}
return nil
}
// addClusterRoles will create role bindings/cluster role bindings named in the format of `<username>-<role>`,
// for the user with the desired cluster role. If no namespace is provided, cluster role bindings will be
// created instead of a role binding. If the binding already exists by the name, no action will be taken.
func addClusterRoles(client kubernetes.Interface, user OCPUser) error {
for _, role := range user.ClusterRoles {
bindingName := user.Username + "-" + role.Name
var err error
if role.Namespace == "" {
_, err = client.RbacV1().ClusterRoleBindings().Get(context.TODO(), bindingName, metav1.GetOptions{})
} else {
_, err = client.RbacV1().RoleBindings(role.Namespace).Get(context.TODO(), bindingName, metav1.GetOptions{})
}
if err == nil {
// Assume this is correct and skip creating the binding
continue
}
if !k8serrors.IsNotFound(err) {
return fmt.Errorf("failed to get the cluster role binding of %s: %w", bindingName, err)
}
roleRef := rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: role.Name,
}
subjectObjs := []rbacv1.Subject{
{
APIGroup: "rbac.authorization.k8s.io",
Kind: "User",
Name: user.Username,
},
}
if role.Namespace == "" {
binding := rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{Name: bindingName},
RoleRef: roleRef,
Subjects: subjectObjs,
}
_, err = client.RbacV1().ClusterRoleBindings().Create(
context.TODO(), &binding, metav1.CreateOptions{},
)
} else {
binding := rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{Name: bindingName},
RoleRef: roleRef,
Subjects: subjectObjs,
}
_, err = client.RbacV1().RoleBindings(role.Namespace).Create(
context.TODO(), &binding, metav1.CreateOptions{},
)
}
if err != nil {
return fmt.Errorf("failed to create the binding of %s: %w", bindingName, err)
}
}
return nil
}
// CleanupOCPUser will revert changes made to the cluster by the CreateOCPUser function.
func CleanupOCPUser(
client kubernetes.Interface, dynamicClient dynamic.Interface, user OCPUser,
) error {
err := deleteHtPasswd(dynamicClient, user.Username, user)
if err != nil {
return fmt.Errorf(
"failed to delete the OpenShift identity provider for the associated secret %s: %w",
user.Username,
err,
)
}
err = client.CoreV1().Secrets(ocpConfigNs).Delete(
context.TODO(), user.Username, metav1.DeleteOptions{},
)
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("failed to delete the secret %s/%s: %w", ocpConfigNs, user.Username, err)
}
err = removeClusterRoleBindings(client, user)
if err != nil {
return err
}
return removeClusterRoles(client, user)
}
// deleteHtPasswd deletes the htpasswd identity provider configuration entry of the input name and
// deletes the User and Identity objects created by OpenShift.
func deleteHtPasswd(dynamicClient dynamic.Interface, authName string, user OCPUser) error {
// Delete the User and Identity objects created by OpenShift
err := dynamicClient.Resource(GvrUser).Delete(
context.TODO(), user.Username, metav1.DeleteOptions{},
)
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf(
`failed to delete the OpenShift "User" of "%s": %w`, user.Username, err,
)
}
// Search for and delete any identities that have our user (which is also the IDP name)
identities, err := dynamicClient.Resource(GvrIdentity).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return fmt.Errorf(
"failed to retrieve the list of identities for cleanup: %w", err,
)
}
for _, identity := range identities.Items {
identityName := identity.GetName()
if strings.Contains(identityName, user.Username) {
err = dynamicClient.Resource(GvrIdentity).Delete(
context.TODO(), identityName, metav1.DeleteOptions{},
)
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf(
`failed to delete the OpenShift "Identity" of "%s": %w`, identityName, err,
)
}
}
}
// Remove the Identity Provider (IDP) from the OAuth object
const oAuthName = "cluster"
clusterOAuth, err := getClusterOAuthConfig(dynamicClient)
if err != nil {
return err
}
spec, ok := clusterOAuth.Object["spec"].(map[string]interface{})
if !ok {
return fmt.Errorf(`the "%s" OAuth object has an invalid spec`, oAuthName)
}
if _, ok := spec["identityProviders"]; !ok {
return nil
}
idps, ok := spec["identityProviders"].([]interface{})
if !ok {
return fmt.Errorf(
`the "%s" OAuth object has an invalid spec.identityProviders field`, oAuthName,
)
}
idpIndex := -1
for i, idp := range idps {
idp, ok := idp.(map[string]interface{})
if !ok {
return fmt.Errorf(
`the "%s" OAuth object has an invalid spec.identityProviders[%d] field`,
oAuthName,
i,
)
}
if name, _, _ := unstructured.NestedString(idp, "name"); name == authName {
// An identity provider of the same name already exists, so assume it is the one
// we are looking for.
idpIndex = i
break
}
}
if idpIndex < 0 {
// The identity provider was not found, so no action is needed.
return nil
}
identityPatchObj := []k8sJSONPatch{
{
Op: "remove",
Path: fmt.Sprintf("/spec/identityProviders/%d", idpIndex),
},
}
identityPatch, err := json.Marshal(identityPatchObj)
if err != nil {
return fmt.Errorf("failed to marshal the identity provider removal patch to JSON: %w", err)
}
oauthRsrc := dynamicClient.Resource(GvrOAuth)
_, err = oauthRsrc.Patch(context.TODO(), oAuthName, types.JSONPatchType, identityPatch, metav1.PatchOptions{})
if err != nil {
return fmt.Errorf(
`failed to remove the "%s" OAuth object spec.identityProviders[%d] entry: %w`,
oAuthName,
idpIndex,
err,
)
}
return nil
}
// removeClusterRoleBindings will remove the user from the desired cluster role bindings. No other
// subjects will be removed.
func removeClusterRoleBindings(client kubernetes.Interface, user OCPUser) error {
for _, binding := range user.ClusterRoleBindings {
bindingObj, err := client.RbacV1().ClusterRoleBindings().Get(
context.TODO(), binding, metav1.GetOptions{},
)
if err != nil {
return fmt.Errorf(
"failed to get the cluster role binding %s: %w", binding, err,
)
}
subjectIndex := -1
for i, subject := range bindingObj.Subjects {
if subject.APIGroup != "rbac.authorization.k8s.io" || subject.Kind != "User" {
continue
}
if subject.Name == user.Username {
subjectIndex = i
break
}
}
if subjectIndex == -1 {
continue
}
patchObj := []k8sJSONPatch{
{
Op: "remove",
Path: fmt.Sprintf("/subjects/%d", subjectIndex),
Value: map[string]interface{}{
"apiGroup": "rbac.authorization.k8s.io",
"kind": "User",
"name": user.Username,
},
},
}
patch, err := json.Marshal(patchObj)
if err != nil {
return fmt.Errorf(
"failed to marshal the cluster role binding delete patch to JSON for %s: %w",
binding,
err,
)
}
_, err = client.RbacV1().ClusterRoleBindings().Patch(
context.TODO(), binding, types.JSONPatchType, patch, metav1.PatchOptions{},
)
if err != nil {
return fmt.Errorf(
"failed to delete the user from the cluster role binding %s: %w", binding, err,
)
}
}
return nil
}
// removeClusterRoles will delete the generated cluster role bindings for the user that were
// created in addClusterRoles.
func removeClusterRoles(client kubernetes.Interface, user OCPUser) error {
for _, role := range user.ClusterRoles {
bindingName := user.Username + "-" + role.Name
var err error
if role.Namespace == "" {
err = client.RbacV1().ClusterRoleBindings().Delete(context.TODO(), bindingName, metav1.DeleteOptions{})
} else {
err = client.RbacV1().RoleBindings(role.Namespace).Delete(
context.TODO(), bindingName, metav1.DeleteOptions{},
)
}
if err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("failed to delete the binding of %s: %w", bindingName, err)
}
}
return nil
}