From: Julien Lancelot Date: Tue, 21 Mar 2017 16:54:21 +0000 (+0100) Subject: SONAR-8954 return every license hashes in api/settings/values X-Git-Tag: 6.3.1~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F1831%2Fhead;p=sonarqube.git SONAR-8954 return every license hashes in api/settings/values --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ScannerSettings.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ScannerSettings.java index f0ffeb902d0..6fadbf2ad93 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ScannerSettings.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ScannerSettings.java @@ -22,11 +22,11 @@ package org.sonar.server.setting.ws; import com.google.common.collect.ImmutableSet; import java.util.Set; import java.util.stream.Stream; -import org.sonar.api.Startable; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.config.PropertyDefinitions; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginRepository; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.property.PropertyDto; import static java.util.stream.Collectors.toSet; import static java.util.stream.Stream.concat; @@ -39,31 +39,25 @@ import static org.sonar.server.setting.ws.SettingsWsSupport.LICENSE_HASH_SUFFIX; /** * This class returns the list of settings required on scanner side (licenses, license hashes, server ids, etc.) */ -public class ScannerSettings implements Startable { +public class ScannerSettings { - private static final String SONAR_PREFIX = "sonar."; private static final Set SERVER_SETTING_KEYS = ImmutableSet.of(PERMANENT_SERVER_ID, SERVER_STARTTIME, SERVER_ID); + private final DbClient dbClient; private final PropertyDefinitions propertyDefinitions; - private final PluginRepository pluginRepository; - private Set scannerSettingKeys; - - public ScannerSettings(PropertyDefinitions propertyDefinitions, PluginRepository pluginRepository) { + public ScannerSettings(DbClient dbClient, PropertyDefinitions propertyDefinitions) { + this.dbClient = dbClient; this.propertyDefinitions = propertyDefinitions; - this.pluginRepository = pluginRepository; } - @Override - public void start() { - this.scannerSettingKeys = concat(concat(loadLicenseKeys(), loadLicenseHashKeys()), + Set getScannerSettingKeys(DbSession dbSession) { + return concat(concat(loadLicenseKeys(), loadLicenseHashKeys(dbSession)), SERVER_SETTING_KEYS.stream()).collect(toSet()); } - private Stream loadLicenseHashKeys() { - return pluginRepository.getPluginInfos().stream() - .map(PluginInfo::getKey) - .map(key -> SONAR_PREFIX + key + LICENSE_HASH_SUFFIX); + private Stream loadLicenseHashKeys(DbSession dbSession) { + return dbClient.propertiesDao().selectGlobalPropertiesByKeyQuery(dbSession, LICENSE_HASH_SUFFIX).stream().map(PropertyDto::getKey); } private Stream loadLicenseKeys() { @@ -73,12 +67,4 @@ public class ScannerSettings implements Startable { .map(PropertyDefinition::key); } - Set getScannerSettingKeys() { - return scannerSettingKeys; - } - - @Override - public void stop() { - // nothing to do - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ValuesAction.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ValuesAction.java index bbf39457fea..836e944cb1b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ValuesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ValuesAction.java @@ -69,7 +69,7 @@ public class ValuesAction implements SettingsWsAction { private final ScannerSettings scannerSettings; public ValuesAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, PropertyDefinitions propertyDefinitions, SettingsFinder settingsFinder, - SettingsWsSupport settingsWsSupport, ScannerSettings scannerSettings) { + SettingsWsSupport settingsWsSupport, ScannerSettings scannerSettings) { this.dbClient = dbClient; this.componentFinder = componentFinder; this.userSession = userSession; @@ -113,7 +113,7 @@ public class ValuesAction implements SettingsWsAction { ValuesRequest valuesRequest = toWsRequest(request); Optional component = loadComponent(dbSession, valuesRequest); - Set keys = loadKeys(valuesRequest); + Set keys = loadKeys(dbSession, valuesRequest); Map keysToDisplayMap = getKeysToDisplayMap(keys); List settings = loadSettings(dbSession, component, keysToDisplayMap.keySet()); return new ValuesResponseBuilder(settings, component, keysToDisplayMap).build(); @@ -129,12 +129,12 @@ public class ValuesAction implements SettingsWsAction { return builder.build(); } - private Set loadKeys(ValuesRequest valuesRequest) { + private Set loadKeys(DbSession dbSession, ValuesRequest valuesRequest) { List keys = valuesRequest.getKeys(); - if (!keys.isEmpty()) { - return new HashSet<>(keys); + if (keys.isEmpty()) { + return concat(propertyDefinitions.getAll().stream().map(PropertyDefinition::key), scannerSettings.getScannerSettingKeys(dbSession).stream()).collect(Collectors.toSet()); } - return concat(propertyDefinitions.getAll().stream().map(PropertyDefinition::key), scannerSettings.getScannerSettingKeys().stream()).collect(Collectors.toSet()); + return new HashSet<>(keys); } private Optional loadComponent(DbSession dbSession, ValuesRequest valuesRequest) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ScannerSettingsTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ScannerSettingsTest.java index 8b8dcfd9957..034d81e613c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ScannerSettingsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ScannerSettingsTest.java @@ -19,44 +19,41 @@ */ package org.sonar.server.setting.ws; +import org.junit.Rule; import org.junit.Test; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.config.PropertyDefinitions; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginRepository; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.sonar.api.PropertyType.LICENSE; +import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto; public class ScannerSettingsTest { + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + private PropertyDefinitions definitions = new PropertyDefinitions(); - private PluginRepository repository = mock(PluginRepository.class); - private ScannerSettings underTest = new ScannerSettings(definitions, repository); + private ScannerSettings underTest = new ScannerSettings(db.getDbClient(), definitions); @Test public void return_license_keys() throws Exception { definitions.addComponents(asList( PropertyDefinition.builder("foo").build(), PropertyDefinition.builder("myplugin.license.secured").type(LICENSE).build())); - underTest.start(); - assertThat(underTest.getScannerSettingKeys()).contains("myplugin.license.secured"); + assertThat(underTest.getScannerSettingKeys(db.getSession())).contains("myplugin.license.secured"); } @Test public void return_license_hash_keys() throws Exception { - PluginInfo pluginInfo = mock(PluginInfo.class); - when(pluginInfo.getKey()).thenReturn("myplugin"); - when(repository.getPluginInfos()).thenReturn(singletonList(pluginInfo)); - underTest.start(); + db.properties().insertProperty(newGlobalPropertyDto("sonar.myplugin.licenseHash.secured", "hash")); - assertThat(underTest.getScannerSettingKeys()).contains("sonar.myplugin.licenseHash.secured"); + assertThat(underTest.getScannerSettingKeys(db.getSession())).contains("sonar.myplugin.licenseHash.secured"); } @Test @@ -64,8 +61,7 @@ public class ScannerSettingsTest { definitions.addComponents(asList( PropertyDefinition.builder("foo").build(), PropertyDefinition.builder("myplugin.license.secured").type(LICENSE).build())); - underTest.start(); - assertThat(underTest.getScannerSettingKeys()).contains("sonar.server_id", "sonar.core.id", "sonar.core.startTime"); + assertThat(underTest.getScannerSettingKeys(db.getSession())).contains("sonar.server_id", "sonar.core.id", "sonar.core.startTime"); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ValuesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ValuesActionTest.java index 02c71e58833..cffd4f4c897 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ValuesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ValuesActionTest.java @@ -34,8 +34,6 @@ import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.PropertyFieldDefinition; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginRepository; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDbTester; @@ -55,11 +53,8 @@ import org.sonarqube.ws.Settings; import org.sonarqube.ws.Settings.ValuesWsResponse; import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.assertj.core.api.Java6Assertions.assertThat; import static org.assertj.core.groups.Tuple.tuple; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.sonar.api.PropertyType.LICENSE; import static org.sonar.api.resources.Qualifiers.MODULE; import static org.sonar.api.resources.Qualifiers.PROJECT; @@ -93,8 +88,7 @@ public class ValuesActionTest { private ComponentDbTester componentDb = new ComponentDbTester(db); private PropertyDefinitions definitions = new PropertyDefinitions(); private SettingsFinder settingsFinder = new SettingsFinder(dbClient, definitions); - private PluginRepository repository = mock(PluginRepository.class); - private ScannerSettings scannerSettings = new ScannerSettings(definitions, repository); + private ScannerSettings scannerSettings = new ScannerSettings(db.getDbClient(), definitions); private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private SettingsWsSupport support = new SettingsWsSupport(defaultOrganizationProvider, userSession); private ComponentDto project; @@ -104,10 +98,6 @@ public class ValuesActionTest { @Before public void setUp() throws Exception { - PluginInfo pluginInfo = mock(PluginInfo.class); - when(pluginInfo.getKey()).thenReturn("plugin"); - when(repository.getPluginInfos()).thenReturn(singletonList(pluginInfo)); - scannerSettings.start(); OrganizationDto organizationDto = db.organizations().insert(); project = componentDb.insertComponent(newProjectDto(organizationDto)); } diff --git a/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java b/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java index 0f6bfe392af..80aaa2219a8 100644 --- a/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java +++ b/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java @@ -36,8 +36,10 @@ import org.sonar.db.Dao; import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; +import org.sonar.db.WildcardPosition; import static com.google.common.base.Preconditions.checkArgument; +import static org.sonar.db.DatabaseUtils.buildLikeValue; import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class PropertiesDao implements Dao { @@ -170,6 +172,10 @@ public class PropertiesDao implements Dao { return executeLargeInputs(keys, partitionKeys -> getMapper(session).selectByKeys(partitionKeys, componentId)); } + public List selectGlobalPropertiesByKeyQuery(DbSession session, String keyQuery) { + return getMapper(session).selectGlobalPropertiesByKeyQuery(buildLikeValue(keyQuery, WildcardPosition.BEFORE_AND_AFTER)); + } + /** * Saves the specified property and its value. *

diff --git a/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java b/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java index e7a5b25d5b0..5cd2d6cf5ce 100644 --- a/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java @@ -46,6 +46,8 @@ public interface PropertiesMapper { List selectDescendantModuleProperties(@Param("moduleUuid") String moduleUuid, @Param(value = "scope") String scope, @Param(value = "excludeDisabled") boolean excludeDisabled); + List selectGlobalPropertiesByKeyQuery(@Param("textQuery") String textQuery); + void insertAsEmpty(@Param("key") String key, @Nullable @Param("userId") Long userId, @Nullable @Param("componentId") Long componentId, @Param("now") long now); diff --git a/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml b/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml index 8feebbb8c7c..64c4b41201e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml @@ -181,6 +181,17 @@ + + insert into properties ( diff --git a/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java b/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java index 570c7d96364..f568dd8a63a 100644 --- a/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java @@ -444,8 +444,7 @@ public class PropertiesDaoTest { .extracting("key", "resourceId").containsOnly( tuple(key, project.getId()), tuple(key, project2.getId()), - tuple(anotherKey, project2.getId()) - ); + tuple(anotherKey, project2.getId())); assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(123456789L))).isEmpty(); } @@ -484,6 +483,24 @@ public class PropertiesDaoTest { assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("unknown"), newHashSet(123456789L))).isEmpty(); } + @Test + public void select_global_properties_by_key_query() throws SQLException { + // global + insertProperty("sonar.plugin1.licenseHash.secured", "one", null, null); + insertProperty("sonar.plugin2.licenseHash.secured", "two", null, null); + // on component and user + insertProperty("sonar.plugin1.licenseHash.secure", "one", 10L, null); + insertProperty("sonar.plugin1.licenseHash.secure", "two", 10L, 100L); + + assertThat(underTest.selectGlobalPropertiesByKeyQuery(dbTester.getSession(), ".licenseHash.secured")).extracting(PropertyDto::getKey, PropertyDto::getValue) + .containsOnly(tuple("sonar.plugin1.licenseHash.secured", "one"), tuple("sonar.plugin2.licenseHash.secured", "two")); + assertThat(underTest.selectGlobalPropertiesByKeyQuery(dbTester.getSession(), "plugin1.licenseHash.secured")).extracting(PropertyDto::getKey, PropertyDto::getValue) + .containsOnly(tuple("sonar.plugin1.licenseHash.secured", "one")); + assertThat(underTest.selectGlobalPropertiesByKeyQuery(dbTester.getSession(), "plugin1")).extracting(PropertyDto::getKey, PropertyDto::getValue) + .containsOnly(tuple("sonar.plugin1.licenseHash.secured", "one")); + assertThat(underTest.selectGlobalPropertiesByKeyQuery(dbTester.getSession(), "unknown")).isEmpty(); + } + @Test public void saveProperty_inserts_global_properties_when_they_do_not_exist_in_db() { when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5);