-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveridentities.go
496 lines (413 loc) · 12.8 KB
/
serveridentities.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lib
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-ca/api"
"github.com/hyperledger/fabric-ca/lib/attr"
"github.com/hyperledger/fabric-ca/lib/caerrors"
"github.com/hyperledger/fabric-ca/lib/server/user"
"github.com/hyperledger/fabric-ca/util"
"github.com/pkg/errors"
)
func newIdentitiesEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Path: "identities/{id}",
Methods: []string{"GET", "DELETE", "PUT"},
Handler: identitiesHandler,
Server: s,
successRC: 200,
}
}
func newIdentitiesStreamingEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Path: "identities",
Methods: []string{"GET", "POST"},
Handler: identitiesStreamingHandler,
Server: s,
successRC: 200,
}
}
func identitiesStreamingHandler(ctx *serverRequestContextImpl) (interface{}, error) {
// Authenticate
callerID, err := ctx.TokenAuthentication()
log.Debugf("Received identity update request from %s", callerID)
if err != nil {
return nil, err
}
caname, err := ctx.getCAName()
if err != nil {
return nil, err
}
caller, err := ctx.GetCaller()
if err != nil {
return nil, err
}
// Process Request
resp, err := processStreamingRequest(ctx, caname, caller)
if err != nil {
return nil, err
}
return resp, nil
}
func identitiesHandler(ctx *serverRequestContextImpl) (interface{}, error) {
var err error
// Authenticate
callerID, err := ctx.TokenAuthentication()
log.Debugf("Received identity update request from %s", callerID)
if err != nil {
return nil, err
}
caname, err := ctx.getCAName()
if err != nil {
return nil, err
}
caller, err := ctx.GetCaller()
if err != nil {
return nil, err
}
// Process Request
resp, err := processRequest(ctx, caname, caller)
if err != nil {
return nil, err
}
return resp, nil
}
// processStreamingRequest will process the configuration request
func processStreamingRequest(ctx *serverRequestContextImpl, caname string, caller user.User) (interface{}, error) {
log.Debug("Processing identity configuration update request")
method := ctx.req.Method
switch method {
case "GET":
return nil, processGetAllIDsRequest(ctx, caller, caname)
case "POST":
return processPostRequest(ctx, caname)
default:
return nil, errors.Errorf("Invalid request: %s", method)
}
}
// processRequest will process the configuration request
func processRequest(ctx *serverRequestContextImpl, caname string, caller user.User) (interface{}, error) {
log.Debug("Processing identity configuration update request")
method := ctx.req.Method
switch method {
case "GET":
return processGetIDRequest(ctx, caller, caname)
case "DELETE":
return processDeleteRequest(ctx, caname)
case "PUT":
return processPutRequest(ctx, caname)
default:
return nil, errors.Errorf("Invalid request: %s", method)
}
}
func processGetAllIDsRequest(ctx *serverRequestContextImpl, caller user.User, caname string) error {
log.Debug("Processing GET all IDs request")
err := getIDs(ctx, caller, caname)
if err != nil {
return err
}
return nil
}
func processGetIDRequest(ctx *serverRequestContextImpl, caller user.User, caname string) (interface{}, error) {
log.Debug("Processing GET ID request")
id, err := ctx.GetVar("id")
if err != nil {
return nil, err
}
resp, err := getID(ctx, caller, id, caname)
if err != nil {
return nil, err
}
return resp, nil
}
func getIDs(ctx *serverRequestContextImpl, caller user.User, caname string) error {
log.Debug("Requesting all identities that the caller is authorized view")
var err error
w := ctx.resp
flusher, _ := w.(http.Flusher)
callerTypes, isRegistrar, err := ctx.isRegistrar()
if err != nil {
return err
}
if !isRegistrar {
return caerrors.NewAuthorizationErr(caerrors.ErrGettingUser, "Caller is not a registrar")
}
// Getting all identities of appropriate affiliation and type
callerAff := user.GetAffiliation(caller)
registry := ctx.ca.registry
rows, err := registry.GetFilteredUsers(callerAff, callerTypes)
if err != nil {
return caerrors.NewHTTPErr(500, caerrors.ErrGettingUser, "Failed to get users by affiliation and type: %s", err)
}
// Get the number of identities to return back to client in a chunk based on the environment variable
// If environment variable not set, default to 100 identities
numberOfIdentities := os.Getenv("FABRIC_CA_SERVER_MAX_IDS_PER_CHUNK")
var numIdentities int
if numberOfIdentities == "" {
numIdentities = 100
} else {
numIdentities, err = strconv.Atoi(numberOfIdentities)
if err != nil {
return caerrors.NewHTTPErr(500, caerrors.ErrGettingUser, "Incorrect format specified for environment variable 'FABRIC_CA_SERVER_MAX_IDS_PER_CHUNK', an integer value is required: %s", err)
}
}
log.Debugf("Number of identities to be delivered in each chunk: %d", numIdentities)
w.Write([]byte(`{"identities":[`))
rowNumber := 0
for rows.Next() {
rowNumber++
var id user.Record
err := rows.StructScan(&id)
if err != nil {
return caerrors.NewHTTPErr(500, caerrors.ErrGettingUser, "Failed to get read row: %s", err)
}
if rowNumber > 1 {
w.Write([]byte(","))
}
var attrs []api.Attribute
json.Unmarshal([]byte(id.Attributes), &attrs)
idInfo := api.IdentityInfo{
ID: id.Name,
Type: id.Type,
Affiliation: id.Affiliation,
MaxEnrollments: id.MaxEnrollments,
Attributes: attrs,
}
resp, err := util.Marshal(idInfo, "identities info")
if err != nil {
return caerrors.NewHTTPErr(500, caerrors.ErrGettingUser, "Failed to marshal identity info: %s", err)
}
w.Write(resp)
// If hit the number of identities requested then flush
if rowNumber%numIdentities == 0 {
flusher.Flush() // Trigger "chunked" encoding and send a chunk...
}
}
// Close the JSON object
w.Write([]byte(fmt.Sprintf("], \"caname\":\"%s\"}", caname)))
flusher.Flush()
return nil
}
func getID(ctx *serverRequestContextImpl, caller user.User, id, caname string) (*api.GetIDResponse, error) {
log.Debugf("Requesting identity '%s'", id)
registry := ctx.ca.registry
caUser, err := registry.GetUser(id, nil)
if err != nil {
return nil, err
}
err = ctx.CanManageUser(caUser)
if err != nil {
return nil, err
}
allAttributes, err := caUser.GetAttributes(nil)
if err != nil {
return nil, err
}
resp := &api.GetIDResponse{
ID: caUser.GetName(),
Type: caUser.GetType(),
Affiliation: user.GetAffiliation(caUser),
Attributes: allAttributes,
MaxEnrollments: caUser.GetMaxEnrollments(),
CAName: caname,
}
return resp, nil
}
func processDeleteRequest(ctx *serverRequestContextImpl, caname string) (*api.IdentityResponse, error) {
log.Debug("Processing DELETE request")
if !ctx.ca.Config.Cfg.Identities.AllowRemove {
return nil, caerrors.NewHTTPErr(403, caerrors.ErrRemoveIdentity, "Identity removal is disabled")
}
removeID, err := ctx.GetVar("id")
if err != nil {
return nil, err
}
if removeID == "" {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrRemoveIdentity, "No ID name specified in remove request")
}
log.Debugf("Removing identity '%s'", removeID)
force, err := ctx.GetBoolQueryParm("force")
if err != nil {
return nil, err
}
if removeID == ctx.caller.GetName() && !force {
return nil, caerrors.NewHTTPErr(403, caerrors.ErrRemoveIdentity, "Need to use 'force' option to delete your own identity")
}
registry := ctx.ca.registry
userToRemove, err := ctx.GetUser(removeID)
if err != nil {
return nil, err
}
_, err = registry.DeleteUser(removeID)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrRemoveIdentity, "Failed to remove identity: %s", err)
}
resp, err := getIDResp(userToRemove, "", caname)
if err != nil {
return nil, err
}
log.Debugf("Identity '%s' successfully removed", removeID)
return resp, nil
}
func processPostRequest(ctx *serverRequestContextImpl, caname string) (*api.IdentityResponse, error) {
log.Debug("Processing POST request")
ctx.endpoint.successRC = 201
var req api.AddIdentityRequest
err := ctx.ReadBody(&req)
if err != nil {
return nil, err
}
if req.ID == "" {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrAddIdentity, "Missing 'ID' in request to add a new identity")
}
addReq := &api.RegistrationRequest{
Name: req.ID,
Secret: req.Secret,
Type: req.Type,
Affiliation: req.Affiliation,
Attributes: req.Attributes,
MaxEnrollments: req.MaxEnrollments,
}
log.Debugf("Adding identity: %+v", util.StructToString(addReq))
caller, err := ctx.GetCaller()
if err != nil {
return nil, errors.WithMessage(err, "Failed to get caller identity")
}
callerID := caller.GetName()
pass, err := registerUser(addReq, callerID, ctx.ca, ctx)
if err != nil {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrAddIdentity, "Failed to add identity: %s", err)
}
user, err := ctx.ca.registry.GetUser(req.ID, nil)
if err != nil {
return nil, err
}
resp, err := getIDResp(user, pass, caname)
if err != nil {
return nil, err
}
log.Debugf("Identity successfully added")
return resp, nil
}
func processPutRequest(ctx *serverRequestContextImpl, caname string) (*api.IdentityResponse, error) {
log.Debug("Processing PUT request")
modifyID, err := ctx.GetVar("id")
if err != nil {
return nil, err
}
if modifyID == "" {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrModifyingIdentity, "No ID name specified in modify request")
}
log.Debugf("Modifying identity '%s'", modifyID)
userToModify, err := ctx.GetUser(modifyID)
if err != nil {
return nil, err
}
registry := ctx.ca.registry
var req api.ModifyIdentityRequest
err = ctx.ReadBody(&req)
if err != nil {
return nil, err
}
var checkAff, checkType, checkAttrs bool
modReq, setPass := getModifyReq(userToModify, &req)
log.Debugf("Modify Request: %+v", util.StructToString(modReq))
if req.Affiliation != "" {
newAff := req.Affiliation
if newAff != "." { // Only need to check if not requesting root affiliation
aff, _ := registry.GetAffiliation(newAff)
if aff == nil {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrModifyingIdentity, "Affiliation '%s' is not supported", newAff)
}
}
checkAff = true
}
if req.Type != "" {
checkType = true
}
if len(req.Attributes) != 0 {
checkAttrs = true
}
err = ctx.CanModifyUser(&req, checkAff, checkType, checkAttrs, userToModify)
if err != nil {
return nil, err
}
err = registry.UpdateUser(modReq, setPass)
if err != nil {
return nil, err
}
userToModify, err = registry.GetUser(modifyID, nil)
if err != nil {
return nil, err
}
resp, err := getIDResp(userToModify, req.Secret, caname)
if err != nil {
return nil, err
}
log.Debugf("Identity successfully modified")
return resp, nil
}
// Function takes the modification request and fills in missing information with the current user information
// and parses the modification request to generate the correct input to be stored in the database
func getModifyReq(caUser user.User, req *api.ModifyIdentityRequest) (*user.Info, bool) {
modifyUserInfo := caUser.(*user.Impl).Info
userPass := caUser.(*user.Impl).GetPass()
setPass := false
if req.Secret != "" {
setPass = true
modifyUserInfo.Pass = req.Secret
modifyUserInfo.IncorrectPasswordAttempts = 0
} else {
modifyUserInfo.Pass = string(userPass)
}
if req.MaxEnrollments == -2 {
modifyUserInfo.MaxEnrollments = 0
} else if req.MaxEnrollments != 0 {
modifyUserInfo.MaxEnrollments = req.MaxEnrollments
}
reqAttrs := req.Attributes
if req.Affiliation == "." {
modifyUserInfo.Affiliation = ""
addAttributeToRequest(attr.Affiliation, "", &reqAttrs)
} else if req.Affiliation != "" {
modifyUserInfo.Affiliation = req.Affiliation
addAttributeToRequest(attr.Affiliation, req.Affiliation, &reqAttrs)
}
if req.Type != "" {
modifyUserInfo.Type = req.Type
addAttributeToRequest(attr.Type, req.Type, &reqAttrs)
}
// Update existing attribute, or add attribute if it does not already exist
if len(reqAttrs) != 0 {
modifyUserInfo.Attributes = user.GetNewAttributes(modifyUserInfo.Attributes, reqAttrs)
}
return &modifyUserInfo, setPass
}
// Get the identity response
// Note that the secret will be the empty string unless the
// caller is permitted to see the secret. For example,
// when adding a new identity and a secret is automatically
// generated, it must be returned to the registrar.
func getIDResp(caUser user.User, secret, caname string) (*api.IdentityResponse, error) {
allAttributes, err := caUser.GetAttributes(nil)
if err != nil {
return nil, err
}
return &api.IdentityResponse{
ID: caUser.GetName(),
Type: caUser.GetType(),
Affiliation: user.GetAffiliation(caUser),
Attributes: allAttributes,
MaxEnrollments: caUser.GetMaxEnrollments(),
Secret: secret,
CAName: caname,
}, nil
}