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

Commit 34bbf26

Browse files
committed
[FABG-681] MSP Client: CAInfo
Change-Id: I9afe80529c1fe3f1b86f28d4507968a019a198b2 Signed-off-by: 乔伦 徐 <jamesxql@gmail.com>
1 parent 1d066cf commit 34bbf26

File tree

11 files changed

+189
-2
lines changed

11 files changed

+189
-2
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,33 @@ func (c *Client) initHTTPClient(serverName string) error {
157157
return nil
158158
}
159159

160+
// GetCAInfo returns generic CA information
161+
func (c *Client) GetCAInfo(req *api.GetCAInfoRequest) (*GetCAInfoResponse, error) {
162+
err := c.Init()
163+
if err != nil {
164+
return nil, err
165+
}
166+
body, err := util.Marshal(req, "GetCAInfo")
167+
if err != nil {
168+
return nil, err
169+
}
170+
cainforeq, err := c.newPost("cainfo", body)
171+
if err != nil {
172+
return nil, err
173+
}
174+
netSI := &common.CAInfoResponseNet{}
175+
err = c.SendReq(cainforeq, netSI)
176+
if err != nil {
177+
return nil, err
178+
}
179+
localSI := &GetCAInfoResponse{}
180+
err = c.net2LocalCAInfo(netSI, localSI)
181+
if err != nil {
182+
return nil, err
183+
}
184+
return localSI, nil
185+
}
186+
160187
// GenCSR generates a CSR (Certificate Signing Request)
161188
func (c *Client) GenCSR(req *api.CSRInfo, id string) ([]byte, core.Key, error) {
162189
log.Debugf("GenCSR %+v", req)

pkg/client/msp/ca.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,18 @@ type IdentityInfo struct {
180180
Attributes []Attribute
181181
MaxEnrollments int
182182
}
183+
184+
// GetCAInfoResponse is the response from the GetCAInfo call
185+
type GetCAInfoResponse struct {
186+
// CAName is the name of the CA
187+
CAName string
188+
// CAChain is the PEM-encoded bytes of the fabric-ca-server's CA chain.
189+
// The 1st element of the chain is the root CA cert
190+
CAChain []byte
191+
// Idemix issuer public key of the CA
192+
IssuerPublicKey []byte
193+
// Idemix issuer revocation public key of the CA
194+
IssuerRevocationPublicKey []byte
195+
// Version of the server
196+
Version string
197+
}

pkg/client/msp/client.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
// Client enables access to Client services
3131
type Client struct {
3232
orgName string
33+
caName string
3334
ctx context.Client
3435
}
3536

@@ -85,8 +86,14 @@ func New(clientProvider context.ClientProvider, opts ...ClientOption) (*Client,
8586
if msp.orgName == "" {
8687
return nil, errors.New("organization is not provided")
8788
}
89+
90+
caConfig, ok := ctx.IdentityConfig().CAConfig(msp.orgName)
91+
if ok {
92+
msp.caName = caConfig.CAName
93+
}
94+
8895
networkConfig := ctx.EndpointConfig().NetworkConfig()
89-
_, ok := networkConfig.Organizations[strings.ToLower(msp.orgName)]
96+
_, ok = networkConfig.Organizations[strings.ToLower(msp.orgName)]
9097
if !ok {
9198
return nil, fmt.Errorf("non-existent organization: '%s'", msp.orgName)
9299
}
@@ -406,6 +413,21 @@ func (c *Client) Revoke(request *RevocationRequest) (*RevocationResponse, error)
406413
}, nil
407414
}
408415

416+
// GetCAInfo returns generic CA information
417+
func (c *Client) GetCAInfo() (*GetCAInfoResponse, error) {
418+
ca, err := newCAClient(c.ctx, c.orgName)
419+
if err != nil {
420+
return nil, err
421+
}
422+
423+
resp, err := ca.GetCAInfo()
424+
if err != nil {
425+
return nil, err
426+
}
427+
428+
return &GetCAInfoResponse{CAName: resp.CAName, CAChain: resp.CAChain[:], IssuerPublicKey: resp.IssuerPublicKey[:], IssuerRevocationPublicKey: resp.IssuerRevocationPublicKey[:], Version: resp.Version}, nil
429+
}
430+
409431
// GetSigningIdentity returns signing identity for id
410432
// Parameters:
411433
// id is user id

pkg/fab/mocks/mockcaclient.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ func (mgr *MockCAClient) ModifyAffiliation(request *api.ModifyAffiliationRequest
9191
func (mgr *MockCAClient) RemoveAffiliation(request *api.AffiliationRequest) (*api.AffiliationResponse, error) {
9292
return nil, errors.New("not implemented")
9393
}
94+
95+
// GetCAInfo returns generic CA information
96+
func (mgr *MockCAClient) GetCAInfo() (*api.GetCAInfoResponse, error) {
97+
return nil, errors.New("not implemented")
98+
}

pkg/msp/api/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type CAClient interface {
2121
Reenroll(enrollmentID string) error
2222
Register(request *RegistrationRequest) (string, error)
2323
Revoke(request *RevocationRequest) (*RevocationResponse, error)
24+
GetCAInfo() (*GetCAInfoResponse, error)
2425
CreateIdentity(request *IdentityRequest) (*IdentityResponse, error)
2526
GetIdentity(id, caname string) (*IdentityResponse, error)
2627
ModifyIdentity(request *IdentityRequest) (*IdentityResponse, error)
@@ -206,3 +207,18 @@ type IdentityInfo struct {
206207
Attributes []Attribute
207208
MaxEnrollments int
208209
}
210+
211+
// GetCAInfoResponse is the response from the GetCAInfo call
212+
type GetCAInfoResponse struct {
213+
// CAName is the name of the CA
214+
CAName string
215+
// CAChain is the PEM-encoded bytes of the fabric-ca-server's CA chain.
216+
// The 1st element of the chain is the root CA cert
217+
CAChain []byte
218+
// Idemix issuer public key of the CA
219+
IssuerPublicKey []byte
220+
// Idemix issuer revocation public key of the CA
221+
IssuerRevocationPublicKey []byte
222+
// Version of the server
223+
Version string
224+
}

pkg/msp/caclient.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var logger = logging.NewLogger("fabsdk/msp")
2323
// CAClientImpl implements api/msp/CAClient
2424
type CAClientImpl struct {
2525
orgName string
26+
caName string // Currently, an organization can be associated with only one CA
2627
orgMSPID string
2728
cryptoSuite core.CryptoSuite
2829
identityManager msp.IdentityManager
@@ -77,6 +78,7 @@ func NewCAClient(orgName string, ctx contextApi.Client) (*CAClientImpl, error) {
7778

7879
mgr := &CAClientImpl{
7980
orgName: orgName,
81+
caName: caName,
8082
orgMSPID: orgConfig.MSPID,
8183
cryptoSuite: ctx.CryptoSuite(),
8284
identityManager: identityManager,
@@ -345,6 +347,15 @@ func (c *CAClientImpl) Revoke(request *api.RevocationRequest) (*api.RevocationRe
345347
return resp, nil
346348
}
347349

350+
// GetCAInfo returns generic CA information
351+
func (c *CAClientImpl) GetCAInfo() (*api.GetCAInfoResponse, error) {
352+
if c.adapter == nil {
353+
return nil, fmt.Errorf("no CAs configured for organization: %s", c.orgName)
354+
}
355+
356+
return c.adapter.GetCAInfo(c.caName)
357+
}
358+
348359
// GetAffiliation returns information about the requested affiliation
349360
func (c *CAClientImpl) GetAffiliation(affiliation, caname string) (*api.AffiliationResponse, error) {
350361
if c.adapter == nil {

pkg/msp/caclient_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,21 @@ func TestGetAllAffiliations(t *testing.T) {
665665
}
666666
}
667667

668+
func TestGetCAInfo(t *testing.T) {
669+
f := textFixture{}
670+
f.setup()
671+
defer f.close()
672+
673+
resp, err := f.caClient.GetCAInfo()
674+
if err != nil {
675+
t.Fatalf("Get CA info return error %s", err)
676+
}
677+
678+
if resp.CAName != "123" {
679+
t.Fatalf("expecting 123, got %s", resp.CAName)
680+
}
681+
}
682+
668683
func getCustomBackend(configPath string) ([]core.ConfigBackend, error) {
669684

670685
configBackends, err := config.FromFile(configPath)()

pkg/msp/fabcaadapter.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ func (c *fabricCAAdapter) Revoke(key core.Key, cert []byte, request *api.Revocat
154154
}, nil
155155
}
156156

157+
// GetCAInfo returns generic CA information
158+
func (c *fabricCAAdapter) GetCAInfo(caname string) (*api.GetCAInfoResponse, error) {
159+
logger.Debugf("Get CA info [%s]", caname)
160+
161+
req := &caapi.GetCAInfoRequest{CAName: caname}
162+
resp, err := c.caClient.GetCAInfo(req)
163+
if err != nil {
164+
return nil, errors.WithMessage(err, "GetCAInfo failed")
165+
}
166+
167+
return getCAInfoResponse(resp), nil
168+
}
169+
170+
func getCAInfoResponse(response *calib.GetCAInfoResponse) *api.GetCAInfoResponse {
171+
return &api.GetCAInfoResponse{
172+
CAName: response.CAName,
173+
CAChain: response.CAChain[:],
174+
IssuerPublicKey: response.IssuerPublicKey[:],
175+
IssuerRevocationPublicKey: response.IssuerRevocationPublicKey[:],
176+
Version: response.Version,
177+
}
178+
}
179+
157180
// CreateIdentity creates new identity
158181
// key: registrar private key
159182
// cert: registrar enrollment certificate

pkg/msp/test/mockmsp/mockfabriccaserver.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
cfsslapi "github.com/cloudflare/cfssl/api"
1515
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/api"
16+
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib"
1617
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/util"
1718
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
1819
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
@@ -88,6 +89,7 @@ func (s *MockFabricCAServer) Start(lis net.Listener, cryptoSuite core.CryptoSuit
8889
http.HandleFunc("/identities/123", s.identity)
8990
http.HandleFunc("/affiliations", s.affiliations)
9091
http.HandleFunc("/affiliations/123", s.affiliation)
92+
http.HandleFunc("/cainfo", s.cainfo)
9193

9294
server := &http.Server{
9395
Addr: addr,
@@ -253,3 +255,13 @@ func (s *MockFabricCAServer) affiliation(w http.ResponseWriter, req *http.Reques
253255
}
254256
}
255257
}
258+
259+
func (s *MockFabricCAServer) cainfo(w http.ResponseWriter, req *http.Request) {
260+
switch req.Method {
261+
case http.MethodPost:
262+
resp := &lib.GetCAInfoResponse{CAName: "123", CAChain: []byte{}}
263+
if err := cfsslapi.SendResponse(w, resp); err != nil {
264+
logger.Error(err)
265+
}
266+
}
267+
}

scripts/third_party_pins/fabric-ca/apply_fabric_ca_client_utils.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ FILTER_MODE="allow"
9292
FILTERS_ENABLED="fn"
9393

9494
FILTER_FILENAME="lib/client.go"
95-
FILTER_FN="Enroll,GenCSR,SendReq,Init,newPost,newEnrollmentResponse,newCertificateRequest,newPut,newGet,newDelete,StreamResponse"
95+
FILTER_FN="Enroll,GetCAInfo,GenCSR,SendReq,Init,newPost,newEnrollmentResponse,newCertificateRequest,newPut,newGet,newDelete,StreamResponse"
9696
FILTER_FN+=",getURL,NormalizeURL,initHTTPClient,net2LocalServerInfo,NewIdentity,newCfsslBasicKeyRequest"
9797
FILTER_FN+=",handleIdemixEnroll,checkX509Enrollment,handleX509Enroll,GetCSP,NewX509Identity,net2LocalCAInfo"
9898
gofilter

0 commit comments

Comments
 (0)