Skip to content

Commit

Permalink
FAB-10298 NodeSDK Client to return proper CA
Browse files Browse the repository at this point in the history
The Client.getCertificateAuthority() method returns the
fabric-ca-client impl class which may be used to talk directly
to the fabric CA. The connection profile has a class called
CertificateAuthority which represents the configuration as defined
in the connection profile. The names and useage of these two class
is causing confusion. To avoid confusion and simplify usage the
fabric-client will wrapper the actual runtime class and provide both
functions, config and impl. The Client.getCertificateAuthority() will
now return the fabric-client class CertificateAuthority allowing for
other implemenations in the future without changing the API's.

Change-Id: Id2d1fe37247dd4935bbdcc38b612fb0f79c90930
Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob committed Jun 13, 2018
1 parent b6e6109 commit 5dc8931
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
5 changes: 5 additions & 0 deletions fabric-ca-client/lib/FabricCAClientImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ const FabricCAServices = class extends BaseClient {
registrar.getSigningIdentity());
}

/**
* Create a new {@link CertificateService} instance
*
* @returns {CertificateService} object
*/
newCertificateService() {
return this._fabricCAClient.newCertificateService();
}
Expand Down
84 changes: 83 additions & 1 deletion fabric-client/lib/CertificateAuthority.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ var utils = require('./utils.js');
var logger = utils.getLogger('CertificateAuthority.js');

/**
* The CertificateAuthority class represents an Certificate Authority in the target blockchain network.
* The CertificateAuthority class represents a Certificate Authority configuration
* as defined in a Connection Profile. This class will wrapper a FabricCAClientImpl
* fabric-ca-client implementation as a FabricCAServices instance when this class
* is returned from the {@link Client#getCertificateAuthority} method. This class
* has all the same methods as the {@link FabricCAServices} so that this class
* may be used directly or use this class's {@link CertificateAuthority#getFabricCAServices}
* method to get the actual FabricCAServices instance.
*
* @class
*/
Expand Down Expand Up @@ -43,6 +49,8 @@ var CertificateAuthority = class {
this._connection_options = connection_options;
this._tlsCACerts = tlsCACerts;
this._registrar = registrar;

this.fabricCAServices = null;
}

/**
Expand Down Expand Up @@ -99,6 +107,80 @@ var CertificateAuthority = class {
return this._registrar;
}

/**
* Set the FabricCAServices implementation
*
* @param {FabricCAServices} ca_services {@link FabricCAServices}
*/
setFabricCAServices(ca_services) {
this.fabricCAServices = ca_services;
}

/**
* Get the FabricCAServices implementation
*
* @return {@link FabricCAServices}
*/
getFabricCAServices() {
return this.fabricCAServices;
}

/**
* see {@link FabricCAServices#register}
*/
register(req, registrar) {
return this.fabricCAServices.register(req, registrar);
}

/**
* see {@link FabricCAServices#enroll}
*/
enroll(req) {
return this.fabricCAServices.enroll(req);
}

/**
* see {@link FabricCAServices#reenroll}
*/
reenroll(currentUser, attr_reqs) {
return this.fabricCAServices.reenroll(currentUser, attr_reqs);
}

/**
* see {@link FabricCAServices#revoke}
*/
revoke(request, registrar) {
return this.fabricCAServices.revoke(request, registrar);
}

/**
* see {@link FabricCAServices#generateCRL}
*/
generateCRL(request, registrar) {
return this.fabricCAServices.generateCRL(request, registrar);
}

/**
* see {@link FabricCAServices#newCertificateService}
*/
newCertificateService() {
return this.fabricCAServices.newCertificateService();
}

/**
* see {@link FabricCAServices#newIdentityService}
*/
newIdentityService() {
return this.fabricCAServices.newIdentityService();
}

/**
* see {@link FabricCAServices#newAffiliationService}
*/
newAffiliationService() {
return this.fabricCAServices.newAffiliationService();
}

/**
* return a printable representation of this object
*/
Expand Down
6 changes: 3 additions & 3 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ const Client = class extends BaseClient {
throw new Error('A crypto suite has not been assigned to this client');
}
let ca_info = null;
let ca_service = null;

if(name) {
ca_info = this._network_config.getCertificateAuthority(name);
Expand All @@ -472,12 +471,13 @@ const Client = class extends BaseClient {
}

if(ca_info) {
ca_service = this._buildCAfromConfig(ca_info);
const ca_service = this._buildCAfromConfig(ca_info);
ca_info.setFabricCAServices(ca_service);
} else {
throw new Error('Network configuration is missing this client\'s organization and certificate authority');
}

return ca_service;
return ca_info;
}

/*
Expand Down
8 changes: 4 additions & 4 deletions test/integration/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ test('\n\n***** use the connection profile file *****\n\n', function(t) {

// get the CA associated with this client's organization
let caService = client_org1.getCertificateAuthority();
t.equals(caService._fabricCAClient._caName,'ca-org1', 'checking that caname is correct after resetting the config');
t.equals(caService.fabricCAServices._fabricCAClient._caName,'ca-org1', 'checking that caname is correct after resetting the config');

let request = {
enrollmentID: 'admin',
Expand All @@ -111,7 +111,7 @@ test('\n\n***** use the connection profile file *****\n\n', function(t) {

// get the CA associated with this client's organization
let caService = client_org2.getCertificateAuthority();
t.equals(caService._fabricCAClient._caName,'ca-org2', 'checking that caname is correct after resetting the config');
t.equals(caService.fabricCAServices._fabricCAClient._caName,'ca-org2', 'checking that caname is correct after resetting the config');
let request = {
enrollmentID: 'admin',
enrollmentSecret: 'adminpw',
Expand Down Expand Up @@ -720,7 +720,7 @@ test('\n\n***** Enroll user and set user context using a specified caName *****\
t.pass('Successfully created the key value store and crypto store based on the config and network config');

let caService = client_org1.getCertificateAuthority();
t.equals(caService._fabricCAClient._caName, ca_name, 'checking that caname is correct after resetting the config');
t.equals(caService.fabricCAServices._fabricCAClient._caName, ca_name, 'checking that caname is correct after resetting the config');

const admin = await client_org1.setUserContext({username:'admin', password: 'adminpw'});
t.pass('Successfully set user context \'admin\' for ' + org_name);
Expand Down Expand Up @@ -784,7 +784,7 @@ test('\n\n***** Enroll user and set user context using a bad caName *****\n\n',
t.pass('Successfully created the key value store and crypto store based on the config and network config');

let caService = client_org1.getCertificateAuthority();
t.equals(caService._fabricCAClient._caName, ca_name, 'checking that caname is correct after resetting the config');
t.equals(caService.fabricCAServices._fabricCAClient._caName, ca_name, 'checking that caname is correct after resetting the config');

const admin = await client_org1.setUserContext({username:'admin', password: 'adminpw'});
t.pass('Successfully set user context \'admin\' for ' + org_name);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ test('\n\n ** configuration testing **\n\n', function (t) {
delete client._network_config._network_config.certificateAuthorities['ca-org1'].httpOptions;
client.setCryptoSuite({cryptoSuite : 'cryptoSuite'});
let certificate_authority = client.getCertificateAuthority();
if(certificate_authority && certificate_authority._cryptoSuite && certificate_authority._cryptoSuite.cryptoSuite === 'cryptoSuite') {
if(certificate_authority && certificate_authority.fabricCAServices._cryptoSuite && certificate_authority.fabricCAServices._cryptoSuite.cryptoSuite === 'cryptoSuite') {
t.pass('Successfully got the certificate_authority');
} else {
t.fail('Failed to get the certificate_authority');
Expand Down

0 comments on commit 5dc8931

Please sign in to comment.