public static GenStatus GetReportGenerationStatus(CxRestContext ctx,
                                                          CancellationToken token, String reportId)
        {
            try
            {
                using (var client = ctx.Json.CreateSastClient())
                {
                    using (var scanReportStatus = client.GetAsync(
                               CxRestContext.MakeUrl(ctx.Url,
                                                     String.Format(URL_SUFFIX, reportId)), token).Result)
                    {
                        if (!scanReportStatus.IsSuccessStatusCode)
                        {
                            return(GenStatus.None);
                        }

                        using (var sr = new StreamReader
                                            (scanReportStatus.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ReadStatus(jt));
                            }
                    }
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
        public static ScanSummary GetReport(CxRestContext ctx, CancellationToken token,
                                            String scanId)
        {
            try
            {
                String url = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <String, String>()
                {
                    { "scanId", Convert.ToString(scanId) }
                });


                using (var client = ctx.Json.CreateSastClient())
                    using (var scanSummary = client.GetAsync(url, token).Result)
                    {
                        if (!scanSummary.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(scanSummary.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (scanSummary.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ParseScanSummary(jt));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #3
0
        public static IEnumerable <RuleDescriptor> GetRulesForPolicy(CxRestContext ctx,
                                                                     CancellationToken token, int policyId)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var rulePayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                   String.Format(URL_SUFFIX, policyId)), token).Result)
                    {
                        if (!rulePayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ($"Unable to retrieve rules for policy {policyId}.");
                        }

                        using (var sr = new StreamReader
                                            (rulePayload.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ParseRules(ctx, token, jt));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #4
0
        public static PolicyCollection GetAllPolicies(CxRestContext ctx,
                                                      CancellationToken token)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var policyPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                     POLICY_LIST_URL_SUFFIX), token).Result)
                    {
                        if (!policyPayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ("Unable to retrieve policies.");
                        }

                        JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                                       (policyPayload.Content.ReadAsStreamAsync().Result)));

                        return(ParsePolicies(ctx, token, jt));
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #5
0
        public static IEnumerable <int> GetPolicyIdsForProject(CxRestContext ctx,
                                                               CancellationToken token, int projectId)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var policyPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                     String.Format(PROJECT_POLICY_URL_SUFFIX, projectId)), token).Result)
                    {
                        if (!policyPayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ($"Unable to retrieve policies for project {projectId}.");
                        }

                        JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                                       (policyPayload.Content.ReadAsStreamAsync().Result)));

                        LinkedList <int> policyIds = new LinkedList <int>();

                        using (JTokenReader reader = new JTokenReader(jt))
                            while (JsonUtils.MoveToNextProperty(reader, "id"))
                            {
                                policyIds.AddLast(Convert.ToInt32(((JProperty)reader.CurrentToken).Value));
                            }

                        return(policyIds);
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
        public static ViolatedPolicyCollection GetViolations(CxRestContext ctx,
                                                             CancellationToken token, int projectId, PolicyCollection policies)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var violationsPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                         String.Format(URL_SUFFIX, projectId)), token).Result)
                    {
                        if (!violationsPayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ($"Unable to retrieve rule violations for project {projectId}.");
                        }

                        using (var sr = new StreamReader
                                            (violationsPayload.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ParseViolatedRules(policies, projectId, jt));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #7
0
        public static IEnumerable <Project> GetProjects(CxRestContext ctx, CancellationToken token)
        {
            try
            {
                using (var client = ctx.Json.CreateSastClient())
                    using (var projects = client.GetAsync(
                               CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX), token).Result)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return(null);
                        }

                        if (!projects.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(projects.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (projects.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);

                                return(new ProjectReader(jt, ctx, token));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #8
0
        public static Stream GetVulnerabilities(CxRestContext ctx,
                                                CancellationToken token, String reportId)
        {
            try
            {
                using (var client = ctx.Xml.CreateSastClient())
                {
                    var reportPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.Url,
                                                                              String.Format(URL_SUFFIX, reportId)), token).Result;

                    if (!reportPayload.IsSuccessStatusCode)
                    {
                        throw new InvalidOperationException($"Unable to retrieve report {reportId}." +
                                                            $" Response reason is {reportPayload.ReasonPhrase}.");
                    }

                    return(reportPayload.Content.ReadAsStreamAsync().Result);
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #9
0
        public static IEnumerable <Scan> GetScans(CxRestContext ctx, CancellationToken token,
                                                  ScanStatus specificStatus)
        {
            try
            {
                String url = null;

                if (specificStatus != ScanStatus.All)
                {
                    url = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <string, string>()
                    {
                        { "scanStatus", specificStatus.ToString() }
                    }
                                                );
                }
                else
                {
                    url = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX);
                }
                using (var client = ctx.Json.CreateSastClient())
                {
                    using (var scans = client.GetAsync(url, token).Result)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return(null);
                        }

                        if (!scans.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(scans.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (scans.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(new ScansReader(jt));
                            }
                    }
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #10
0
 public static bool CalculateViolations(CxRestContext ctx,
                                        CancellationToken token, int projectId)
 {
     try
     {
         using (var client = ctx.Json.CreateMnoClient())
             using (var calculationResponse = client.PostAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                     String.Format(URL_SUFFIX, projectId)), null, token).Result)
                 return(calculationResponse.StatusCode == HttpStatusCode.Created);
     }
     catch (HttpRequestException hex)
     {
         _log.Error("Communication error.", hex);
         throw hex;
     }
 }
예제 #11
0
        public static IEnumerable <Scan> GetScans(CxRestContext ctx, CancellationToken token,
                                                  int projectId)
        {
            try
            {
                String url = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <String, String>()
                {
                    { "projectId", Convert.ToString(projectId) },
                    { "itemsPerPage", "5000" }
                });

                using (var client = ctx.Json.CreateSastClient())
                    using (var scans = client.GetAsync(url, token).Result)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return(null);
                        }

                        // If no OSA license, result is 403.  Return an empty set of scans.
                        if (scans.StatusCode == System.Net.HttpStatusCode.Forbidden)
                        {
                            return(new ScansReader());
                        }

                        if (!scans.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(scans.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (scans.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(new ScansReader(jt, projectId));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #12
0
        public static String GetGeneratedReportId(CxRestContext ctx, CancellationToken token,
                                                  String scanId, ReportTypes type)
        {
            try
            {
                using (var client = ctx.Json.CreateSastClient())
                {
                    var dict = new Dictionary <String, String>()
                    {
                        { "reportType", type.ToString() },
                        { "scanId", scanId }
                    };

                    using (var payload = new FormUrlEncodedContent(dict))
                    {
                        using (var scanReportTicket = client.PostAsync(
                                   CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX), payload).Result)
                        {
                            if (!scanReportTicket.IsSuccessStatusCode)
                            {
                                throw new InvalidOperationException
                                          ($"Scan report generation request for scan {scanId} returned " +
                                          $"{scanReportTicket.StatusCode}");
                            }

                            using (var sr = new StreamReader
                                                (scanReportTicket.Content.ReadAsStreamAsync().Result))
                                using (var jtr = new JsonTextReader(sr))
                                {
                                    JToken jt = JToken.Load(jtr);
                                    return(ReadReportId(jt));
                                }
                        }
                    }
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #13
0
        public static String GetProjectPoliciesSingleField(CxRestContext ctx,
                                                           CancellationToken token, int projectId)
        {
            using (var client = ctx.Json.CreateMnoClient())
            {
                using (var policyPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                 String.Format(PROJECT_POLICY_URL_SUFFIX, projectId)), token).Result)
                {
                    if (!policyPayload.IsSuccessStatusCode)
                    {
                        throw new InvalidOperationException
                                  ($"Unable to retrieve policies for project {projectId}.");
                    }

                    JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                                   (policyPayload.Content.ReadAsStreamAsync().Result)));

                    return(GetFlatPolicyNames(jt));
                }
            }
        }
예제 #14
0
        public static IEnumerable <Library> GetLibraries(CxRestContext ctx, CancellationToken token,
                                                         String scanId)
        {
            try
            {
                String url = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <String, String>()
                {
                    { "scanId", Convert.ToString(scanId) }
                });

                using (var client = ctx.Json.CreateSastClient())
                {
                    var libraries = client.GetAsync(url, token).Result;

                    if (token.IsCancellationRequested)
                    {
                        return(null);
                    }

                    if (!libraries.IsSuccessStatusCode)
                    {
                        throw new InvalidOperationException(libraries.ReasonPhrase);
                    }

                    using (var sr = new StreamReader
                                        (libraries.Content.ReadAsStreamAsync().Result))
                        using (var jtr = new JsonTextReader(sr))
                        {
                            JToken jt = JToken.Load(jtr);
                            return(new LibrariesReader(jt));
                        }
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
        public static ScanSettings GetScanSettings(CxRestContext ctx, CancellationToken token, int projectId)
        {
            try
            {
                String restUrl = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX);

                using (var client = ctx.Json.CreateSastClient())
                    using (var settings = client.GetAsync(CxRestContext.MakeUrl(restUrl,
                                                                                Convert.ToString(projectId)), token).Result)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return(null);
                        }

                        if (!settings.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(settings.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (settings.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);

                                return(new ScanSettings(jt));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
예제 #16
0
        public static Stream GetVulnerabilities(CxRestContext ctx,
                                                CancellationToken token, String reportId)
        {
            try
            {
                int retryCount = 0;

                while (retryCount < RETRY_MAX)
                {
                    using (var client = ctx.Xml.CreateSastClient())
                    {
                        if (retryCount > 0)
                        {
                            int delay = RETRY_DELAY_MS * retryCount;
                            _log.Info($"Waiting {delay}ms before retrying download of report {reportId}");
                            Task.Delay(delay, token);
                        }

                        try
                        {
                            var reportPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.Url,
                                                                                      String.Format(URL_SUFFIX, reportId)), token).Result;

                            if (!reportPayload.IsSuccessStatusCode)
                            {
                                _log.Warn($"Unable to retrieve XML report {reportId}. Response status [{reportPayload.StatusCode}:{reportPayload.ReasonPhrase}]." +
                                          $"  Attempt #{retryCount + 1} of {RETRY_MAX}.");

                                retryCount++;
                                continue;
                            }


                            return(reportPayload.Content.ReadAsStreamAsync().Result);
                        }
                        catch (AggregateException aex)
                        {
                            _log.Warn($"Multiple exceptions caught attempting to retrieve XML report {reportId} during " +
                                      $"attempt #{retryCount + 1} of {RETRY_MAX}.");

                            _log.Warn("BEGIN exception report");

                            int exCount = 0;

                            aex.Handle((x) =>
                            {
                                _log.Warn($"Exception #{++exCount}", x);

                                return(true);
                            });

                            _log.Warn("END exception report");

                            retryCount++;
                        }
                        catch (Exception ex)
                        {
                            _log.Warn($"Exception caught attempting to retrieve XML report {reportId} during " +
                                      $"attempt #{retryCount + 1} of {RETRY_MAX}.", ex);

                            retryCount++;
                        }
                    }
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }

            throw new InvalidOperationException($"Unable to retrieve XML report {reportId}.");
        }