diff options
author | Pierre <pierre.guillot@sonarsource.com> | 2021-03-02 10:52:52 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-03-08 20:07:54 +0000 |
commit | 129ca5f3534a69799f5ea95975c3f0a49fbb2804 (patch) | |
tree | a47445e1d0f0e84b2a6a6d4aa85e517a474a77d7 | |
parent | d03ccfee3c9e9d5ab9583b02f1192a344d2b1147 (diff) | |
download | sonarqube-129ca5f3534a69799f5ea95975c3f0a49fbb2804.tar.gz sonarqube-129ca5f3534a69799f5ea95975c3f0a49fbb2804.zip |
SONAR-14519 add ALM info to system info file
8 files changed, 175 insertions, 7 deletions
diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java index 02ea532af16..3ece1b02483 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java @@ -34,6 +34,8 @@ public class AlmSettingsTesting { .setKey(randomAlphanumeric(200)) .setUrl(randomAlphanumeric(2000)) .setAppId(randomNumeric(8)) + .setClientId(randomNumeric(8)) + .setClientSecret(randomAlphanumeric(80)) .setPrivateKey(randomAlphanumeric(2000)) .setAlm(ALM.GITHUB); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java index ca2bc9a8c2b..ee41ff6c490 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/SystemInfoWriterModule.java @@ -22,6 +22,7 @@ package org.sonar.server.platform; import org.sonar.core.platform.Module; import org.sonar.process.systeminfo.JvmPropertiesSection; import org.sonar.process.systeminfo.JvmStateSection; +import org.sonar.server.platform.monitoring.AlmConfigurationSection; import org.sonar.server.platform.monitoring.DbConnectionSection; import org.sonar.server.platform.monitoring.DbSection; import org.sonar.server.platform.monitoring.EsIndexesSection; @@ -58,7 +59,8 @@ public class SystemInfoWriterModule extends Module { EsIndexesSection.class, LoggingSection.class, PluginsSection.class, - SettingsSection.class + SettingsSection.class, + AlmConfigurationSection.class ); if (standalone) { diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java new file mode 100644 index 00000000000..5af5cfa6f52 --- /dev/null +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/AlmConfigurationSection.java @@ -0,0 +1,76 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.monitoring; + +import java.util.List; +import org.sonar.api.server.ServerSide; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.alm.setting.AlmSettingDto; +import org.sonar.process.systeminfo.SystemInfoSection; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; + +import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; + +@ServerSide +public class AlmConfigurationSection implements SystemInfoSection { + private final DbClient dbClient; + + public AlmConfigurationSection(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public ProtobufSystemInfo.Section toProtobuf() { + ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); + protobuf.setName("ALMs"); + + try (DbSession dbSession = dbClient.openSession(false)) { + List<AlmSettingDto> almSettingDtos = dbClient.almSettingDao().selectAll(dbSession); + + for (AlmSettingDto almSettingDto : almSettingDtos) { + setAttribute(protobuf, almSettingDto.getKey(), buildValue(almSettingDto)); + } + } + + return protobuf.build(); + } + + private static String buildValue(AlmSettingDto almSettingDto) { + String value = String.format("alm:%s", almSettingDto.getRawAlm()); + if (almSettingDto.getUrl() != null) { + value += String.format(", url:%s", almSettingDto.getUrl()); + } + switch (almSettingDto.getAlm()) { + case GITHUB: + // add APP_ID and CLIENT_ID + value += String.format(", appId:%s, clientId:%s", almSettingDto.getAppId(), almSettingDto.getClientId()); + break; + case BITBUCKET_CLOUD: + // WORKSPACE ID & OAuth key + value += String.format(", workspace id:%s, OAuth Key:%s", almSettingDto.getAppId(), almSettingDto.getClientId()); + break; + default: + // no additional information for the other ALMs + break; + } + return value; + } +} diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java index 1965b19940f..4c190bebb06 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/SystemInfoWriterModuleTest.java @@ -42,7 +42,7 @@ public class SystemInfoWriterModuleTest { Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters(); assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 17); + .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 18); } @Test @@ -54,7 +54,7 @@ public class SystemInfoWriterModuleTest { Collection<ComponentAdapter<?>> adapters = container.getPicoContainer().getComponentAdapters(); assertThat(adapters) - .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 11); + .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 12); } } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java new file mode 100644 index 00000000000..08b858c2ee2 --- /dev/null +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/AlmConfigurationSectionTest.java @@ -0,0 +1,88 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.monitoring; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.DbTester; +import org.sonar.db.alm.setting.AlmSettingDto; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Attribute; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class AlmConfigurationSectionTest { + + @Rule + public DbTester db = DbTester.create(); + + private AlmConfigurationSection underTest = new AlmConfigurationSection(db.getDbClient()); + + @Test + public void alm_are_listed() { + AlmSettingDto azure = db.almSettings().insertAzureAlmSetting(); + AlmSettingDto github = db.almSettings().insertGitHubAlmSetting(); + AlmSettingDto gitlab = db.almSettings().insertGitlabAlmSetting(); + AlmSettingDto bitbucket = db.almSettings().insertBitbucketAlmSetting(); + AlmSettingDto bitbucketCloud = db.almSettings().insertBitbucketCloudAlmSetting(); + + ProtobufSystemInfo.Section section = underTest.toProtobuf(); + + assertThat(section.getAttributesList()).hasSize(5); + assertThat(section.getAttributesList()) + .extracting(Attribute::getKey, Attribute::getStringValue) + .containsExactlyInAnyOrder( + tuple(azure.getKey(), String.format("alm:%s, url:%s", azure.getRawAlm(), azure.getUrl())), + tuple(github.getKey(), String.format("alm:%s, url:%s, appId:%s, clientId:%s", github.getRawAlm(), github.getUrl(), github.getAppId(), github.getClientId())), + tuple(gitlab.getKey(), String.format("alm:%s, url:%s", gitlab.getRawAlm(), gitlab.getUrl())), + tuple(bitbucket.getKey(), String.format("alm:%s, url:%s", bitbucket.getRawAlm(), bitbucket.getUrl())), + tuple(bitbucketCloud.getKey(), String.format("alm:%s, workspace id:%s, OAuth Key:%s", bitbucketCloud.getRawAlm(), bitbucketCloud.getAppId(), bitbucketCloud.getClientId()))); + } + + @Test + public void several_alm_same_type() { + AlmSettingDto gitlab1 = db.almSettings().insertGitlabAlmSetting(); + AlmSettingDto gitlab2 = db.almSettings().insertGitlabAlmSetting(); + + ProtobufSystemInfo.Section section = underTest.toProtobuf(); + + assertThat(section.getAttributesList()).hasSize(2); + assertThat(section.getAttributesList()) + .extracting(Attribute::getKey, Attribute::getStringValue) + .containsExactlyInAnyOrder( + tuple(gitlab1.getKey(), String.format("alm:%s, url:%s", gitlab1.getRawAlm(), gitlab1.getUrl())), + tuple(gitlab2.getKey(), String.format("alm:%s, url:%s", gitlab2.getRawAlm(), gitlab2.getUrl()))); + } + + @Test + public void null_url_are_ignored() { + AlmSettingDto azure = db.almSettings().insertAzureAlmSetting(a -> a.setUrl(null)); + + ProtobufSystemInfo.Section section = underTest.toProtobuf(); + + assertThat(section.getAttributesList()).hasSize(1); + assertThat(section.getAttributesList()) + .extracting(Attribute::getKey, Attribute::getStringValue) + .containsExactlyInAnyOrder( + tuple(azure.getKey(), String.format("alm:%s", azure.getRawAlm()))); + } + +} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/GetGithubClientIdActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/GetGithubClientIdActionTest.java index 082c330b8f8..c935483a22d 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/GetGithubClientIdActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/github/GetGithubClientIdActionTest.java @@ -84,7 +84,7 @@ public class GetGithubClientIdActionTest { public void fail_when_client_id_does_not_exist() { UserDto user = db.users().insertUser(); userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS); - AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(); + AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(s -> s.setClientId(null)); TestRequest request = ws.newRequest() .setParam(GetGithubClientIdAction.PARAM_ALM_SETTING, githubAlmSetting.getKey()); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ListDefinitionsActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ListDefinitionsActionTest.java index ba8964dfe4b..f0e2007c365 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ListDefinitionsActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ListDefinitionsActionTest.java @@ -63,7 +63,7 @@ public class ListDefinitionsActionTest { public void list_github_settings() { UserDto user = db.users().insertUser(); userSession.logIn(user).setSystemAdministrator(); - AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting(); + AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting(s -> s.setClientId("")); AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_id").setClientSecret("client_secret")); ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java index 946d72f6a03..a5b6f47d117 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/ValidateActionTest.java @@ -138,7 +138,7 @@ public class ValidateActionTest { @Test public void github_validation_checks_missing_clientId() { - AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(settings -> settings.setClientSecret("clientSecret"))); + AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(s -> s.setClientId(null))); assertThatThrownBy(() -> ws.newRequest() .setParam("key", almSetting.getKey()) @@ -147,7 +147,7 @@ public class ValidateActionTest { @Test public void github_validation_checks_missing_clientSecret() { - AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(settings -> settings.setClientId("clientId"))); + AlmSettingDto almSetting = insertAlmSetting(db.almSettings().insertGitHubAlmSetting(s -> s.setClientSecret(null))); assertThatThrownBy(() -> ws.newRequest() .setParam("key", almSetting.getKey()) |