SonarQube delegate authentication to GitLab fail [404 not found]

Context

  • Sonarqube: 9.2.4.50792 → https[://]sonar[.]dev[.]mycompany[.]com
  • Gitlab: 13.4.3-ee → http[://]git[.]mycomany[.]com

What I am trying to achieve:

I try to delegate SonarQube authentication to Gitlab Self-Hosted.

What I’ve done:

Following the official SonarQube documentation (GitLab Integration | SonarQube Docs), I’ve created a “GitLab OAuth app”:

  1. Went to git.mycompany.com/admin/applications
  2. New Application:
    • Name: SonarQube
    • Redirect URI: https[://]sonar[.]dev[.]mycompany[.]com/oauth2/callback/gitlab
    • Trusted: Checked
    • Confidential: Checked
    • Scopes: all checked, to be sure my issue was not an access issue. (I’ll tune it later)
  3. I have copied both Application ID and Secret to use later.
  4. Went to https[://]sonar[.]dev[.]mycompany[.]com/admin/settings?alm=gitlab&category=almintegration
  5. GitLab Authentication:
    • Enabled: Yes, of course
    • GitLab URL: http[://]git[.]mycomany[.]com/users/auth/gitlab/callback
    • Application ID: The one given by GitLab during step 2
    • Secret: The one given by GitLab during step 2
    • Allow users to sign-up: Enabled
    • Synchronize user groups: Enabled
  6. Disconnected to my admin account
  7. The new Log in with GitLab is well shown:

Log in with GitLab logo

  1. When I clicked on this new button, I was well redirected to my GitLab instance (URL: http[://]git[.]mycomany[.]com/users/sign_in).

  1. I was using my GitLab credentials then clicked on “Sign in”
  2. I’ve got a GitLab 404 error page:

The URL was:

http://git.mycomany.com/users/auth/gitlab/callback/oauth/authorize?response_type=code&client_id=<MY_APPLICATION_ID>&redirect_uri=http%3A%2F%2Fsonar.dev.mycompany.com%3A9000%2Foauth2%2Fcallback%2Fgitlab&scope=api&state=ai1rq82joi504ggv3nc1qa0h29

The redirect_uri seems to be constructed with my sonar.properties informations (html_encoded):

"http://" + sonar.web.host + ":" sonar.web.port + "oauth2/callback/gitlab"

Investigation:

While I’m reproducing this error, I’m following (tail -f) some log files:

root@sonar.dev.mycompany.com: tail -f /var/log/httpd/*_log /var/log/message /home/sonar/sonarqube/logs/*.log

As expected, there is no information regarding the error 404 on GitLab. On my GitLab server :

root@git.mycompany.com: tail -f /var/log/httpd/*_log /var/log/message /var/log/gitlab/*/current

And a message appears:

==> /var/log/gitlab/gitlab-workhorse/current <==
{
   "correlation_id":"ZUVztnRFFe9",
   "duration_ms":62,
   "host":"git.mycompany.com",
   "level":"info",
   "method":"GET",
   "msg":"access",
   "proto":"HTTP/1.1",
   "referrer":"",
   "remote_addr":"127.0.0.1:0",
   "remote_ip":"127.0.0.1",
   "status":404,
   "system":"http",
   "time":"2022-01-19T18:06:47+01:00",
   "uri":"/users/auth/gitlab/callback/oauth/authorize?response_type=code\u0026client_id=<MY_APPLICATION_ID>\u0026redirect_uri=http%3A%2F%2Fsonar.dev.mycompany.com%3A9000%2Foauth2%2Fcallback%2Fgitlab\u0026scope=api\u0026state=16r2cvc196v4fj8k0rv7oprs6h",
   "user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62",
   "written_bytes":28281
}

Is there anyone who can help me?

Note: In the next days, I’ll try to do my install process using Sonarqube 9.3.0.51899 and keeping the same version of GitLab EE

Note 2: Same question on stackoverflow: oauth 2.0 - SonarQube delegate authentication to GitLab fail [404 not found] - Stack Overflow

Hi,

Welcome to the community & congrats on such a thorough post!

Yes, that’s going to be the default value. Have you set your Server base URL (Administration → Server base URL)?

 
Ann

Hello Ann,

Thank you very much for your reply.

Indeed, the field “Server base URL” (sonar.core.serverBaseURL) was empty.
I have added my sonar instance URL https://sonar.dev.mycompany.com then saved parameters.

BTW, I’ve updated my sonarqube version to 9.3.0.51899 following the official doc part “Upgrading from the ZIP file”
No update planned regarding our GitLab-EE (unfortunately).

I’ve tried again after clearing the cache and I’ve the same result.

First step, when I’m on SonarQube login page, I click on the “Log in with GitLab” button then get this on GitLab log file (/var/log/gitlab/gitlab-workhorse/current)

{
   "correlation_id":"Ou37QVnzY49",
   "duration_ms":8,
   "host":"git.mycompany.com",
   "level":"info",
   "method":"GET",
   "msg":"access",
   "proto":"HTTP/1.1",
   "referrer":"",
   "remote_addr":"127.0.0.1:0",
   "remote_ip":"127.0.0.1",
   "status":302,
   "system":"http",
   "time":"2022-03-17T11:21:55+01:00",
   "uri":"/users/auth/gitlab/callback/oauth/authorize?response_type=code\u0026client_id=<MY_CLIENT_ID>\u0026redirect_uri=https%3A%2F%2Fsonar.dev.mycompany.com%2Foauth2%2Fcallback%2Fgitlab\u0026scope=api\u0026state=sbm9qg9lkdpsmm7char48rc1vv",
   "user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
   "written_bytes":98
}

{
   "correlation_id":"oMdWoCFkR22",
   "duration_ms":20,
   "host":"git.mycompany.com",
   "level":"info",
   "method":"GET",
   "msg":"access",
   "proto":"HTTP/1.1",
   "referrer":"",
   "remote_addr":"127.0.0.1:0",
   "remote_ip":"127.0.0.1",
   "status":200,
   "system":"http",
   "time":"2022-03-17T11:21:55+01:00",
   "uri":"/users/sign_in",
   "user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
   "written_bytes":10945
}

My browser show the GitLab login page, as expected, the current URL is http://git.mycompany.com/users/sign_in
I log in to my gitlab account using my LDAP credentials, then click on the green “Sign in” button to get the following log messages.

{
   "correlation_id":"YjEKOaeeml8",
   "duration_ms":58,
   "host":"git.mycompany.com",
   "level":"info",
   "method":"POST",
   "msg":"access",
   "proto":"HTTP/1.1",
   "referrer":"http://git.mycompany.com/users/sign_in",
   "remote_addr":"127.0.0.1:0",
   "remote_ip":"127.0.0.1",
   "status":302,
   "system":"http",
   "time":"2022-03-17T11:25:37+01:00",
   "uri":"/users/auth/ldapmain/callback",
   "user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
   "written_bytes":354
}

{
   "correlation_id":"CP7EXOJhV16",
   "duration_ms":15,
   "host":"git.mycompany.com",
   "level":"info",
   "method":"GET",
   "msg":"access",
   "proto":"HTTP/1.1",
   "referrer":"http://git.mycompany.com/users/sign_in",
   "remote_addr":"127.0.0.1:0",
   "remote_ip":"127.0.0.1",
   "status":404,
   "system":"http",
   "time":"2022-03-17T11:25:37+01:00",
   "uri":"/users/auth/gitlab/callback/oauth/authorize?response_type=code\u0026client_id=<MY_CLIENT_ID>\u0026redirect_uri=https%3A%2F%2Fsonar.dev.mycompany.com%2Foauth2%2Fcallback%2Fgitlab\u0026scope=api\u0026state=a4q92mie48oe2df1f62qkh4r9p",
   "user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
   "written_bytes":28281
}

{
   "correlation_id":"hW4NhFCJLV6",
   "encoding":"gzip",
   "file":"/opt/gitlab/embedded/service/gitlab-rails/public/assets/illustrations/error-404-4ac0f2ed92ff27c0f80853181c4dceb1858dac25d9e744d6594f067d60a45b48.svg",
   "level":"info",
   "method":"GET",
   "msg":"Send static file",
   "time":"2022-03-17T11:25:37+01:00",
   "uri":"/assets/illustrations/error-404-4ac0f2ed92ff27c0f80853181c4dceb1858dac25d9e744d6594f067d60a45b48.svg"
}


{
   "correlation_id":"hW4NhFCJLV6",
   "duration_ms":0,
   "host":"git.mycompany.com",
   "level":"info",
   "method":"GET",
   "msg":"access",
   "proto":"HTTP/1.1",
   "referrer":"http://git.mycompany.com/users/auth/gitlab/callback/oauth/authorize?response_type=code\u0026client_id=<MY_CLIENT_ID>\u0026redirect_uri=https%3A%2F%2Fsonar.dev.mycompany.com%2Foauth2%2Fcallback%2Fgitlab\u0026scope=api\u0026state=a4q92mie48oe2df1f62qkh4r9p",
   "remote_addr":"127.0.0.1:0",
   "remote_ip":"127.0.0.1",
   "status":200,
   "system":"http",
   "time":"2022-03-17T11:25:37+01:00",
   "uri":"/assets/illustrations/error-404-4ac0f2ed92ff27c0f80853181c4dceb1858dac25d9e744d6594f067d60a45b48.svg",
   "user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
   "written_bytes":3788
}

My issue seems to be more GitLab side than SonarQube side, isn’t it ?

I’ve just tried again but with a local gitlab-ee 14.8.4 to check if the issue is linked to our old GitLab 13.4.3-ee version.

Unfortunately, I’ve reach the same 404 error page :cry: