/// <summary>
        /// Format the report results.
        /// </summary>
        /// <param name="reportResults">
        /// The report results. This cannot be null.
        /// </param>
        /// <param name="output">
        /// The <see cref="TextWriter"/> to write the results to. This cannot be null.
        /// </param>
        /// <param name="options">
        /// Command line options. This cannot be null.
        /// </param>
        /// <param name="username">
        /// The user the report was run by. This cannot be null, empty or whitespace.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// No argument can be null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="username"/> cannot be null, empty or whitespace.
        /// </exception>
        public void Format(IList <ScanResult> reportResults, TextWriter output, CheckmarxReportOptions options, string username)
        {
            if (reportResults == null)
            {
                throw new ArgumentNullException(nameof(reportResults));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException("Cannot be null, empty or whitespace", nameof(username));
            }

            WriteHeader(output);
            foreach (ScanResult scanResult in reportResults.OrderBy(sr => sr.ProjectName).ThenBy(sr => sr.RuleName))
            {
                WriteRow(output, scanResult);
            }
        }
        /// <summary>
        /// Determine the project predicate to exclude projects.
        /// </summary>
        /// <param name="options">
        /// The command line options. This cannot be null.
        /// </param>
        /// <returns>
        /// A predicate determining which projects to include in the report.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="options"/> cannot be null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Cannot have both include and exclude projects in <paramref name="options"/>.
        /// </exception>
        public static Func <ProjectScannedDisplayData, bool> GetProjectPredicate(CheckmarxReportOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (options.ExcludeProjects != null && options.ExcludeProjects.Any() &&
                options.Projects != null && options.Projects.Any())
            {
                throw new ArgumentException("Cannot have both exclude and include projects");
            }

            Func <ProjectScannedDisplayData, bool> result;

            if (options.ExcludeProjects != null &&
                options.ExcludeProjects.Any())
            {
                result = project => !options.ExcludeProjects.Contains(project.ProjectName);
            }
            else if (options.Projects != null &&
                     options.Projects.Any())
            {
                result = project => options.Projects.Contains(project.ProjectName);
            }
            else
            {
                result = project => true;
            }

            return(result);
        }
        /// <summary>
        /// Format the report results.
        /// </summary>
        /// <param name="reportResults">
        /// The report results. This cannot be null.
        /// </param>
        /// <param name="output">
        /// The <see cref="TextWriter"/> to write the results to. This cannot be null.
        /// </param>
        /// <param name="options">
        /// Command line options. This cannot be null.
        /// </param>
        /// <param name="username">
        /// The user the report was run by. This cannot be null, empty or whitespace.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// No argument can be null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="username"/> cannot be null, empty or whitespace.
        /// </exception>
        public void Format(IList <ScanResult> reportResults, TextWriter output, CheckmarxReportOptions options, string username)
        {
            if (reportResults == null)
            {
                throw new ArgumentNullException(nameof(reportResults));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException("Cannot be null, empty or whitespace", nameof(username));
            }

            using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(output))
            {
                htmlTextWriter.WriteLine("<!DOCTYPE html>");
                htmlTextWriter.AddAttribute("lang", "en");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Html);
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Head);
                WriteBootstrapHeaders(htmlTextWriter);
                htmlTextWriter.RenderEndTag();
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Body);
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "container-fluid");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Div);

                // Header
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.H1);
                htmlTextWriter.WriteEncodedText("Not False Positive Checkmarx Results");
                htmlTextWriter.RenderEndTag();
                htmlTextWriter.WriteEncodedText($"Generated at {DateTime.Now} on {options.Server} by {username}");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Br);
                htmlTextWriter.RenderEndTag();

                // Results table
                htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Class, "table table-striped");
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);
                WriteHeader(htmlTextWriter);
                htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tbody);
                foreach (ScanResult scanResult in reportResults.OrderBy(sr => sr.ProjectName).ThenBy(sr => sr.RuleName))
                {
                    WriteRow(htmlTextWriter, scanResult);
                }
                htmlTextWriter.RenderEndTag();
                htmlTextWriter.RenderEndTag();

                htmlTextWriter.RenderEndTag();
                WriteBootstrapScripts(htmlTextWriter);
                htmlTextWriter.RenderEndTag();
                htmlTextWriter.RenderEndTag();
                htmlTextWriter.Flush();
            }
        }
        /// <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());
        }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Format the report results.
        /// </summary>
        /// <param name="reportResults">
        /// The report results. This cannot be null.
        /// </param>
        /// <param name="output">
        /// The <see cref="TextWriter"/> to write the results to. This cannot be null.
        /// </param>
        /// <param name="options">
        /// Command line options. This cannot be null.
        /// </param>
        /// <param name="username">
        /// The user the report was run by. This cannot be null, empty or whitespace.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// No argument can be null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="username"/> cannot be null, empty or whitespace.
        /// </exception>
        public void Format(IList <string> reportResults, TextWriter output, CheckmarxReportOptions options, string username)
        {
            if (reportResults == null)
            {
                throw new ArgumentNullException(nameof(reportResults));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException("Cannot be null, empty or whitespace", nameof(username));
            }

            foreach (string result in reportResults)
            {
                output.WriteLine(result);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Run the report.
        /// </summary>
        /// <param name="reportRunner">
        /// A <see cref="IReportRunner{TReportResult}"/> to run the report. This cannot be null.
        /// </param>
        /// <param name="reportResultFormatter">
        /// A <see cref="IReportResultFormatter{TReportResult}"/> to format the report results. This cannot be null.
        /// </param>
        /// <param name="credentialRepository">
        /// Used to load previously saved credentials. Cannot be null.
        /// </param>
        /// <param name="options">
        /// The command line options. Cannot be null.
        /// </param>
        /// <returns>
        /// The process return value.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="reportRunner"/>, <paramref name="reportResultFormatter"/>, <paramref name="credentialRepository"/>
        /// and <paramref name="options"/> cannot be null.
        /// </exception>
        private static int RunReport <TReportResult>(IReportRunner <TReportResult> reportRunner, IReportResultFormatter <TReportResult> reportResultFormatter,
                                                     ICredentialRepository credentialRepository, CheckmarxReportOptions options)
        {
            if (reportRunner == null)
            {
                throw new ArgumentNullException(nameof(reportRunner));
            }
            if (reportResultFormatter == null)
            {
                throw new ArgumentNullException(nameof(reportResultFormatter));
            }
            if (credentialRepository == null)
            {
                throw new ArgumentNullException(nameof(credentialRepository));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            string userName;
            string password;

            GetCredentials(credentialRepository, options.Server, options.UserName, options.Password, out userName, out password);

            using (Stream stream = string.IsNullOrWhiteSpace(options.OutputPath)
                ? Console.OpenStandardOutput() : new FileStream(options.OutputPath, FileMode.Create))
                using (StreamWriter output = new StreamWriter(stream, Encoding.UTF8))
                    using (CheckmarxApiSession checkmarxApiSession = new CheckmarxApiSession(options.Server, userName, password))
                    {
                        reportResultFormatter.Format(reportRunner.Run(checkmarxApiSession, options), output, options, userName);
                    }

            return(ExitSuccess);
        }