Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit a43e084

Browse files
committed
[FABG-682] MSP Client: Affiliation Service
Change-Id: Ic12be3c04d6f6d89c4962a9b5cacf62cc364f6f5 Signed-off-by: 乔伦 徐 <jamesxql@gmail.com>
1 parent 6a3c34e commit a43e084

File tree

21 files changed

+983
-38
lines changed

21 files changed

+983
-38
lines changed

internal/github.com/hyperledger/fabric-ca/lib/identity.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,107 @@ func (i *Identity) RemoveIdentity(req *api.RemoveIdentityRequest) (*api.Identity
235235
return result, nil
236236
}
237237

238+
// GetAffiliation returns information about the requested affiliation
239+
func (i *Identity) GetAffiliation(affiliation, caname string) (*api.AffiliationResponse, error) {
240+
log.Debugf("Entering identity.GetAffiliation %+v", affiliation)
241+
result := &api.AffiliationResponse{}
242+
err := i.Get(fmt.Sprintf("affiliations/%s", affiliation), caname, result)
243+
if err != nil {
244+
return nil, err
245+
}
246+
log.Debugf("Successfully retrieved affiliation: %+v", result)
247+
return result, nil
248+
}
249+
250+
// GetAllAffiliations returns all affiliations that the caller is authorized to see
251+
func (i *Identity) GetAllAffiliations(caname string) (*api.AffiliationResponse, error) {
252+
log.Debugf("Entering identity.GetAllAffiliations")
253+
result := &api.AffiliationResponse{}
254+
err := i.Get("affiliations", caname, result)
255+
if err != nil {
256+
return nil, err
257+
}
258+
log.Debug("Successfully retrieved affiliations")
259+
return result, nil
260+
}
261+
262+
// AddAffiliation adds a new affiliation to the server
263+
func (i *Identity) AddAffiliation(req *api.AddAffiliationRequest) (*api.AffiliationResponse, error) {
264+
log.Debugf("Entering identity.AddAffiliation with request: %+v", req)
265+
if req.Name == "" {
266+
return nil, errors.New("Affiliation to add was not specified")
267+
}
268+
269+
reqBody, err := util.Marshal(req, "addAffiliation")
270+
if err != nil {
271+
return nil, err
272+
}
273+
274+
// Send a post to the "affiliations" endpoint with req as body
275+
result := &api.AffiliationResponse{}
276+
queryParam := make(map[string]string)
277+
queryParam["force"] = strconv.FormatBool(req.Force)
278+
err = i.Post("affiliations", reqBody, result, queryParam)
279+
if err != nil {
280+
return nil, err
281+
}
282+
283+
log.Debugf("Successfully added new affiliation")
284+
return result, nil
285+
}
286+
287+
// ModifyAffiliation renames an existing affiliation on the server
288+
func (i *Identity) ModifyAffiliation(req *api.ModifyAffiliationRequest) (*api.AffiliationResponse, error) {
289+
log.Debugf("Entering identity.ModifyAffiliation with request: %+v", req)
290+
modifyAff := req.Name
291+
if modifyAff == "" {
292+
return nil, errors.New("Affiliation to modify was not specified")
293+
}
294+
295+
if req.NewName == "" {
296+
return nil, errors.New("New affiliation not specified")
297+
}
298+
299+
reqBody, err := util.Marshal(req, "modifyIdentity")
300+
if err != nil {
301+
return nil, err
302+
}
303+
304+
// Send a put to the "affiliations" endpoint with req as body
305+
result := &api.AffiliationResponse{}
306+
queryParam := make(map[string]string)
307+
queryParam["force"] = strconv.FormatBool(req.Force)
308+
err = i.Put(fmt.Sprintf("affiliations/%s", modifyAff), reqBody, queryParam, result)
309+
if err != nil {
310+
return nil, err
311+
}
312+
313+
log.Debugf("Successfully modified affiliation")
314+
return result, nil
315+
}
316+
317+
// RemoveAffiliation removes an existing affiliation from the server
318+
func (i *Identity) RemoveAffiliation(req *api.RemoveAffiliationRequest) (*api.AffiliationResponse, error) {
319+
log.Debugf("Entering identity.RemoveAffiliation with request: %+v", req)
320+
removeAff := req.Name
321+
if removeAff == "" {
322+
return nil, errors.New("Affiliation to remove was not specified")
323+
}
324+
325+
// Send a delete to the "affiliations" endpoint with the affiliation as a path parameter
326+
result := &api.AffiliationResponse{}
327+
queryParam := make(map[string]string)
328+
queryParam["force"] = strconv.FormatBool(req.Force)
329+
queryParam["ca"] = req.CAName
330+
err := i.Delete(fmt.Sprintf("affiliations/%s", removeAff), result, queryParam)
331+
if err != nil {
332+
return nil, err
333+
}
334+
335+
log.Debugf("Successfully removed affiliation")
336+
return result, nil
337+
}
338+
238339
// Get sends a get request to an endpoint
239340
func (i *Identity) Get(endpoint, caname string, result interface{}) error {
240341
req, err := i.client.newGet(endpoint)

pkg/client/msp/ca.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,47 @@ type RemoveIdentityRequest struct {
136136
// Name of the CA
137137
CAName string
138138
}
139+
140+
// AffiliationRequest represents the request to add/remove affiliation to the fabric-ca-server
141+
type AffiliationRequest struct {
142+
// Name of the affiliation
143+
Name string
144+
145+
// Creates parent affiliations if they do not exist
146+
Force bool
147+
148+
// Name of the CA
149+
CAName string
150+
}
151+
152+
// ModifyAffiliationRequest represents the request to modify an existing affiliation on the
153+
// fabric-ca-server
154+
type ModifyAffiliationRequest struct {
155+
AffiliationRequest
156+
157+
// New name of the affiliation
158+
NewName string
159+
}
160+
161+
// AffiliationResponse contains the response for get, add, modify, and remove an affiliation
162+
type AffiliationResponse struct {
163+
AffiliationInfo
164+
CAName string
165+
}
166+
167+
// AffiliationInfo contains the affiliation name, child affiliation info, and identities
168+
// associated with this affiliation.
169+
type AffiliationInfo struct {
170+
Name string
171+
Affiliations []AffiliationInfo
172+
Identities []IdentityInfo
173+
}
174+
175+
// IdentityInfo contains information about an identity
176+
type IdentityInfo struct {
177+
ID string
178+
Type string
179+
Affiliation string
180+
Attributes []Attribute
181+
MaxEnrollments int
182+
}

pkg/client/msp/client.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,163 @@ func (c *Client) prepareOptsFromOptions(ctx context.Client, options ...RequestOp
441441
}
442442
return opts, nil
443443
}
444+
445+
// GetAffiliation returns information about the requested affiliation
446+
func (c *Client) GetAffiliation(affiliation string, options ...RequestOption) (*AffiliationResponse, error) {
447+
// Read request options
448+
opts, err := c.prepareOptsFromOptions(c.ctx, options...)
449+
if err != nil {
450+
return nil, err
451+
}
452+
453+
ca, err := newCAClient(c.ctx, c.orgName)
454+
if err != nil {
455+
return nil, err
456+
}
457+
458+
r, err := ca.GetAffiliation(affiliation, opts.CA)
459+
if err != nil {
460+
return nil, err
461+
}
462+
463+
resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
464+
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
465+
466+
return resp, err
467+
}
468+
469+
// GetAllAffiliations returns all affiliations that the caller is authorized to see
470+
func (c *Client) GetAllAffiliations(options ...RequestOption) (*AffiliationResponse, error) {
471+
// Read request options
472+
opts, err := c.prepareOptsFromOptions(c.ctx, options...)
473+
if err != nil {
474+
return nil, err
475+
}
476+
477+
ca, err := newCAClient(c.ctx, c.orgName)
478+
if err != nil {
479+
return nil, err
480+
}
481+
482+
r, err := ca.GetAllAffiliations(opts.CA)
483+
if err != nil {
484+
return nil, err
485+
}
486+
487+
resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
488+
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
489+
490+
return resp, err
491+
}
492+
493+
// AddAffiliation adds a new affiliation to the server
494+
func (c *Client) AddAffiliation(request *AffiliationRequest) (*AffiliationResponse, error) {
495+
ca, err := newCAClient(c.ctx, c.orgName)
496+
if err != nil {
497+
return nil, err
498+
}
499+
500+
req := &mspapi.AffiliationRequest{
501+
Name: request.Name,
502+
Force: request.Force,
503+
CAName: request.CAName,
504+
}
505+
506+
r, err := ca.AddAffiliation(req)
507+
if err != nil {
508+
return nil, err
509+
}
510+
511+
resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
512+
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
513+
514+
return resp, err
515+
}
516+
517+
// ModifyAffiliation renames an existing affiliation on the server
518+
func (c *Client) ModifyAffiliation(request *ModifyAffiliationRequest) (*AffiliationResponse, error) {
519+
ca, err := newCAClient(c.ctx, c.orgName)
520+
if err != nil {
521+
return nil, err
522+
}
523+
524+
req := &mspapi.ModifyAffiliationRequest{
525+
NewName: request.NewName,
526+
AffiliationRequest: mspapi.AffiliationRequest{
527+
Name: request.Name,
528+
Force: request.Force,
529+
CAName: request.CAName,
530+
},
531+
}
532+
533+
r, err := ca.ModifyAffiliation(req)
534+
if err != nil {
535+
return nil, err
536+
}
537+
538+
resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
539+
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
540+
541+
return resp, err
542+
}
543+
544+
// RemoveAffiliation removes an existing affiliation from the server
545+
func (c *Client) RemoveAffiliation(request *AffiliationRequest) (*AffiliationResponse, error) {
546+
ca, err := newCAClient(c.ctx, c.orgName)
547+
if err != nil {
548+
return nil, err
549+
}
550+
551+
req := &mspapi.AffiliationRequest{
552+
Name: request.Name,
553+
Force: request.Force,
554+
CAName: request.CAName,
555+
}
556+
557+
r, err := ca.RemoveAffiliation(req)
558+
if err != nil {
559+
return nil, err
560+
}
561+
562+
resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
563+
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
564+
565+
return resp, err
566+
}
567+
568+
func fillAffiliationInfo(info *AffiliationInfo, name string, affiliations []mspapi.AffiliationInfo, identities []mspapi.IdentityInfo) error {
569+
info.Name = name
570+
571+
// Add identities which have this affiliation
572+
idents := []IdentityInfo{}
573+
for _, identity := range identities {
574+
idents = append(idents, IdentityInfo{ID: identity.ID, Type: identity.Type, Affiliation: identity.Affiliation, Attributes: getAllAttributes(identity.Attributes), MaxEnrollments: identity.MaxEnrollments})
575+
}
576+
if len(idents) > 0 {
577+
info.Identities = idents
578+
}
579+
580+
// Create child affiliations (if any)
581+
children := []AffiliationInfo{}
582+
for _, aff := range affiliations {
583+
childAff := AffiliationInfo{Name: aff.Name}
584+
err := fillAffiliationInfo(&childAff, aff.Name, aff.Affiliations, aff.Identities)
585+
if err != nil {
586+
return err
587+
}
588+
children = append(children, childAff)
589+
}
590+
if len(children) > 0 {
591+
info.Affiliations = children
592+
}
593+
return nil
594+
}
595+
596+
func getAllAttributes(attrs []mspapi.Attribute) []Attribute {
597+
attriburtes := []Attribute{}
598+
for _, attr := range attrs {
599+
attriburtes = append(attriburtes, Attribute{Name: attr.Name, Value: attr.Value, ECert: attr.ECert})
600+
}
601+
602+
return attriburtes
603+
}

pkg/fab/channel/membership/membership_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ import (
1313
"crypto/x509"
1414
"crypto/x509/pkix"
1515
"encoding/asn1"
16+
"encoding/pem"
17+
"fmt"
1618
"log"
1719
"math/big"
1820
"strings"
1921
"testing"
2022
"time"
2123

22-
"fmt"
23-
24-
"encoding/pem"
25-
2624
"github.com/golang/protobuf/proto"
2725
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/comm/tls"
2826
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"

pkg/fab/mocks/mockbroadcastserver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ var broadcastResponseError = &po.BroadcastResponse{Status: common.Status_INTERNA
3939
// MockBroadcastServer mock broadcast server
4040
type MockBroadcastServer struct {
4141
DeliverError error
42-
BroadcastInternalServerError bool
43-
DeliverResponse *po.DeliverResponse
4442
BroadcastError error
45-
BroadcastCustomResponse *po.BroadcastResponse
4643
Creds credentials.TransportCredentials
44+
blkNum uint64
45+
DeliverResponse *po.DeliverResponse
46+
BroadcastCustomResponse *po.BroadcastResponse
4747
srv *grpc.Server
4848
wg sync.WaitGroup
49+
BroadcastInternalServerError bool
4950
// Use the MockBroadCastServer with either a common.Block or a pb.FilteredBlock channel (do not set both)
5051
Deliveries chan *common.Block
5152
FilteredDeliveries chan *pb.FilteredBlock
52-
blkNum uint64
5353
// mutexes to ensure parallel channel deliveries are sent in sequence
5454
delMtx sync.Mutex
5555
filteredDelMtx sync.Mutex

pkg/fab/mocks/mockcaclient.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,28 @@ func (mgr *MockCAClient) ModifyIdentity(request *api.IdentityRequest) (*api.Iden
6666
func (mgr *MockCAClient) RemoveIdentity(request *api.RemoveIdentityRequest) (*api.IdentityResponse, error) {
6767
return nil, errors.New("not implemented")
6868
}
69+
70+
// AddAffiliation add affiliation
71+
func (mgr *MockCAClient) AddAffiliation(request *api.AffiliationRequest) (*api.AffiliationResponse, error) {
72+
return nil, errors.New("not implemented")
73+
}
74+
75+
// GetAllAffiliations get all affiliations
76+
func (mgr *MockCAClient) GetAllAffiliations(caname string) (*api.AffiliationResponse, error) {
77+
return nil, errors.New("not implemented")
78+
}
79+
80+
// GetAffiliation get an affiliation
81+
func (mgr *MockCAClient) GetAffiliation(affiliation, caname string) (*api.AffiliationResponse, error) {
82+
return nil, errors.New("not implemented")
83+
}
84+
85+
// ModifyAffiliation update an affiliation
86+
func (mgr *MockCAClient) ModifyAffiliation(request *api.ModifyAffiliationRequest) (*api.AffiliationResponse, error) {
87+
return nil, errors.New("not implemented")
88+
}
89+
90+
// RemoveAffiliation remove an affiliation
91+
func (mgr *MockCAClient) RemoveAffiliation(request *api.AffiliationRequest) (*api.AffiliationResponse, error) {
92+
return nil, errors.New("not implemented")
93+
}

0 commit comments

Comments
 (0)