1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.auth.gitlab;
import com.google.common.base.Strings;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.sonar.api.PropertyType;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.auth.DevOpsPlatformSettings;
import org.sonar.db.alm.setting.ALM;
import static java.lang.String.valueOf;
import static org.sonar.api.PropertyType.BOOLEAN;
import static org.sonar.api.PropertyType.PASSWORD;
import static org.sonar.db.ce.CeTaskTypes.GITLAB_PROJECT_PERMISSIONS_PROVISIONING;
@ComputeEngineSide
public class GitLabSettings implements DevOpsPlatformSettings {
public static final String GITLAB_AUTH_ENABLED = "sonar.auth.gitlab.enabled";
public static final String GITLAB_AUTH_URL = "sonar.auth.gitlab.url";
public static final String GITLAB_AUTH_APPLICATION_ID = "sonar.auth.gitlab.applicationId.secured";
public static final String GITLAB_AUTH_SECRET = "sonar.auth.gitlab.secret.secured";
public static final String GITLAB_AUTH_ALLOW_USERS_TO_SIGNUP = "sonar.auth.gitlab.allowUsersToSignUp";
public static final String GITLAB_AUTH_ALLOWED_GROUPS = "sonar.auth.gitlab.allowedGroups";
public static final String GITLAB_AUTH_SYNC_USER_GROUPS = "sonar.auth.gitlab.groupsSync";
public static final String GITLAB_AUTH_PROVISIONING_TOKEN = "provisioning.gitlab.token.secured";
public static final String GITLAB_AUTH_PROVISIONING_ENABLED = "provisioning.gitlab.enabled";
public static final String GITLAB_USER_CONSENT_FOR_PERMISSION_PROVISIONING_REQUIRED = "sonar.auth.gitlab.userConsentForPermissionProvisioningRequired";
private static final String CATEGORY = "authentication";
private static final String SUBCATEGORY = "gitlab";
private final Configuration configuration;
public GitLabSettings(Configuration configuration) {
this.configuration = configuration;
}
public String url() {
String url = configuration.get(GITLAB_AUTH_URL).orElse(null);
if (url != null && url.endsWith("/")) {
return url.substring(0, url.length() - 1);
}
return url;
}
public String apiUrl() {
return url() + "/api/v4";
}
public String applicationId() {
return configuration.get(GITLAB_AUTH_APPLICATION_ID).orElse(null);
}
public String secret() {
return configuration.get(GITLAB_AUTH_SECRET).orElse(null);
}
public boolean isEnabled() {
return configuration.getBoolean(GITLAB_AUTH_ENABLED).orElse(false) && applicationId() != null && secret() != null;
}
public boolean allowUsersToSignUp() {
return configuration.getBoolean(GITLAB_AUTH_ALLOW_USERS_TO_SIGNUP).orElse(false);
}
public Set<String> allowedGroups() {
return Set.of(configuration.getStringArray(GITLAB_AUTH_ALLOWED_GROUPS));
}
public boolean syncUserGroups() {
return configuration.getBoolean(GITLAB_AUTH_SYNC_USER_GROUPS).orElse(false);
}
public String provisioningToken() {
return configuration.get(GITLAB_AUTH_PROVISIONING_TOKEN).map(Strings::emptyToNull).orElse(null);
}
@Override
public String getDevOpsPlatform() {
return ALM.GITLAB.getId();
}
@Override
public boolean isProvisioningEnabled() {
return isEnabled() && configuration.getBoolean(GITLAB_AUTH_PROVISIONING_ENABLED).orElse(false);
}
@Override
public boolean isProjectVisibilitySynchronizationActivated() {
return true;
}
@Override
public boolean isUserConsentRequiredAfterUpgrade() {
return configuration.getBoolean(GITLAB_USER_CONSENT_FOR_PERMISSION_PROVISIONING_REQUIRED).isPresent();
}
@Override
public String getProjectsPermissionsProvisioningTaskName() {
return GITLAB_PROJECT_PERMISSIONS_PROVISIONING;
}
public boolean isAllowedGroup(String group) {
return allowedGroups().stream().anyMatch(allowedGroup -> isExactGroupOrParentGroup(group, allowedGroup));
}
private static boolean isExactGroupOrParentGroup(String group, String allowedGroup) {
return group.equals(allowedGroup) || group.startsWith(allowedGroup + "/");
}
static List<PropertyDefinition> definitions() {
return Arrays.asList(
PropertyDefinition.builder(GITLAB_AUTH_ENABLED)
.name("Enabled")
.description("Enable Gitlab users to login. Value is ignored if URL, Application ID, and Secret are not set.")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.type(BOOLEAN)
.defaultValue(valueOf(false))
.index(1)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_URL)
.name("GitLab URL")
.description("Base URL to access GitLab. https://gitlab.com for Gitlab SaaS")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.defaultValue("https://gitlab.com")
.index(2)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_APPLICATION_ID)
.name("Application ID")
.description("Application ID provided by GitLab when registering the application.")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.index(3)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_SECRET)
.name("Secret")
.description("Secret provided by GitLab when registering the application.")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.type(PASSWORD)
.index(4)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_ALLOW_USERS_TO_SIGNUP)
.name("Allow users to sign up")
.description("Allow new users to authenticate. When set to disabled, only existing users will be able to authenticate to the server.")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.type(BOOLEAN)
.defaultValue(valueOf(true))
.index(5)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_ALLOWED_GROUPS)
.name("Allowed groups")
.description("Only members of these groups (and sub-groups) will be allowed to authenticate. " +
"Please enter the group slug as it appears in the GitLab URL, for instance `my-gitlab-group`. " +
"If you use Auto-provisioning, only members of these groups (and sub-groups) will be provisioned")
.multiValues(true)
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.index(6)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_SYNC_USER_GROUPS)
.deprecatedKey("sonar.auth.gitlab.sync_user_groups")
.name("Synchronize user groups")
.description("For each GitLab group they belong to, the user will be associated to a group with the same name (if it exists) in SonarQube." +
" If enabled, the GitLab OAuth 2 application will need to provide the api scope.")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.type(PropertyType.BOOLEAN)
.defaultValue(valueOf(false))
.index(7)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_PROVISIONING_TOKEN)
.name("Provisioning token")
.description("Token used for user provisioning." +
" You can either use a group or a personal access token, as long as it has visibility on the groups that need to be imported.")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.type(PASSWORD)
.index(8)
.build(),
PropertyDefinition.builder(GITLAB_AUTH_PROVISIONING_ENABLED)
.name("Provisioning enabled")
.description("Enable Gitlab provisioning for users.")
.category(CATEGORY)
.subCategory(SUBCATEGORY)
.type(BOOLEAN)
.defaultValue(valueOf(false))
.index(9)
.build());
}
}
|