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;
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.authentication.IdentityProvider;
31 import org.sonar.core.util.stream.MoreCollectors;
32 import org.sonar.process.systeminfo.BaseSectionMBean;
33 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
34 import org.sonar.server.authentication.IdentityProviderRepository;
35 import org.sonar.server.log.ServerLogging;
36 import org.sonar.server.platform.DockerSupport;
37 import org.sonar.server.platform.OfficialDistribution;
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.ProcessProperties.Property.PATH_DATA;
42 import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
43 import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
44 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
46 public class StandaloneSystemSection extends BaseSectionMBean implements SystemSectionMBean {
48 private static final Joiner COMMA_JOINER = Joiner.on(", ");
50 private final Configuration config;
51 private final SecurityRealmFactory securityRealmFactory;
52 private final IdentityProviderRepository identityProviderRepository;
53 private final Server server;
54 private final ServerLogging serverLogging;
55 private final OfficialDistribution officialDistribution;
56 private final DockerSupport dockerSupport;
58 public StandaloneSystemSection(Configuration config, SecurityRealmFactory securityRealmFactory,
59 IdentityProviderRepository identityProviderRepository, Server server, ServerLogging serverLogging,
60 OfficialDistribution officialDistribution, DockerSupport dockerSupport) {
62 this.securityRealmFactory = securityRealmFactory;
63 this.identityProviderRepository = identityProviderRepository;
65 this.serverLogging = serverLogging;
66 this.officialDistribution = officialDistribution;
67 this.dockerSupport = dockerSupport;
71 public String getServerId() {
72 return server.getId();
76 public String getVersion() {
77 return server.getVersion();
81 public String getLogLevel() {
82 return serverLogging.getRootLoggerLevel().name();
86 private String getExternalUserAuthentication() {
87 SecurityRealm realm = securityRealmFactory.getRealm();
88 return realm == null ? null : realm.getName();
91 private List<String> getEnabledIdentityProviders() {
92 return identityProviderRepository.getAllEnabledAndSorted()
94 .filter(IdentityProvider::isEnabled)
95 .map(IdentityProvider::getName)
96 .collect(MoreCollectors.toList());
99 private List<String> getAllowsToSignUpEnabledIdentityProviders() {
100 return identityProviderRepository.getAllEnabledAndSorted()
102 .filter(IdentityProvider::isEnabled)
103 .filter(IdentityProvider::allowsUsersToSignUp)
104 .map(IdentityProvider::getName)
105 .collect(MoreCollectors.toList());
108 private boolean getForceAuthentication() {
109 return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(CORE_FORCE_AUTHENTICATION_DEFAULT_VALUE);
113 public String name() {
119 public ProtobufSystemInfo.Section toProtobuf() {
120 ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
121 protobuf.setName("System");
123 setAttribute(protobuf, "Server ID", server.getId());
124 setAttribute(protobuf, "Version", getVersion());
125 setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker());
126 setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication());
127 addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders());
128 addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders());
129 setAttribute(protobuf, "High Availability", false);
130 setAttribute(protobuf, "Official Distribution", officialDistribution.check());
131 setAttribute(protobuf, "Force authentication", getForceAuthentication());
132 setAttribute(protobuf, "Home Dir", config.get(PATH_HOME.getKey()).orElse(null));
133 setAttribute(protobuf, "Data Dir", config.get(PATH_DATA.getKey()).orElse(null));
134 setAttribute(protobuf, "Temp Dir", config.get(PATH_TEMP.getKey()).orElse(null));
135 setAttribute(protobuf, "Processors", Runtime.getRuntime().availableProcessors());
136 return protobuf.build();
139 private static void addIfNotEmpty(ProtobufSystemInfo.Section.Builder protobuf, String key, @Nullable List<String> values) {
140 if (values != null && !values.isEmpty()) {
141 setAttribute(protobuf, key, COMMA_JOINER.join(values));