/**
         * From a yaml file
         *
         * @param yamlPolicyFile File location for the chaincode endorsement policy specification.
         * @throws IOException
         * @throws ChaincodeEndorsementPolicyParseException
         */

        public void FromYamlFile(string yamlPolicyFile)
        {
            IDeserializer nl = new DeserializerBuilder().Build();
            Dictionary <object, object> bld = (Dictionary <object, object>)nl.Deserialize(new StreamReader(File.OpenRead(yamlPolicyFile)));
            var mpy = (Dictionary <object, object>)bld.GetOrNull("policy");

            if (null == mpy)
            {
                throw new ChaincodeEndorsementPolicyParseException("The policy file has no policy section");
            }
            Dictionary <object, object> idsp = (Dictionary <object, object>)bld.GetOrNull("identities");

            if (null == idsp)
            {
                throw new ChaincodeEndorsementPolicyParseException("The policy file has no identities section");
            }
            IndexedHashMap <string, MSPPrincipal> identities = ParseIdentities(idsp);
            SignaturePolicy         sp  = ParsePolicy(identities, mpy);
            SignaturePolicyEnvelope env = new SignaturePolicyEnvelope {
                Rule = sp, Version = 0
            };

            env.Identities.AddRange(identities.Values);
            ChaincodeEndorsementPolicyAsBytes = env.ToByteArray();
        }
        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");
        }
예제 #3
0
        private SignaturePolicyEnvelope ParseSignaturePolicyEnvelope(JObject scf)
        {
            JObject signaturePolicyEnvelope = scf["SignaturePolicyEnvelope"] as JObject;

            if (signaturePolicyEnvelope == null)
            {
                throw new ChaincodeCollectionConfigurationException($"Expected SignaturePolicyEnvelope to be Object type but got: {scf.Type.ToString()}");
            }
            JArray ids = signaturePolicyEnvelope["identities"] as JArray;
            Dictionary <string, MSPPrincipal> identities = ParseIdentities(ids);
            SignaturePolicy         sp  = ParsePolicy(identities, signaturePolicyEnvelope["policy"] as JObject);
            SignaturePolicyEnvelope env = new SignaturePolicyEnvelope();

            env.Identities.Add(identities.Values);
            env.Rule = sp;
//        env.Version = signaturePolicyEnvelope["identities"].ToObject<int>();
            return(env);
        }
        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);
        }
        public void TestSDKIntegrationYaml()
        {
            ChaincodeEndorsementPolicy itTestPolicy = new ChaincodeEndorsementPolicy();

            itTestPolicy.FromYamlFile("Fixture/sdkintegration/chaincodeendorsementpolicy.yaml".Locate());
            SignaturePolicyEnvelope sigPolEnv      = SignaturePolicyEnvelope.Parser.ParseFrom(itTestPolicy.ChaincodeEndorsementPolicyAsBytes);
            List <MSPPrincipal>     identitiesList = sigPolEnv.Identities.ToList();

            foreach (MSPPrincipal ident in identitiesList)
            {
                MSPPrincipal mspPrincipal = MSPPrincipal.Parser.ParseFrom(ident.Principal);
                MSPPrincipal.Types.Classification principalClassification = mspPrincipal.PrincipalClassification;
                Assert.AreEqual(principalClassification.ToString(), MSPPrincipal.Types.Classification.Role.ToString());
                MSPRole mspRole = MSPRole.Parser.ParseFrom(ident.Principal);
                string  iden    = mspRole.MspIdentifier;
                Assert.IsTrue("Org1MSP".Equals(iden) || "Org2MSP".Equals(iden));
                Assert.IsTrue(mspRole.Role == MSPRole.Types.MSPRoleType.Admin || mspRole.Role == MSPRole.Types.MSPRoleType.Member);
            }

            SignaturePolicy rule = sigPolEnv.Rule;

            SignaturePolicy.TypeOneofCase typeCase = rule.TypeCase;
            Assert.AreEqual(SignaturePolicy.TypeOneofCase.NOutOf, typeCase);
        }
예제 #6
0
 /// <summary>The default constructor for PolicyValue.</summary>
 /// <remarks>The default constructor for PolicyValue.</remarks>
 public PolicyValue()
 {
     this.policy = SignaturePolicy.IMPLICIT;
 }
예제 #7
0
 /// <summary>The default constructor for PolicyValue.</summary>
 /// <remarks>The default constructor for PolicyValue.</remarks>
 /// <param name="signaturePolicyId"></param>
 public PolicyValue(string signaturePolicyId)
 {
     this.signaturePolicyId = signaturePolicyId;
     this.policy            = SignaturePolicy.EXPLICIT;
 }
예제 #8
0
		/// <summary>The default constructor for PolicyValue.</summary>
		/// <remarks>The default constructor for PolicyValue.</remarks>
		public PolicyValue()
		{
			this.policy = SignaturePolicy.IMPLICIT;
		}
예제 #9
0
		/// <summary>The default constructor for PolicyValue.</summary>
		/// <remarks>The default constructor for PolicyValue.</remarks>
		/// <param name="signaturePolicyId"></param>
		public PolicyValue(string signaturePolicyId)
		{
			this.signaturePolicyId = signaturePolicyId;
			this.policy = SignaturePolicy.EXPLICIT;
		}
예제 #10
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);
        }