/// <summary>
        /// Run the report.
        /// </summary>
        /// <param name="checkmarxApiSession">
        /// A <see cref="ICheckmarxApiSession"/> used to run the report. This cannot be null.
        /// </param>
        /// <param name="options">
        /// Command line options. This cannot be null.
        /// </param>
        /// <returns>
        /// The report results.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="checkmarxApiSession"/> cannot be null.
        /// </exception>
        /// <exception cref="CheckmarxErrorException">
        /// Checkmarx returned an unexpected result or error.
        /// </exception>
        /// <exception cref="CheckmarxCommunicationException">
        /// Communication with the Checkmarx server failed.
        /// </exception>
        public IList <string> Run(ICheckmarxApiSession checkmarxApiSession, CheckmarxReportOptions options)
        {
            if (checkmarxApiSession == null)
            {
                throw new ArgumentNullException(nameof(checkmarxApiSession));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(checkmarxApiSession.GetProjectScans()
                   .AsParallel()
                   .WithDegreeOfParallelism(ReportRunnerHelper.MaxParallelization)
                   .Where(ReportRunnerHelper.GetProjectPredicate(options))
                   .Select(
                       project =>
                       CheckmarxApiSessionHelper.GenerateLastScanCsvReport(checkmarxApiSession, project).ToString())
                   .ToList());
        }
        /// <summary>
        /// Generate a Checkmarx scan report for the most recent scan for the given project.
        /// </summary>
        /// <param name="checkmarxApiSession">
        /// The <see cref="ICheckmarxApiSession"/> to generate the report with. This cannot be null.
        /// </param>
        /// <param name="project">
        /// The project to get the last scan for. This cannot be null.
        /// </param>
        /// <returns>
        /// An <see cref="XDocument"/> containing the loaded scan report.
        /// </returns>
        /// <exception cref="CheckmarxErrorException">
        /// Either the report generation failed or Checkmarx returned invalid XML for the scan report.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// no argument can be null.
        /// </exception>
        public static XDocument GenerateLastScanXmlReport(ICheckmarxApiSession checkmarxApiSession, ProjectScannedDisplayData project)
        {
            XDocument xDocument;

            byte[] report = GenerateLastScanReport(checkmarxApiSession, project, CxWSReportType.XML);
            using (MemoryStream memoryStream = new MemoryStream(report))
            {
                try
                {
                    xDocument = XDocument.Load(memoryStream, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
                }
                catch (XmlException ex)
                {
                    throw new CheckmarxErrorException(
                              $"Checkmarx returned invalid XML for report on scan {project.LastScanID} on project {project.ProjectName}", ex);
                }
            }

            return(xDocument);
        }
        /// <summary>
        /// Generate a Checkmarx scan report for the most recent scan for the given project.
        /// </summary>
        /// <param name="checkmarxApiSession">
        /// The <see cref="ICheckmarxApiSession"/> to generate the report with. This cannot be null.
        /// </param>
        /// <param name="project">
        /// The project to get the last scan for. This cannot be null.
        /// </param>
        /// <param name="reportType">
        /// The result format for the report.
        /// </param>
        /// <returns>
        /// An <see cref="XDocument"/> containing the loaded scan report.
        /// </returns>
        /// <exception cref="CheckmarxErrorException">
        /// Either the report generation failed or Checkmarx returned invalid XML for the scan report.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// no argument can be null.
        /// </exception>
        private static byte[] GenerateLastScanReport(ICheckmarxApiSession checkmarxApiSession,
                                                     ProjectScannedDisplayData project, CxWSReportType reportType)
        {
            if (checkmarxApiSession == null)
            {
                throw new ArgumentNullException(nameof(checkmarxApiSession));
            }
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }
            if (!Enum.IsDefined(typeof(CxWSReportType), reportType))
            {
                throw new ArgumentOutOfRangeException(nameof(reportType));
            }

            long reportId;

            reportId = checkmarxApiSession.CreateScanReport(project.LastScanID, reportType);
            for (;;)
            {
                CxWSReportStatusResponse reportStatusResponse = checkmarxApiSession.GetScanReportStatus(reportId);
                if (reportStatusResponse.IsFailed)
                {
                    throw new CheckmarxErrorException(
                              $"Generating report ID {reportId} on scan {project.LastScanID} on project {project.ProjectName} failed");
                }
                else if (reportStatusResponse.IsReady)
                {
                    break;
                }

                // TODO: Consider a better mechanism
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

                // TODO: Consider a timeout
            }

            return(checkmarxApiSession.GetScanReport(reportId));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Run the report.
        /// </summary>
        /// <param name="checkmarxApiSession">
        /// A <see cref="ICheckmarxApiSession"/> used to run the report. This cannot be null.
        /// </param>
        /// <param name="options">
        /// Command line options. This cannot be null.
        /// </param>
        /// <returns>
        /// The report results.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="checkmarxApiSession"/> cannot be null.
        /// </exception>
        /// <exception cref="CheckmarxErrorException">
        /// Checkmarx returned an unexpected result or error.
        /// </exception>
        /// <exception cref="CheckmarxCommunicationException">
        /// Communication with the Checkmarx server failed.
        /// </exception>
        public IList <ScanResult> Run(ICheckmarxApiSession checkmarxApiSession, CheckmarxReportOptions options)
        {
            if (checkmarxApiSession == null)
            {
                throw new ArgumentNullException(nameof(checkmarxApiSession));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(checkmarxApiSession.GetProjectScans()
                   .AsParallel()
                   .WithDegreeOfParallelism(ReportRunnerHelper.MaxParallelization)
                   .Where(ReportRunnerHelper.GetProjectPredicate(options))
                   .SelectMany(
                       project =>
                       CheckmarxApiSessionHelper.GenerateLastScanXmlReport(checkmarxApiSession, project)
                       .XPathSelectElements("//Result[@FalsePositive=\"False\"]")
                       .Select(xmlNode => XmlNodeToScanResult(xmlNode, project.ProjectName)))
                   .ToList());
        }
 /// <summary>
 /// Generate a Checkmarx scan report for the most recent scan for the given project.
 /// </summary>
 /// <param name="checkmarxApiSession">
 /// The <see cref="ICheckmarxApiSession"/> to generate the report with. This cannot be null.
 /// </param>
 /// <param name="project">
 /// The project to get the last scan for. This cannot be null.
 /// </param>
 /// <returns>
 /// An string containing a CSV version of the report.
 /// </returns>
 /// <exception cref="CheckmarxErrorException">
 /// Either the report generation failed or Checkmarx returned invalid XML for the scan report.
 /// </exception>
 /// <exception cref="ArgumentNullException">
 /// no argument can be null.
 /// </exception>
 public static string GenerateLastScanCsvReport(ICheckmarxApiSession checkmarxApiSession, ProjectScannedDisplayData project)
 {
     return(Encoding.UTF8.GetString(GenerateLastScanReport(checkmarxApiSession, project, CxWSReportType.CSV)));
 }