private static IEnumerable <RuleDescriptor> ParseRules(CxRestContext ctx, CancellationToken token, JToken rulePayload) { using (var reader = new JTokenReader(rulePayload)) { LinkedList <RuleDescriptor> rules = new LinkedList <RuleDescriptor>(); while (JsonUtils.MoveToNextProperty(reader, "ruleId")) { RuleDescriptor rule = new RuleDescriptor() { RuleId = Convert.ToInt32(((JProperty)reader.CurrentToken).Value) }; if (!JsonUtils.MoveToNextProperty(reader, "name")) { continue; } rule.Name = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "description")) { continue; } rule.Description = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "scanType")) { continue; } rule.ScanProduct = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "ruleType")) { continue; } rule.RuleType = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "createdOn")) { continue; } rule.CreatedOn = JsonUtils.UtcEpochTimeToDateTime (Convert.ToInt64(((JProperty)reader.CurrentToken).Value) / 1000); rules.AddLast(rule); } return(rules); } }
private static PolicyCollection ParsePolicies(CxRestContext ctx, CancellationToken token, JToken policyPayload) { PolicyCollection result = new PolicyCollection(); using (JTokenReader reader = new JTokenReader(policyPayload)) while (JsonUtils.MoveToNextProperty(reader, "id")) { PolicyDescriptor policy = new PolicyDescriptor() { PolicyId = Convert.ToInt32(((JProperty)reader.CurrentToken).Value) }; if (!JsonUtils.MoveToNextProperty(reader, "name")) { continue; } policy.Name = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "description")) { continue; } policy.Description = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "isActive")) { continue; } policy.isActive = Convert.ToBoolean(((JProperty)reader.CurrentToken).Value); if (!JsonUtils.MoveToNextProperty(reader, "createdOn")) { continue; } policy.CreatedOn = JsonUtils.UtcEpochTimeToDateTime (Convert.ToInt64(((JProperty)reader.CurrentToken).Value) / 1000); var rules = CxMnoPolicyRules.GetRulesForPolicy(ctx, token, policy.PolicyId); policy.AddRule(rules); result.AddPolicy(policy); } return(result); }
private static ViolatedPolicyCollection ParseViolatedRules (PolicyCollection policies, int projectId, JToken token) { ViolatedPolicyCollection violatedRules = new ViolatedPolicyCollection(); using (JTokenReader reader = new JTokenReader(token)) { while (reader.Read() && reader.CurrentToken.Type != JTokenType.Array) { ; } if (reader.CurrentToken == null || reader.CurrentToken.Type != JTokenType.Array) { return(violatedRules); } JArray policyViolations = (JArray)reader.CurrentToken; for (int y = 0; y < policyViolations.Count; y++) { if (!JsonUtils.MoveToNextProperty(reader, "policyId")) { continue; } int currentPolicyId = Convert.ToInt32(((JProperty)reader.CurrentToken).Value); if (!JsonUtils.MoveToNextProperty(reader, "violations")) { continue; } JArray ruleViolations = (JArray)((JProperty)reader.CurrentToken).Value; for (int x = 0; x < ruleViolations.Count; x++) { if (!JsonUtils.MoveToNextProperty(reader, "ruleId")) { break; } var ruleId = Convert.ToInt32(((JProperty)reader.CurrentToken).Value); var rule = policies.GetPolicyByRuleId(ruleId).Rules[ruleId]; ViolatedRuleDescriptor curRule = new ViolatedRuleDescriptor(rule) { ProjectId = projectId, PolicyId = currentPolicyId }; if (!JsonUtils.MoveToNextProperty(reader, "firstDetectionDateByArm")) { break; } curRule.FirstDetectionDate = JsonUtils.UtcEpochTimeToDateTime (Convert.ToInt64(((JProperty)reader.CurrentToken).Value) / 1000); if (!JsonUtils.MoveToNextProperty(reader, "scanId")) { break; } curRule.ScanId = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "name")) { break; } curRule.ViolationName = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "severity")) { break; } curRule.ViolationSeverity = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "date")) { break; } if (((JProperty)reader.CurrentToken).Value.Type != JTokenType.Null) { curRule.ViolationOccured = JsonUtils.UtcEpochTimeToDateTime (Convert.ToInt64(((JProperty)reader.CurrentToken).Value)); } if (!JsonUtils.MoveToNextProperty(reader, "riskScore")) { break; } if (((JProperty)reader.CurrentToken).Value.Type != JTokenType.Null) { curRule.ViolationRiskScore = Convert.ToDouble (((JProperty)reader.CurrentToken).Value.ToString()); } if (!JsonUtils.MoveToNextProperty(reader, "status")) { break; } curRule.ViolationStatus = ((JProperty)reader.CurrentToken).Value.ToString(); if (!JsonUtils.MoveToNextProperty(reader, "state")) { break; } curRule.ViolationState = ((JProperty)reader.CurrentToken).Value.ToString(); violatedRules.AddViolatedRule(curRule); } } } return(violatedRules); }