forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
402 lines (332 loc) · 11.4 KB
/
user.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
package auth
import (
"fmt"
"strings"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"github.com/sirupsen/logrus"
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
type userLifecycle struct {
prtb v3.ProjectRoleTemplateBindingInterface
crtb v3.ClusterRoleTemplateBindingInterface
grb v3.GlobalRoleBindingInterface
users v3.UserInterface
tokens v3.TokenInterface
namespaces v1.NamespaceInterface
namespaceLister v1.NamespaceLister
secrets v1.SecretInterface
secretsLister v1.SecretLister
prtbLister v3.ProjectRoleTemplateBindingLister
crtbLister v3.ClusterRoleTemplateBindingLister
grbLister v3.GlobalRoleBindingLister
prtbIndexer cache.Indexer
crtbIndexer cache.Indexer
grbIndexer cache.Indexer
tokenIndexer cache.Indexer
userManager user.Manager
}
const (
crtbByUserRefKey = "auth.management.cattle.io/crtb-by-user-ref"
prtbByUserRefKey = "auth.management.cattle.io/prtb-by-user-ref"
grbByUserRefKey = "auth.management.cattle.io/grb-by-user-ref"
tokenByUserRefKey = "auth.management.cattle.io/token-by-user-ref"
userController = "mgmt-auth-users-controller"
)
func newUserLifecycle(management *config.ManagementContext) *userLifecycle {
lfc := &userLifecycle{
prtb: management.Management.ProjectRoleTemplateBindings(""),
crtb: management.Management.ClusterRoleTemplateBindings(""),
grb: management.Management.GlobalRoleBindings(""),
users: management.Management.Users(""),
tokens: management.Management.Tokens(""),
namespaces: management.Core.Namespaces(""),
secrets: management.Core.Secrets(""),
secretsLister: management.Core.Secrets("").Controller().Lister(),
prtbLister: management.Management.ProjectRoleTemplateBindings("").Controller().Lister(),
crtbLister: management.Management.ClusterRoleTemplateBindings("").Controller().Lister(),
grbLister: management.Management.GlobalRoleBindings("").Controller().Lister(),
namespaceLister: management.Core.Namespaces("").Controller().Lister(),
userManager: management.UserManager,
}
prtbInformer := management.Management.ProjectRoleTemplateBindings("").Controller().Informer()
prtbInformer.AddIndexers(map[string]cache.IndexFunc{
prtbByUserRefKey: prtbByUserRefFunc,
})
lfc.prtbIndexer = prtbInformer.GetIndexer()
crtbInformer := management.Management.ClusterRoleTemplateBindings("").Controller().Informer()
crtbInformer.AddIndexers(map[string]cache.IndexFunc{
crtbByUserRefKey: crtbByUserRefFunc,
})
lfc.crtbIndexer = crtbInformer.GetIndexer()
grbInformer := management.Management.GlobalRoleBindings("").Controller().Informer()
grbInformer.AddIndexers(map[string]cache.IndexFunc{
grbByUserRefKey: grbByUserRefFunc,
})
lfc.grbIndexer = grbInformer.GetIndexer()
tokenInformer := management.Management.Tokens("").Controller().Informer()
tokenInformer.AddIndexers(map[string]cache.IndexFunc{
tokenByUserRefKey: tokenByUserRefFunc,
})
lfc.tokenIndexer = tokenInformer.GetIndexer()
return lfc
}
func grbByUserRefFunc(obj interface{}) ([]string, error) {
globalRoleBinding, ok := obj.(*v3.GlobalRoleBinding)
if !ok {
return []string{}, nil
}
return []string{globalRoleBinding.UserName}, nil
}
func prtbByUserRefFunc(obj interface{}) ([]string, error) {
projectRoleBinding, ok := obj.(*v3.ProjectRoleTemplateBinding)
if !ok || projectRoleBinding.UserName == "" {
return []string{}, nil
}
return []string{projectRoleBinding.UserName}, nil
}
func crtbByUserRefFunc(obj interface{}) ([]string, error) {
clusterRoleBinding, ok := obj.(*v3.ClusterRoleTemplateBinding)
if !ok || clusterRoleBinding.UserName == "" {
return []string{}, nil
}
return []string{clusterRoleBinding.UserName}, nil
}
func tokenByUserRefFunc(obj interface{}) ([]string, error) {
token, ok := obj.(*v3.Token)
if !ok {
return []string{}, nil
}
return []string{token.UserID}, nil
}
func (l *userLifecycle) Create(user *v3.User) (*v3.User, error) {
var match = false
for _, id := range user.PrincipalIDs {
if strings.HasPrefix(id, "local://") {
match = true
break
}
}
if !match {
user.PrincipalIDs = append(user.PrincipalIDs, "local://"+user.Name)
}
// creatorIDAnn indicates it was created through the API, create the new
// user bindings and add the annotation UserConditionInitialRolesPopulated
if user.ObjectMeta.Annotations[creatorIDAnn] != "" {
u, err := v3.UserConditionInitialRolesPopulated.DoUntilTrue(user, func() (runtime.Object, error) {
err := l.userManager.CreateNewUserClusterRoleBinding(user.Name, user.UID)
if err != nil {
return nil, err
}
return user, nil
})
if err != nil {
return nil, err
}
user = u.(*v3.User)
}
return user, nil
}
func (l *userLifecycle) Updated(user *v3.User) (*v3.User, error) {
err := l.userManager.CreateNewUserClusterRoleBinding(user.Name, user.UID)
if err != nil {
return nil, err
}
return user, nil
}
func (l *userLifecycle) Remove(user *v3.User) (*v3.User, error) {
clusterRoles, err := l.getCRTBByUserName(user.Name)
if err != nil {
return nil, err
}
err = l.deleteAllCRTB(clusterRoles)
if err != nil {
return nil, err
}
projectRoles, err := l.getPRTBByUserName(user.Name)
if err != nil {
return nil, err
}
err = l.deleteAllPRTB(projectRoles)
if err != nil {
return nil, err
}
globalRoles, err := l.getGRBByUserName(user.Name)
if err != nil {
return nil, err
}
err = l.deleteAllGRB(globalRoles)
if err != nil {
return nil, err
}
tokens, err := l.getTokensByUserName(user.Name)
if err != nil {
return nil, err
}
err = l.deleteAllTokens(tokens)
if err != nil {
return nil, err
}
err = l.deleteUserNamespace(user.Name)
if err != nil {
return nil, err
}
err = l.deleteUserSecret(user.Name)
if err != nil {
return nil, err
}
return user, nil
}
func (l *userLifecycle) getCRTBByUserName(username string) ([]*v3.ClusterRoleTemplateBinding, error) {
obj, err := l.crtbIndexer.ByIndex(crtbByUserRefKey, username)
if err != nil {
return nil, fmt.Errorf("error getting cluster roles: %v", err)
}
var crtbs []*v3.ClusterRoleTemplateBinding
for _, o := range obj {
crtb, ok := o.(*v3.ClusterRoleTemplateBinding)
if !ok {
return nil, fmt.Errorf("error converting obj to cluster role template binding: %v", o)
}
crtbs = append(crtbs, crtb)
}
return crtbs, nil
}
func (l *userLifecycle) getPRTBByUserName(username string) ([]*v3.ProjectRoleTemplateBinding, error) {
objs, err := l.prtbIndexer.ByIndex(prtbByUserRefKey, username)
if err != nil {
return nil, fmt.Errorf("error getting indexed project roles: %v", err)
}
var prtbs []*v3.ProjectRoleTemplateBinding
for _, obj := range objs {
prtb, ok := obj.(*v3.ProjectRoleTemplateBinding)
if !ok {
return nil, fmt.Errorf("could not convert obj to v3.ProjectRoleTemplateBinding")
}
prtbs = append(prtbs, prtb)
}
return prtbs, nil
}
func (l *userLifecycle) getGRBByUserName(username string) ([]*v3.GlobalRoleBinding, error) {
objs, err := l.grbIndexer.ByIndex(grbByUserRefKey, username)
if err != nil {
return nil, fmt.Errorf("error getting indexed global roles: %v", err)
}
var grbs []*v3.GlobalRoleBinding
for _, obj := range objs {
grb, ok := obj.(*v3.GlobalRoleBinding)
if !ok {
return nil, fmt.Errorf("could not convert obj to v3.GlobalRoleBinding")
}
grbs = append(grbs, grb)
}
return grbs, nil
}
func (l *userLifecycle) getTokensByUserName(username string) ([]*v3.Token, error) {
objs, err := l.tokenIndexer.ByIndex(tokenByUserRefKey, username)
if err != nil {
return nil, fmt.Errorf("error getting indexed tokens: %v", err)
}
var tokens []*v3.Token
for _, obj := range objs {
token, ok := obj.(*v3.Token)
if !ok {
return nil, fmt.Errorf("could not convert to *v3.Token: %v", obj)
}
tokens = append(tokens, token)
}
return tokens, nil
}
func (l *userLifecycle) deleteAllCRTB(crtbs []*v3.ClusterRoleTemplateBinding) error {
for _, crtb := range crtbs {
var err error
if crtb.Namespace == "" {
logrus.Infof("[%v] Deleting clusterRoleTemplateBinding %v for user %v", userController, crtb.Name, crtb.UserName)
err = l.crtb.Delete(crtb.Name, &metav1.DeleteOptions{})
} else {
logrus.Infof("[%v] Deleting clusterRoleTemplateBinding %v for user %v", userController, crtb.Name, crtb.UserName)
err = l.crtb.DeleteNamespaced(crtb.Namespace, crtb.Name, &metav1.DeleteOptions{})
}
if err != nil {
return fmt.Errorf("error deleting cluster role: %v", err)
}
}
return nil
}
func (l *userLifecycle) deleteAllPRTB(prtbs []*v3.ProjectRoleTemplateBinding) error {
for _, prtb := range prtbs {
var err error
if prtb.Namespace == "" {
logrus.Infof("[%v] Deleting projectRoleTemplateBinding %v for user %v", userController, prtb.Name, prtb.UserName)
err = l.prtb.Delete(prtb.Name, &metav1.DeleteOptions{})
} else {
logrus.Infof("[%v] Deleting projectRoleTemplateBinding %v for user %v", userController, prtb.Name, prtb.UserName)
err = l.prtb.DeleteNamespaced(prtb.Namespace, prtb.Name, &metav1.DeleteOptions{})
}
if err != nil {
return fmt.Errorf("error deleting projet role: %v", err)
}
}
return nil
}
func (l *userLifecycle) deleteAllGRB(grbs []*v3.GlobalRoleBinding) error {
for _, grb := range grbs {
var err error
if grb.Namespace == "" {
logrus.Infof("[%v] Deleting globalRoleBinding %v for user %v", userController, grb.Name, grb.UserName)
err = l.grb.Delete(grb.Name, &metav1.DeleteOptions{})
} else {
logrus.Infof("[%v] Deleting globalRoleBinding %v for user %v", userController, grb.Name, grb.UserName)
err = l.grb.DeleteNamespaced(grb.Namespace, grb.Name, &metav1.DeleteOptions{})
}
if err != nil {
return fmt.Errorf("error deleting global role template %v: %v", grb.Name, err)
}
}
return nil
}
func (l *userLifecycle) deleteAllTokens(tokens []*v3.Token) error {
for _, token := range tokens {
logrus.Infof("[%v] Deleting token %v for user %v", userController, token.Name, token.UserID)
err := l.tokens.DeleteNamespaced(token.Namespace, token.Name, &metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("error deleting token: %v", err)
}
}
return nil
}
func (l *userLifecycle) deleteUserNamespace(username string) error {
namespace, err := l.namespaceLister.Get("", username)
if err != nil {
if errors.IsNotFound(err) {
return nil // nothing to delete
}
return fmt.Errorf("error getting user namespace: %v", err)
}
if namespace.Status.Phase == v12.NamespaceTerminating {
return nil // nothing to do namespace is already deleting
}
logrus.Infof("[%v] Deleting namespace backing user %v", userController, username)
err = l.namespaces.Delete(username, &metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("error deleting user namespace: %v", err)
}
return nil
}
func (l *userLifecycle) deleteUserSecret(username string) error {
_, err := l.secretsLister.Get("cattle-system", username+"-secret")
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("error getting user secret: %v", err)
}
logrus.Infof("[%v] Deleting secret backing user %v", userController, username)
return l.secrets.DeleteNamespaced("cattle-system", username+"-secret", &metav1.DeleteOptions{})
}