@@ -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); | |||
} |
@@ -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) { |
@@ -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; | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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()))); | |||
} | |||
} |
@@ -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()); |
@@ -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); |
@@ -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()) |