use of org.hyperledger.fabric.protos.common.Policies.SignaturePolicy in project fabric-sdk-java by hyperledger.
the class ChaincodeEndorsementPolicy method fromYamlFile.
/**
* From a yaml file
*
* @param yamlPolicyFile File location for the chaincode endorsement policy specification.
* @throws IOException
* @throws ChaincodeEndorsementPolicyParseException
*/
public void fromYamlFile(File yamlPolicyFile) throws IOException, ChaincodeEndorsementPolicyParseException {
final Yaml yaml = new Yaml();
final Map<?, ?> load = (Map<?, ?>) yaml.load(new FileInputStream(yamlPolicyFile));
Map<?, ?> mp = (Map<?, ?>) load.get("policy");
if (null == mp) {
throw new ChaincodeEndorsementPolicyParseException("The policy file has no policy section");
}
IndexedHashMap<String, MSPPrincipal> identities = parseIdentities((Map<?, ?>) load.get("identities"));
SignaturePolicy sp = parsePolicy(identities, mp);
policyBytes = Policies.SignaturePolicyEnvelope.newBuilder().setVersion(0).addAllIdentities(identities.values()).setRule(sp).build().toByteArray();
}
use of org.hyperledger.fabric.protos.common.Policies.SignaturePolicy in project fabric-sdk-java by hyperledger.
the class ChaincodeEndorsementPolicy method parsePolicy.
private static SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, Map<?, ?> mp) throws ChaincodeEndorsementPolicyParseException {
if (mp == null) {
throw new ChaincodeEndorsementPolicyParseException("No policy section was found in the document.");
}
if (!(mp instanceof Map)) {
throw new ChaincodeEndorsementPolicyParseException("Policy expected object section was not found in the document.");
}
for (Map.Entry<?, ?> ks : mp.entrySet()) {
Object ko = ks.getKey();
Object vo = ks.getValue();
final String key = (String) ko;
if ("signed-by".equals(key)) {
if (!(vo instanceof String)) {
throw new ChaincodeEndorsementPolicyParseException("signed-by expecting a string value");
}
MSPPrincipal mspPrincipal = identities.get(vo);
if (null == mspPrincipal) {
throw new ChaincodeEndorsementPolicyParseException(format("No identity found by name %s in signed-by.", vo));
}
return SignaturePolicy.newBuilder().setSignedBy(identities.getKeysIndex((String) vo)).build();
} else {
Matcher match = noofPattern.matcher(key);
if (match.matches() && match.groupCount() == 1) {
String matchStingNo = match.group(1).trim();
int matchNo = Integer.parseInt(matchStingNo);
if (!(vo instanceof List)) {
throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have list but found %s.", key, String.valueOf(vo)));
}
@SuppressWarnings("unchecked") final List<Map<?, ?>> voList = (List<Map<?, ?>>) vo;
if (voList.size() < matchNo) {
throw new ChaincodeEndorsementPolicyParseException(format("%s expected to have at least %d items to match but only found %d.", key, matchNo, voList.size()));
}
SignaturePolicy.NOutOf.Builder spBuilder = SignaturePolicy.NOutOf.newBuilder().setN(matchNo);
for (Map<?, ?> nlo : voList) {
SignaturePolicy sp = parsePolicy(identities, nlo);
spBuilder.addRules(sp);
}
return SignaturePolicy.newBuilder().setNOutOf(spBuilder.build()).build();
} else {
throw new ChaincodeEndorsementPolicyParseException(format("Unsupported policy type %s", key));
}
}
}
throw new ChaincodeEndorsementPolicyParseException("No values found for policy");
}
Aggregations