forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
416 lines (344 loc) · 13.1 KB
/
users.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
neturl "net/url"
"strings"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
)
var orgRoleToPathMap = map[models.Role]string{
models.RoleOrgUser: "users",
models.RoleOrgManager: "managers",
models.RoleBillingManager: "billing_managers",
models.RoleOrgAuditor: "auditors",
}
var spaceRoleToPathMap = map[models.Role]string{
models.RoleSpaceManager: "managers",
models.RoleSpaceDeveloper: "developers",
models.RoleSpaceAuditor: "auditors",
}
type apiErrResponse struct {
Code int `json:"code,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Description string `json:"description,omitempty"`
}
//go:generate counterfeiter . UserRepository
type UserRepository interface {
FindByUsername(username string) (user models.UserFields, apiErr error)
FindAllByUsername(username string) (users []models.UserFields, apiErr error)
ListUsersInOrgForRole(orgGUID string, role models.Role) ([]models.UserFields, error)
ListUsersInOrgForRoleWithNoUAA(orgGUID string, role models.Role) ([]models.UserFields, error)
ListUsersInSpaceForRoleWithNoUAA(spaceGUID string, role models.Role) ([]models.UserFields, error)
Create(username, password string) (apiErr error)
Delete(userGUID string) (apiErr error)
SetOrgRoleByGUID(userGUID, orgGUID string, role models.Role) (apiErr error)
SetOrgRoleByUsername(username, orgGUID string, role models.Role) (apiErr error)
UnsetOrgRoleByGUID(userGUID, orgGUID string, role models.Role) (apiErr error)
UnsetOrgRoleByUsername(username, orgGUID string, role models.Role) (apiErr error)
SetSpaceRoleByGUID(userGUID, spaceGUID, orgGUID string, role models.Role) (apiErr error)
SetSpaceRoleByUsername(username, spaceGUID, orgGUID string, role models.Role) (apiErr error)
UnsetSpaceRoleByGUID(userGUID, spaceGUID string, role models.Role) (apiErr error)
UnsetSpaceRoleByUsername(userGUID, spaceGUID string, role models.Role) (apiErr error)
}
type CloudControllerUserRepository struct {
config coreconfig.Reader
uaaGateway net.Gateway
ccGateway net.Gateway
}
func NewCloudControllerUserRepository(config coreconfig.Reader, uaaGateway net.Gateway, ccGateway net.Gateway) (repo CloudControllerUserRepository) {
repo.config = config
repo.uaaGateway = uaaGateway
repo.ccGateway = ccGateway
return
}
func (repo CloudControllerUserRepository) FindByUsername(username string) (user models.UserFields, apiErr error) {
users, apiErr := repo.FindAllByUsername(username)
if apiErr != nil {
return user, apiErr
}
user = users[0]
return user, nil
}
func (repo CloudControllerUserRepository) FindAllByUsername(username string) (users []models.UserFields, apiErr error) {
uaaEndpoint, apiErr := repo.getAuthEndpoint()
if apiErr != nil {
return users, apiErr
}
usernameFilter := neturl.QueryEscape(fmt.Sprintf(`userName Eq "%s"`, username))
path := fmt.Sprintf("%s/Users?attributes=id,userName&filter=%s", uaaEndpoint, usernameFilter)
users, apiErr = repo.updateOrFindUsersWithUAAPath([]models.UserFields{}, path)
if apiErr != nil {
errType, ok := apiErr.(errors.HTTPError)
if ok {
if errType.StatusCode() == 403 {
return users, errors.NewAccessDeniedError()
}
}
return users, apiErr
} else if len(users) == 0 {
return users, errors.NewModelNotFoundError("User", username)
}
return users, apiErr
}
func (repo CloudControllerUserRepository) ListUsersInOrgForRole(orgGUID string, roleName models.Role) (users []models.UserFields, apiErr error) {
return repo.listUsersWithPath(fmt.Sprintf("/v2/organizations/%s/%s", orgGUID, orgRoleToPathMap[roleName]))
}
func (repo CloudControllerUserRepository) ListUsersInOrgForRoleWithNoUAA(orgGUID string, roleName models.Role) (users []models.UserFields, apiErr error) {
return repo.listUsersWithPathWithNoUAA(fmt.Sprintf("/v2/organizations/%s/%s", orgGUID, orgRoleToPathMap[roleName]))
}
func (repo CloudControllerUserRepository) ListUsersInSpaceForRoleWithNoUAA(spaceGUID string, roleName models.Role) (users []models.UserFields, apiErr error) {
return repo.listUsersWithPathWithNoUAA(fmt.Sprintf("/v2/spaces/%s/%s", spaceGUID, spaceRoleToPathMap[roleName]))
}
func (repo CloudControllerUserRepository) listUsersWithPathWithNoUAA(path string) (users []models.UserFields, apiErr error) {
apiErr = repo.ccGateway.ListPaginatedResources(
repo.config.APIEndpoint(),
path,
resources.UserResource{},
func(resource interface{}) bool {
user := resource.(resources.UserResource).ToFields()
users = append(users, user)
return true
})
if apiErr != nil {
return
}
return
}
func (repo CloudControllerUserRepository) listUsersWithPath(path string) (users []models.UserFields, apiErr error) {
guidFilters := []string{}
apiErr = repo.ccGateway.ListPaginatedResources(
repo.config.APIEndpoint(),
path,
resources.UserResource{},
func(resource interface{}) bool {
user := resource.(resources.UserResource).ToFields()
users = append(users, user)
guidFilters = append(guidFilters, fmt.Sprintf(`ID eq "%s"`, user.GUID))
return true
})
if apiErr != nil {
return
}
if len(guidFilters) == 0 {
return
}
uaaEndpoint, apiErr := repo.getAuthEndpoint()
if apiErr != nil {
return
}
filter := strings.Join(guidFilters, " or ")
usersURL := fmt.Sprintf("%s/Users?attributes=id,userName&filter=%s", uaaEndpoint, neturl.QueryEscape(filter))
users, apiErr = repo.updateOrFindUsersWithUAAPath(users, usersURL)
return
}
func (repo CloudControllerUserRepository) updateOrFindUsersWithUAAPath(ccUsers []models.UserFields, path string) (updatedUsers []models.UserFields, apiErr error) {
uaaResponse := new(resources.UAAUserResources)
apiErr = repo.uaaGateway.GetResource(path, uaaResponse)
if apiErr != nil {
return
}
for _, uaaResource := range uaaResponse.Resources {
var ccUserFields models.UserFields
for _, u := range ccUsers {
if u.GUID == uaaResource.ID {
ccUserFields = u
break
}
}
updatedUsers = append(updatedUsers, models.UserFields{
GUID: uaaResource.ID,
Username: uaaResource.Username,
IsAdmin: ccUserFields.IsAdmin,
})
}
return
}
func (repo CloudControllerUserRepository) Create(username, password string) (err error) {
uaaEndpoint, err := repo.getAuthEndpoint()
if err != nil {
return
}
path := "/Users"
body, err := json.Marshal(resources.NewUAAUserResource(username, password))
if err != nil {
return
}
createUserResponse := &resources.UAAUserFields{}
err = repo.uaaGateway.CreateResource(uaaEndpoint, path, bytes.NewReader(body), createUserResponse)
switch httpErr := err.(type) {
case nil:
case errors.HTTPError:
if httpErr.StatusCode() == http.StatusConflict {
err = errors.NewModelAlreadyExistsError("user", username)
return
}
return
default:
return
}
path = "/v2/users"
body, err = json.Marshal(resources.Metadata{
GUID: createUserResponse.ID,
})
if err != nil {
return
}
return repo.ccGateway.CreateResource(repo.config.APIEndpoint(), path, bytes.NewReader(body))
}
func (repo CloudControllerUserRepository) Delete(userGUID string) (apiErr error) {
path := fmt.Sprintf("/v2/users/%s", userGUID)
apiErr = repo.ccGateway.DeleteResource(repo.config.APIEndpoint(), path)
if httpErr, ok := apiErr.(errors.HTTPError); ok && httpErr.ErrorCode() != errors.UserNotFound {
return
}
uaaEndpoint, apiErr := repo.getAuthEndpoint()
if apiErr != nil {
return
}
path = fmt.Sprintf("/Users/%s", userGUID)
return repo.uaaGateway.DeleteResource(uaaEndpoint, path)
}
func (repo CloudControllerUserRepository) SetOrgRoleByGUID(userGUID string, orgGUID string, role models.Role) (err error) {
path, err := userGUIDPath(repo.config.APIEndpoint(), userGUID, orgGUID, role)
if err != nil {
return
}
err = repo.callAPI("PUT", path, nil)
if err != nil {
return
}
return repo.assocUserWithOrgByUserGUID(userGUID, orgGUID)
}
func (repo CloudControllerUserRepository) UnsetOrgRoleByGUID(userGUID, orgGUID string, role models.Role) (err error) {
path, err := userGUIDPath(repo.config.APIEndpoint(), userGUID, orgGUID, role)
if err != nil {
return
}
return repo.callAPI("DELETE", path, nil)
}
func (repo CloudControllerUserRepository) UnsetOrgRoleByUsername(username, orgGUID string, role models.Role) error {
rolePath, err := rolePath(role)
if err != nil {
return err
}
path := fmt.Sprintf("%s/v2/organizations/%s/%s", repo.config.APIEndpoint(), orgGUID, rolePath)
return repo.callAPI("DELETE", path, usernamePayload(username))
}
func (repo CloudControllerUserRepository) UnsetSpaceRoleByUsername(username, spaceGUID string, role models.Role) error {
rolePath := spaceRoleToPathMap[role]
path := fmt.Sprintf("%s/v2/spaces/%s/%s", repo.config.APIEndpoint(), spaceGUID, rolePath)
return repo.callAPI("DELETE", path, usernamePayload(username))
}
func (repo CloudControllerUserRepository) SetOrgRoleByUsername(username string, orgGUID string, role models.Role) error {
rolePath, err := rolePath(role)
if err != nil {
return err
}
path := fmt.Sprintf("%s/v2/organizations/%s/%s", repo.config.APIEndpoint(), orgGUID, rolePath)
err = repo.callAPI("PUT", path, usernamePayload(username))
if err != nil {
return err
}
return repo.assocUserWithOrgByUsername(username, orgGUID, nil)
}
func (repo CloudControllerUserRepository) callAPI(verb, path string, body io.ReadSeeker) (err error) {
request, err := repo.ccGateway.NewRequest(verb, path, repo.config.AccessToken(), body)
if err != nil {
return
}
_, err = repo.ccGateway.PerformRequest(request)
if err != nil {
return
}
return
}
func userGUIDPath(apiEndpoint, userGUID, orgGUID string, role models.Role) (string, error) {
rolePath, err := rolePath(role)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/v2/organizations/%s/%s/%s", apiEndpoint, orgGUID, rolePath, userGUID), nil
}
func (repo CloudControllerUserRepository) SetSpaceRoleByGUID(userGUID, spaceGUID, orgGUID string, role models.Role) error {
rolePath, found := spaceRoleToPathMap[role]
if !found {
return fmt.Errorf(T("Invalid Role {{.Role}}", map[string]interface{}{"Role": role}))
}
err := repo.assocUserWithOrgByUserGUID(userGUID, orgGUID)
if err != nil {
return err
}
path := fmt.Sprintf("/v2/spaces/%s/%s/%s", spaceGUID, rolePath, userGUID)
return repo.ccGateway.UpdateResource(repo.config.APIEndpoint(), path, nil)
}
func (repo CloudControllerUserRepository) SetSpaceRoleByUsername(username, spaceGUID, orgGUID string, role models.Role) (apiErr error) {
rolePath, apiErr := repo.checkSpaceRole(spaceGUID, role)
if apiErr != nil {
return
}
setOrgRoleErr := apiErrResponse{}
apiErr = repo.assocUserWithOrgByUsername(username, orgGUID, &setOrgRoleErr)
if setOrgRoleErr.Code == 10003 {
//operator lacking the privilege to set org role
//user might already be in org, so ignoring error and attempt to set space role
} else if apiErr != nil {
return
}
setSpaceRoleErr := apiErrResponse{}
apiErr = repo.ccGateway.UpdateResourceSync(repo.config.APIEndpoint(), rolePath, usernamePayload(username), &setSpaceRoleErr)
if setSpaceRoleErr.Code == 1002 {
return errors.New(T("Server error, error code: 1002, message: cannot set space role because user is not part of the org"))
}
return apiErr
}
func (repo CloudControllerUserRepository) UnsetSpaceRoleByGUID(userGUID, spaceGUID string, role models.Role) error {
rolePath, found := spaceRoleToPathMap[role]
if !found {
return fmt.Errorf(T("Invalid Role {{.Role}}", map[string]interface{}{"Role": role}))
}
return repo.ccGateway.DeleteResource(repo.config.APIEndpoint(), rolePath)
}
func (repo CloudControllerUserRepository) checkSpaceRole(spaceGUID string, role models.Role) (string, error) {
var apiErr error
rolePath, found := spaceRoleToPathMap[role]
if !found {
apiErr = fmt.Errorf(T("Invalid Role {{.Role}}",
map[string]interface{}{"Role": role}))
}
apiPath := fmt.Sprintf("/v2/spaces/%s/%s", spaceGUID, rolePath)
return apiPath, apiErr
}
func (repo CloudControllerUserRepository) assocUserWithOrgByUsername(username, orgGUID string, resource interface{}) (apiErr error) {
path := fmt.Sprintf("/v2/organizations/%s/users", orgGUID)
return repo.ccGateway.UpdateResourceSync(repo.config.APIEndpoint(), path, usernamePayload(username), resource)
}
func (repo CloudControllerUserRepository) assocUserWithOrgByUserGUID(userGUID, orgGUID string) (apiErr error) {
path := fmt.Sprintf("/v2/organizations/%s/users/%s", orgGUID, userGUID)
return repo.ccGateway.UpdateResource(repo.config.APIEndpoint(), path, nil)
}
func (repo CloudControllerUserRepository) getAuthEndpoint() (string, error) {
uaaEndpoint := repo.config.UaaEndpoint()
if uaaEndpoint == "" {
return "", errors.New(T("UAA endpoint missing from config file"))
}
return uaaEndpoint, nil
}
func rolePath(role models.Role) (string, error) {
path, found := orgRoleToPathMap[role]
if !found {
return "", fmt.Errorf(T("Invalid Role {{.Role}}",
map[string]interface{}{"Role": role}))
}
return path, nil
}
func usernamePayload(username string) *strings.Reader {
return strings.NewReader(`{"username": "` + username + `"}`)
}