Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent accidental misuse of Sonar token #109

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/actions/perform-static-analysis/action.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
name: "Perform static analysis"
description: "Perform static analysis"
inputs:
sonar_organisation_key:
description: "Sonar organisation key, used to identify the project"
required: false
sonar_project_key:
description: "Sonar project key, used to identify the project"
required: false
sonar_token:
description: "Sonar token API key"
description: "Sonar token, the API key"
required: false
jacobgill1 marked this conversation as resolved.
Show resolved Hide resolved
runs:
using: "composite"
Expand All @@ -16,5 +22,7 @@ runs:
if: steps.check.outputs.secret_exist == 'true'
run: |
export BRANCH_NAME=${GITHUB_HEAD_REF:-$(echo $GITHUB_REF | sed 's#refs/heads/##')}
export SONAR_ORGANISATION_KEY=${{ inputs.sonar_organisation_key }}
export SONAR_PROJECT_KEY=${{ inputs.sonar_project_key }}
export SONAR_TOKEN=${{ inputs.sonar_token }}
./scripts/reports/perform-static-analysis.sh
2 changes: 2 additions & 0 deletions .github/workflows/stage-2-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@ jobs:
- name: "Perform static analysis"
uses: ./.github/actions/perform-static-analysis
with:
sonar_organisation_key: "${{ secrets.SONAR_ORGANISATION_KEY }}"
sonar_project_key: "${{ secrets.SONAR_PROJECT_KEY }}"
sonar_token: "${{ secrets.SONAR_TOKEN }}"
9 changes: 5 additions & 4 deletions docs/user-guides/Perform_static_analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ Static code analysis is an essential part of modern software development. It pro
- Create your [SonarCloud](https://sonarcloud.io) project
- Navigate to project `Administration > Analysis Method > Manually` and select `Other (for JS, TS, Go, Python, PHP, ...)`
- In the [sonar-scanner.properties](../../scripts/config/sonar-scanner.properties) file, set the following properties according to the information provided above
- `sonar.organization`
- `sonar.projectKey`
- `sonar.[language].[coverage-tool].reportPaths` to ensure the unit test coverage is reported back to Sonar
- Follow the documentation on [creating encrypted secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) to add the `SONAR_TOKEN` secret to your repository. The GitHub action is already configured to fetch that secret and pass it as a variable
- Do not set the `sonar.organization` and `sonar.projectKey` properties in this file; do the next step instead
- Follow the documentation on [creating encrypted secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) to add the `SONAR_TOKEN` secret to your repository. The GitHub action is already configured to fetch that secret and pass it as a variable. In addition to that:
- Add `SONAR_ORGANISATION_KEY` secret
- Add `SONAR_PROJECT_KEY` secret
- Navigate to project `Administration > Analysis Method` and turn off the `Automatic Analysis` option
- Please, refrain from adding your repository to the GitHub SonarCloud App. Doing so will duplicate reports and initiate them outside the primary pipeline workflow
- Confirm that the GitHub action is part of your GitHub CI/CD workflow and enforces the "Sonar Way" quality gates. You can find more information about this in the [NHSE Software Engineering Quality Framework](https://github.com/NHSDigital/software-engineering-quality-framework/blob/main/tools/sonarqube.md)
Expand All @@ -37,6 +38,6 @@ Static code analysis is an essential part of modern software development. It pro
You can run and test static analysis locally on a developer's workstation using the following command

```shell
export SONAR_TOKEN=1234567890abcdef1234567890abcdef12345678
export SONAR_TOKEN=[replace-with-your-sonar-token]
./scripts/perform-static-analysis.sh
```
4 changes: 2 additions & 2 deletions scripts/config/sonar-scanner.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Please, DO NOT set the following properties `sonar.organization` and `sonar.projectKey` in this file. They must be stored as `SONAR_ORGANISATION_KEY` and `SONAR_PROJECT_KEY` GitHub secrets.

sonar.host.url=https://sonarcloud.io
sonar.organization=nhs-england-tools
sonar.projectKey=repository-template
sonar.qualitygate.wait=true
sonar.sourceEncoding=UTF-8
sonar.sources=.
Expand Down
2 changes: 2 additions & 0 deletions scripts/reports/perform-static-analysis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ function create-report() {
sonarsource/sonar-scanner-cli:$image_version \
-Dproject.settings=/usr/src/scripts/config/sonar-scanner.properties \
-Dsonar.branch.name="${BRANCH_NAME:-$(git rev-parse --abbrev-ref HEAD)}" \
-Dsonar.organization="$(echo $SONAR_ORGANISATION_KEY)" \
-Dsonar.projectKey="$(echo $SONAR_PROJECT_KEY)" \
-Dsonar.token="$(echo $SONAR_TOKEN)"
}

Expand Down
Loading