3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.monitoring.cluster;
22 import com.google.common.base.Joiner;
23 import java.util.List;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.sonar.api.CoreProperties;
27 import org.sonar.api.config.Configuration;
28 import org.sonar.api.platform.Server;
29 import org.sonar.api.security.SecurityRealm;
30 import org.sonar.api.server.ServerSide;
31 import org.sonar.api.server.authentication.IdentityProvider;
32 import org.sonar.core.util.stream.MoreCollectors;
33 import org.sonar.process.systeminfo.Global;
34 import org.sonar.process.systeminfo.SystemInfoSection;
35 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
36 import org.sonar.server.authentication.IdentityProviderRepository;
37 import org.sonar.server.platform.DockerSupport;
38 import org.sonar.server.user.SecurityRealmFactory;
40 import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE;
41 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
44 public class GlobalSystemSection implements SystemInfoSection, Global {
45 private static final Joiner COMMA_JOINER = Joiner.on(", ");
47 private final Configuration config;
48 private final Server server;
49 private final SecurityRealmFactory securityRealmFactory;
50 private final IdentityProviderRepository identityProviderRepository;
51 private final DockerSupport dockerSupport;
53 public GlobalSystemSection(Configuration config, Server server, SecurityRealmFactory securityRealmFactory,
54 IdentityProviderRepository identityProviderRepository, DockerSupport dockerSupport) {
57 this.securityRealmFactory = securityRealmFactory;
58 this.identityProviderRepository = identityProviderRepository;
59 this.dockerSupport = dockerSupport;
63 public ProtobufSystemInfo.Section toProtobuf() {
64 ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
65 protobuf.setName("System");
67 setAttribute(protobuf, "Server ID", server.getId());
68 setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker());
69 setAttribute(protobuf, "High Availability", true);
70 setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication());
71 addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders());
72 addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders());
73 setAttribute(protobuf, "Force authentication", getForceAuthentication());
74 return protobuf.build();
77 private List<String> getEnabledIdentityProviders() {
78 return identityProviderRepository.getAllEnabledAndSorted()
80 .filter(IdentityProvider::isEnabled)
81 .map(IdentityProvider::getName)
82 .collect(MoreCollectors.toList());
85 private List<String> getAllowsToSignUpEnabledIdentityProviders() {
86 return identityProviderRepository.getAllEnabledAndSorted()
88 .filter(IdentityProvider::isEnabled)
89 .filter(IdentityProvider::allowsUsersToSignUp)
90 .map(IdentityProvider::getName)
91 .collect(MoreCollectors.toList());
94 private boolean getForceAuthentication() {
95 return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE);
98 private static void addIfNotEmpty(ProtobufSystemInfo.Section.Builder protobuf, String key, @Nullable List<String> values) {
99 if (values != null && !values.isEmpty()) {
100 setAttribute(protobuf, key, COMMA_JOINER.join(values));
105 private String getExternalUserAuthentication() {
106 SecurityRealm realm = securityRealmFactory.getRealm();
107 return realm == null ? null : realm.getName();