-
Notifications
You must be signed in to change notification settings - Fork 24
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
|
@@ -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( | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, Addressed. |
||
return ConfigPolicy.newBuilder() | ||
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
HashMap<String, Configtx.ConfigPolicy> defaultOrgRolePolicy = new HashMap<>(); | ||
// add Admins, Readers, Writers and Endorsement policies | ||
defaultOrgRolePolicy.put( | ||
|
@@ -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); | ||
|
@@ -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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍