3 * Copyright (C) 2009-2020 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.process.ProcessProperties.Property.PATH_DATA;
40 import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
41 import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
42 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
44 public class StandaloneSystemSection extends BaseSectionMBean implements SystemSectionMBean {
46 private static final Joiner COMMA_JOINER = Joiner.on(", ");
48 private final Configuration config;
49 private final SecurityRealmFactory securityRealmFactory;
50 private final IdentityProviderRepository identityProviderRepository;
51 private final Server server;
52 private final ServerLogging serverLogging;
53 private final OfficialDistribution officialDistribution;
54 private final DockerSupport dockerSupport;
56 public StandaloneSystemSection(Configuration config, SecurityRealmFactory securityRealmFactory,
57 IdentityProviderRepository identityProviderRepository, Server server, ServerLogging serverLogging,
58 OfficialDistribution officialDistribution, DockerSupport dockerSupport) {
60 this.securityRealmFactory = securityRealmFactory;
61 this.identityProviderRepository = identityProviderRepository;
63 this.serverLogging = serverLogging;
64 this.officialDistribution = officialDistribution;
65 this.dockerSupport = dockerSupport;
69 public String getServerId() {
70 return server.getId();
74 public String getVersion() {
75 return server.getVersion();
79 public String getLogLevel() {
80 return serverLogging.getRootLoggerLevel().name();
84 private String getExternalUserAuthentication() {
85 SecurityRealm realm = securityRealmFactory.getRealm();
86 return realm == null ? null : realm.getName();
89 private List<String> getEnabledIdentityProviders() {
90 return identityProviderRepository.getAllEnabledAndSorted()
92 .filter(IdentityProvider::isEnabled)
93 .map(IdentityProvider::getName)
94 .collect(MoreCollectors.toList());
97 private List<String> getAllowsToSignUpEnabledIdentityProviders() {
98 return identityProviderRepository.getAllEnabledAndSorted()
100 .filter(IdentityProvider::isEnabled)
101 .filter(IdentityProvider::allowsUsersToSignUp)
102 .map(IdentityProvider::getName)
103 .collect(MoreCollectors.toList());
106 private boolean getForceAuthentication() {
107 return config.getBoolean(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY).orElse(false);
111 public String name() {
117 public ProtobufSystemInfo.Section toProtobuf() {
118 ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
119 protobuf.setName("System");
121 setAttribute(protobuf, "Server ID", server.getId());
122 setAttribute(protobuf, "Version", getVersion());
123 setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker());
124 setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication());
125 addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders());
126 addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up", getAllowsToSignUpEnabledIdentityProviders());
127 setAttribute(protobuf, "High Availability", false);
128 setAttribute(protobuf, "Official Distribution", officialDistribution.check());
129 setAttribute(protobuf, "Force authentication", getForceAuthentication());
130 setAttribute(protobuf, "Home Dir", config.get(PATH_HOME.getKey()).orElse(null));
131 setAttribute(protobuf, "Data Dir", config.get(PATH_DATA.getKey()).orElse(null));
132 setAttribute(protobuf, "Temp Dir", config.get(PATH_TEMP.getKey()).orElse(null));
133 setAttribute(protobuf, "Processors", Runtime.getRuntime().availableProcessors());
134 return protobuf.build();
137 private static void addIfNotEmpty(ProtobufSystemInfo.Section.Builder protobuf, String key, @Nullable List<String> values) {
138 if (values != null && !values.isEmpty()) {
139 setAttribute(protobuf, key, COMMA_JOINER.join(values));