3 * Copyright (C) 2009-2017 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.security.SecurityRealm;
29 import org.sonar.api.server.ServerSide;
30 import org.sonar.api.server.authentication.IdentityProvider;
31 import org.sonar.core.util.stream.MoreCollectors;
32 import org.sonar.process.systeminfo.Global;
33 import org.sonar.process.systeminfo.SystemInfoSection;
34 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
35 import org.sonar.server.authentication.IdentityProviderRepository;
36 import org.sonar.server.health.ClusterHealth;
37 import org.sonar.server.health.HealthChecker;
38 import org.sonar.server.platform.ServerIdLoader;
39 import org.sonar.server.user.SecurityRealmFactory;
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 ServerIdLoader serverIdLoader;
49 private final SecurityRealmFactory securityRealmFactory;
50 private final IdentityProviderRepository identityProviderRepository;
51 private final HealthChecker healthChecker;
53 public GlobalSystemSection(Configuration config, ServerIdLoader serverIdLoader, SecurityRealmFactory securityRealmFactory,
54 IdentityProviderRepository identityProviderRepository, HealthChecker healthChecker) {
56 this.serverIdLoader = serverIdLoader;
57 this.securityRealmFactory = securityRealmFactory;
58 this.identityProviderRepository = identityProviderRepository;
59 this.healthChecker = healthChecker;
63 public ProtobufSystemInfo.Section toProtobuf() {
64 ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
65 protobuf.setName("System");
67 ClusterHealth health = healthChecker.checkCluster();
69 setAttribute(protobuf, "Health", health.getHealth().getStatus().name());
70 setAttribute(protobuf, "Health Causes", health.getHealth().getCauses());
71 serverIdLoader.get().ifPresent(serverId -> {
72 setAttribute(protobuf, "Server ID", serverId.getId());
73 setAttribute(protobuf, "Server ID validated", serverId.isValid());
75 setAttribute(protobuf, "High Availability", true);
76 setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication());
77 addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders());
78 addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders());
79 setAttribute(protobuf, "Force authentication", getForceAuthentication());
80 return protobuf.build();
83 private List<String> getEnabledIdentityProviders() {
84 return identityProviderRepository.getAllEnabledAndSorted()
86 .filter(IdentityProvider::isEnabled)
87 .map(IdentityProvider::getName)
88 .collect(MoreCollectors.toList());
91 private List<String> getAllowsToSignUpEnabledIdentityProviders() {
92 return identityProviderRepository.getAllEnabledAndSorted()
94 .filter(IdentityProvider::isEnabled)
95 .filter(IdentityProvider::allowsUsersToSignUp)
96 .map(IdentityProvider::getName)
97 .collect(MoreCollectors.toList());
100 private boolean getForceAuthentication() {
101 return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(false);
104 private static void addIfNotEmpty(ProtobufSystemInfo.Section.Builder protobuf, String key, @Nullable List<String> values) {
105 if (values != null && !values.isEmpty()) {
106 setAttribute(protobuf, key, COMMA_JOINER.join(values));
111 private String getExternalUserAuthentication() {
112 SecurityRealm realm = securityRealmFactory.getRealm();
113 return realm == null ? null : realm.getName();