Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract identities from a version specific wallet and store in the in memory wallet #1055

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you change allIDNames to allIdentityNames as we don't want to use abbreviations

// eslint-disable-next-line no-console
console.log({allIDNames});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you left some debug code in, could you delete the console.log and the comment above it as well.

for (const identityName in allIDNames){
Copy link
Contributor

@davidkel davidkel Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allIDNames to allIdentityNames here too, plus change the in to of which means you will get the individual identityName as it loops round

const identity = await walletFacade.export(identityName);
await this._addToWallet(identity.mspid, allIDNames[identityName], identity.certificate, identity.privateKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the change to of in the for loop, you can replace the allIDNames[identityName] with just identityName

}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,17 @@ describe('A valid Connector Configuration', () => {
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.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']);
Expand Down
104 changes: 95 additions & 9 deletions packages/caliper-fabric/test/identity-management/IdentityManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand All @@ -213,8 +232,16 @@ 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);
stubWalletFacade.getAllIdentityNames.resolves(['admin', 'user', '_org2MSP_admin', '_org2MSP_issuer']);
secondStubWalletFacade.getAllIdentityNames.resolves([]);
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();
Expand Down Expand Up @@ -242,8 +269,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 () => {
Expand Down Expand Up @@ -385,12 +421,62 @@ describe('An Identity Manager', () => {
await identityManager.getWallet().should.equal('IamAwallet');
});

it('should return a wallet facade when requested', async () => {
const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory);
const stubWalletFacade = sinon.createStubInstance(IWalletFacade);
stubWalletFacadeFactory.create.resolves(stubWalletFacade);
const identityManagerFactory = new IdentityManagerFactory();
const identityManager = await identityManagerFactory.create(stubWalletFacadeFactory, [org1MSP, org2MSP]);
await identityManager.getWalletFacade().should.equal(stubWalletFacade);
describe('when extracting identities from a specific wallet', () => {

it('should not add anything to the wallet when no identities are available', async () => {
const stubWalletFacadeFactory = sinon.createStubInstance(IWalletFacadeFactory);
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.initialize();

sinon.assert.notCalled(stubWalletFacade.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 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]);
await identityManager.initialize();

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.initialize();

sinon.assert.calledWithExactly(stubAddToWallet, testIdentity.mspid, 'User1', testIdentity.certificate, testIdentity.privateKey);
});
});
});