private static SignaturePolicy ParsePolicy(IndexedHashMap <string, MSPPrincipal> identities, Dictionary <object, object> mp)
        {
            if (mp == null)
            {
                throw new ChaincodeEndorsementPolicyParseException("No policy section was found in the document.");
            }

            foreach (KeyValuePair <object, object> ks in mp)
            {
                string key = (string)ks.Key;
                object vo  = ks.Value;
                if ("signed-by".Equals(key, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (!(vo is string))
                    {
                        throw new ChaincodeEndorsementPolicyParseException("signed-by expecting a string value");
                    }
                    MSPPrincipal mspPrincipal = identities.GetOrNull((string)vo);
                    if (null == mspPrincipal)
                    {
                        throw new ChaincodeEndorsementPolicyParseException($"No identity found by name {(string) vo} in signed-by.");
                    }
                    return(new SignaturePolicy {
                        SignedBy = identities.Index((string)vo)
                    });
                }

                Match match = noofPattern.Match(key);
                if (match.Success && match.Groups.Count > 0)
                {
                    string matchStingNo = match.Groups[1].Value.Trim();
                    int.TryParse(matchStingNo, out int matchNo);
                    if (!(vo is List <object> voList))
                    {
                        throw new ChaincodeEndorsementPolicyParseException($"{key} expected to have list but found {vo}x.");
                    }
                    if (voList.Count < matchNo)
                    {
                        throw new ChaincodeEndorsementPolicyParseException($"{key} expected to have at least {matchNo} items to match but only found {voList.Count}.");
                    }
                    SignaturePolicy.Types.NOutOf spB = new SignaturePolicy.Types.NOutOf {
                        N = matchNo
                    };
                    foreach (Dictionary <object, object> nlo in voList)
                    {
                        SignaturePolicy sp = ParsePolicy(identities, nlo);
                        spB.Rules.Add(sp);
                    }

                    return(new SignaturePolicy {
                        NOutOf = spB
                    });
                }

                throw new ChaincodeEndorsementPolicyParseException($"Unsupported policy type {key}");
            }

            throw new ChaincodeEndorsementPolicyParseException("No values found for policy");
        }
 /**
  * Creates a policy which requires N out of the slice of policies to evaluate to true
  *
  * @param n
  * @param policies
  * @return
  */
 public static SignaturePolicy NOutOf(int n, List <SignaturePolicy> policies)
 {
     SignaturePolicy.Types.NOutOf no = new SignaturePolicy.Types.NOutOf {
         N = n
     };
     no.Rules.AddRange(policies);
     return(new SignaturePolicy {
         NOutOf = no
     });
 }
        public void TestLoadFromConfigFileYamlBasic()
        {
            ChaincodeCollectionConfiguration config = ChaincodeCollectionConfiguration.FromYamlFile("Fixture/collectionProperties/testCollection.yaml".Locate());

            Assert.IsNotNull(config);
            byte[] configAsBytes = config.GetAsBytes();
            Assert.IsNotNull(configAsBytes);
            Assert.AreEqual(configAsBytes.Length, 137);
            CollectionConfigPackage collectionConfigPackage = CollectionConfigPackage.Parser.ParseFrom(configAsBytes);

            Assert.AreEqual(collectionConfigPackage.Config.Count, 1);
            CollectionConfig colConfig = collectionConfigPackage.Config.FirstOrDefault();

            Assert.IsNotNull(colConfig);
            StaticCollectionConfig staticCollectionConfig = colConfig.StaticCollectionConfig;

            Assert.IsNotNull(staticCollectionConfig);
            Assert.AreEqual(staticCollectionConfig.BlockToLive, (ulong)3);
            Assert.AreEqual(staticCollectionConfig.Name, "rick");
            Assert.AreEqual(staticCollectionConfig.MaximumPeerCount, 9);
            Assert.AreEqual(staticCollectionConfig.RequiredPeerCount, 7);
            CollectionPolicyConfig memberOrgsPolicy = staticCollectionConfig.MemberOrgsPolicy;

            Assert.IsNotNull(memberOrgsPolicy);
            SignaturePolicyEnvelope signaturePolicy = memberOrgsPolicy.SignaturePolicy;

            Assert.IsNotNull(signaturePolicy);
            Assert.AreEqual(signaturePolicy.Version, 0);
            SignaturePolicy rule = signaturePolicy.Rule;

            Assert.IsNotNull(rule);
            Assert.AreEqual(rule.TypeCase, SignaturePolicy.TypeOneofCase.NOutOf);
            SignaturePolicy.Types.NOutOf nOutOf = rule.NOutOf;
            Assert.IsNotNull(nOutOf);
            Assert.AreEqual(2, nOutOf.N);
            List <MSPPrincipal> identitiesList = signaturePolicy.Identities?.ToList();

            Assert.IsNotNull(identitiesList);
            Assert.AreEqual(3, identitiesList.Count);
        }
Пример #4
0
        private SignaturePolicy ParsePolicy(Dictionary <string, MSPPrincipal> identities, JObject policy)
        {
            SignaturePolicy sp = new SignaturePolicy();

            if (policy.Count != 1)
            {
                throw new ChaincodeCollectionConfigurationException($"Expected policy size of 1 but got {policy.Count}");
            }

            JToken jo = policy["signed-by"];

            if (jo != null)
            {
                string vo = jo.ToObject <string>();
                if (!identities.ContainsKey(vo))
                {
                    throw new ChaincodeCollectionConfigurationException($"No identity found by name {vo} in signed-by.");
                }
                sp.SignedBy = identities.Values.ToList().IndexOf(identities[vo]);
            }
            else
            {
                string key   = policy.Properties().Select(p => p.Name).First();
                Match  match = noofPattern.Match(key);
                JArray vo    = policy[key] as JArray;
                if (vo == null)
                {
                    throw new ChaincodeCollectionConfigurationException($"{key} expected to have at least an array of items");
                }
                if (match.Success && match.Groups.Count >= 1)
                {
                    string matchStingNo = match.Groups[1].Value.Trim();
                    int    matchNo      = int.Parse(matchStingNo);

                    if (vo.Count < matchNo)
                    {
                        throw new ChaincodeCollectionConfigurationException($"{key} expected to have at least {matchNo} items to match but only found {vo.Count}.");
                    }

                    SignaturePolicy.Types.NOutOf nsp = new SignaturePolicy.Types.NOutOf();
                    nsp.N = matchNo;
                    for (int i = vo.Count - 1; i >= 0; --i)
                    {
                        JToken jsonValue = vo[i];
                        if (jsonValue.Type != JTokenType.Object)
                        {
                            throw new ChaincodeCollectionConfigurationException($"Expected object type in Nof but got {jsonValue.Type.ToString()}");
                        }

                        SignaturePolicy spp = ParsePolicy(identities, jsonValue as JObject);
                        nsp.Rules.Add(spp);
                    }

                    sp.NOutOf = nsp;
                }
                else
                {
                    throw new ChaincodeCollectionConfigurationException($"Unsupported policy type {key}");
                }
            }

            return(sp);
        }