Exemplo n.º 1
0
        private void ExecuteSweep()
        {
            if (ScanDescriptors == null)
            {
                _log.Error("Scans to crawl do not appear to be resolved, unable to crawl scan data.");
                return;
            }
            else
            {
                _log.Debug("Crawling scans.");
            }


            // Lookup policy violations, report the project information records.
            Parallel.ForEach <ScanDescriptor>(ScanDescriptors, ThreadOpts,
                                              (scan) =>
            {
                if (PolicyViolations.TryAdd(scan.Project.ProjectId,
                                            new ViolatedPolicyCollection()))
                {
                    if (Policies != null)
                    {
                        try
                        {
                            // Collect policy violations, only once per project
                            PolicyViolations[scan.Project.ProjectId] = CxMnoRetreivePolicyViolations.
                                                                       GetViolations(RestContext, CancelToken, scan.Project.ProjectId, Policies);
                        }
                        catch (Exception ex)
                        {
                            _log.Debug($"Policy violations for project {scan.Project.ProjectId}: " +
                                       $"{scan.Project.ProjectName} are unavailable.", ex);
                        }
                    }

                    OutputProjectInfoRecords(scan);
                }

                // Increment the policy violation stats for each scan.
                scan.IncrementPolicyViolations(PolicyViolations[scan.Project.ProjectId].
                                               GetViolatedRulesByScanId(scan.ScanId));

                // Does something appropriate for the type of scan in the scan descriptor.
                scan.MapAction(scan, this);

                OutputPolicyViolationDetails(scan);
            });
        }
Exemplo n.º 2
0
        private void ExecuteSweep()
        {
            if (_state.Projects == null)
            {
                _log.Error("Scans to crawl do not appear to be resolved, unable to crawl scan data.");
                return;
            }


            _log.Info($"Crawling {_state.ScanCount} scans.");


            // Lookup policy violations, report the project information records.
            Parallel.ForEach <ProjectDescriptor>(_state.Projects, ThreadOpts,
                                                 (project) =>
            {
                // Do not output project info if a project has no scans.
                if (_state.GetScanCountForProject(project.ProjectId) <= 0)
                {
                    _log.Info($"Project {project.ProjectId}:{project.TeamName}:{project.ProjectName} has no new scans to process.");
                    return;
                }

                // Project info is a moment-in-time sample of the state of the project.  This can be output
                // in a transaction context different than the scans.
                using (var pinfoTrx = Output.StartTransaction())
                    if (PolicyViolations.TryAdd(project.ProjectId, new ViolatedPolicyCollection()))
                    {
                        if (Policies != null)
                        {
                            try
                            {
                                // Collect policy violations, only once per project
                                var violations = CxMnoRetreivePolicyViolations.GetViolations(RestContext, CancelToken, project.ProjectId, Policies);
                                if (violations != null)
                                {
                                    PolicyViolations[project.ProjectId] = violations;
                                }
                            }
                            catch (Exception ex)
                            {
                                _log.Debug($"Policy violations for project {project.ProjectId}:" +
                                           $"{project.ProjectName} are unavailable.", ex);
                            }
                        }

                        OutputProjectInfoRecords(pinfoTrx, project);

                        if (!CancelToken.IsCancellationRequested)
                        {
                            pinfoTrx.Commit();
                        }
                    }

                // One transaction per scan since the entire set of scan records should be output
                // before the scan date is updated.
                foreach (var scan in _state.GetScansForProject(project.ProjectId))
                {
                    if (CancelToken.IsCancellationRequested)
                    {
                        break;
                    }

                    using (var scanTrx = Output.StartTransaction())
                    {
                        // Increment the policy violation stats for each scan.
                        scan.IncrementPolicyViolations(PolicyViolations[scan.Project.ProjectId].GetViolatedRulesByScanId(scan.ScanId));

                        _log.Info($"Processing {scan.ScanProduct} scan {scan.ScanId}:{scan.Project.ProjectId}:{scan.Project.TeamName}:{scan.Project.ProjectName}[{scan.FinishedStamp}]");

                        switch (scan.ScanProduct)
                        {
                        case ScanProductType.SAST:
                            SastReportOutput(scanTrx, scan);
                            break;

                        case ScanProductType.SCA:
                            ScaReportOutput(scanTrx, scan);
                            break;
                        }

                        OutputPolicyViolationDetails(scanTrx, scan);

                        // Persist the date of this scan since it has been output.
                        if (!CancelToken.IsCancellationRequested && scanTrx.Commit())
                        {
                            _state.ScanCompleted(scan);
                        }
                        else
                        {
                            // Stop processing further scans in this project if the commit
                            // for the scan information fails.
                            return;
                        }
                    }
                }
            });
        }