Skip to content

Commit

Permalink
[FAB-6707] NodeSDK - getCertificate auth
Browse files Browse the repository at this point in the history
NodeSDK should not build a certificate authority
without a crypto suite available.

Change-Id: I26c965f87fc897f954ef9da48c1fe5f908743836
Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob committed Oct 27, 2017
1 parent 0fca3f5 commit f534a3a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
15 changes: 11 additions & 4 deletions fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,13 @@ var Client = class extends BaseClient {
}

/**
* Returns a CertificateAuthority implementation as defined by the settings in the
* currently loaded network configuration and the client configuration. A network
* configuration must be loaded for this get method to return a Certificate Authority.
* Returns a CertificateAuthority implementation as defined by the settings
* in the currently loaded network configuration and the client configuration.
* A network configuration must be loaded for this get method to return a
* Certificate Authority.
* A crypto suite must be assigned to this client instance. Running the
* 'initCredentialStores' method will build the stores and create a crypto
* suite as defined in the network configuration.
*/
getCertificateAuthority() {
if(this._certificate_authority) {
Expand All @@ -360,6 +364,9 @@ var Client = class extends BaseClient {
if(!this._network_config) {
throw new Error('No network configuration has been loaded');
}
if(!this._cryptoSuite) {
throw new Error('A crypto suite has not been assigned to this client');
}
let ca_url, tls_options, ca_name = null;
let client_config = this._network_config.getClientConfig();
if(client_config && client_config.organization) {
Expand Down Expand Up @@ -395,7 +402,7 @@ var Client = class extends BaseClient {

let ca_service_class = Client.getConfigSetting('certificate-authority-client');
let ca_service_impl = require(ca_service_class);
let ca_service = new ca_service_impl( {url : ca_url, tlsOptions : tls_options, caName : ca_name, cryptoSuite : this._crypto_suite});
let ca_service = new ca_service_impl( {url : ca_url, tlsOptions : tls_options, caName : ca_name, cryptoSuite : this._cryptoSuite});
this._certificate_authority = ca_service;

return ca_service;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ test('\n\n***** use the network configuration file *****\n\n', function(t) {

return client.initCredentialStores();
}).then((nothing) => {
t.pass('Successfully created the key value store and crypto store based on the config and network');
t.pass('Successfully created the key value store and crypto store based on the config and network');

return client.setUserContext({username:'admin', password:'adminpw'});
}).then((admin) => {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/network-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ test('\n\n ** configuration testing **\n\n', function (t) {
'Should not be able to instantiate a new instance of "Channel" with a bad channel'
);

t.throws(
() => {
var client = Client.loadFromConfig('test/fixtures/network.json');
var ca = client.getCertificateAuthority();
},
/A crypto suite has not been assigned to this client/,
'Should not be able to instantiate a new instance of a certificate authority until a crypto suite is assigned'
);

t.doesNotThrow(
() => {
var client = Client.loadFromConfig('test/fixtures/network.yaml');
Expand Down Expand Up @@ -157,7 +166,13 @@ test('\n\n ** configuration testing **\n\n', function (t) {

delete client._network_config._network_config.certificateAuthorities['ca-org1'].tlsCACerts;
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') {
t.pass('Successfully got the certificate_authority');
} else {
t.fail('Failed to get the certificate_authority');
}

},
null,
Expand Down Expand Up @@ -195,6 +210,7 @@ test('\n\n ** configuration testing **\n\n', function (t) {
() => {
var client = new Client();
client._network_config = new NetworkConfig({}, client);
client.setCryptoSuite({cryptoSuite : 'cryptoSuite'});
client.getCertificateAuthority();
},
/Network configuration is missing this client\'s organization and certificate authority/,
Expand Down

0 comments on commit f534a3a

Please sign in to comment.