-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveraffiliations.go
564 lines (488 loc) · 15.1 KB
/
serveraffiliations.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lib
import (
"fmt"
"strconv"
"strings"
"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/spi"
"github.com/pkg/errors"
)
func newAffiliationsEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Methods: []string{"GET", "DELETE", "PUT"},
Handler: affiliationsHandler,
Server: s,
successRC: 200,
}
}
func newAffiliationsStreamingEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Methods: []string{"GET", "POST"},
Handler: affiliationsStreamingHandler,
Server: s,
successRC: 200,
}
}
func affiliationsHandler(ctx *serverRequestContextImpl) (interface{}, error) {
var err error
// Authenticate
callerID, err := ctx.TokenAuthentication()
log.Debugf("Received affiliation 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
}
err = ctx.HasRole(attr.AffiliationMgr)
if err != nil {
return nil, err
}
// Process Request
resp, err := processAffiliationRequest(ctx, caname, caller)
if err != nil {
return nil, err
}
return resp, nil
}
func affiliationsStreamingHandler(ctx *serverRequestContextImpl) (interface{}, error) {
var err error
// Authenticate
callerID, err := ctx.TokenAuthentication()
log.Debugf("Received affiliation 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
}
err = ctx.HasRole(attr.AffiliationMgr)
if err != nil {
return nil, err
}
// Process Request
resp, err := processStreamingAffiliationRequest(ctx, caname, caller)
if err != nil {
return nil, err
}
return resp, nil
}
// processStreamingAffiliationRequest will process the configuration request
func processStreamingAffiliationRequest(ctx *serverRequestContextImpl, caname string, caller spi.User) (interface{}, error) {
log.Debug("Processing affiliation configuration update request")
method := ctx.req.Method
switch method {
case "GET":
return processGetAllAffiliationsRequest(ctx, caller, caname)
case "POST":
return processAffiliationPostRequest(ctx, caname)
default:
return nil, errors.Errorf("Invalid request: %s", method)
}
}
// processRequest will process the configuration request
func processAffiliationRequest(ctx *serverRequestContextImpl, caname string, caller spi.User) (interface{}, error) {
log.Debug("Processing affiliation configuration update request")
method := ctx.req.Method
switch method {
case "GET":
return processGetAffiliationRequest(ctx, caller, caname)
case "DELETE":
return processAffiliationDeleteRequest(ctx, caname)
case "PUT":
return processAffiliationPutRequest(ctx, caname)
default:
return nil, errors.Errorf("Invalid request: %s", method)
}
}
func processGetAllAffiliationsRequest(ctx *serverRequestContextImpl, caller spi.User, caname string) (*api.AffiliationResponse, error) {
log.Debug("Processing GET all affiliations request")
resp, err := getAffiliations(ctx, caller, caname)
if err != nil {
return nil, err
}
return resp, nil
}
func processGetAffiliationRequest(ctx *serverRequestContextImpl, caller spi.User, caname string) (*api.AffiliationResponse, error) {
log.Debug("Processing GET affiliation request")
affiliation, err := ctx.GetVar("affiliation")
if err != nil {
return nil, err
}
resp, err := getAffiliation(ctx, caller, affiliation, caname)
if err != nil {
return nil, err
}
return resp, nil
}
func getAffiliations(ctx *serverRequestContextImpl, caller spi.User, caname string) (*api.AffiliationResponse, error) {
log.Debug("Requesting all affiliations that the caller is authorized view")
var err error
registry := ctx.ca.registry
callerAff := GetUserAffiliation(caller)
rows, err := registry.GetAllAffiliations(callerAff)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrGettingUser, "Failed to get affiliation: %s", err)
}
an := &affiliationNode{}
for rows.Next() {
var aff AffiliationRecord
err := rows.StructScan(&aff)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrGettingAffiliation, "Failed to get read row: %s", err)
}
an.insertByName(aff.Name)
}
root := an.GetRoot()
if root == nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrGettingAffiliation, "No affiliations are configured on the CA")
}
resp := &api.AffiliationResponse{
CAName: caname,
}
resp.Name = root.Name
resp.Affiliations = root.Affiliations
resp.Identities = root.Identities
return resp, nil
}
func getAffiliation(ctx *serverRequestContextImpl, caller spi.User, requestedAffiliation, caname string) (*api.AffiliationResponse, error) {
log.Debugf("Requesting affiliation '%s'", requestedAffiliation)
registry := ctx.ca.registry
err := ctx.ContainsAffiliation(requestedAffiliation)
if err != nil {
return nil, err
}
result, err := registry.GetAffiliationTree(requestedAffiliation)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrGettingAffiliation, "Failed to get affiliation: %s", err)
}
resp, err := getResponse(result, caname)
if err != nil {
return nil, err
}
return resp, nil
}
func processAffiliationDeleteRequest(ctx *serverRequestContextImpl, caname string) (*api.AffiliationResponse, error) {
log.Debug("Processing DELETE request")
if !ctx.ca.Config.Cfg.Affiliations.AllowRemove {
return nil, caerrors.NewAuthorizationErr(caerrors.ErrUpdateConfigRemoveAff, "Affiliation removal is disabled")
}
removeAffiliation, err := ctx.GetVar("affiliation")
if err != nil {
return nil, err
}
log.Debugf("Request to remove affiliation '%s'", removeAffiliation)
callerAff := GetUserAffiliation(ctx.caller)
if callerAff == removeAffiliation {
return nil, caerrors.NewAuthorizationErr(caerrors.ErrUpdateConfigRemoveAff, "Can't remove affiliation '%s' because the caller is associated with this affiliation", removeAffiliation)
}
err = ctx.ContainsAffiliation(removeAffiliation)
if err != nil {
return nil, err
}
force, err := ctx.GetBoolQueryParm("force")
if err != nil {
return nil, err
}
_, isRegistrar, err := ctx.isRegistrar()
if err != nil {
httpErr := getHTTPErr(err)
if httpErr.GetRemoteCode() != 20 {
return nil, err
}
}
identityRemoval := ctx.ca.Config.Cfg.Identities.AllowRemove
result, err := ctx.ca.registry.DeleteAffiliation(removeAffiliation, force, identityRemoval, isRegistrar)
if err != nil {
return nil, err
}
resp, err := getResponse(result, caname)
if err != nil {
return nil, err
}
return resp, nil
}
func processAffiliationPostRequest(ctx *serverRequestContextImpl, caname string) (*api.AffiliationResponse, error) {
log.Debug("Processing POST request")
ctx.endpoint.successRC = 201
var req api.AddAffiliationRequestNet
err := ctx.ReadBody(&req)
if err != nil {
return nil, err
}
addAffiliation := req.Name
log.Debugf("Request to add affiliation '%s'", addAffiliation)
registry := ctx.ca.registry
_, err = registry.GetAffiliation(addAffiliation)
if err == nil {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrUpdateConfigAddAff, "Affiliation already exists")
}
err = ctx.ContainsAffiliation(addAffiliation)
if err != nil {
return nil, err
}
force, err := ctx.GetBoolQueryParm("force")
if err != nil {
return nil, err
}
addAffiliationSlice := strings.Split(addAffiliation, ".")
var parentAffiliationPath string
affLevel := ctx.ca.server.levels.Affiliation
if force {
// With force option, add any parent affiliations that don't exist
var affiliationPath string
for _, addAff := range addAffiliationSlice {
affiliationPath = affiliationPath + addAff
err := registry.InsertAffiliation(affiliationPath, parentAffiliationPath, affLevel)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrUpdateConfigAddAff, "Failed to add affiliations '%s': %s", addAffiliation, err)
}
parentAffiliationPath = affiliationPath
affiliationPath = affiliationPath + "."
}
} else {
// If the affiliation being added has a parent affiliation, check to make sure that parent affiliation exists
if len(addAffiliationSlice) > 1 {
parentAffiliationPath = strings.Join(addAffiliationSlice[:len(addAffiliationSlice)-1], ".") // Get the path up until the last affiliation
_, err = registry.GetAffiliation(parentAffiliationPath)
if err != nil {
httpErr := getHTTPErr(err)
if httpErr.GetStatusCode() == 400 {
return nil, caerrors.NewHTTPErr(400, caerrors.ErrUpdateConfigAddAff, "Parent affiliation does not exist, 'force' option required on request to add affiliation")
}
return nil, err
}
err := registry.InsertAffiliation(addAffiliation, parentAffiliationPath, affLevel)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrUpdateConfigAddAff, "Failed to add affiliation '%s': %s", addAffiliation, err)
}
} else {
err := registry.InsertAffiliation(addAffiliation, "", affLevel)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrUpdateConfigAddAff, "Failed to add affiliation '%s': %s", addAffiliation, err)
}
}
}
resp := &api.AffiliationResponse{CAName: caname}
resp.Name = addAffiliation
return resp, nil
}
func processAffiliationPutRequest(ctx *serverRequestContextImpl, caname string) (*api.AffiliationResponse, error) {
log.Debug("Processing PUT request")
modifyAffiliation, err := ctx.GetVar("affiliation")
if err != nil {
return nil, err
}
var req api.ModifyAffiliationRequestNet
err = ctx.ReadBody(&req)
if err != nil {
return nil, err
}
newAffiliation := req.NewName
log.Debugf("Request to modify affiliation '%s' to '%s'", modifyAffiliation, newAffiliation)
err = ctx.ContainsAffiliation(modifyAffiliation)
if err != nil {
return nil, err
}
err = ctx.ContainsAffiliation(newAffiliation)
if err != nil {
return nil, err
}
force := false
forceStr := ctx.req.URL.Query().Get("force")
if forceStr != "" {
force, err = strconv.ParseBool(forceStr)
if err != nil {
return nil, caerrors.NewHTTPErr(500, caerrors.ErrUpdateConfigAddAff, "The 'force' query parameter value must be a boolean: %s", err)
}
}
_, isRegistrar, err := ctx.isRegistrar()
if err != nil {
httpErr := getHTTPErr(err)
if httpErr.GetLocalCode() != 20 {
return nil, err
}
}
registry := ctx.ca.registry
result, err := registry.ModifyAffiliation(modifyAffiliation, newAffiliation, force, isRegistrar)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("Failed to modify affiliation from '%s' to '%s'", modifyAffiliation, newAffiliation))
}
resp, err := getResponse(result, caname)
if err != nil {
return nil, err
}
return resp, nil
}
func getResponse(result *spi.DbTxResult, caname string) (*api.AffiliationResponse, error) {
resp := &api.AffiliationResponse{CAName: caname}
// Get all root affiliation names from the result
rootNames := getRootAffiliationNames(result.Affiliations)
if len(rootNames) == 0 {
return resp, nil
}
if len(rootNames) != 1 {
return nil, errors.Errorf("multiple root affiliations found: %+v", rootNames)
}
affInfo := &api.AffiliationInfo{}
err := fillAffiliationInfo(affInfo, rootNames[0], result, result.Affiliations)
if err != nil {
return nil, err
}
resp.AffiliationInfo = *affInfo
return resp, nil
}
// Get all of the root affiliation names from this list of affiliations
func getRootAffiliationNames(affiliations []spi.Affiliation) []string {
roots := []string{}
for _, aff1 := range affiliations {
isRoot := true
for _, aff2 := range affiliations {
if isChildAffiliation(aff2.GetName(), aff1.GetName()) {
isRoot = false
break
}
}
if isRoot {
roots = append(roots, aff1.GetName())
}
}
return roots
}
// Fill 'info' with affiliation info associated with affiliation 'name' hierarchically.
// Use 'affiliations' to find child affiliations for this affiliation, and
// 'identities' to find identities associated with this affiliation.
func fillAffiliationInfo(info *api.AffiliationInfo, name string, result *spi.DbTxResult, affiliations []spi.Affiliation) error {
info.Name = name
// Add identities which have this affiliation
identities := []api.IdentityInfo{}
for _, identity := range result.Identities {
idAff := strings.Join(identity.GetAffiliationPath(), ".")
if idAff == name {
id, err := getIDInfo(identity)
if err != nil {
return err
}
identities = append(identities, *id)
}
}
if len(identities) > 0 {
info.Identities = identities
}
// Create child affiliations (if any)
children := []api.AffiliationInfo{}
var child spi.Affiliation
for {
child = nil
// Search for a child affiliations
for idx, aff := range affiliations {
affName := aff.GetName()
if isChildAffiliation(name, affName) {
child = aff
// Remove this child affiliation
affiliations = append(affiliations[:idx], affiliations[idx+1:]...)
break
}
}
if child == nil {
// No more children of this affiliation 'name' found
break
}
// Found a child of affiliation 'name'
childAff := api.AffiliationInfo{Name: child.GetName()}
err := fillAffiliationInfo(&childAff, child.GetName(), result, affiliations)
if err != nil {
return err
}
children = append(children, childAff)
}
if len(children) > 0 {
info.Affiliations = children
}
return nil
}
// Determine if the affiliation with name 'child' is a child of affiliation with name 'name'
func isChildAffiliation(name, child string) bool {
if !strings.HasPrefix(child, name+".") {
return false
}
nameParts := strings.Split(name, ".")
childParts := strings.Split(child, ".")
if len(childParts) != len(nameParts)+1 {
return false
}
return true
}
func getIDInfo(user spi.User) (*api.IdentityInfo, error) {
allAttributes, err := user.GetAttributes(nil)
if err != nil {
return nil, err
}
return &api.IdentityInfo{
ID: user.GetName(),
Type: user.GetType(),
Affiliation: GetUserAffiliation(user),
Attributes: allAttributes,
MaxEnrollments: user.GetMaxEnrollments(),
}, nil
}
type affiliationNode struct {
children map[string]*affiliationNode
}
func (an *affiliationNode) insertByName(name string) {
an.insertByPath(strings.Split(name, "."))
}
func (an *affiliationNode) insertByPath(path []string) {
if len(path) == 0 {
return
}
if an.children == nil {
an.children = map[string]*affiliationNode{}
}
node := an.children[path[0]]
if node == nil {
node = &affiliationNode{}
an.children[path[0]] = node
}
node.insertByPath(path[1:])
}
func (an *affiliationNode) GetRoot() *api.AffiliationInfo {
result := &api.AffiliationInfo{}
an.fill([]string{}, result)
switch len(result.Affiliations) {
case 0:
return nil
case 1:
return &result.Affiliations[0]
default:
return result
}
}
func (an *affiliationNode) fill(path []string, ai *api.AffiliationInfo) {
ai.Name = strings.Join(path, ".")
if len(an.children) > 0 {
ai.Affiliations = make([]api.AffiliationInfo, len(an.children))
idx := 0
for key, child := range an.children {
child.fill(append(path, key), &ai.Affiliations[idx])
idx++
}
}
}