-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
185 lines (160 loc) · 4.95 KB
/
token.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
package clusterauthtoken
import (
"reflect"
"sort"
"github.com/rancher/rancher/pkg/controllers/user/clusterauthtoken/common"
clusterv3 "github.com/rancher/types/apis/cluster.cattle.io/v3"
managementv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"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 tokenAttributeCompare struct {
username string
expiresAt string
enabled bool
}
type tokenHandler struct {
namespace string
clusterAuthToken clusterv3.ClusterAuthTokenInterface
clusterAuthTokenLister clusterv3.ClusterAuthTokenLister
clusterUserAttribute clusterv3.ClusterUserAttributeInterface
clusterUserAttributeLister clusterv3.ClusterUserAttributeLister
tokenIndexer cache.Indexer
userLister managementv3.UserLister
userAttributeLister managementv3.UserAttributeLister
}
func (h *tokenHandler) Create(token *managementv3.Token) (runtime.Object, error) {
_, err := h.clusterAuthTokenLister.Get(h.namespace, token.Name)
if !errors.IsNotFound(err) {
return h.Updated(token)
}
err = h.updateClusterUserAttribute(token)
if err != nil {
return nil, err
}
clusterAuthToken, err := common.NewClusterAuthToken(token)
if err != nil {
return nil, err
}
_, err = h.clusterAuthToken.Create(clusterAuthToken)
return nil, err
}
func (h *tokenHandler) Updated(token *managementv3.Token) (runtime.Object, error) {
clusterAuthToken, err := h.clusterAuthTokenLister.Get(h.namespace, token.Name)
if errors.IsNotFound(err) {
return h.Create(token)
}
if err != nil {
return nil, err
}
err = h.updateClusterUserAttribute(token)
if err != nil {
return nil, err
}
tokenEnabled := token.Enabled == nil || *token.Enabled
current := tokenAttributeCompare{
enabled: tokenEnabled,
expiresAt: token.ExpiresAt,
username: token.UserID,
}
old := tokenAttributeCompare{
enabled: clusterAuthToken.Enabled,
expiresAt: clusterAuthToken.ExpiresAt,
username: clusterAuthToken.UserName,
}
if reflect.DeepEqual(current, old) {
return nil, nil
}
clusterAuthToken.UserName = token.UserID
clusterAuthToken.Enabled = tokenEnabled
clusterAuthToken.ExpiresAt = token.ExpiresAt
_, err = h.clusterAuthToken.Update(clusterAuthToken)
if errors.IsNotFound(err) {
_, err = h.clusterAuthToken.Create(clusterAuthToken)
}
return nil, err
}
func (h *tokenHandler) Remove(token *managementv3.Token) (runtime.Object, error) {
tokens, err := h.tokenIndexer.ByIndex(tokenByUserAndClusterIndex, tokenUserClusterKey(token))
if err != nil && !errors.IsNotFound(err) {
return nil, err
}
err = h.clusterAuthToken.Delete(token.Name, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return nil, err
}
if len(tokens) == 1 {
lastToken := tokens[0].(*managementv3.Token)
if token.Name == lastToken.Name {
// we are about to remove the last token for this user & cluster,
// also remove user data from cluster
err = h.clusterUserAttribute.Delete(token.UserID, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return nil, err
}
}
}
return nil, nil
}
func (h *tokenHandler) updateClusterUserAttribute(token *managementv3.Token) error {
userID := token.UserID
user, err := h.userLister.Get("", userID)
if err != nil {
return err
}
userAttribute, err := h.userAttributeLister.Get("", userID)
if err != nil {
return err
}
var groups []string
for _, gp := range userAttribute.GroupPrincipals {
for i := range gp.Items {
groups = append(groups, gp.Items[i].Name)
}
}
sort.Strings(groups)
userEnabled := user.Enabled == nil || *user.Enabled
clusterUserAttribute, err := h.clusterUserAttributeLister.Get(h.namespace, userID)
if errors.IsNotFound(err) {
_, err = h.clusterUserAttribute.Create(&clusterv3.ClusterUserAttribute{
ObjectMeta: metav1.ObjectMeta{
Name: userID,
},
TypeMeta: metav1.TypeMeta{
Kind: "ClusterUserAttribute",
},
Groups: groups,
Enabled: userEnabled,
LastRefresh: userAttribute.LastRefresh,
NeedsRefresh: userAttribute.NeedsRefresh,
})
return err
}
if err != nil {
return err
}
current := userAttributeCompare{
groups: groups,
lastRefresh: userAttribute.LastRefresh,
needsRefresh: userAttribute.NeedsRefresh,
enabled: userEnabled,
}
old := userAttributeCompare{
groups: clusterUserAttribute.Groups,
lastRefresh: clusterUserAttribute.LastRefresh,
needsRefresh: clusterUserAttribute.NeedsRefresh,
enabled: clusterUserAttribute.Enabled,
}
if reflect.DeepEqual(current, old) {
return nil
}
clusterUserAttribute = clusterUserAttribute.DeepCopy()
clusterUserAttribute.Groups = groups
clusterUserAttribute.Enabled = userEnabled
clusterUserAttribute.LastRefresh = userAttribute.LastRefresh
clusterUserAttribute.NeedsRefresh = userAttribute.NeedsRefresh
_, err = h.clusterUserAttribute.Update(clusterUserAttribute)
return err
}