forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_store.go
190 lines (159 loc) · 4.69 KB
/
user_store.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
package authn
import (
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/cache"
)
const userByUsernameIndex = "auth.management.cattle.io/user-by-username"
type userStore struct {
types.Store
mu sync.Mutex
userIndexer cache.Indexer
userManager user.Manager
}
func SetUserStore(schema *types.Schema, mgmt *config.ScaledContext) {
userInformer := mgmt.Management.Users("").Controller().Informer()
userIndexers := map[string]cache.IndexFunc{
userByUsernameIndex: userByUsername,
}
userInformer.AddIndexers(userIndexers)
store := &userStore{
Store: schema.Store,
mu: sync.Mutex{},
userIndexer: userInformer.GetIndexer(),
userManager: mgmt.UserManager,
}
t := &transform.Store{
Store: store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
// filter system users out of the api
if princIds, ok := data[client.UserFieldPrincipalIDs].([]interface{}); ok {
for _, p := range princIds {
pid, _ := p.(string)
if strings.HasPrefix(pid, "system://") {
if opt != nil && opt.Options["ByID"] == "true" {
return nil, httperror.NewAPIError(httperror.NotFound, "resource not found")
}
return nil, nil
}
}
}
// set "me" field on user
userID := apiContext.Request.Header.Get("Impersonate-User")
if userID != "" {
id, ok := data[types.ResourceFieldID].(string)
if ok {
if id == userID {
data["me"] = "true"
}
}
}
return data, nil
},
}
schema.Store = t
}
func userByUsername(obj interface{}) ([]string, error) {
u, ok := obj.(*v3.User)
if !ok {
return []string{}, nil
}
return []string{u.Username}, nil
}
func hashPassword(data map[string]interface{}) error {
pass, ok := data[client.UserFieldPassword].(string)
if !ok {
return errors.New("password not a string")
}
hashed, err := HashPasswordString(pass)
if err != nil {
return err
}
data[client.UserFieldPassword] = string(hashed)
return nil
}
func HashPasswordString(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", errors.Wrap(err, "problem encrypting password")
}
return string(hash), nil
}
func (s *userStore) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if err := hashPassword(data); err != nil {
return nil, err
}
created, err := s.create(apiContext, schema, data)
if err != nil {
return nil, err
}
userName := created["id"].(string)
userUUID := created["uuid"].(string)
uid := apitypes.UID(userUUID)
err = s.userManager.CreateNewUserClusterRoleBinding(userName, uid)
if err != nil {
return nil, err
}
Tries:
for x := 0; x < 3; x++ {
if id, ok := created[types.ResourceFieldID].(string); ok {
time.Sleep(time.Duration((x+1)*100) * time.Millisecond)
created, err = s.ByID(apiContext, schema, id)
if err != nil {
logrus.Warnf("error while getting user: %v", err)
continue
}
var principalIDs []interface{}
if pids, ok := created[client.UserFieldPrincipalIDs].([]interface{}); ok {
principalIDs = pids
}
for _, pid := range principalIDs {
if pidString, ok := pid.(string); ok {
if strings.HasPrefix(pidString, "local://") {
break Tries
}
}
}
created[client.UserFieldPrincipalIDs] = append(principalIDs, "local://"+id)
created, err = s.Update(apiContext, schema, created, id)
if err != nil {
if httperror.IsConflict(err) {
continue
}
logrus.Warnf("error while updating user: %v", err)
break
}
break
}
}
delete(created, client.UserFieldPassword)
return created, nil
}
func (s *userStore) create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
username, ok := data[client.UserFieldUsername].(string)
if !ok {
return nil, errors.New("invalid username")
}
s.mu.Lock()
defer s.mu.Unlock()
users, err := s.userIndexer.ByIndex(userByUsernameIndex, username)
if err != nil {
return nil, err
}
if len(users) > 0 {
return nil, httperror.NewFieldAPIError(httperror.NotUnique, "username", "Username is already in use.")
}
return s.Store.Create(apiContext, schema, data)
}