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