Skip to content

Commit

Permalink
Set Correct policy while channel update
Browse files Browse the repository at this point in the history
Signed-off-by: n0s09by <Nidhi.singh0@walmart.com>
  • Loading branch information
nidhi-singh02 committed Dec 26, 2023
1 parent 10533b6 commit 3bb082c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
10 changes: 10 additions & 0 deletions src/main/java/hlf/java/rest/client/service/ChannelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import hlf.java.rest.client.model.ChannelOperationRequest;
import hlf.java.rest.client.model.ClientResponseModel;
import java.util.HashMap;
import java.util.Set;
import org.hyperledger.fabric.protos.common.Configtx;

public interface ChannelService {

Expand Down Expand Up @@ -31,4 +33,12 @@ public interface ChannelService {
* @return
*/
Set<String> getChannelMembersMSPID(String channelName);

/**
* get default configuration policy for org MSP that maps the roles.
*
* @param orgMSPId Org MSP ID
* @return HashMap with role and the configuration policy
*/
HashMap<String, Configtx.ConfigPolicy> getDefaultRolePolicy(String orgMSPId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hlf.java.rest.client.model.AnchorPeerDTO;
import hlf.java.rest.client.model.NewOrgParamsDTO;
import hlf.java.rest.client.service.AddOrgToChannelWriteSetBuilder;
import hlf.java.rest.client.service.ChannelService;
import hlf.java.rest.client.util.FabricClientConstants;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -28,12 +29,14 @@
import org.hyperledger.fabric.protos.msp.MspConfigPackage.MSPConfig;
import org.hyperledger.fabric.protos.peer.Configuration.AnchorPeer;
import org.hyperledger.fabric.protos.peer.Configuration.AnchorPeers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AddOrgToChannelWriteSetBuilderImpl implements AddOrgToChannelWriteSetBuilder {

private NewOrgParamsDTO organizationDetails;
@Autowired private ChannelService channelService;
private static final int DEFAULT_VERSION = 0;

@Override
Expand All @@ -44,25 +47,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<>();
ConfigGroup applicationConfigGroup =
readset.getGroupsOrThrow(FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION);
applicationConfigGroup
.getGroupsMap()
.forEach(
(k, v) ->
organizations.put(
k, setEmptyGroup(retrieveGroupVersionFromReadset(applicationConfigGroup, k))));
existingOrganizations.put(
k,
setEmptyGroup(retrieveMSPGroupVersionFromReadset(applicationConfigGroup, k))));
// The "Application" group
ConfigGroup applicationGroup =
ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(setApplicationPolicies(readset))
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId))
.putAllGroups(organizations)
// putAllGroups excludes new organization
.putAllGroups(existingOrganizations)
// Application group version
.setVersion(
retrieveGroupVersionFromReadset(
retrieveMSPGroupVersionFromReadset(
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
+ 1) // will
// be
Expand All @@ -84,17 +89,17 @@ public ConfigGroup buildWriteset(ConfigGroup readset, NewOrgParamsDTO organizati
.build();
}

private long retrieveGroupVersionFromReadset(ConfigGroup readset, String groupName)
private long retrieveMSPGroupVersionFromReadset(ConfigGroup readset, String mspId)
throws ServiceException {
long versionLong = DEFAULT_VERSION;
try {
ConfigGroup group = readset.getGroupsOrThrow(groupName);
ConfigGroup group = readset.getGroupsOrThrow(mspId);
versionLong = group.getVersion();
} catch (IllegalArgumentException e) {
throw new ServiceException(
ErrorCode.NOT_FOUND,
"WriteBuilder version iteration error: ConfigGroup with name - \""
+ groupName
+ mspId
+ "\" - not found in Readset",
e);
}
Expand Down Expand Up @@ -150,8 +155,9 @@ private Map<String, ConfigPolicy> setApplicationPolicies(ConfigGroup readset) {
.setModPolicy("")
.setVersion(map.get(FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_WRITERS))
.build();

Map<String, ConfigPolicy> applicationPoliciesMap = new HashMap<>();
// add Admins, Readers, Writers, Endorsement and LifeCycle Endorsement policies at the channel
// level
applicationPoliciesMap.put(
FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_ADMINS, adminPolicy);
applicationPoliciesMap.put(
Expand All @@ -177,34 +183,16 @@ private ConfigGroup setNewOrgGroup(String newOrgMspId) {

return ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(setNewOrgPolicies(newOrgMspId))
.putAllPolicies(channelService.getDefaultRolePolicy(newOrgMspId))
.putAllValues(valueMap)
.setVersion(0)
.setVersion(0) // First time update, hence version is 0
.build();
}

private ConfigGroup setEmptyGroup(long version) {
return ConfigGroup.newBuilder().setModPolicy("").setVersion(version).build();
}

private Map<String, ConfigPolicy> setNewOrgPolicies(String newOrgName) {
Map<String, ConfigPolicy> applicationPoliciesMap = new HashMap<>();
applicationPoliciesMap.put(
FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_ADMINS,
setNewOrgPolicy(newOrgName, FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_ADMINS));
applicationPoliciesMap.put(
FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_ENDORSEMENT,
setNewOrgPolicy(newOrgName, FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_ENDORSEMENT));
applicationPoliciesMap.put(
FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_READERS,
setNewOrgPolicy(newOrgName, FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_READERS));
applicationPoliciesMap.put(
FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_WRITERS,
setNewOrgPolicy(newOrgName, FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_WRITERS));

return applicationPoliciesMap;
}

private ConfigPolicy setNewOrgPolicy(String newOrgName, String policyTarget) {
return ConfigPolicy.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,16 @@ private Configtx.ConfigGroup getMSPConfigGroup(hlf.java.rest.client.model.Peer p
.build();
}

// The method returns a default policy for each organization
// that maps the roles. The policy type is signature. Roles
// are identified by their signatures, as those signatures
// represent the certificate.
private HashMap<String, Configtx.ConfigPolicy> getDefaultRolePolicy(String orgMSPId) {
/**
* get default configuration policy for organization that maps the roles. The policy type is
* signature. Roles are identified by their signatures, as those signatures represent the
* certificate.
*
* @param orgMSPId Org MSP ID
* @return HashMap with role and the configuration policy
*/
@Override
public HashMap<String, Configtx.ConfigPolicy> getDefaultRolePolicy(String orgMSPId) {
HashMap<String, Configtx.ConfigPolicy> defaultOrgRolePolicy = new HashMap<>();
// add Admins, Readers, Writers and Endorsement policies
defaultOrgRolePolicy.put(
Expand Down Expand Up @@ -488,8 +493,13 @@ private List<MspPrincipal.MSPPrincipal> getRolesFor(String policyFor, String org
return mspPrincipals;
}

// The method returns a ConfigPolicy of type signature for the
// passed organization's MSP ID.
/**
* returns a ConfigPolicy of type signature for the passed organization's MSP ID
*
* @param policyFor Policy for which role
* @param orgMSPId new org MSP ID
* @return configuration policy
*/
private Configtx.ConfigPolicy getDefaultRoleConfigPolicyForMSP(
String policyFor, String orgMSPId) {
List<MspPrincipal.MSPPrincipal> mspPrincipals = getRolesFor(policyFor, orgMSPId);
Expand Down Expand Up @@ -653,8 +663,7 @@ private Policies.Policy getImplicitMetaPolicy(String subPolicyName, int rule) {
* @param modPolicy
* @return
*/
private Configtx.ConfigPolicy getConfigPolicy(
String subPolicyName, int rule, String modPolicy) {
private Configtx.ConfigPolicy getConfigPolicy(String subPolicyName, int rule, String modPolicy) {
return Configtx.ConfigPolicy.newBuilder()
.setPolicy(getImplicitMetaPolicy(subPolicyName, rule))
.setModPolicy(modPolicy)
Expand Down

0 comments on commit 3bb082c

Please sign in to comment.