Skip to content

Commit

Permalink
[FABG-682] MSP Client: Affiliation Service
Browse files Browse the repository at this point in the history
Change-Id: Ic12be3c04d6f6d89c4962a9b5cacf62cc364f6f5
Signed-off-by: 乔伦 徐 <jamesxql@gmail.com>
  • Loading branch information
gotoxu committed Oct 19, 2018
1 parent 6a3c34e commit a43e084
Show file tree
Hide file tree
Showing 21 changed files with 983 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,107 @@ func (i *Identity) RemoveIdentity(req *api.RemoveIdentityRequest) (*api.Identity
return result, nil
}

// GetAffiliation returns information about the requested affiliation
func (i *Identity) GetAffiliation(affiliation, caname string) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.GetAffiliation %+v", affiliation)
result := &api.AffiliationResponse{}
err := i.Get(fmt.Sprintf("affiliations/%s", affiliation), caname, result)
if err != nil {
return nil, err
}
log.Debugf("Successfully retrieved affiliation: %+v", result)
return result, nil
}

// GetAllAffiliations returns all affiliations that the caller is authorized to see
func (i *Identity) GetAllAffiliations(caname string) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.GetAllAffiliations")
result := &api.AffiliationResponse{}
err := i.Get("affiliations", caname, result)
if err != nil {
return nil, err
}
log.Debug("Successfully retrieved affiliations")
return result, nil
}

// AddAffiliation adds a new affiliation to the server
func (i *Identity) AddAffiliation(req *api.AddAffiliationRequest) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.AddAffiliation with request: %+v", req)
if req.Name == "" {
return nil, errors.New("Affiliation to add was not specified")
}

reqBody, err := util.Marshal(req, "addAffiliation")
if err != nil {
return nil, err
}

// Send a post to the "affiliations" endpoint with req as body
result := &api.AffiliationResponse{}
queryParam := make(map[string]string)
queryParam["force"] = strconv.FormatBool(req.Force)
err = i.Post("affiliations", reqBody, result, queryParam)
if err != nil {
return nil, err
}

log.Debugf("Successfully added new affiliation")
return result, nil
}

// ModifyAffiliation renames an existing affiliation on the server
func (i *Identity) ModifyAffiliation(req *api.ModifyAffiliationRequest) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.ModifyAffiliation with request: %+v", req)
modifyAff := req.Name
if modifyAff == "" {
return nil, errors.New("Affiliation to modify was not specified")
}

if req.NewName == "" {
return nil, errors.New("New affiliation not specified")
}

reqBody, err := util.Marshal(req, "modifyIdentity")
if err != nil {
return nil, err
}

// Send a put to the "affiliations" endpoint with req as body
result := &api.AffiliationResponse{}
queryParam := make(map[string]string)
queryParam["force"] = strconv.FormatBool(req.Force)
err = i.Put(fmt.Sprintf("affiliations/%s", modifyAff), reqBody, queryParam, result)
if err != nil {
return nil, err
}

log.Debugf("Successfully modified affiliation")
return result, nil
}

// RemoveAffiliation removes an existing affiliation from the server
func (i *Identity) RemoveAffiliation(req *api.RemoveAffiliationRequest) (*api.AffiliationResponse, error) {
log.Debugf("Entering identity.RemoveAffiliation with request: %+v", req)
removeAff := req.Name
if removeAff == "" {
return nil, errors.New("Affiliation to remove was not specified")
}

// Send a delete to the "affiliations" endpoint with the affiliation as a path parameter
result := &api.AffiliationResponse{}
queryParam := make(map[string]string)
queryParam["force"] = strconv.FormatBool(req.Force)
queryParam["ca"] = req.CAName
err := i.Delete(fmt.Sprintf("affiliations/%s", removeAff), result, queryParam)
if err != nil {
return nil, err
}

log.Debugf("Successfully removed affiliation")
return result, nil
}

// Get sends a get request to an endpoint
func (i *Identity) Get(endpoint, caname string, result interface{}) error {
req, err := i.client.newGet(endpoint)
Expand Down
44 changes: 44 additions & 0 deletions pkg/client/msp/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,47 @@ type RemoveIdentityRequest struct {
// Name of the CA
CAName string
}

// AffiliationRequest represents the request to add/remove affiliation to the fabric-ca-server
type AffiliationRequest struct {
// Name of the affiliation
Name string

// Creates parent affiliations if they do not exist
Force bool

// Name of the CA
CAName string
}

// ModifyAffiliationRequest represents the request to modify an existing affiliation on the
// fabric-ca-server
type ModifyAffiliationRequest struct {
AffiliationRequest

// New name of the affiliation
NewName string
}

// AffiliationResponse contains the response for get, add, modify, and remove an affiliation
type AffiliationResponse struct {
AffiliationInfo
CAName string
}

// AffiliationInfo contains the affiliation name, child affiliation info, and identities
// associated with this affiliation.
type AffiliationInfo struct {
Name string
Affiliations []AffiliationInfo
Identities []IdentityInfo
}

// IdentityInfo contains information about an identity
type IdentityInfo struct {
ID string
Type string
Affiliation string
Attributes []Attribute
MaxEnrollments int
}
160 changes: 160 additions & 0 deletions pkg/client/msp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,163 @@ func (c *Client) prepareOptsFromOptions(ctx context.Client, options ...RequestOp
}
return opts, nil
}

// GetAffiliation returns information about the requested affiliation
func (c *Client) GetAffiliation(affiliation string, options ...RequestOption) (*AffiliationResponse, error) {
// Read request options
opts, err := c.prepareOptsFromOptions(c.ctx, options...)
if err != nil {
return nil, err
}

ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return nil, err
}

r, err := ca.GetAffiliation(affiliation, opts.CA)
if err != nil {
return nil, err
}

resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)

return resp, err
}

// GetAllAffiliations returns all affiliations that the caller is authorized to see
func (c *Client) GetAllAffiliations(options ...RequestOption) (*AffiliationResponse, error) {
// Read request options
opts, err := c.prepareOptsFromOptions(c.ctx, options...)
if err != nil {
return nil, err
}

ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return nil, err
}

r, err := ca.GetAllAffiliations(opts.CA)
if err != nil {
return nil, err
}

resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)

return resp, err
}

// AddAffiliation adds a new affiliation to the server
func (c *Client) AddAffiliation(request *AffiliationRequest) (*AffiliationResponse, error) {
ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return nil, err
}

req := &mspapi.AffiliationRequest{
Name: request.Name,
Force: request.Force,
CAName: request.CAName,
}

r, err := ca.AddAffiliation(req)
if err != nil {
return nil, err
}

resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)

return resp, err
}

// ModifyAffiliation renames an existing affiliation on the server
func (c *Client) ModifyAffiliation(request *ModifyAffiliationRequest) (*AffiliationResponse, error) {
ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return nil, err
}

req := &mspapi.ModifyAffiliationRequest{
NewName: request.NewName,
AffiliationRequest: mspapi.AffiliationRequest{
Name: request.Name,
Force: request.Force,
CAName: request.CAName,
},
}

r, err := ca.ModifyAffiliation(req)
if err != nil {
return nil, err
}

resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)

return resp, err
}

// RemoveAffiliation removes an existing affiliation from the server
func (c *Client) RemoveAffiliation(request *AffiliationRequest) (*AffiliationResponse, error) {
ca, err := newCAClient(c.ctx, c.orgName)
if err != nil {
return nil, err
}

req := &mspapi.AffiliationRequest{
Name: request.Name,
Force: request.Force,
CAName: request.CAName,
}

r, err := ca.RemoveAffiliation(req)
if err != nil {
return nil, err
}

resp := &AffiliationResponse{CAName: r.CAName, AffiliationInfo: AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)

return resp, err
}

func fillAffiliationInfo(info *AffiliationInfo, name string, affiliations []mspapi.AffiliationInfo, identities []mspapi.IdentityInfo) error {
info.Name = name

// Add identities which have this affiliation
idents := []IdentityInfo{}
for _, identity := range identities {
idents = append(idents, IdentityInfo{ID: identity.ID, Type: identity.Type, Affiliation: identity.Affiliation, Attributes: getAllAttributes(identity.Attributes), MaxEnrollments: identity.MaxEnrollments})
}
if len(idents) > 0 {
info.Identities = idents
}

// Create child affiliations (if any)
children := []AffiliationInfo{}
for _, aff := range affiliations {
childAff := AffiliationInfo{Name: aff.Name}
err := fillAffiliationInfo(&childAff, aff.Name, aff.Affiliations, aff.Identities)
if err != nil {
return err
}
children = append(children, childAff)
}
if len(children) > 0 {
info.Affiliations = children
}
return nil
}

func getAllAttributes(attrs []mspapi.Attribute) []Attribute {
attriburtes := []Attribute{}
for _, attr := range attrs {
attriburtes = append(attriburtes, Attribute{Name: attr.Name, Value: attr.Value, ECert: attr.ECert})
}

return attriburtes
}
6 changes: 2 additions & 4 deletions pkg/fab/channel/membership/membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"fmt"
"log"
"math/big"
"strings"
"testing"
"time"

"fmt"

"encoding/pem"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/comm/tls"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
Expand Down
8 changes: 4 additions & 4 deletions pkg/fab/mocks/mockbroadcastserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ var broadcastResponseError = &po.BroadcastResponse{Status: common.Status_INTERNA
// MockBroadcastServer mock broadcast server
type MockBroadcastServer struct {
DeliverError error
BroadcastInternalServerError bool
DeliverResponse *po.DeliverResponse
BroadcastError error
BroadcastCustomResponse *po.BroadcastResponse
Creds credentials.TransportCredentials
blkNum uint64
DeliverResponse *po.DeliverResponse
BroadcastCustomResponse *po.BroadcastResponse
srv *grpc.Server
wg sync.WaitGroup
BroadcastInternalServerError bool
// Use the MockBroadCastServer with either a common.Block or a pb.FilteredBlock channel (do not set both)
Deliveries chan *common.Block
FilteredDeliveries chan *pb.FilteredBlock
blkNum uint64
// mutexes to ensure parallel channel deliveries are sent in sequence
delMtx sync.Mutex
filteredDelMtx sync.Mutex
Expand Down
25 changes: 25 additions & 0 deletions pkg/fab/mocks/mockcaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ func (mgr *MockCAClient) ModifyIdentity(request *api.IdentityRequest) (*api.Iden
func (mgr *MockCAClient) RemoveIdentity(request *api.RemoveIdentityRequest) (*api.IdentityResponse, error) {
return nil, errors.New("not implemented")
}

// AddAffiliation add affiliation
func (mgr *MockCAClient) AddAffiliation(request *api.AffiliationRequest) (*api.AffiliationResponse, error) {
return nil, errors.New("not implemented")
}

// GetAllAffiliations get all affiliations
func (mgr *MockCAClient) GetAllAffiliations(caname string) (*api.AffiliationResponse, error) {
return nil, errors.New("not implemented")
}

// GetAffiliation get an affiliation
func (mgr *MockCAClient) GetAffiliation(affiliation, caname string) (*api.AffiliationResponse, error) {
return nil, errors.New("not implemented")
}

// ModifyAffiliation update an affiliation
func (mgr *MockCAClient) ModifyAffiliation(request *api.ModifyAffiliationRequest) (*api.AffiliationResponse, error) {
return nil, errors.New("not implemented")
}

// RemoveAffiliation remove an affiliation
func (mgr *MockCAClient) RemoveAffiliation(request *api.AffiliationRequest) (*api.AffiliationResponse, error) {
return nil, errors.New("not implemented")
}
Loading

0 comments on commit a43e084

Please sign in to comment.