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;
/**
* 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<String> 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<String> 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<String> getScannerSettingKeys(DbSession dbSession) {
+ return concat(concat(loadLicenseKeys(), loadLicenseHashKeys(dbSession)),
SERVER_SETTING_KEYS.stream()).collect(toSet());
}
- private Stream<String> loadLicenseHashKeys() {
- return pluginRepository.getPluginInfos().stream()
- .map(PluginInfo::getKey)
- .map(key -> SONAR_PREFIX + key + LICENSE_HASH_SUFFIX);
+ private Stream<String> loadLicenseHashKeys(DbSession dbSession) {
+ return dbClient.propertiesDao().selectGlobalPropertiesByKeyQuery(dbSession, LICENSE_HASH_SUFFIX).stream().map(PropertyDto::getKey);
}
private Stream<String> loadLicenseKeys() {
.map(PropertyDefinition::key);
}
- Set<String> getScannerSettingKeys() {
- return scannerSettingKeys;
- }
-
- @Override
- public void stop() {
- // nothing to do
- }
}
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;
ValuesRequest valuesRequest = toWsRequest(request);
Optional<ComponentDto> component = loadComponent(dbSession, valuesRequest);
- Set<String> keys = loadKeys(valuesRequest);
+ Set<String> keys = loadKeys(dbSession, valuesRequest);
Map<String, String> keysToDisplayMap = getKeysToDisplayMap(keys);
List<Setting> settings = loadSettings(dbSession, component, keysToDisplayMap.keySet());
return new ValuesResponseBuilder(settings, component, keysToDisplayMap).build();
return builder.build();
}
- private Set<String> loadKeys(ValuesRequest valuesRequest) {
+ private Set<String> loadKeys(DbSession dbSession, ValuesRequest valuesRequest) {
List<String> 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<ComponentDto> loadComponent(DbSession dbSession, ValuesRequest valuesRequest) {
*/
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
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");
}
}
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;
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;
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;
@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));
}
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 {
return executeLargeInputs(keys, partitionKeys -> getMapper(session).selectByKeys(partitionKeys, componentId));
}
+ public List<PropertyDto> selectGlobalPropertiesByKeyQuery(DbSession session, String keyQuery) {
+ return getMapper(session).selectGlobalPropertiesByKeyQuery(buildLikeValue(keyQuery, WildcardPosition.BEFORE_AND_AFTER));
+ }
+
/**
* Saves the specified property and its value.
* <p>
List<PropertyDto> selectDescendantModuleProperties(@Param("moduleUuid") String moduleUuid, @Param(value = "scope") String scope,
@Param(value = "excludeDisabled") boolean excludeDisabled);
+ List<PropertyDto> 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);
</where>
</select>
+ <select id="selectGlobalPropertiesByKeyQuery" resultType="ScrapProperty">
+ select
+ <include refid="columnsToScrapPropertyDto"/>
+ from
+ properties p
+ where
+ p.resource_id is null
+ and p.user_id is null
+ and p.prop_key like #{textQuery,jdbcType=VARCHAR}
+ </select>
+
<insert id="insertAsEmpty" parameterType="Map" useGeneratedKeys="false">
insert into properties
(
.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();
}
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);