Unable to enroll user in new org added to balance transfer sample

删除回忆录丶 提交于 2019-12-18 02:58:31

问题


I am following Balance transfer from Hyperledger Fabric samples from this link. I have modified it a bit, now I have 3 Orgs with 1 Peer each. All goes fine till I enroll users to Org1 and Org2 but, when I try to enroll a user to my 3rd Org I get following error

Failed to get registered user: xyz with error: Error: fabric-ca request register failed with errors [[{"code":63,"message":"Failed to get Affiliation: sql: no rows in result set"}]]

My Binaries of Hyperledger Fabric are from version 1.1.0 Alfa


回答1:


The issue is that the fabric-ca being used in the sample does not know about the affiliation. By default, fabric-ca only has the following affiliations:

org1.department1 org1.department2 org2.department1

The code which registers and enrolls users in the sample takes the org name and concatenates it with "department1" :

let secret = await caClient.register({
                enrollmentID: username,
                affiliation: userOrg.toLowerCase() + '.department1'
            }, adminUserObj);

So when you pass in Org3, it tries to register the user with the affiliation org3.department1 which does not exist.

Since you are using 1.1.0-alpha, you are in luck as this version of fabric-ca supports has an API for adding new affiliations. The easiest way is to use the fabric-ca-client to do this:

fabric-ca-client affiliation add org3
fabric-ca-client affiliation add org3.department1

You can look at the fabric-ca docs ( http://hyperledger-fabric-ca.readthedocs.io/ ) for more details. You'll need to first enroll the admin user which has the password "adminpw" with the fabric-ca-client and then run the above commands.




回答2:


This may be useless (and very late), but I'd like to add to Gari's answer. You can also add affiliations using the node client SDK's AffiliationService class.
To do this in the balance transfer code, modify helper.js with:

let adminUserObj = await client.setUserContext({
                        username: admins[0].username, 
                        password: admins[0].secret});

let caClient = client.getCertificateAuthority();
let affiliationService = caClient.newAffiliationService();

let registeredAffiliations = await affiliationService.getAll(adminUserObj);
if(!registeredAffiliations.result.affiliations.some(
    x => x.name == userOrg.toLowerCase())){
        let affiliation = 'org3.department1'; 
        await affiliationService.create({
                        name: affiliation, 
                        force: true}, adminUserObj);
}

Note:
The above code only adds the affiliations (which I think is the only thing missing in your case). To successfully enroll a user for Org3, you'll also have specify org3 in artifacts/channel/cryptogen.yaml, artifacts/channel/configtx.yaml, artifacts/docker-compose.yaml, artifacts/network-config.yaml and config.json. And create an org3.yaml file.

I built a chaincode sample app with the balance transfer sample as a base which has 3 orgs. You can take a look at it to see the necessary changes.




回答3:


Please pay attention to this line in helper.js,

let secret = await caClient.register({ enrollmentID: username, affiliation: userOrg.toLowerCase() + '.department1' }, adminUserObj);

Are you sure you are lowercasing for Org3 (in config yaml files) ?




回答4:


I had seen the same problem in fabric 1.3, I have changed the names and configured my own. by removing below statement helped me

just comment the below line in 'helper.js' helped me.

//affiliation: userOrg.toLowerCase() + '.department1'




回答5:


You can add more organizations and there departments by changing the fabric-ca-server-config by going to the fabric-ca docker container.

docker exec -it ca.org1.example.com bash 

Go the root@c3804e2852fd:/etc/hyperledger/fabric-ca-server# directory inside the container

Now edit fabric-ca-server-config.yaml file. Add organizations and their departments as per your choice.

Below i have added one "Manufacturer" organization. You can add Org3 like this.




回答6:


Replace below function in app/helper.js file. Restart node server and try enrolling users on org3.

var getRegisteredUser = async function(username, userOrg, isJson) {
    try {
        var client = await getClientForOrg(userOrg);
        logger.debug('Successfully initialized the credential stores');
        // client can now act as an agent for organization Org1
        // first check to see if the user is already enrolled
        var user = await client.getUserContext(username, true);
        if (user && user.isEnrolled()) {
            logger.info('Successfully loaded member from persistence');
            var response = {
                success: false,
            };
            return response;
        } else {
        // user was not enrolled, so we will need an admin user object to register
            logger.info('User %s was not enrolled, so we will need an admin user object to register',username);
            var admins = hfc.getConfigSetting('admins');
            let adminUserObj = await client.setUserContext({username: admins[0].username, password: admins[0].secret});
            //newly added
            if(adminUserObj.getAffiliation() != userOrg.toLowerCase()){
                logger.info('Admin affiliation not registered. Registering now.');
                adminUserObj.setAffiliation(userOrg.toLowerCase());
                adminUserObj.setRoles(['peer','orderer','client','user']);
                adminUserObj = await client.setUserContext(adminUserObj);
            }
            logger.info('Admin User: %s', adminUserObj);
            let affiliation = userOrg.toLowerCase() + '.department1';
            let caClient = client.getCertificateAuthority();
            //check if org exists
            const affiliationService = caClient.newAffiliationService();
            const registeredAffiliations = await affiliationService.getAll(adminUserObj);
            if(!registeredAffiliations.result.affiliations.some(x => x.name == userOrg.toLowerCase())){
                logger.info('Register the new affiliation: %s ', affiliation);
                await affiliationService.create({name: affiliation, force: true}, adminUserObj);
            }
            let secret = await caClient.register({
                enrollmentID: username,
                affiliation: affiliation
            }, adminUserObj);
            logger.debug('Successfully got the secret for user %s — %s',username);

            user = await client.setUserContext({username:username, password:secret});
            logger.debug('Successfully enrolled username %s and setUserContext on the client object', username);

        }
        if(user && user.isEnrolled) {
            if (isJson && isJson === true) {
                var response = {
                    success: true,
                    secret: user._enrollmentSecret,
                    message: username + ' enrolled Successfully',
                };
                return response;
            }
        } else {
            throw new Error('User was not enrolled ');
        }
    } catch(error) {
            let message = 'Failed to register the username ' + username + ' for organization ' + userOrg + ' with error: ' + error.toString();
            logger.error(message);
            var response = {
            success: false,
            message: message,
        };
        return response;
    }
};


来源:https://stackoverflow.com/questions/48836728/unable-to-enroll-user-in-new-org-added-to-balance-transfer-sample

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!