From c60d73e4cae732c66cc7337b23db4fe8ae62f39d Mon Sep 17 00:00:00 2001 From: "nkl199@yahoo.co.uk" Date: Mon, 17 Feb 2020 16:10:02 +0000 Subject: [PATCH] fix config validator for permitted properties Signed-off-by: nkl199@yahoo.co.uk --- .../caliper-fabric/lib/configValidator.js | 17 ++++++++++--- .../caliper-fabric/test/configValidator.js | 25 ++++++++++++++++++- .../fabric/myWorkspace/networkconfig.yaml | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/packages/caliper-fabric/lib/configValidator.js b/packages/caliper-fabric/lib/configValidator.js index 8f86d8fb8f..5ed0123cda 100644 --- a/packages/caliper-fabric/lib/configValidator.js +++ b/packages/caliper-fabric/lib/configValidator.js @@ -39,6 +39,12 @@ class ConfigValidator { throw new Error('Use of service discovery is only valid with a `caliper-flow-only-test` flag'); } + // registrar requirement removed if only-test + let requireRegistrar = 'required'; + if (flowOptions.performTest && (!flowOptions.performInit && !flowOptions.performInstall)) { + requireRegistrar = 'optional'; + } + let tls; // undefined => we don't know yet // the TLS setting might not be known after the individual section if they are missing // the first existing node will determine its value, and after that every node is validated against that value @@ -53,7 +59,7 @@ class ConfigValidator { cas = Object.keys(config.certificateAuthorities); for (let ca of cas) { try { - ConfigValidator.validateCertificateAuthority(config.certificateAuthorities[ca], tls); + ConfigValidator.validateCertificateAuthority(config.certificateAuthorities[ca], tls, requireRegistrar); tls = (tls || false) || config.certificateAuthorities[ca].url.startsWith('https://'); } catch (err) { throw new Error(`Invalid "${ca}" CA configuration: ${err.message}`); @@ -206,6 +212,7 @@ class ConfigValidator { const binary = !!config.configBinary; const def = !!config.definition; const ordererModif = discovery ? 'optional' : 'required'; + const peerModif = discovery ? 'optional' : 'required'; let binaryModif; let defModif; @@ -306,7 +313,7 @@ class ConfigValidator { })[defModif](), orderers: j.array().sparse(false).items(j.string().valid(validOrderers)).unique()[ordererModif](), - peers: j.object().keys(createPeersSchema()).required(), + peers: j.object().keys(createPeersSchema())[peerModif](), // leave this embedded, so the validation error messages are more meaningful chaincodes: j.array().sparse(false).items(j.object().keys({ @@ -358,11 +365,13 @@ class ConfigValidator { * Validates the given CA configuration object. * @param {object} config The configuration object. * @param {boolean} tls Indicates whether TLS is enabled or known at this point. + * @param {string} requireRegistrar Indicates whether a registrar is optional or required. */ - static validateCertificateAuthority(config, tls) { + static validateCertificateAuthority(config, tls, requireRegistrar) { let urlRegex = tls === undefined ? /^(https|http):\/\// : (tls ? /^https:\/\// : /^http:\/\//); const schema = j.object().keys({ + caName: j.string().optional(), url: j.string().uri().regex(urlRegex).required(), httpOptions: j.object().optional(), @@ -380,7 +389,7 @@ class ConfigValidator { registrar: j.array().items(j.object().keys({ enrollId: j.string().min(1).required(), enrollSecret: j.string().min(1).required() - })).min(1).sparse(false).unique('enrollId').required() + })).min(1).sparse(false).unique('enrollId')[requireRegistrar]() }); let options = { diff --git a/packages/caliper-fabric/test/configValidator.js b/packages/caliper-fabric/test/configValidator.js index 1cabaad30b..8aa2352bda 100644 --- a/packages/caliper-fabric/test/configValidator.js +++ b/packages/caliper-fabric/test/configValidator.js @@ -825,22 +825,45 @@ describe('Class: ConfigValidator', () => { }; let configString = JSON.stringify(config); + let configNoRegistrar = { + url: 'https://localhost:7054', + httpOptions: { + verify: false + }, + tlsCACerts: { + path: 'my/path/tocert' + } + }; + let configStringNoRegistrar = JSON.stringify(configNoRegistrar); + // reset the local config before every test beforeEach(() => { config = JSON.parse(configString); + configNoRegistrar = JSON.parse(configStringNoRegistrar); }); /** * Wraps the actual call, so "should" can call this function without parameters */ function call() { - ConfigValidator.validateCertificateAuthority(config, tls); + ConfigValidator.validateCertificateAuthority(config, tls, 'required'); + } + + /** + * Wraps the actual call, so "should" can call this function without parameters + */ + function callNoRegistrar() { + ConfigValidator.validateCertificateAuthority(configNoRegistrar, tls, 'optional'); } it('should not throw for a valid value', () => { call.should.not.throw(); }); + it('should not throw for a valid value', () => { + callNoRegistrar.should.not.throw(); + }); + it('should throw for an unknown child property', () => { const err = '"unknown" is not allowed'; config.unknown = ''; diff --git a/packages/caliper-tests-integration/generator_tests/fabric/myWorkspace/networkconfig.yaml b/packages/caliper-tests-integration/generator_tests/fabric/myWorkspace/networkconfig.yaml index 10ab49c8e3..f12f2a11d8 100644 --- a/packages/caliper-tests-integration/generator_tests/fabric/myWorkspace/networkconfig.yaml +++ b/packages/caliper-tests-integration/generator_tests/fabric/myWorkspace/networkconfig.yaml @@ -13,7 +13,7 @@ # name: Fabric -version: "1.0" +version: "1.0.0" mutual-tls: true caliper: