Skip to content

Commit

Permalink
FABN-1321 Validate boolean type value of Config
Browse files Browse the repository at this point in the history
By using yn package, validate discovery-as-localhost and crypto-hsm.

Signed-off-by: Atsushi Neki <[email protected]>
Change-Id: Ia4ee19cf27266ff833a5ab90e042a6736272a782
  • Loading branch information
nekia committed Jul 26, 2019
1 parent 5d1a88b commit 58c70b5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
3 changes: 2 additions & 1 deletion fabric-client/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const MSPManager = require('./msp/msp-manager.js');
const Policy = require('./Policy.js');
const Constants = require('./Constants.js');
const CollectionConfig = require('./SideDB.js');
const yn = require('yn');

const ChannelHelper = require('./utils/ChannelHelper');

Expand Down Expand Up @@ -109,7 +110,7 @@ const Channel = class {
this._discovery_results = null;
this._last_discover_timestamp = null;
this._use_discovery = sdk_utils.getConfigSetting('initialize-with-discovery', false);
this._as_localhost = sdk_utils.getConfigSetting('discovery-as-localhost', true);
this._as_localhost = yn(sdk_utils.getConfigSetting('discovery-as-localhost', true));
this._discovery_cache_life = sdk_utils.getConfigSetting('discovery-cache-life', 300000); // default is 5 minutes
this._endorsement_handler = null;
this._commit_handler = null;
Expand Down
3 changes: 2 additions & 1 deletion fabric-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"promise-settle": "^0.3.0",
"stream-buffers": "3.0.1",
"tar-stream": "1.6.1",
"url": "^0.11.0"
"url": "^0.11.0",
"yn": "^3.1.0"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
32 changes: 32 additions & 0 deletions fabric-client/test/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,38 @@ describe('Channel', () => {
expect(channel._as_localhost).to.be.true;
});

it('set channel._as_localhost if getConfigSetting returns string value \'true\'', async () => {
sinon.stub(sdk_utils, 'getConfigSetting').returns('true');
const cli = new Client();
const ch = new Channel('does-not-matter', cli);

expect(ch._as_localhost).to.be.true;
});

it('set channel._as_localhost to false if getConfigSetting returns string value \'false\'', async () => {
sinon.stub(sdk_utils, 'getConfigSetting').returns('false');
const cli = new Client();
const ch = new Channel('does-not-matter', cli);

expect(ch._as_localhost).to.be.false;
});

it('set channel._as_localhost if getConfigSetting returns boolean value true', async () => {
sinon.stub(sdk_utils, 'getConfigSetting').returns(true);
const cli = new Client();
const ch = new Channel('does-not-matter', cli);

expect(ch._as_localhost).to.be.true;
});

it('set channel._as_localhost to false if getConfigSetting returns boolean value false', async () => {
sinon.stub(sdk_utils, 'getConfigSetting').returns(false);
const cli = new Client();
const ch = new Channel('does-not-matter', cli);

expect(ch._as_localhost).to.be.false;
});

it('throws if discovery is set and target peer has no msp information', () => {
sinon.stub(peer1, 'sendDiscovery').resolves({results: []});
return expect(channel.initialize({discover: true, target: peer1})).to.be.rejectedWith('No MSP information found');
Expand Down
3 changes: 2 additions & 1 deletion fabric-common/lib/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const os = require('os');

const Config = require('./Config');
const sjcl = require('sjcl');
const yn = require('yn');

//
// The following methods are for loading the proper implementation of an extensible APIs.
Expand All @@ -41,7 +42,7 @@ module.exports.newCryptoSuite = (setting) => {
if (setting && typeof setting.software === 'boolean') {
useHSM = !setting.software;
} else {
useHSM = exports.getConfigSetting('crypto-hsm');
useHSM = yn(exports.getConfigSetting('crypto-hsm'));
}

csImpl = useHSM ? exports.getConfigSetting('crypto-suite-hsm') : exports.getConfigSetting('crypto-suite-software');
Expand Down
15 changes: 8 additions & 7 deletions fabric-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
"test": "nyc mocha --exclude 'test/data/**/*.js' --recursive -t 10000"
},
"engines": {
"node": "^8.9.0 || ^10.15.3",
"npm": "^5.5.1 || ^6.4.1"
},
"node": "^8.9.0 || ^10.15.3",
"npm": "^5.5.1 || ^6.4.1"
},
"dependencies": {
"elliptic": "^6.2.3",
"winston": "^2.2.0",
"nconf": "^0.10.0",
"nano": "^6.4.4",
"js-sha3": "^0.7.0",
"nano": "^6.4.4",
"nconf": "^0.10.0",
"pkcs11js": "^1.0.6",
"sjcl": "1.0.7"
"sjcl": "1.0.7",
"winston": "^2.2.0",
"yn": "^3.1.0"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
29 changes: 29 additions & 0 deletions test/unit/cryptosuite-ecdsa-aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ test('\n\n** utils.newCryptoSuite tests **\n\n', (t) => {
expectedError,
'Should attempt to load the bccsp_pkcs11 module and fail because of the dummy library path'
);

// Control crypto-hsm settings via env variable
process.env.CRYPTO_HSM = false;
testutil.resetDefaults();
cs = utils.newCryptoSuite({keysize: 384, algorithm: 'EC'});
t.equal(cs instanceof CryptoSuite_ECDSA_AES, true, 'Should return an instance of CryptoSuite_ECDSA_AES');
t.equal(cs._keySize, 384, 'Returned instance should have keysize of 384');

process.env.CRYPTO_HSM = true;
testutil.resetDefaults();
t.throws(
() => {
cs = utils.newCryptoSuite({keysize: 384, algorithm: 'EC'});
},
/PKCS11 library path must be specified/,
'Should attempt to load the bccsp_pkcs11 module and fail because of the lack of library path'
);

process.env.CRYPTO_HSM = true;
testutil.resetDefaults();
t.throws(
() => {
cs = utils.newCryptoSuite({lib: '/usr/local/lib', slot: 0, pin: '1234'});
},
expectedError,
'Should attempt to load the bccsp_pkcs11 module and fail because of the dummy library path'
);
// Need to set it 'undefined' to prevent side effect to the other test suites
process.env.CRYPTO_HSM = undefined;
t.end();
});

Expand Down

0 comments on commit 58c70b5

Please sign in to comment.