SonarSource/sonarqube · warning · BadRequestException

GitLab configuration already exists. Only one Gitlab…

Error message

GitLab configuration already exists. Only one Gitlab configuration is supported.

What it means

GitlabConfigurationService allows only one GitLab configuration. throwIfConfigurationAlreadyExists checks for an existing GITLAB_AUTH_ENABLED global property; if present, createConfiguration aborts with BadRequestException (HTTP 400) saying only one configuration is supported. This prevents duplicate/conflicting GitLab global configs.

Solutions

  1. Check existence first (getConfiguration) and call updateConfiguration instead of createConfiguration
  2. Catch the 400 BadRequestException and fall back to an update flow
  3. Make IaC/scripts idempotent: create only if the GitLab config API returns 404
  4. Remove the existing GitLab configuration first if a clean re-create is intended

Example fix

// before
service.createConfiguration(cfg); // 400 if already exists
// after
try { service.createConfiguration(cfg); }
catch (BadRequestException e) { service.updateConfiguration(UNIQUE_GITLAB_CONFIGURATION_ID, cfg); }
Defensive patterns

Strategy: try-catch

Validate before calling

// upsert pattern
boolean exists;
try { client.getGitlabConfiguration(); exists = true; }
catch (NotFoundException e) { exists = false; }
if (exists) client.updateGitlabConfiguration(cfg); else client.createGitlabConfiguration(cfg);

Try / catch

try {
  service.createConfiguration(cfg);
} catch (BadRequestException e) {
  // already exists — switch to update instead of failing
  service.updateConfiguration(UNIQUE_GITLAB_CONFIGURATION_ID, cfg);
}

Prevention

When it happens

Trigger: POST/createConfiguration called when a GitLab configuration already exists (GITLAB_AUTH_ENABLED global property set) — e.g. double-clicking save, re-running an idempotent-unaware Terraform/script, or creating GitLab config after migration from ALM settings.

Common situations: Infrastructure-as-code re-apply without existence check; two admins configuring GitLab simultaneously; automation that creates instead of updates.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/330568432d4b32d7. Report an issue: GitHub.

Appendix: source

Thrown at server/sonar-webserver-common/src/main/java/org/sonar/server/common/gitlab/config/GitlabConfigurationService.java:243

      setProperty(dbSession, GITLAB_AUTH_ALLOWED_GROUPS, String.join(",", configuration.allowedGroups()));
      setProperty(dbSession, GITLAB_AUTH_ALLOW_ALL_GROUPS, String.valueOf(configuration.allowAllGroups()));
      setProperty(dbSession, GITLAB_AUTH_PROVISIONING_ENABLED, String.valueOf(enableAutoProvisioning));
      setProperty(dbSession, GITLAB_AUTH_ALLOW_USERS_TO_SIGNUP, String.valueOf(configuration.allowUsersToSignUp()));
      setProperty(dbSession, GITLAB_AUTH_PROVISIONING_TOKEN, configuration.provisioningToken());
      if (enableAutoProvisioning) {
        triggerRun(configuration);
      }
      GitlabConfiguration createdConfiguration = getConfiguration(UNIQUE_GITLAB_CONFIGURATION_ID, dbSession);
      dbSession.commit();
      return createdConfiguration;
    }

  }

  private void throwIfConfigurationAlreadyExists() {
    Optional.ofNullable(dbClient.propertiesDao().selectGlobalProperty(GITLAB_AUTH_ENABLED)).ifPresent(property -> {
      throw BadRequestException.create("GitLab configuration already exists. Only one Gitlab configuration is supported.");
    });
  }

  private static void throwIfInvalidAllowedGroupConfigurationAndAutoProvisioning(ProvisioningType provisioningType, Set<String> allowedGroups, boolean allowAllGroups) {
    if (provisioningType == AUTO_PROVISIONING && allowedGroups.isEmpty() && !allowAllGroups) {
      throw new IllegalArgumentException("allowedGroups cannot be empty when Auto-provisioning is enabled and allowAllGroups is set to false.");
    }
  }

  private static void throwIfAllowAllGroupsAndJit(ProvisioningType provisioningType, boolean allowAllGroups) {
    if (allowAllGroups && provisioningType != AUTO_PROVISIONING) {
      throw new IllegalArgumentException("allowAllGroups can only be enabled when Auto-provisioning is enabled.");
    }
  }

  private static void throwIfAllowAllGroupsAndGitlabCloud(String url, boolean allowAllGroups) {
    if (allowAllGroups && isGitlabCloudUrl(url)) {
      throw new IllegalArgumentException(
        "allowAllGroups cannot be enabled when the GitLab URL is gitlab.com (GitLab SaaS). "

View on GitHub (pinned to 184c821202)