Unable to generate Sonar API Token

In GitLab, we have a job that extends .sonar-scan-dotnet8-template which has been failing to complete on our RiskMetrics application for many months. The most recent pipeline has the following job to trigger our sonar scan:

code_scan_sonar:
  stage: code_scan
  extends: .sonar-scan-dotnet8-template
  #needs: [code_scan_synopsys]
  inherit:
    variables: true
  variables:
    APPLICATION_NAME: risk-metrics
    SONAR_BASE_DIR: "${CI_PROJECT_DIR}"
    SONAR_LANGUAGE: "dotnet"
    SONAR_PROJECT_KEY: RISKMETRICS:RiskMetrics
    SONAR_PROJECT_NAME: RiskMetrics
    SONAR_PROJECT_VERSION: "1.0.${CI_PIPELINE_IID}"
    DOTNET_SOLUTION_PATHS: Applications/RiskMetrics/RiskMetrics.sln
    NUGET_PACKAGE_SOURCES: "https://artifactory.us.bank-dns.com/artifactory/api/nuget/v3/riskportal-nuget-virtual"
    NUGET_CONFIG_PATH : "${CI_PROJECT_DIR}/.nuget/nuget.config"
    DOTNET_COLLECT_COVERAGE: "false"
    CARID: 8267
    ASSIGNMENT_GROUP: "APL_RTS RiskID (NonTOS)"

The job fails on every attempt, returning the following summary:

====================== Sonar - Scan : Finished ==========================

[200](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L200)Sonar Scan Overview00:00

[201](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L201)=============================

[202](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L202)Sonar Scan Overview

[203](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L203)=============================

[204](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L204)Total Execution Time = 1.6170035s

[205](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L205)Execution Successful = false

[206](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L206)Execution Skipped = false

[207](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L207)Validation Successful = false

[208](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L208)Validation Skipped = false

[209](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L209)Cleanup Successful = false

[210](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L210)Evidence Successful = false

[211](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L211)Evidence Skipped = false

[212](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L212)Persist State Successful = false

[213](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L213)Authorization Successful = false

[214](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L214)Authorization Skipped = false

[215](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L215)Metrics Submitted = false

[216](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L216)====================== Detailed Error Information ==========================

[217](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L217)Error Message = failed to inject dependencies: failed to create new sonar client: Unable to generate Sonar API Token. Returned HTTP status of:401

[218](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L218)Solution = Try a rerun and then contact the SonarQube team for further assistance if still failing

[219](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L219)Error Type = System

[220](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L220)Raw Error = unexpected status code of 401 received from endpoint. Response body: failed to read response body: unable to execute request (POST to sonarqube.us.bank-dns.com/api/user_tokens/generate)

[221](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L221)Function = gitlab.us.bank-dns.com/engineering/pipelinecli/pkg/httpclient/clients/sonar.parseGeneratedTokenResponse

[222](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L222)File = /builds/engineering/pipelinecli/pkg/httpclient/clients/sonar/client.go

[223](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L223)Line = 145

[224](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L224)====================================================
URLS:

[226](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L226)- Sonar URL: N/A

[227](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L227)Results:

[228](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L228)- Quality Gate Status: N/A

[229](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L229)- Failed Metrics: N/A

[230](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L230)- Metrics: N/A

[231](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L231)====================================================

[232](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L232)Job Failed00:00

[233](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L233){

[234](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L234) "Resource": {},

[235](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L235) "Attributes": {

[236](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L236) "action": "scan",

[237](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L237) "application.type": "service",

[238](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L238) "assignment.group": "APL_RTS RiskID (NonTOS)",

[239](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L239) "build.pipeline": "gitlab",

[240](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L240) "car.id": 8267,

[241](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L241) "ci.job.id": "28936574",

[242](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L242) "ci.job.name": "code_scan_sonar",

[243](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L243) "ci.job.url": "https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574",

[244](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L244) "ci.pipeline.id": "5428725",

[245](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L245) "ci.pipeline.trigger": "push",

[246](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L246) "ci.pipeline.url": "https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/pipelines/5428725",

[247](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L247) "ci.pipeline.user": "rigille",

[248](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L248) "cloud.provider": "azure",

[249](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L249) "cmdb.ci": "23ed44a11b4454d89a74ed317e4bcb5b",

[250](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L250) "cost.center": "03000001114",

[251](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L251) "data.classification": "internal",

[252](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L252) "deployment.environment": "all",

[253](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L253) "detailed.error": "\n====================== \u001b[1;94;4mDetailed Error Information\u001b[22;0;24m ==========================\n\n\u001b[91mError Message = failed to inject dependencies: failed to create new sonar client: Unable to generate Sonar API Token. Returned HTTP status of:401\nSolution = Try a rerun and then contact the SonarQube team for further assistance if still failing\nError Type = System\nRaw Error = unexpected status code of 401 received from endpoint. Response body: failed to read response body: unable to execute request (POST to sonarqube.us.bank-dns.com/api/user_tokens/generate)\nFunction = gitlab.us.bank-dns.com/engineering/pipelinecli/pkg/httpclient/clients/sonar.parseGeneratedTokenResponse\nFile = /builds/engineering/pipelinecli/pkg/httpclient/clients/sonar/client.go\nLine = 145\u001b[0m\n\n====================================================",

[254](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L254) "error": "unexpected status code of 401 received from endpoint. Response body: failed to read response body: unable to execute request (POST to sonarqube.us.bank-dns.com/api/user_tokens/generate)",

[255](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L255) "event.type": "job.lifecycle",

[256](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L256) "finops.key": "migrating-app",

[257](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L257) "gitlab.apiurl": "https://gitlab.us.bank-dns.com/api/v4",

[258](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L258) "gitlab.branch.name": "feature/jira6588-PipelineTechDebt-FixSonar",

[259](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L259) "gitlab.commit.ref.name": "feature/jira6588-PipelineTechDebt-FixSonar",

[260](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L260) "gitlab.commit.sha": "3ec14d0437116cc3523f5dda00aebbdb8961bdb9",

[261](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L261) "gitlab.commit.short.sha": "3ec14d04",

[262](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L262) "gitlab.project.id": "31843",

[263](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L263) "gitlab.project.name": "risk-metrics",

[264](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L264) "gitlab.project.path": "ERM_Portal/risk-metrics",

[265](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L265) "gitlab.project.url": "https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics",

[266](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L266) "gitlab.repo.languages": [

[267](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L267) "css",

[268](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L268) "c#",

[269](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L269) "javascript",

[270](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L270) "tsql",

[271](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L271) "html+razor"

[272](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L272) ],

[273](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L273) "id": "28936574",

[274](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L274) "lob": "99990006421",

[275](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L275) "metta.application": "riskmetrics",

[276](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L276) "metta.component": "app",

[277](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L277) "pipeline.cli.version": "0.1.1-rc49",

[278](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L278) "plugin": "sonar",

[279](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L279) "source": "pipelinecli",

[280](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L280) "team.name": "riskmetrics",

[281](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L281) "tech.owner": "nmpitze"

[282](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L282) },

[283](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L283) "SeverityText": "INFO",

[284](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L284) "Body": "Job Failed",

[285](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L285) "InstrumentationScope": "gitlab.us.bank-dns.com/engineering/pipelinecli/pkg/plugin.(*Runner).Execute.func2.1",

[286](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L286) "Timestamp": 1745514661211,

[287](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L287) "SeverityNumber": 9

[288](https://gitlab.us.bank-dns.com/ERM_Portal/risk-metrics/-/jobs/28936574#L288) }

Please help to determine what needs to be updated to get our sonar scan to succeed.

Hi,

Why are you trying to generate a user token during the pipeline? The token should be generated well before the pipeline runs, and then made available to the pipeline to use.

 
HTH,
Ann

I have limited knowledge of how the YML file should be written to create a proper call to SonarQube. The job I have listed in my question is written almost identically to the same job that is succeeding for us in another project.

Can you help identify what change I should attempt to make so that it is generating the user token at the Right time?

Hi,

You should generate a token manually, independent of the pipeline. Then add it to your pipeline.

 
HTH,
Ann

I do not know how to accomplish either of those tasks, and the web link you provided does not make it clear what I need to do. Can you provide succinct, step-by-step instructions?

Hi,

Here are the docs on tokens.

 
HTH,
Ann

The links you are providing appear to assume a starting point of which I am unaware. Whether this is an application, or web-page, or something else, I cannot determine what it is referring to. The only item I have been working with is our .gitlab-ci.yml file, with the relelvant script that I am aware of written in this question.

The first step in your most recent link says “You can generate new tokens at User > My Account > Security .”

Where is this User menu(?) located?

Hi,

The “User” menu is in the SonarQube UI. Once you’ve logged in, click on your avatar at the top-right.

 
Ann

How do I get SonarQube UI and do I need to create an account, or should my company have a shared account for it, or is there a way to determine if I already have an account possibly associated with my employee identity?

Hi,

The SonarQube UI would be where you go to look at the results of successful analyses. You should probably talk to your SonarQube admin.

 
HTH,
Ann