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"); }
private void AddOrg(MSPPrincipal identity) { try { MSPRole mspRole = MSPRole.Parser.ParseFrom(identity.Principal); orgs[mspRole.MspIdentifier] = mspRole.Role; } catch (InvalidProtocolBufferException e) { logger.Warning("error unmarshaling msp principal"); throw new ArgumentException("error unmarshaling msp principal", e); } }
/** * Creates a {@link SignaturePolicyEnvelope} * requiring 1 signature from any fabric entity, having the passed role, of the specified MSP * * @param mspId * @param role * @return */ public static SignaturePolicyEnvelope SignedByFabricEntity(string mspId, MSPRole.Types.MSPRoleType role) { // specify the principal: it's a member of the msp we just found MSPPrincipal principal = new MSPPrincipal { PrincipalClassification = MSPPrincipal.Types.Classification.Role, Principal = new MSPRole { MspIdentifier = mspId, Role = role }.ToByteString() }; SignaturePolicyEnvelope spe = new SignaturePolicyEnvelope { Version = 0, Rule = NOutOf(1, new List <SignaturePolicy> { SignedBy(0) }) }; spe.Identities.Add(principal); return(spe); }
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); }
private static IndexedHashMap <string, MSPPrincipal> ParseIdentities(Dictionary <object, object> identities) { //Only Role types are excepted at this time. IndexedHashMap <string, MSPPrincipal> ret = new IndexedHashMap <string, MSPPrincipal>(); foreach (KeyValuePair <object, object> kp in identities) { string key = (string)kp.Key; object val = kp.Value; /* * if (!(key instanceof String)) { * throw new ChaincodeEndorsementPolicyParseException(format("In identities key expected String got %s ", key == null ? "null" : key.getClass().getName())); * } */ if (ret.ContainsKey(key)) { throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} is listed more than once "); } if (!(val is Dictionary <object, object> dictval)) { string str = val == null ? "null" : val.GetType().Name; throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} value expected Map got {str}"); } object role = dictval.ContainsKey("role") ? dictval["role"] : null; if (!(role is Dictionary <object, object> roleMap)) { string str = role == null ? "null" : role.GetType().Name; throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} value expected Map for role got {str}"); } object nameObj = roleMap.ContainsKey("name") ? roleMap["name"] : null; if (!(nameObj is string name)) { string str = nameObj == null ? "null" : nameObj.GetType().Name; throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} name expected String in role got {str}"); } name = name.Trim(); object mspId = roleMap.ContainsKey("mspId") ? roleMap["mspId"] : null; if (!(mspId is string)) { string str = mspId == null ? "null" : mspId.GetType().Name; throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} mspId expected String in role got {str}"); } if (string.IsNullOrEmpty((string)mspId)) { throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} mspId must not be null or empty String in role"); } MSPRole.Types.MSPRoleType mspRoleType; switch (name) { case "member": mspRoleType = MSPRole.Types.MSPRoleType.Member; break; case "admin": mspRoleType = MSPRole.Types.MSPRoleType.Admin; break; case "client": mspRoleType = MSPRole.Types.MSPRoleType.Client; break; case "peer": mspRoleType = MSPRole.Types.MSPRoleType.Peer; break; default: throw new ChaincodeEndorsementPolicyParseException($"In identities with key {key} name expected member, admin, client, or peer in role got {name}"); } MSPRole mspRole = new MSPRole { MspIdentifier = (string)mspId, Role = mspRoleType }; MSPPrincipal principal = new MSPPrincipal { Principal = mspRole.ToByteString(), PrincipalClassification = MSPPrincipal.Types.Classification.Role }; ret.Add(key, principal); } if (ret.Count == 0) { throw new ChaincodeEndorsementPolicyParseException("No identities were found in the policy specification"); } return(ret); }
private Dictionary <string, MSPPrincipal> ParseIdentities(JArray identities) { Dictionary <string, MSPPrincipal> ret = new Dictionary <string, MSPPrincipal>(); foreach (JToken jsonValue in identities) { if (jsonValue.Type != JTokenType.Object) { throw new ChaincodeCollectionConfigurationException($"Expected in identities user to be Object type but got: {jsonValue.Type.ToString()}"); } JObject user = jsonValue as JObject; if (user?.Count != 1) { throw new ChaincodeCollectionConfigurationException("Only expected on property for user entry in identities."); } string key = user.Properties().Select(p => p.Name).First(); JToken vv = user[key]; if (vv.Type != JTokenType.Object) { throw new ChaincodeCollectionConfigurationException($"Expected in identities role to be Object type but got: {vv.Type.ToString()}"); } JObject role = vv as JObject; if (role == null) { throw new ChaincodeCollectionConfigurationException($"Expected a valid role"); } JObject roleObj = role["role"] as JObject; if (roleObj == null) { throw new ChaincodeCollectionConfigurationException($"Expected a valid role"); } string roleName = roleObj["name"].Value <string>(); string mspId = roleObj["mspId"].Value <string>(); MSPRole.Types.MSPRoleType mspRoleType; switch (roleName.ToLowerInvariant()) { case "member": mspRoleType = MSPRole.Types.MSPRoleType.Member; break; case "admin": mspRoleType = MSPRole.Types.MSPRoleType.Admin; break; case "client": mspRoleType = MSPRole.Types.MSPRoleType.Client; break; case "peer": mspRoleType = MSPRole.Types.MSPRoleType.Peer; break; default: throw new ChaincodeCollectionConfigurationException($"In identities with key {key} name expected member, admin, client, or peer in role got {roleName}"); } MSPRole mspRole = new MSPRole(); mspRole.Role = mspRoleType; mspRole.MspIdentifier = mspId; MSPPrincipal principal = new MSPPrincipal(); principal.PrincipalClassification = MSPPrincipal.Types.Classification.Role; principal.Principal = mspRole.ToByteString(); ret.Add(key, principal); } return(ret); }