CodeSonar-GitHub Integration

NOTICE: A private GitHub repository with a GitHub Enterprise license is required for this CodeSonar integration.

GitHub provides a Continuous Integration (CI) and Continuous Delivery (CD) solution with the GitHub Actions feature. CodeSonar can be used within a GitHub Action workflow to automatically scan and analyze the source code in your GitHub repository.

This CodeSonar-GitHub integration package provides tools and examples to help you configure CodeSonar to analyze your code and report results to your GitHub repository web location.

Contents

Prerequisites

IMPORTANT: A private GitHub repository with a GitHub Enterprise license is required. Limitations imposed by the CodeSonar End User License Agreement (EULA) prohibit the use of CodeSonar for Code scanning on a public GitHub repository. Code scanning on a private GitHub repository requires a GitHub Enterprise license. A "self-hosted" GitHub runner will be needed in order to build code and communicate analysis results to your CodeSonar hub.

You will need to prepare one or more GitHub workflow runner hosts.

GitHub Advanced Security Code Scanning

GitHub provides an Advanced Security Code scanning feature for reporting and managing code analysis results associated with a GitHub repository. Typically a code scan is configured to be done when new code is submitted to a GitHub Pull Request. Results of the CodeSonar analysis associated with a GitHub Pull Request can be viewed under the Pull Request "Checks" tab.

Code scanning

GitHub provides an upload-sarif workflow Action which allows code analysis results in SARIF format to be reported to GitHub. Since CodeSonar can generate a SARIF representation of your code analysis, you can use CodeSonar in a GitHub workflow to perform an automatic code scan.

Overview: Continuous Integration Workflow Setup

There are many benefits to setting up a continuous integration (CI) workflow using a GitHub Actions workflow. However, the set-up process can be complicated for a variety of reasons. The information below is intended to help you navigate the complexities of introducing CodeSonar static analysis into a workflow and managing the code analysis data produced when the workflow is executed.

The following general steps are required for preparing a GitHub workflow for CodeSonar analysis:

The remaining sections in this document outline the steps above.

For detailed, system-specific instructions, see the linked pages:

A. Create and install an analysis data server

If you are using CodeSonar SaaS, or if your CodeSonar hub is already set up with remote analysis launch daemons for use with -remote and -remote-archive, you can use your existing infrastructure and do not need to set up a separate host: go on to B. Create a basic workflow that can build your code.

A CodeSonar analysis produces several analysis data artifacts which contain information about the source code as it was interpreted by your compilers. The analysis data artifacts are created on the host machine that does the analysis, and can be quite large. The CodeSonar hub needs access to the analysis data artifacts in order to enable many advanced features of code navigation, etc.

A typical workflow consists of several jobs, each of which generates artifacts and transfers them to subsequent jobs upon completion. Artifacts that are not transferred will be deleted by the workflow job runner process. The CodeSonar analysis data created in a workflow must therefore be transferred to a persistent storage location outside the runner environment so that they will remain available after the workflow runs.

We will refer to the host system for the analysis data persistent storage as an "analysis data server".

Alternative: relocating analysis data server

If you are using CodeSonar 7.2 or earlier, you will need to extend your workflow to explicitly relocate the analysis directory to your analysis data server.

  1. Copy the analysis directory and all its contents to your analysis data server.
  2. Use the codesonar relocate command to inform the hub of the new analysis directory location.

For details, see the Tips and Troubleshooting page.

B. Create a basic workflow that can build your code

In order to analyze your code using CodeSonar in a GitHub workflow, you must first create a workflow that can compile and build your code. This requires the following.

If you do not already have a working build workflow, you must create one specific to your code before introducing CodeSonar.

In practice, it is generally necessary to prepare a "self-hosted" GitHub runner that can build your code. This will allow CodeSonar analysis results to be communicated securely to a private CodeSonar hub.

C. Install CodeSonar and integration tools in the build runner environment

In order to initiate the CodeSonar analysis, you must install CodeSonar in your GitHub runner environment. For Docker-based runners, this involves adding CodeSonar to the Docker image used when building your code. Without Docker, this involves installing CodeSonar on the runner host itself.

Install the following items to your GitHub build runner environment

CodeSonar and the integration tools may not automatically trust the HTTPS certificates provided by your hub or your GitHub server. You may need to take steps to ensure that the CodeSonar installation in your runner environment will trust the certificates: see the Tips and Troubleshooting section for details.

D. Update the workflow definition to perform CodeSonar analysis

CodeSonar works by creating an internal representation (IR) of your software, then performing a suite of analyses on that representation. The IR creation stage works differently for different source languages.

To modify the GitHub workflow job you identified in step B so that it also builds and analyzes a CodeSonar project based on your code, do the following.

  1. Add zero or more codesonar build commands, plus a final codesonar analyze command.

    For full details of the CodeSonar build/analysis command line, see the CodeSonar manual: Using CodeSonar > Building and Analyzing Projects > Command Line Build/Analysis

  2. Make sure your codesonar build and codesonar analyze commands account for the following.

    • Authentication credentials for a hub user account that can authorize the analysis. With factory settings, it is sufficient for the account to have the built-in User role. (See Authentication Notes, below.)

    • For CodeSonar SaaS, your codesonar analyze command must include -wait and -remote "./saas/*". It must not include -foregound.

    • For non-SaaS CodeSonar, your codesonar analyze command must do one of the following.

      • Specify -wait and -remote <analysis-launchd>, where <analysis-launchd> is a suitable remote analysis launch daemon: either one you set up on your analysis data server host, or one that existed previously. <analysis-launchd> will manage both the analysis of your project and the hub interaction with analysis files when the analysis has completed. The command must not include -foregound.

        • Specify -wait and -remote-archive <archive-launchd>, where <archive-launchd> is a suitable remote analysis launch daemon: either one you set up on your set up on your analysis data server host, or one that existed previously. The analysis files will be transferred to <archive-launchd> after the analysis completes, and will manage all subsequent hub interaction with analysis files. The command must not include -foregound.

        If you are using CodeSonar 7.2 or earlier, -remote-archive is not available. See the Tips and Troubleshooting page for further options.

    • You may wish to specify -name <analysis-name> to provide a custom analysis name: for example, to relate the analysis to a specific GitHub pull request record.

Execute the workflow

The typical way to trigger a workflow is by pushing new code commits to a GitHub pull request. After a successful analysis in the workflow, you can view your analysis results in GitHub and on the CodeSonar hub. This allows you to automatically scan for newly introduced issues in your code and to verify that known issues have been fixed.

Authentication Notes

These instructions discuss two options for authenticating a codesonar command line: certificate and password.

You can use GitHub Secrets to provide credentials for authentication.

For more information about authentication in CodeSonar, see the CodeSonar manual: How CodeSonar Works > CodeSonar Structure > Hub > Authentication and Access Control

Bearer Authentication

From CodeSonar 8.0 you also have the option of bearer authentication.

For more information about sessions and bearer token authentication, see the CodeSonar manual: How CodeSonar Works > CodeSonar Structure > Hub > User Session and Anonymous Sessions

Other Notes

The CodeSonar-GitHub integration package includes a few script programs which are used to process analysis results and share the results with GitHub. These scripts must be executed using the cspython Python interpreter included with CodeSonar.

Further Information

See the Tips and Troubleshooting document for additional information.