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

Channel configuration MSP information refactoring #115

Merged
merged 2 commits into from
Dec 28, 2023
Merged
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 @@ -8,6 +8,7 @@
import hlf.java.rest.client.model.ChannelOperationRequest;
import hlf.java.rest.client.model.ChannelOperationType;
import hlf.java.rest.client.model.ClientResponseModel;
import hlf.java.rest.client.model.MSPDTO;
import hlf.java.rest.client.service.ChannelService;
import hlf.java.rest.client.service.HFClientWrapper;
import hlf.java.rest.client.util.FabricChannelUtil;
Expand Down Expand Up @@ -121,7 +122,7 @@ public ClientResponseModel joinChannel(ChannelOperationRequest channelOperationR
throw new ChannelOperationException(validationResult);
}

Channel channel = null;
Channel channel;
try {
String channelName = channelOperationRequest.getChannelName();

Expand Down Expand Up @@ -243,37 +244,31 @@ private Common.Envelope getChannelCreationTransaction(
/**
* config update for channel creation: empty readset with msp listed writeset with default setting
*
* @param channelOperationRequest
* @param channelOperationRequest incoming request from the caller of the API
* @return config update
*/
private Configtx.ConfigUpdate newConfigUpdate(ChannelOperationRequest channelOperationRequest) {
Map<String, MSPDTO> mspMap = new HashMap<>();
channelOperationRequest.getPeers().forEach(p -> mspMap.putIfAbsent(p.getMspid(), p.getMspDTO()));
return Configtx.ConfigUpdate.newBuilder()
.setChannelId(channelOperationRequest.getChannelName())
.setReadSet(
newChannelGroup(
channelOperationRequest.getConsortiumName(),
channelOperationRequest.getPeers(),
false))
.setWriteSet(
newChannelGroup(
channelOperationRequest.getConsortiumName(),
channelOperationRequest.getPeers(),
true))
.setReadSet(newChannelGroup(channelOperationRequest.getConsortiumName(), mspMap, false))
.setWriteSet(newChannelGroup(channelOperationRequest.getConsortiumName(), mspMap, true))
.build();
}

/**
* generate default read/write set
*
* @param consortiumName
* @param peers list of peers to be added to channel
* @param consortiumName consortium where the channel is created
* @param mspMap MSPID to MSPDTO mapping for all the to-be added organizations
* @param isWriteSet true if it's for a writeset, false if it's for a readset
* @return
* @return new channel group information with the constructed payload for either read/write set
*/
private Configtx.ConfigGroup newChannelGroup(
String consortiumName, List<hlf.java.rest.client.model.Peer> peers, boolean isWriteSet) {
String consortiumName, Map<String, MSPDTO> mspMap, boolean isWriteSet) {
Configtx.ConfigGroup.Builder channelGroupBuilder = Configtx.ConfigGroup.newBuilder();
channelGroupBuilder.putGroups(GROUP_TAG_APPLICATION, newApplicationGroup(peers, isWriteSet));
channelGroupBuilder.putGroups(GROUP_TAG_APPLICATION, newApplicationGroup(mspMap, isWriteSet));
if (isWriteSet) {
channelGroupBuilder.putValues(VALUE_TAG_CONSORTIUM, getConsortium(consortiumName));
} else {
Expand All @@ -289,32 +284,32 @@ private Configtx.ConfigGroup newChannelGroup(
/**
* generate the application group
*
* @param peers list of peers to be added to the channel
* @param mspMap MSPID to MSPDTO mapping of all the organizations to be added
* @param isWriteSet true if it's for a writeset, false if it's for a readset
* @return application config group
*/
private Configtx.ConfigGroup newApplicationGroup(
List<hlf.java.rest.client.model.Peer> peers, boolean isWriteSet) {
private Configtx.ConfigGroup newApplicationGroup(Map<String, MSPDTO> mspMap, boolean isWriteSet) {
Configtx.ConfigGroup.Builder appGroupBuilder = Configtx.ConfigGroup.newBuilder();
appGroupBuilder.setModPolicy(DEFAULT_MOD_POLICY).setVersion(EMPTY_VERSION);
if (isWriteSet) {
addDefaultImplicitMetaPolicy(appGroupBuilder);
appGroupBuilder.setVersion(INIT_VERSION);
appGroupBuilder.putValues(VALUE_TAG_CAPABILITIES, getCapabilities(FABRIC_2_0));
}
for (hlf.java.rest.client.model.Peer peer : peers) {
if (peer.getMspDTO() != null) {
appGroupBuilder.putGroups(peer.getMspid(), getMSPConfigGroup(peer));
for (Map.Entry<String, MSPDTO> entry : mspMap.entrySet()) {
if (entry.getValue() != null) {
appGroupBuilder.putGroups(
entry.getKey(), getMSPConfigGroup(entry.getKey(), entry.getValue()));
} else {
appGroupBuilder.putGroups(peer.getMspid(), emptyMSPConfigGroup());
appGroupBuilder.putGroups(entry.getKey(), emptyMSPConfigGroup());
}
}
return appGroupBuilder.build();
}

/**
* @param capabilities capabilities need to be added to config
* @return
* @return channel capabilities
*/
private Configtx.ConfigValue getCapabilities(String... capabilities) {
Configtx.ConfigValue.Builder valueBuilder = Configtx.ConfigValue.newBuilder();
Expand Down Expand Up @@ -347,12 +342,13 @@ private Configtx.ConfigGroup emptyMSPConfigGroup() {
/**
* generate msp config group based on the mspdto passed
*
* @param peer
* @return
* @param mspId organization's MSP ID
* @param msp organization's MSP information
* @return policies set as config group for that particular MSP
*/
private Configtx.ConfigGroup getMSPConfigGroup(hlf.java.rest.client.model.Peer peer) {
private Configtx.ConfigGroup getMSPConfigGroup(String mspId, MSPDTO msp) {
Map<String, Configtx.ConfigValue> valueMap = new HashMap<>();
valueMap.put(FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_MSP, getOrgMspValue(peer));
valueMap.put(FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_MSP, getOrgMspValue(mspId, msp));

// Organization's role policy defines what role can perform what operation
// For example, there are typically four roles policies defined
Expand All @@ -367,43 +363,42 @@ private Configtx.ConfigGroup getMSPConfigGroup(hlf.java.rest.client.model.Peer p
.putAllGroups(new HashMap<>())
.setModPolicy(EMPTY_MOD_POLICY)
.putAllPolicies(
FabricChannelUtil.getDefaultRolePolicy(peer.getMspid())) // Organization's role policies
FabricChannelUtil.getDefaultRolePolicy(mspId)) // Organization's role policies
.putAllValues(valueMap)
.build();
}

private Configtx.ConfigValue getOrgMspValue(hlf.java.rest.client.model.Peer peer) {
private Configtx.ConfigValue getOrgMspValue(String mspId, MSPDTO msp) {
return Configtx.ConfigValue.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.setValue(getMspConfig(peer).toByteString())
.setValue(getMspConfig(mspId, msp).toByteString())
.build();
}

private MspConfigPackage.MSPConfig getMspConfig(hlf.java.rest.client.model.Peer peer) {
private MspConfigPackage.MSPConfig getMspConfig(String mspId, MSPDTO msp) {
return MspConfigPackage.MSPConfig.newBuilder()
.setType(0)
.setConfig(getFabricMSPConfig(peer).toByteString())
.setConfig(getFabricMSPConfig(mspId, msp).toByteString())
.build();
}

private MspConfigPackage.FabricMSPConfig getFabricMSPConfig(
hlf.java.rest.client.model.Peer peer) {
private MspConfigPackage.FabricMSPConfig getFabricMSPConfig(String mspId, MSPDTO msp) {

List<ByteString> rootCertCollection = new ArrayList<>();
List<ByteString> tlsRootCertCollection = new ArrayList<>();
byte[] adminCert = null;
byte[] clientCert = null;
byte[] peerCert = null;
byte[] adminCert;
byte[] clientCert;
byte[] peerCert;

for (String rootCerts : peer.getMspDTO().getRootCerts()) {
for (String rootCerts : msp.getRootCerts()) {
rootCertCollection.add(ByteString.copyFrom(rootCerts.getBytes()));
}
for (String tlsRootCerts : peer.getMspDTO().getTlsRootCerts()) {
for (String tlsRootCerts : msp.getTlsRootCerts()) {
tlsRootCertCollection.add(ByteString.copyFrom(tlsRootCerts.getBytes()));
}
adminCert = peer.getMspDTO().getAdminOUCert().getBytes();
clientCert = peer.getMspDTO().getClientOUCert().getBytes();
peerCert = peer.getMspDTO().getPeerOUCert().getBytes();
adminCert = msp.getAdminOUCert().getBytes();
clientCert = msp.getClientOUCert().getBytes();
peerCert = msp.getPeerOUCert().getBytes();

return MspConfigPackage.FabricMSPConfig.newBuilder()
.setCryptoConfig(
Expand Down Expand Up @@ -431,7 +426,7 @@ private MspConfigPackage.FabricMSPConfig getFabricMSPConfig(
FabricClientConstants.CHANNEL_CONFIG_ORGANIZATIONAL_UNIT_ID_PEER)
.setCertificate(ByteString.copyFrom(peerCert)))
.setEnable(true))
.setName(peer.getMspid())
.setName(mspId)
.addAllRootCerts(rootCertCollection)
.addAllTlsRootCerts(tlsRootCertCollection)
.build();
Expand All @@ -440,12 +435,12 @@ private MspConfigPackage.FabricMSPConfig getFabricMSPConfig(
/**
* get consortium config
*
* @param consortiumName
* @param consortiumName name of the consortium where channel is created
* @return consortium config
*/
private Configtx.ConfigValue getConsortium(String consortiumName) {
return Configtx.ConfigValue.newBuilder()
.setVersion(0)
.setVersion(EMPTY_VERSION)
.setValue(
Configuration.Consortium.newBuilder().setName(consortiumName).build().toByteString())
.build();
Expand All @@ -459,30 +454,25 @@ private Configtx.ConfigValue getConsortium(String consortiumName) {
private void addDefaultImplicitMetaPolicy(Configtx.ConfigGroup.Builder builder) {
builder.putPolicies(
"Admins",
getConfigPolicy(
"Admins", Policies.ImplicitMetaPolicy.Rule.MAJORITY_VALUE, DEFAULT_MOD_POLICY));
getDefaultConfigPolicy("Admins", Policies.ImplicitMetaPolicy.Rule.MAJORITY_VALUE));
builder.putPolicies(
"Writers",
getConfigPolicy("Writers", Policies.ImplicitMetaPolicy.Rule.ANY_VALUE, DEFAULT_MOD_POLICY));
"Writers", getDefaultConfigPolicy("Writers", Policies.ImplicitMetaPolicy.Rule.ANY_VALUE));
builder.putPolicies(
"Readers",
getConfigPolicy("Readers", Policies.ImplicitMetaPolicy.Rule.ANY_VALUE, DEFAULT_MOD_POLICY));
"Readers", getDefaultConfigPolicy("Readers", Policies.ImplicitMetaPolicy.Rule.ANY_VALUE));
builder.putPolicies(
"Endorsement",
getConfigPolicy(
"Endorsement", Policies.ImplicitMetaPolicy.Rule.MAJORITY_VALUE, DEFAULT_MOD_POLICY));
getDefaultConfigPolicy("Endorsement", Policies.ImplicitMetaPolicy.Rule.MAJORITY_VALUE));
builder.putPolicies(
"LifecycleEndorsement",
getConfigPolicy(
"Endorsement", Policies.ImplicitMetaPolicy.Rule.MAJORITY_VALUE, DEFAULT_MOD_POLICY));
getDefaultConfigPolicy("Endorsement", Policies.ImplicitMetaPolicy.Rule.MAJORITY_VALUE));
}

/**
* get implicit meta policy
*
* @param subPolicyName
* @param rule
* @return
* @param subPolicyName what is this policy for? is it an admin, writer, reader
* @param rule whether majority or any signature
* @return configuration policy
*/
private Policies.Policy getImplicitMetaPolicy(String subPolicyName, int rule) {
Policies.ImplicitMetaPolicy metaPolicy =
Expand All @@ -497,23 +487,22 @@ private Policies.Policy getImplicitMetaPolicy(String subPolicyName, int rule) {
}

/**
* @param subPolicyName
* @param rule
* @param modPolicy
* @return
* @param subPolicyName what is this policy for? is it an admin, writer, reader
* @param rule whether majority or any signature
* @return configuration policy
*/
private Configtx.ConfigPolicy getConfigPolicy(String subPolicyName, int rule, String modPolicy) {
private Configtx.ConfigPolicy getDefaultConfigPolicy(String subPolicyName, int rule) {
return Configtx.ConfigPolicy.newBuilder()
.setPolicy(getImplicitMetaPolicy(subPolicyName, rule))
.setModPolicy(modPolicy)
.setModPolicy(DEFAULT_MOD_POLICY)
.build();
}

/**
* validate the request
*
* @param channelOperationRequest
* @param channelOperationType
* @param channelOperationRequest input parameters from the API
* @param channelOperationType whether interested in creation/joining etc.
* @return status code 0 if it's valid, otherwise return 400
*/
private ErrorCode validateRequest(
Expand Down
Loading