API authentication issue with tokens - insufficient privileges

Hi,

I have noticed that each Sonarqube API call I make on projects endpoint using token previously generated on the UI are failing with “Insufficient privileges” message:

curl -u sqa_my-personal-token: https://mysonarqube.instance.com/api/projects/search?projects=my-project-key
{"errors":[{"msg":"Insufficient privileges"}]}

The user related to the token belongs to a group with sufficient rights normally.

I can even see, after having set up debug logs in Sonarqube application, that the user is correctly identified from the token:

2022.07.25 21:42:07 DEBUG web[AYI3T2pWX8YUvABtAAAA][auth.event] login success [method|BASIC_TOKEN][provider|LOCAL|local][IP|172.20.0.4|10.1.133.53][login|my-name17289

I have also generated a user token from admin account, and the API call still drew the “Insufficient privileges” message.

However, the API call did work using direct admin account credentials:

curl -u admin:admin-password https://mysonarqube.instance.com/api/projects/search?projects=my-project-key
{"paging":{"pageIndex":1,"pageSize":100,"total":0},"components":[]}

Please note the following elements about our Sonarqube installation:

  • Sonarqube version: Developer Edition Version 9.5 (build 56709)
  • Authentication: Azure Active Directory
  • Architecture: dockerized installation with Sonarqube running in a container behind a Nginx reverse proxy to handle the SSL, with a PostgreSQL 13 database

Below is the docker-compose.yml file used to manage this architecture;

version: "3"
services:
  sonarqube:
    image: sonarqube:9.5-developer
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonarqube
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar-password
      SONAR_WEB_JAVAOPTS: "-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError"
      SONAR_SEARCH_JAVAOPTS: "-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError"
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
      - /usr/lib/jvm/java-11-openjdk-amd64/lib/security/cacerts:/usr/lib/jvm/java-11-openjdk/lib/security/cacerts:ro
    ports:
      - "9000"
    networks:
      - sonarnet

  db:
    image: postgres:13
    networks:
      - sonarnet
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar-password
      POSTGRES_DB: sonarqube
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

  reverse_proxy:
    container_name: reverse_proxy
    depends_on:
      - sonarqube
    image: nginx
    networks:
      - sonarnet
    ports:
      - 80:80
      - 443:443
    restart: always
    volumes:
      - /var/opt/nginx/conf/nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - /etc/pki/my-cert.crt:/etc/ssl/certs/sonarqube.crt:ro
      - /etc/pki/my-cert.key:/etc/ssl/private/sonarqube.key:ro
      - /var/opt/nginx/log:/var/log/nginx

networks:
  sonarnet:
    driver: bridge

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  postgresql:
  postgresql_data:

Please also find the nginx reverse proxy configuration:

server {
  listen 80;
  listen [::]:80;
  server_name mysonarqube.instance.com;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  listen [::]:443 ssl;
  client_max_body_size 100M;

  client_body_in_file_only on;

  server_name mysonarqube.instance.com;

  ssl_certificate /etc/ssl/certs/sonarqube.crt;
  ssl_certificate_key /etc/ssl/private/sonarqube.key;

  access_log /var/log/nginx/sonarqube.access.log;
  error_log /var/log/nginx/sonarqube.error.log debug;

  location / {
    proxy_set_header Host              $host;
    proxy_set_header X-Real-IP         $remote_addr;
    #proxy_set_header X-Forwarded-By   $server_addr:$server_port;
    proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Authorization     $http_authorization;
    proxy_pass_header Authorization;

    proxy_pass http://sonarqube:9000;
    proxy_redirect off;
  }
}

Would this issue be related to the new token rights management (squ, sqp and sqa type) from 9.5 release ? Or is it related to nginx reverse proxy configuration which might remove some HTTP header elements, or the Azure authentication ?

Thanks.

Hi,

Welcome to the community!

Great question! What kind of token are you making the call with? Because unless it’s a user token, I would expect the call to fail, even if the user has permissions.

In fact, I just tested this twice, once with Global Admin via group rights & once with perms via individual rights. Both times worked, but I was using (freshly generated) user tokens.

 
Ann

1 Like

Hi Ann,

Thanks for your answer and indeed, I was using global analysis tokens (beginning with sqa_).
I have tested with a user token (beginning with squ_) and it is working:

curl -u squ_7069bbc29588713a5f5e50de63c507f6f27ab35f: https://mysonarqube.instance.com/api/projects/search?projects=my-project-id
{"paging":{"pageIndex":1,"pageSize":100,"total":0},"components":[]}

I think that I also need to use a user token in order to automatically create a non-existent project by launching a sonar-scanner command, isn’t it (I am using Gitlab integration FYI) ?

Thanks a lot for your help!

Hi,

Uhm… I’ll be honest & say I don’t know. I would expect that a Global Analysis Token would allow project creation on first analysis. I’ll have to test it, but I’ve got a lot of balls in the air at the moment. So if you get a chance to test it first, let me know.

 
:wink:
Ann

Hi,

My testing shows that a global analysis token doesn’t allow project creation. TBH, I’m a bit surprised by this and am planning to raise the question internally.

 
Ann

Hi,

Thanks for your analysis, currently I am using a user token in Gitlab and it is working, although the rights scope might be indeed a bit too extended for the use case (creating / updating a Sonarqube project).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.