You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitLabSettings.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth.gitlab;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import org.sonar.api.CoreProperties;
  24. import org.sonar.api.PropertyType;
  25. import org.sonar.api.config.Configuration;
  26. import org.sonar.api.config.PropertyDefinition;
  27. import static java.lang.String.valueOf;
  28. import static org.sonar.api.PropertyType.BOOLEAN;
  29. public class GitLabSettings {
  30. static final String GITLAB_AUTH_ENABLED = "sonar.auth.gitlab.enabled";
  31. static final String GITLAB_AUTH_URL = "sonar.auth.gitlab.url";
  32. static final String GITLAB_AUTH_APPLICATION_ID = "sonar.auth.gitlab.applicationId";
  33. static final String GITLAB_AUTH_SECRET = "sonar.auth.gitlab.secret";
  34. static final String GITLAB_AUTH_ALLOW_USERS_TO_SIGNUP = "sonar.auth.gitlab.allowUsersToSignUp";
  35. static final String GITLAB_AUTH_SYNC_USER_GROUPS = "sonar.auth.gitlab.groupsSync";
  36. private static final String CATEGORY = CoreProperties.CATEGORY_ALM_INTEGRATION;
  37. private static final String SUBCATEGORY = "gitlab";
  38. private final Configuration configuration;
  39. public GitLabSettings(Configuration configuration) {
  40. this.configuration = configuration;
  41. }
  42. public String url() {
  43. String url = configuration.get(GITLAB_AUTH_URL).orElse(null);
  44. if (url != null && url.endsWith("/")) {
  45. return url.substring(0, url.length() - 1);
  46. }
  47. return url;
  48. }
  49. public String applicationId() {
  50. return configuration.get(GITLAB_AUTH_APPLICATION_ID).orElse(null);
  51. }
  52. public String secret() {
  53. return configuration.get(GITLAB_AUTH_SECRET).orElse(null);
  54. }
  55. public boolean isEnabled() {
  56. return configuration.getBoolean(GITLAB_AUTH_ENABLED).orElse(false) && applicationId() != null && secret() != null;
  57. }
  58. public boolean allowUsersToSignUp() {
  59. return configuration.getBoolean(GITLAB_AUTH_ALLOW_USERS_TO_SIGNUP).orElse(false);
  60. }
  61. public boolean syncUserGroups() {
  62. return configuration.getBoolean(GITLAB_AUTH_SYNC_USER_GROUPS).orElse(false);
  63. }
  64. static List<PropertyDefinition> definitions() {
  65. return Arrays.asList(
  66. PropertyDefinition.builder(GITLAB_AUTH_ENABLED)
  67. .name("Enabled")
  68. .description("Enable Gitlab users to login. Value is ignored if URL, Application ID, and Secret are not set.")
  69. .category(CATEGORY)
  70. .subCategory(SUBCATEGORY)
  71. .type(BOOLEAN)
  72. .defaultValue(valueOf(false))
  73. .index(1)
  74. .build(),
  75. PropertyDefinition.builder(GITLAB_AUTH_URL)
  76. .name("GitLab URL")
  77. .description("URL to access GitLab.")
  78. .category(CATEGORY)
  79. .subCategory(SUBCATEGORY)
  80. .defaultValue("https://gitlab.com")
  81. .index(2)
  82. .build(),
  83. PropertyDefinition.builder(GITLAB_AUTH_APPLICATION_ID)
  84. .name("Application ID")
  85. .description("Application ID provided by GitLab when registering the application.")
  86. .category(CATEGORY)
  87. .subCategory(SUBCATEGORY)
  88. .index(3)
  89. .build(),
  90. PropertyDefinition.builder(GITLAB_AUTH_SECRET)
  91. .name("Secret")
  92. .description("Secret provided by GitLab when registering the application.")
  93. .category(CATEGORY)
  94. .subCategory(SUBCATEGORY)
  95. .index(4)
  96. .build(),
  97. PropertyDefinition.builder(GITLAB_AUTH_ALLOW_USERS_TO_SIGNUP)
  98. .name("Allow users to sign-up")
  99. .description("Allow new users to authenticate. When set to 'false', only existing users will be able to authenticate to the server.")
  100. .category(CATEGORY)
  101. .subCategory(SUBCATEGORY)
  102. .type(BOOLEAN)
  103. .defaultValue(valueOf(true))
  104. .index(5)
  105. .build(),
  106. PropertyDefinition.builder(GITLAB_AUTH_SYNC_USER_GROUPS)
  107. .deprecatedKey("sonar.auth.gitlab.sync_user_groups")
  108. .name("Synchronize user groups")
  109. .description("For each GitLab group he belongs to, the user will be associated to a group with the same name (if it exists) in SonarQube. If enabled, the GitLab Oauth2 application will need to provide the api scope")
  110. .category(CATEGORY)
  111. .subCategory(SUBCATEGORY)
  112. .type(PropertyType.BOOLEAN)
  113. .defaultValue(valueOf(false))
  114. .index(6)
  115. .build());
  116. }
  117. }