From d63f9d99b4f067ab4ba0b1d5f5f2b905e7af7a3f Mon Sep 17 00:00:00 2001 From: RosieMurphy0 Date: Mon, 12 Oct 2020 14:44:26 +0100 Subject: [PATCH 1/4] Extracts identities from a wallet and stores in the in memory wallet Signed-off-by: RosieMurphy0 --- .../identity-management/IdentityManager.js | 9 ++- .../identity-management/IdentityManager.js | 71 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/packages/caliper-fabric/lib/identity-management/IdentityManager.js b/packages/caliper-fabric/lib/identity-management/IdentityManager.js index 0f8487995..1d97530bf 100644 --- a/packages/caliper-fabric/lib/identity-management/IdentityManager.js +++ b/packages/caliper-fabric/lib/identity-management/IdentityManager.js @@ -184,7 +184,14 @@ class IdentityManager { * @private */ async _extractIdentitiesFromWallet(mspId, wallet) { - // TODO: To be implemented + const walletFacade = await this.walletFacadeFactory.create(wallet.path); + const allIDNames = await walletFacade.getAllIdentityNames(); + for (const identityNames in allIDNames){ + const identity = await walletFacade.export(identityNames); + if (identity){ + await this._addToWallet(identity.mspid, allIDNames[identityNames], identity.certificate, identity.privateKey); + } + } } /** diff --git a/packages/caliper-fabric/test/identity-management/IdentityManager.js b/packages/caliper-fabric/test/identity-management/IdentityManager.js index 9f7b557a5..fbe970566 100644 --- a/packages/caliper-fabric/test/identity-management/IdentityManager.js +++ b/packages/caliper-fabric/test/identity-management/IdentityManager.js @@ -144,12 +144,28 @@ const org3MSP = { } }; +const org4MSP = { + mspid: 'org4MSP', + identities: { + wallet: { + path: 'some/path/to/org-specific-wallet' + } + }, + connectionProfile: { + path: 'some/path/to/org-specific-profile', + discover: true + } +}; + describe('An Identity Manager', () => { describe('When being created by it\'s factory', () => { const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); it('should return Identity Manager instance if an array of valid organizations are supplied', async () => { + const stubWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.resolves(stubWalletFacade); + stubWalletFacade.getAllIdentityNames.resolves([]); const identityManagerFactory = new IdentityManagerFactory(); const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [blankMSP]); identityManager.should.be.instanceOf(IdentityManager); @@ -187,6 +203,9 @@ describe('An Identity Manager', () => { const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); it('should not prefix for the default organisation', async () => { + const stubWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.resolves(stubWalletFacade); + stubWalletFacade.getAllIdentityNames.resolves([]); const identityManagerFactory = new IdentityManagerFactory(); const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [blankMSP]); identityManager.getAliasNameFromOrganizationAndIdentityName('org1MSP', 'admin').should.equal('admin'); @@ -386,4 +405,56 @@ describe('An Identity Manager', () => { const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org1MSP, org2MSP]); await identityManager.getWallet().should.equal('IamAwallet'); }); + + describe('when extracting identities from a specific wallet and store in the in memory wallet', () => { + + it('if not identities available should not add anything to wallet', async () => { + const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); + const spyWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.resolves(spyWalletFacade); + spyWalletFacade.getAllIdentityNames.resolves([]); + const identityManagerFactory = new IdentityManagerFactory(); + + const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org4MSP]); + await identityManager._extractIdentitiesFromWallet(org4MSP, spyWalletFacade); + + sinon.assert.notCalled(spyWalletFacade.export); + }); + + it('should export the correct identities information', async () => { + const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); + const stubWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.resolves(stubWalletFacade); + stubWalletFacade.getAllIdentityNames.resolves(['User1']); + const identityManagerFactory = new IdentityManagerFactory(); + + const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org4MSP]); + await identityManager._extractIdentitiesFromWallet(org4MSP, stubWalletFacade); + sinon.assert.called(stubWalletFacade.export); + }); + + it('should add the identities to the memory wallet', async() => { + const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); + const stubWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.resolves(stubWalletFacade); + stubWalletFacade.getAllIdentityNames.resolves(['User1']); + const identityManagerFactory = new IdentityManagerFactory(); + + const testIdentity = { + mspid : 'org4MSP', + certificate : 'cert/path/to/somewhere.pem', + privateKey : 'key/path/to/somewhere.pem', + }; + stubWalletFacade.export.resolves(testIdentity); + + const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org4MSP]); + + const stubAddToWallet = sinon.stub(); + identityManager._addToWallet = stubAddToWallet; + + await identityManager._extractIdentitiesFromWallet(org4MSP, stubWalletFacade); + + sinon.assert.calledWithExactly(stubAddToWallet, testIdentity.mspid, 'User1', testIdentity.certificate, testIdentity.privateKey); + }); + }); }); From eaddb9eddee35bdb5727a75da3d283938324748a Mon Sep 17 00:00:00 2001 From: RosieMurphy0 Date: Tue, 13 Oct 2020 10:58:43 +0100 Subject: [PATCH 2/4] Extract identities from a version specific wallet and store in the in memory wallet Signed-off-by: RosieMurphy0 --- .../identity-management/IdentityManager.js | 8 +++---- .../identity-management/IdentityManager.js | 24 ++++++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/caliper-fabric/lib/identity-management/IdentityManager.js b/packages/caliper-fabric/lib/identity-management/IdentityManager.js index 1d97530bf..83b4fdae0 100644 --- a/packages/caliper-fabric/lib/identity-management/IdentityManager.js +++ b/packages/caliper-fabric/lib/identity-management/IdentityManager.js @@ -186,11 +186,9 @@ class IdentityManager { async _extractIdentitiesFromWallet(mspId, wallet) { const walletFacade = await this.walletFacadeFactory.create(wallet.path); const allIDNames = await walletFacade.getAllIdentityNames(); - for (const identityNames in allIDNames){ - const identity = await walletFacade.export(identityNames); - if (identity){ - await this._addToWallet(identity.mspid, allIDNames[identityNames], identity.certificate, identity.privateKey); - } + for (const identityName in allIDNames){ + const identity = await walletFacade.export(identityName); + await this._addToWallet(identity.mspid, allIDNames[identityName], identity.certificate, identity.privateKey); } } diff --git a/packages/caliper-fabric/test/identity-management/IdentityManager.js b/packages/caliper-fabric/test/identity-management/IdentityManager.js index fbe970566..94af9e1ff 100644 --- a/packages/caliper-fabric/test/identity-management/IdentityManager.js +++ b/packages/caliper-fabric/test/identity-management/IdentityManager.js @@ -234,6 +234,11 @@ describe('An Identity Manager', () => { const stubWalletFacade = sinon.createStubInstance(IWalletFacade); stubWalletFacadeFactory.create.resolves(stubWalletFacade); stubWalletFacade.getAllIdentityNames.resolves(['admin', 'user', '_org2MSP_admin', '_org2MSP_issuer']); + stubWalletFacade.export.resolves({ + mspid : 'mspidstand', + certificate : 'cert/path/to/somewhere.pem', + privateKey : 'key/path/to/somewhere.pem', + }); it('should return the correct aliases for the default organisation', async () => { const identityManagerFactory = new IdentityManagerFactory(); @@ -406,19 +411,19 @@ describe('An Identity Manager', () => { await identityManager.getWallet().should.equal('IamAwallet'); }); - describe('when extracting identities from a specific wallet and store in the in memory wallet', () => { + describe('when extracting identities from a specific wallet', () => { - it('if not identities available should not add anything to wallet', async () => { + it('should not add anything to the wallet when no identities are available', async () => { const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); - const spyWalletFacade = sinon.createStubInstance(IWalletFacade); - stubWalletFacadeFactory.create.resolves(spyWalletFacade); - spyWalletFacade.getAllIdentityNames.resolves([]); + const stubWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.resolves(stubWalletFacade); + stubWalletFacade.getAllIdentityNames.resolves([]); const identityManagerFactory = new IdentityManagerFactory(); const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org4MSP]); - await identityManager._extractIdentitiesFromWallet(org4MSP, spyWalletFacade); + await identityManager.initialize(); - sinon.assert.notCalled(spyWalletFacade.export); + sinon.assert.notCalled(stubWalletFacade.export); }); it('should export the correct identities information', async () => { @@ -429,7 +434,8 @@ describe('An Identity Manager', () => { const identityManagerFactory = new IdentityManagerFactory(); const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org4MSP]); - await identityManager._extractIdentitiesFromWallet(org4MSP, stubWalletFacade); + await identityManager.initialize(); + sinon.assert.called(stubWalletFacade.export); }); @@ -452,7 +458,7 @@ describe('An Identity Manager', () => { const stubAddToWallet = sinon.stub(); identityManager._addToWallet = stubAddToWallet; - await identityManager._extractIdentitiesFromWallet(org4MSP, stubWalletFacade); + await identityManager.initialize(); sinon.assert.calledWithExactly(stubAddToWallet, testIdentity.mspid, 'User1', testIdentity.certificate, testIdentity.privateKey); }); From 3d946c5fa8a4c5583934e0b4c9a923f04d739aa0 Mon Sep 17 00:00:00 2001 From: RosieMurphy0 Date: Thu, 15 Oct 2020 09:48:56 +0100 Subject: [PATCH 3/4] Extract identities from a version specific wallet and store in the in memory wallet Signed-off-by: RosieMurphy0 --- .../test/identity-management/IdentityManager.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/caliper-fabric/test/identity-management/IdentityManager.js b/packages/caliper-fabric/test/identity-management/IdentityManager.js index 94af9e1ff..b2bef73b0 100644 --- a/packages/caliper-fabric/test/identity-management/IdentityManager.js +++ b/packages/caliper-fabric/test/identity-management/IdentityManager.js @@ -232,13 +232,14 @@ describe('An Identity Manager', () => { describe('when getting a list of alias names from an organisation', () => { const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); const stubWalletFacade = sinon.createStubInstance(IWalletFacade); - stubWalletFacadeFactory.create.resolves(stubWalletFacade); + const secondStubWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.onFirstCall().resolves(stubWalletFacade); stubWalletFacade.getAllIdentityNames.resolves(['admin', 'user', '_org2MSP_admin', '_org2MSP_issuer']); - stubWalletFacade.export.resolves({ - mspid : 'mspidstand', - certificate : 'cert/path/to/somewhere.pem', - privateKey : 'key/path/to/somewhere.pem', - }); + + // const stubAllIdentityNames = sinon.stub(); + // secondStubWalletFacade.getAllIdentityNames = stubAllIdentityNames.resolves([]); + secondStubWalletFacade.getAllIdentityNames.resolves([]); + stubWalletFacadeFactory.create.onSecondCall().resolves(secondStubWalletFacade); it('should return the correct aliases for the default organisation', async () => { const identityManagerFactory = new IdentityManagerFactory(); From 28fc3ee8bfed2c45e38f71b7894950fd0c58ca9e Mon Sep 17 00:00:00 2001 From: RosieMurphy0 Date: Thu, 15 Oct 2020 14:31:52 +0100 Subject: [PATCH 4/4] Extracts identities from a wallet and stores in the in memory wallet Signed-off-by: RosieMurphy0 --- .../identity-management/IdentityManager.js | 2 ++ .../ConnectorConfiguration.js | 10 ++++++- .../identity-management/IdentityManager.js | 29 +++++++++++++++---- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/caliper-fabric/lib/identity-management/IdentityManager.js b/packages/caliper-fabric/lib/identity-management/IdentityManager.js index 83b4fdae0..f967ce1f8 100644 --- a/packages/caliper-fabric/lib/identity-management/IdentityManager.js +++ b/packages/caliper-fabric/lib/identity-management/IdentityManager.js @@ -186,6 +186,8 @@ class IdentityManager { async _extractIdentitiesFromWallet(mspId, wallet) { const walletFacade = await this.walletFacadeFactory.create(wallet.path); const allIDNames = await walletFacade.getAllIdentityNames(); + // eslint-disable-next-line no-console + console.log({allIDNames}); for (const identityName in allIDNames){ const identity = await walletFacade.export(identityName); await this._addToWallet(identity.mspid, allIDNames[identityName], identity.certificate, identity.privateKey); diff --git a/packages/caliper-fabric/test/connector-configuration/ConnectorConfiguration.js b/packages/caliper-fabric/test/connector-configuration/ConnectorConfiguration.js index a0b5c7819..18668e681 100644 --- a/packages/caliper-fabric/test/connector-configuration/ConnectorConfiguration.js +++ b/packages/caliper-fabric/test/connector-configuration/ConnectorConfiguration.js @@ -301,9 +301,17 @@ describe('A valid Connector Configuration', () => { const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); const stubWalletFacade = sinon.createStubInstance(IWalletFacade); - stubWalletFacadeFactory.create.resolves(stubWalletFacade); + const secondStubWalletFacade = sinon.createStubInstance(IWalletFacade); + stubWalletFacadeFactory.create.onCall(0).resolves(stubWalletFacade); + stubWalletFacadeFactory.create.onCall(1).resolves(secondStubWalletFacade); + stubWalletFacadeFactory.create.onCall(2).resolves(secondStubWalletFacade); + secondStubWalletFacade.getAllIdentityNames.resolves([]); stubWalletFacade.getAllIdentityNames.resolves(['admin', 'user', '_org2MSP_admin', '_org2MSP_issuer']); + afterEach(() => { + sinon.resetHistory(); + }); + it('should return the correct aliases for the default organisation', async () => { const connectorConfiguration = await new ConnectorConfigurationFactory().create('./test/sample-configs/BasicConfig.yaml', stubWalletFacadeFactory); await connectorConfiguration.getAliasNamesForOrganization('org1MSP').should.eventually.deep.equal(['admin', 'user']); diff --git a/packages/caliper-fabric/test/identity-management/IdentityManager.js b/packages/caliper-fabric/test/identity-management/IdentityManager.js index b2bef73b0..bf2e7e5e8 100644 --- a/packages/caliper-fabric/test/identity-management/IdentityManager.js +++ b/packages/caliper-fabric/test/identity-management/IdentityManager.js @@ -233,13 +233,15 @@ describe('An Identity Manager', () => { const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory); const stubWalletFacade = sinon.createStubInstance(IWalletFacade); const secondStubWalletFacade = sinon.createStubInstance(IWalletFacade); - stubWalletFacadeFactory.create.onFirstCall().resolves(stubWalletFacade); stubWalletFacade.getAllIdentityNames.resolves(['admin', 'user', '_org2MSP_admin', '_org2MSP_issuer']); - - // const stubAllIdentityNames = sinon.stub(); - // secondStubWalletFacade.getAllIdentityNames = stubAllIdentityNames.resolves([]); secondStubWalletFacade.getAllIdentityNames.resolves([]); - stubWalletFacadeFactory.create.onSecondCall().resolves(secondStubWalletFacade); + stubWalletFacadeFactory.create.onCall(0).resolves(stubWalletFacade); + stubWalletFacadeFactory.create.onCall(1).resolves(secondStubWalletFacade); + stubWalletFacadeFactory.create.onCall(2).resolves(secondStubWalletFacade); + + afterEach(() => { + sinon.resetHistory(); + }); it('should return the correct aliases for the default organisation', async () => { const identityManagerFactory = new IdentityManagerFactory(); @@ -268,8 +270,17 @@ describe('An Identity Manager', () => { beforeEach(() => { stubWalletFacade = sinon.createStubInstance(IWalletFacade); - stubWalletFacadeFactory.create.resolves(stubWalletFacade); stubWalletFacade.getAllIdentityNames.resolves(['admin', 'user', '_org2MSP_admin', '_org2MSP_issuer']); + const secondStubWalletFacade = sinon.createStubInstance(IWalletFacade); + secondStubWalletFacade.getAllIdentityNames.resolves([]); + stubWalletFacadeFactory.create.onCall(0).resolves(stubWalletFacade); + stubWalletFacadeFactory.create.onCall(1).resolves(secondStubWalletFacade); + stubWalletFacadeFactory.create.onCall(2).resolves(secondStubWalletFacade); + secondStubWalletFacade.getAllIdentityNames.resolves([]); + }); + + afterEach(() => { + sinon.resetHistory(); }); it('should throw an error if certificates section isn\'t an array', async () => { @@ -432,6 +443,12 @@ describe('An Identity Manager', () => { const stubWalletFacade = sinon.createStubInstance(IWalletFacade); stubWalletFacadeFactory.create.resolves(stubWalletFacade); stubWalletFacade.getAllIdentityNames.resolves(['User1']); + const testIdentity = { + mspid : 'org4MSP', + certificate : 'cert/path/to/somewhere.pem', + privateKey : 'key/path/to/somewhere.pem', + }; + stubWalletFacade.export.resolves(testIdentity); const identityManagerFactory = new IdentityManagerFactory(); const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org4MSP]);