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

Set Correct policy while channel update #114

Merged
merged 3 commits into from
Dec 27, 2023

Conversation

nidhi-singh02
Copy link
Contributor

The policy while channel update for the newly added organization is not correct.

Signed-off-by: n0s09by <Nidhi.singh0@walmart.com>
* @return HashMap with role and the configuration policy
*/
@Override
public HashMap<String, Configtx.ConfigPolicy> getDefaultRolePolicy(String orgMSPId) {
Copy link
Member

Choose a reason for hiding this comment

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

[nitpick] What do you think?
Looks like this can be a channel utility function and it can be moved to a different class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion, created a class for channel utility and moved the functions to this class so it can be used anywhere.


return applicationPoliciesMap;
}

private ConfigPolicy setNewOrgPolicy(String newOrgName, String policyTarget) {
Copy link
Member

Choose a reason for hiding this comment

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

[nitpick] setNewOrgPolicy setTypeOnePolicy is not used and can be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, Addressed.

Signed-off-by: n0s09by <Nidhi.singh0@walmart.com>
import org.hyperledger.fabric.protos.common.Policies;

@Slf4j
public class FabricChannelUtil {
Copy link
Contributor

Choose a reason for hiding this comment

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

[nitpick] @UtilityClass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

Comment on lines 104 to 142
case FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_ADMINS:
mspRole =
MspPrincipal.MSPRole.newBuilder()
.setMspIdentifier(orgMSPId)
.setRole(MspPrincipal.MSPRole.MSPRoleType.ADMIN)
.build();
mspPrincipal =
MspPrincipal.MSPPrincipal.newBuilder()
.setPrincipal(mspRole.toByteString())
.setPrincipalClassification(MspPrincipal.MSPPrincipal.Classification.ROLE)
.build();
mspPrincipals.add(mspPrincipal);
break;
case FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_WRITERS:
// any member who is an admin can write
mspRole =
MspPrincipal.MSPRole.newBuilder()
.setMspIdentifier(orgMSPId)
.setRole(MspPrincipal.MSPRole.MSPRoleType.ADMIN)
.build();
mspPrincipal =
MspPrincipal.MSPPrincipal.newBuilder()
.setPrincipal(mspRole.toByteString())
.setPrincipalClassification(MspPrincipal.MSPPrincipal.Classification.ROLE)
.build();
mspPrincipals.add(mspPrincipal);
// any client can also write
mspRole =
MspPrincipal.MSPRole.newBuilder()
.setMspIdentifier(orgMSPId)
.setRole(MspPrincipal.MSPRole.MSPRoleType.CLIENT)
.build();
mspPrincipal =
MspPrincipal.MSPPrincipal.newBuilder()
.setPrincipal(mspRole.toByteString())
.setPrincipalClassification(MspPrincipal.MSPPrincipal.Classification.ROLE)
.build();
mspPrincipals.add(mspPrincipal);
break;
Copy link
Contributor

@nithin-pankaj nithin-pankaj Dec 27, 2023

Choose a reason for hiding this comment

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

There are multiple fragments of duplicate code here. We can simplify this as

switch (policyFor) {
        case FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_ADMINS:
            mspPrincipals.add(createPrincipal(orgMSPId, MspPrincipal.MSPRole.MSPRoleType.ADMIN));
            break;
        case FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_WRITERS:
            mspPrincipals.add(createPrincipal(orgMSPId, MspPrincipal.MSPRole.MSPRoleType.ADMIN));
            mspPrincipals.add(createPrincipal(orgMSPId, MspPrincipal.MSPRole.MSPRoleType.CLIENT));
            break;
            .
            .
            // Other case statements

createPrinciple would look like

private static MspPrincipal.MSPPrincipal createPrincipal(String mspId, MspPrincipal.MSPRole.MSPRoleType roleType) {
    MspPrincipal.MSPRole mspRole = MspPrincipal.MSPRole.newBuilder()
        .setMspIdentifier(mspId)
        .setRole(roleType)
        .build();
    return MspPrincipal.MSPPrincipal.newBuilder()
        .setPrincipal(mspRole.toByteString())
        .setPrincipalClassification(MspPrincipal.MSPPrincipal.Classification.ROLE)
        .build();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion, Incorporated changes.

import org.hyperledger.fabric.protos.common.MspPrincipal;
import org.hyperledger.fabric.protos.common.Policies;

@Slf4j
Copy link
Contributor

@nithin-pankaj nithin-pankaj Dec 27, 2023

Choose a reason for hiding this comment

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

[nitpick] you can safely remove this since nothing is being logged in this class, or can introduce log statements if required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed, removed it.

@@ -44,25 +39,27 @@ public ConfigGroup buildWriteset(ConfigGroup readset, NewOrgParamsDTO organizati
// Get existing organizations in the channel and set with as objects and their
// version to prevent deletion or modification
// Omitting existing groups results in their deletion.
Map<String, ConfigGroup> organizations = new HashMap<>();
Map<String, ConfigGroup> existingOrganizations = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Member

@arsulegai arsulegai left a comment

Choose a reason for hiding this comment

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

Looks good, thank you. Please address open comments.

Signed-off-by: n0s09by <Nidhi.singh0@walmart.com>
@nidhi-singh02
Copy link
Contributor Author

Looks good, thank you. Please address open comments.

Thanks Arun, Open comments has been addressed.

@nidhi-singh02 nidhi-singh02 merged commit a3516ef into hyperledger-labs:main Dec 27, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants