> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/security-testing-orchestration/3.0/use-sto/sto-scanner-configuration/sonarqube-sonar-scanner-reference.md).

# SonarQube step configuration

Harness STO integrates with [SonarQube](https://docs.sonarqube.org/latest/) to scan your code repositories for vulnerabilities, enforce policies, and maintain code quality. The SonarQube step supports all the three STO scan modes: **Orchestration**, **Ingestion**, and **Extraction**.

**Language Support**: All languages supported by SonarQube are compatible. Refer to the [SonarQube language reference](https://docs.sonarqube.org/latest/analysis/languages/overview/) for prerequisites specific to your repository's language.

{% embed url="<https://www.youtube.com/embed/qP0TUQuTSfI?si=yzQslx3sXdQjXWTi>" %}

#### SonarQube Issue categorization in STO <a href="#sonarqube-issue-categorization-in-sto" id="sonarqube-issue-categorization-in-sto"></a>

STO categorizes the SonarQube issues with severities: **Critical**, **High**, **Medium**, **Low**, and **Info**, refer to [STO severity levels](/security-testing-orchestration/3.0/new-to-sto/key-concepts/severities.md) for more details. The table below outlines how specific SonarQube issue types are classified in STO.

| SonarQube Issue Type                                                                                                                                                                                                                                                         | STO Categorization                                                                                                                                                                                                 |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Vulnerabilities                                                                                                                                                                                                                                                              | Imported, normalized, deduplicated and assigned [STO severity levels](/security-testing-orchestration/3.0/new-to-sto/key-concepts/severities.md). See [severity mapping table](#sonarqube-severity-mapping) below. |
| Code Smells, Bug Smells                                                                                                                                                                                                                                                      | By default, imported and categorized under **Info** severity.                                                                                                                                                      |
| If you enable the **Treat Code Smells and Bug Smells as Vulnerabilities** in **Default Settings** at Account level Settings (disabled by default), STO imports these issues with their original severities. See [severity mapping table](#sonarqube-severity-mapping) below. |                                                                                                                                                                                                                    |
| Maintainability issues                                                                                                                                                                                                                                                       | Imported and categorized under **Info** severity.                                                                                                                                                                  |
| [Quality Gates (Policies)](#view-sonarqube-quality-gate-failures)                                                                                                                                                                                                            | Imported and categorized as policy issues with **Info** severity.                                                                                                                                                  |
| [Code Coverage](#view-sonarqube-code-coverage-results)                                                                                                                                                                                                                       | Imported as both a step output variable and a policy issue with **Info** severity.                                                                                                                                 |
| Hotspots                                                                                                                                                                                                                                                                     | Currently not supported by STO.                                                                                                                                                                                    |

#### SonarQube Severity Mapping <a href="#sonarqube-severity-mapping" id="sonarqube-severity-mapping"></a>

When STO imports SonarQube issues, it maps the SonarQube severity levels to the STO severity levels as follows:

| SonarQube Severity | STO Severity |
| ------------------ | ------------ |
| INFO               | Info         |
| MINOR              | Low          |
| MAJOR              | Medium       |
| CRITICAL           | High         |
| BLOCKER            | Critical     |

#### Step configuration guidelines <a href="#step-configuration-guidelines" id="step-configuration-guidelines"></a>

Use the following guidelines when configuring and running SonarQube scans in STO:

**Resource Allocation**

* By default, STO allocates **500Mi memory** for SonarQube scans, suitable primarily for Ingestion scans.
* For Orchestration and Extraction scans, allocate at least **2GB memory**. Customize resource limits per [Set Container Resources](/continuous-integration/use-harness-ci/use-harness-ci/manage-dependencies/background-step-settings.md#set-container-resources).

**Certificates and Root Access**

* Run scans with root access if adding trusted certificates at runtime.
* Alternatively, configure STO images and pipelines to run as non-root users and manage self-signed certificates. For details, see [Configure your pipeline to use STO images from private registry](/security-testing-orchestration/3.0/troubleshooting-and-resources/sto-use-cases/set-up-sto-pipelines/configure-pipeline-to-use-sto-images-from-private-registry.md).

{% hint style="info" %}
STO supports three different approaches for loading self-signed certificates. For more information, refer [Run STO scans with custom SSL certificates](/security-testing-orchestration/3.0/troubleshooting-and-resources/sto-use-cases/secure-sto-pipelines/ssl-setup-in-sto.md#supported-workflows-for-adding-custom-ssl-certificates-in-sto).
{% endhint %}

### SonarQube step settings <a href="#sonarqube-step-settings" id="sonarqube-step-settings"></a>

The recommended workflow is to add a SonarQube step to a Security or Build stage and then configure it as described below.

A Docker-in-Docker background step is not required for this workflow.

#### Scan <a href="#scan" id="scan"></a>

**Scan Mode**

**Scan Configuration**

The predefined configuration to use for the scan.

* **Default** Extract results for the Main branch defined in SonarQube. SonarQube Community Edition supports extracting scan results for the Main branch only.
* **Branch Scan** In Orchestration or Extraction mode, extract results based on how the pipeline is executed:
  * Manual executions - The branch defined in SonarQube ([Target variant](#variant))
  * Triggered executions - The pull request defined in SonarQube

#### Target <a href="#target" id="target"></a>

**Type**

**Target and Variant Detection**

**Name**

If you're running an Extraction scan, this field should match the code repository name in SonarQube.

**Variant**

If you're running an Extraction scan, this field should match the branch or PR defined in SonarQube.

**Workspace**

#### Ingestion File <a href="#ingestion-file" id="ingestion-file"></a>

#### Authentication <a href="#authentication" id="authentication"></a>

**Domain**

The URL of the SonarQube server. This is required for Orchestration and Extraction scans. This value corresponds to the [`sonar.host.url`](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/analysis-parameters/#mandatory-parameters) setting in SonarQube.

**Enforce SSL**

**Access Token**

{% hint style="info" %}
**NOTE**

Harness recommends that you use a SonarQube **user** token that includes permissions to run scans and to create projects.

If you use a **project** token, you must have access to the SonarQube project that you want to scan.

For more information, go to [Generating and using tokens](https://docs.sonarsource.com/sonarqube/latest/user-guide/user-account/generating-and-using-tokens/) in the SonarQube documentation.
{% endhint %}

#### Scan Tool <a href="#scan-tool" id="scan-tool"></a>

**Project key**

The unique key of the SonarQube project to scan.

**Exclude**

If you want to exclude some files from a scan, you can use this setting to configure the `sonar.exclusions` in your SonarQube project. For more information, go to [Narrowing the Focus](https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/) in the SonarQube docs.

**Java Libraries**

A comma-separated list of paths to files with third-party libraries used by your tests. For SonarQube scans, this corresponds to the `sonar.java.libraries` parameter.

**Java Binaries**

A comma-separated list of paths to the folders with the bytecode files you want to scan. For SonarQube scans, this corresponds to the `sonar.java.binaries` parameter.

#### Log Level <a href="#log-level" id="log-level"></a>

#### Additional CLI flags <a href="#additional-cli-flags" id="additional-cli-flags"></a>

You can add CLI flags to run the [sonar-scanner binary](https://docs.sonarqube.org/9.6/analyzing-source-code/analysis-parameters/) with specific command-line arguments. Here are some examples:

* `-Dsonar.ws.timeout=300`: Suppose the scan is experiencing timeouts due to long response times from a web service. This flag increases the timeout window.
* `-Dsonar.projectName=<project_name>`: The project name.
* `-Dsonar.projectVersion=<version_number>`: The project version to scan.
* `-Dsonar.projectKey=<project_key>`: The unique key of the project to scan.
* `-Dsonar.test.exclusions=**src/test/**/*.*`: The test files to exclude from the scan.
* `-Dsonar.organization=<your-org-key>`: Your SonarCloud organization key. Required for **SonarCloud Orchestration scans only**. Not required for self-hosted SonarQube or SonarCloud Extraction scans.

**YAML example**

```yaml
              - step:
                  type: Sonarqube
                  spec:
                    advanced:
                      args:
                        cli: "-Dsonar.projectVersion=1.2.3"
```

#### Fail on Severity <a href="#fail-on-severity" id="fail-on-severity"></a>

#### Settings <a href="#settings" id="settings"></a>

Use this field to add environment variables to your SonarQube scans. For example, you can add [proxy settings](#proxy-settings) if your SonarQube instance is behind a proxy.

**SonarCloud Organization ID**

If you're using **SonarCloud**, add the following key-value pair under **Settings**:

| Key                       | Value                                                 |
| ------------------------- | ----------------------------------------------------- |
| `product_organization_id` | Your SonarCloud organization key (e.g., `my-org-key`) |

This setting is required for both **Extraction** and **Orchestration** scans with SonarCloud. It is not required for self-hosted SonarQube.

#### Additional Configuration <a href="#additional-configuration" id="additional-configuration"></a>

#### Advanced settings <a href="#advanced-settings" id="advanced-settings"></a>

### Proxy settings <a href="#proxy-settings" id="proxy-settings"></a>

### View scan results in SonarQube portal <a href="#view-scan-results-in-sonarqube-portal" id="view-scan-results-in-sonarqube-portal"></a>

You can access scan results for your targets in the SonarQube portal. These results are organized under projects titled with your target/repository name. Further, you can view scan results specific to branches, PRs, and tags based on the scans performed in STO.

#### Locate STO scan results in SonarQube portal <a href="#locate-sto-scan-results-in-sonarqube-portal" id="locate-sto-scan-results-in-sonarqube-portal"></a>

1. ***Branch*****&#x20;scan results**: If you scanned a branch, you can find the results in SonarQube with the same branch name.
2. **Pull Request scan results**: If you scanned a PR, the results are located under the name `refs/pull/<PR_NUMBER>`, where `<PR_NUMBER>` is the specific number of the pull request.
3. **Tag scan results**: If you scanned a tag, the results are available with the same tag name.

#### Set the name for your scans <a href="#set-the-name-for-your-scans" id="set-the-name-for-your-scans"></a>

When using the scan configuration **Branch Scan**, you can set the naming of the scan as it will appear in the SonarQube portal. This can be configured as follows:

1. Under **Scan Tool**, set the **Analysis Type** to **Manual**.
2. Set the **Branch Name** to the name you want to use for your scan results in the SonarQube portal.

#### Best practices <a href="#best-practices" id="best-practices"></a>

You can use [Harness expressions](/harness-ai/use-harness-platform/variables-and-expressions/harness-variables.md) to automatically populate values from triggers. For example, you can define the "Branch Name" as `<+trigger.sourceBranch>/<+trigger.prNumber>`. This will generate a naming convention in the format SOURCE\_BRANCH\_NAME/PR\_NUMBER, which is particularly useful when triggering scans on a specific PR.

This setup ensures that the scan result name reflects both the branch and the PR, making it easier to identify and manage scan results in the SonarQube portal.

### View SonarQube quality gate failures <a href="#view-sonarqube-quality-gate-failures" id="view-sonarqube-quality-gate-failures"></a>

SonarQube quality gate failures will appear in scan results as 'Info' severity issues, with the issue type set to `EXTERNAL_POLICY`. Additionally, you can apply an OPA policy to fail the pipeline based on the quality gate failures. This can be achieved using the [Security Tests - External Policy Failures](/security-testing-orchestration/3.0/use-sto/enforce-sto-policies-for-governance/create-opa-policies.md#block-the-pipeline-based-on-external-policy-failures) policy from the [security tests policy samples](/security-testing-orchestration/3.0/use-sto/enforce-sto-policies-for-governance/create-opa-policies.md#security-test-policy-samples).

To retrieve quality gate failure data from SonarQube, ensure the access token used in the SonarQube step configuration has **Browse Project** or **Administer** [permissions](https://docs.sonarsource.com/sonarqube/latest/instance-administration/user-management/user-permissions/) for the project being scanned.

### Configure External Policy Failures <a href="#configure-external-policy-failures" id="configure-external-policy-failures"></a>

You can configure Harness STO to treat external policy failures as vulnerabilities. To do this, navigate to Account Settings, go to the Default Settings page, select Security Test Orchestration, and click the External Policy Failures setting.

Enable the external policy failures setting to map them to a selected severity level — `Critical`, `High`, `Medium`, or `Low` to treat them as vulnerabilities instead of `Info` level issues. This setting takes effect from the next pipeline execution.

{% hint style="info" %}
This feature is behind the Feature flag `STO_EXTERNAL_POLICY_FAILURES_AS_VULNS`. Contact [Harness Support](mailto:support@harness.io) to enable this flag.
{% endhint %}

### View SonarQube code coverage results <a href="#view-sonarqube-code-coverage-results" id="view-sonarqube-code-coverage-results"></a>

SonarQube code coverage data appears in the scan results as `Info` issues. To locate it, search for `Code Coverage` within the Info issues, the issue type will be labeled as Code Coverage. Additionally, you can apply an OPA policy to fail the pipeline based on the code coverage results. This can be achieved using the [Security Tests - Code Coverage](/security-testing-orchestration/3.0/use-sto/enforce-sto-policies-for-governance/create-opa-policies.md#block-the-pipeline-based-on-the-code-coverage-results) policy from the [security tests policy samples](/security-testing-orchestration/3.0/use-sto/enforce-sto-policies-for-governance/create-opa-policies.md#security-test-policy-samples).

To retrieve code coverage data from SonarQube, ensure the access token used in the SonarQube step configuration has **Browse Project** or **Administer** [permissions](https://docs.sonarsource.com/sonarqube/latest/instance-administration/user-management/user-permissions/) for the project being scanned.

{% embed url="<https://www.youtube.com/embed/OCSTG5nfK1A?si=u9pI1f3iayDicAzv>" %}

### Generate coverage reports and upload to SonarQube <a href="#generate-coverage-reports-and-upload-to-sonarqube" id="generate-coverage-reports-and-upload-to-sonarqube"></a>

You can set up your pipeline to generate test coverage reports and then get them pushed up to your SonarQube instance. To do this:

1. Add a **Run** step to your pipeline before the SonarQube step.
2. Set the **Image** field to a base image that's compatible with the repo you're scanning.
3. Add commands to install the binary and any other dependencies required to generate the coverage report.
4. Add the commands necessary to generate the report.
5. Add a [failure strategy](/continuous-delivery/use-continuous-delivery/cd-building-blocks/executions/step-failure-strategy-settings.md) to the Run step and configure it to ignore all failures.

   This step is required if you want the pipeline to proceed even if it can't generate a coverage report.
6. Update your SonarQube step with the path to the coverage report.
   * This step is required only if you saved your report to a non-default folder and/or filename.
   * To specify the report path, add the CLI argument for the report path to [Additional CLI Flags](#additional-cli-flags) in your SonarQube scan step.

{% hint style="info" %}
**IMPORTANT NOTES**

* You must ensure that you generate reports that `sonar-cli` can find and upload, and that your SonarQube instance can ingest.
* For more information, go to [Test Coverage](https://docs.sonarsource.com/sonarqube/9.8/analyzing-source-code/test-coverage/overview/) in the SonarQube documentation.
* Carefully review the specific language reference to make sure that you install the required binaries and dependencies, and that you publish your reports in the correct format.
* Note that there may be additional settings for downloads such as PDF reports that are required. For example, because [a Temporary branch is created, administrators will need to enable the `Keep when inactive` setting](https://docs.sonarsource.com/sonarqube-server/10.4/project-administration/pdf-reports/#temporary-branches). Please refer to the SonarQube's documentation regarding these adjustments
  {% endhint %}

**Example: generate a Python coverage report**

Here's an example workflow for generating a Python 3.9 coverage report:

1. Add the **Run** step.
2. Set the **Image** to `python:3.9-alpine`.
3. Add commands to install `coverage` and any other dependencies required to generate the report.
4. Add a `coverage` command to generate a coverage report. The specific usage depends on the language and platform.
5. Add a second `coverage` command to convert the report to a SonarQube-compatible XML report.
6. If the Run step saves the coverage report to a non-default location, add the report path to [Additional CLI Flags](#additional-cli-flags) in your SonarQube scan step. For example: `-Dsonar.python.coverage.reportPaths=/shared/sonarqube/coverage.xml`.

Here's what the Run step looks like in YAML:

```yaml

- step:
    type: Run
    name: Run_Tests
    identifier: generate_python_coverage_report
    spec:
      connectorRef: account.harnessImage
      image: python:3.9-alpine
      shell: Sh
      command: |-
        # Install coverage and other
        # dependencies required by the code repo.
        pip install pytest-django pytest-cov 
        python3 -m pip install coverage
        pip install -r requirements.txt

        # Run coverage commands to generate a report
        # and then convert the report to XML.
        # This method ensures that SonarQube can ingest the resulting report.
        coverage run -m pytest **/tests 
        coverage xml

```

### Troubleshoot Sonar Scans <a href="#troubleshoot-sonar-scans" id="troubleshoot-sonar-scans"></a>

#### Can't generate SonarQube report due to shallow clone <a href="#cant-generate-sonarqube-report-due-to-shallow-clone" id="cant-generate-sonarqube-report-due-to-shallow-clone"></a>

* Error message: `Shallow clone detected, no blame information will be provided. You can convert to non-shallow with 'git fetch --unshallow'`
* Cause: If the [depth setting](/continuous-integration/use-harness-ci/use-harness-ci/codebase-configuration/create-and-configure-a-codebase.md#depth) in your pipeline's codebase configuration is shallow, SonarQube can't generate a report. This is a [known SonarQube issue](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/scm-integration/#known-issues).
* Solution: Change the `depth` to `0`.

#### Add the sonar.projectVersion to a Harness pipeline <a href="#add-the-sonarprojectversion-to-a-harness-pipeline" id="add-the-sonarprojectversion-to-a-harness-pipeline"></a>

In your SonarQube step, declare `-Dsonar.projectVersion` under [Additional CLI Flags](#additional-cli-flags).

#### SonarQube doesn't scan the main branch and pull request branches in the same pipeline <a href="#sonarqube-doesnt-scan-the-main-branch-and-pull-request-branches-in-the-same-pipeline" id="sonarqube-doesnt-scan-the-main-branch-and-pull-request-branches-in-the-same-pipeline"></a>

{% hint style="info" %}
Harness introduced a fix in [STO release 1.83.1](/release-notes/security-testing-orchestration.md#version-1831) to provide better support for orchestrated branch and pull-request scanning with SonarQube Enterprise.

* With this fix, the orchestration step always downloads results for the scanned branch or pull request.
* To scan a branch or pull request, select **Branch Scan** in [Scan Configuration](#scan-configuration). With this option selected, the step scans the branch or pull request specified in the pipeline execution.
  {% endhint %}

If SonarQube doesn't scan both the main branch and pull request (PR) branches within the same pipeline, it might indicate an issue with the pull request setup in SonarQube.

One potential solution involves configuring conditional arguments within the Harness Platform to handle PR and branch scan requests separately. To implement this solution, you can use [conditional executions](/harness-ai/use-harness-platform/pipelines/step-skip-condition-settings.md) to run specific steps based on whether it's a PR scan request or a branch scan request. For example, your conditional executions could use JEXL expressions with [codebase variables](/continuous-integration/use-harness-ci/use-harness-ci/codebase-configuration/built-in-cie-codebase-variables-reference.md) like `<+codebase.build.type>=="branch"` or `<+codebase.build.type>=="pr"`.

This approach ensures proper configuration and execution of SonarQube scans for both main and PR branches within your pipeline.
