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