Search in sources :

Example 1 with SignaturePolicy

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();
}
Also used : ChaincodeEndorsementPolicyParseException(org.hyperledger.fabric.sdk.exception.ChaincodeEndorsementPolicyParseException) SignaturePolicy(org.hyperledger.fabric.protos.common.Policies.SignaturePolicy) MSPPrincipal(org.hyperledger.fabric.protos.common.MspPrincipal.MSPPrincipal) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream)

Example 2 with SignaturePolicy

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");
}
Also used : ChaincodeEndorsementPolicyParseException(org.hyperledger.fabric.sdk.exception.ChaincodeEndorsementPolicyParseException) SignaturePolicy(org.hyperledger.fabric.protos.common.Policies.SignaturePolicy) Matcher(java.util.regex.Matcher) List(java.util.List) MSPPrincipal(org.hyperledger.fabric.protos.common.MspPrincipal.MSPPrincipal) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 MSPPrincipal (org.hyperledger.fabric.protos.common.MspPrincipal.MSPPrincipal)2 SignaturePolicy (org.hyperledger.fabric.protos.common.Policies.SignaturePolicy)2 ChaincodeEndorsementPolicyParseException (org.hyperledger.fabric.sdk.exception.ChaincodeEndorsementPolicyParseException)2 FileInputStream (java.io.FileInputStream)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Yaml (org.yaml.snakeyaml.Yaml)1