diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-10-11 18:42:33 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-10-16 09:36:55 +0200 |
commit | 36e1afc3d93ab23b01bb3911d311f157ef8c8fb0 (patch) | |
tree | e18b1c6c4f0cdac1ca9219ec41147be7aae9df1a /server | |
parent | bbec8759238610b63147422338ce4fdd23b53570 (diff) | |
download | sonarqube-36e1afc3d93ab23b01bb3911d311f157ef8c8fb0.tar.gz sonarqube-36e1afc3d93ab23b01bb3911d311f157ef8c8fb0.zip |
SONAR-9863 drop internal WS api/server_id and api/licenses
Diffstat (limited to 'server')
17 files changed, 0 insertions, 1449 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/license/ws/LicensesWs.java b/server/sonar-server/src/main/java/org/sonar/server/license/ws/LicensesWs.java deleted file mode 100644 index 58aafe35868..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/license/ws/LicensesWs.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.license.ws; - -import org.sonar.api.server.ws.WebService; - -import static org.sonarqube.ws.client.license.LicensesWsParameters.CONTROLLER_SETTINGS; - -public class LicensesWs implements WebService { - - private final ListAction listAction; - - public LicensesWs(ListAction listAction) { - this.listAction = listAction; - } - - @Override - public void define(Context context) { - NewController controller = context.createController(CONTROLLER_SETTINGS) - .setDescription("Manage licenses") - .setSince("6.1"); - listAction.define(controller); - controller.done(); - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/license/ws/LicensesWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/license/ws/LicensesWsModule.java deleted file mode 100644 index 5f965f7dd52..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/license/ws/LicensesWsModule.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.license.ws; - -import org.sonar.core.platform.Module; - -public class LicensesWsModule extends Module { - @Override - protected void configureModule() { - add( - LicensesWs.class, - ListAction.class); - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/license/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/license/ws/ListAction.java deleted file mode 100644 index 69cd1f677ee..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/license/ws/ListAction.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.license.ws; - -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.function.Function; -import javax.annotation.Nullable; -import org.sonar.api.config.License; -import org.sonar.api.config.PropertyDefinition; -import org.sonar.api.config.PropertyDefinitions; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.core.util.stream.MoreCollectors; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.server.setting.ws.Setting; -import org.sonar.server.setting.ws.SettingsFinder; -import org.sonar.server.user.UserSession; -import org.sonar.server.ws.WsAction; -import org.sonarqube.ws.Licenses; -import org.sonarqube.ws.Licenses.ListWsResponse; - -import static com.google.common.base.Strings.isNullOrEmpty; -import static org.sonar.api.CoreProperties.PERMANENT_SERVER_ID; -import static org.sonar.api.PropertyType.LICENSE; -import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; -import static org.sonar.server.ws.WsUtils.writeProtobuf; -import static org.sonarqube.ws.client.license.LicensesWsParameters.ACTION_LIST; - -public class ListAction implements WsAction { - - private static final String ALL_SERVERS_VALUE = "*"; - - private final UserSession userSession; - private final PropertyDefinitions definitions; - private final DbClient dbClient; - private final SettingsFinder settingsFinder; - - public ListAction(UserSession userSession, PropertyDefinitions definitions, DbClient dbClient, SettingsFinder settingsFinder) { - this.userSession = userSession; - this.definitions = definitions; - this.dbClient = dbClient; - this.settingsFinder = settingsFinder; - } - - @Override - public void define(WebService.NewController context) { - context.createAction(ACTION_LIST) - .setDescription("List licenses settings.<br>" + - "Requires 'Administer System' permission") - .setResponseExample(getClass().getResource("list-example.json")) - .setSince("6.1") - .setInternal(true) - .setHandler(this); - } - - @Override - public void handle(Request request, Response response) throws Exception { - userSession.checkIsSystemAdministrator(); - - try (DbSession dbSession = dbClient.openSession(true)) { - writeProtobuf(doHandle(dbSession), request, response); - } - } - - private ListWsResponse doHandle(DbSession dbSession) { - Map<String, PropertyDefinition> licenseDefinitionsByKeys = definitions.getAll().stream() - .filter(definition -> LICENSE.equals(definition.type())) - .collect(MoreCollectors.uniqueIndex(PropertyDefinition::key, Function.identity())); - Set<String> settingsKeys = new HashSet<>(licenseDefinitionsByKeys.keySet()); - settingsKeys.add(PERMANENT_SERVER_ID); - List<Setting> settings = settingsFinder.loadGlobalSettings(dbSession, settingsKeys); - return new ListResponseBuilder(licenseDefinitionsByKeys, settings).build(); - } - - private static class ListResponseBuilder { - private final Optional<String> serverId; - private final Map<String, Setting> licenseSettingsByKey; - private final Collection<PropertyDefinition> licenseDefinitions; - - ListResponseBuilder(Map<String, PropertyDefinition> licenseDefinitionsByKeys, List<Setting> settings) { - this.serverId = getServerId(settings); - this.licenseDefinitions = licenseDefinitionsByKeys.values(); - this.licenseSettingsByKey = settings.stream().collect(uniqueIndex(Setting::getKey, Function.identity())); - } - - ListWsResponse build() { - ListWsResponse.Builder wsResponse = ListWsResponse.newBuilder(); - licenseDefinitions.forEach(def -> wsResponse.addLicenses(buildLicense(def, licenseSettingsByKey.get(def.key())))); - return wsResponse.build(); - } - - private Licenses.License buildLicense(PropertyDefinition definition, @Nullable Setting setting) { - Licenses.License.Builder licenseBuilder = Licenses.License.newBuilder() - .setKey(definition.key()); - String name = definition.name(); - if (!isNullOrEmpty(name)) { - licenseBuilder.setName(name); - } - if (setting != null) { - License license = License.readBase64(setting.getValue()); - licenseBuilder.setValue(setting.getValue()); - setProduct(licenseBuilder, license, setting); - setOrganization(licenseBuilder, license); - setExpiration(licenseBuilder, license); - setServerId(licenseBuilder, license); - setType(licenseBuilder, license); - setAdditionalProperties(licenseBuilder, license); - } - return licenseBuilder.build(); - } - - private static void setProduct(Licenses.License.Builder licenseBuilder, License license, Setting setting) { - String product = license.getProduct(); - if (product != null) { - licenseBuilder.setProduct(product); - } - if (product == null || !setting.getKey().contains(product)) { - licenseBuilder.setInvalidProduct(true); - } - } - - private static void setOrganization(Licenses.License.Builder licenseBuilder, License license) { - String licenseOrganization = license.getOrganization(); - if (licenseOrganization != null) { - licenseBuilder.setOrganization(licenseOrganization); - } - } - - private void setServerId(Licenses.License.Builder licenseBuilder, License license) { - String licenseServerId = license.getServer(); - if (licenseServerId != null) { - licenseBuilder.setServerId(licenseServerId); - } - boolean isValidServerId = Objects.equals(licenseServerId, ALL_SERVERS_VALUE) - || (serverId.isPresent() && Objects.equals(licenseServerId, serverId.get())); - if (!isValidServerId) { - licenseBuilder.setInvalidServerId(true); - } - } - - private static void setExpiration(Licenses.License.Builder licenseBuilder, License license) { - String expiration = license.getExpirationDateAsString(); - if (expiration != null) { - licenseBuilder.setExpiration(expiration); - } - if (license.isExpired()) { - licenseBuilder.setInvalidExpiration(true); - } - } - - private static void setType(Licenses.License.Builder licenseBuilder, License license) { - String type = license.getType(); - if (type != null) { - licenseBuilder.setType(type); - } - } - - private static void setAdditionalProperties(Licenses.License.Builder licenseBuilder, License license) { - Map<String, String> additionalProperties = license.additionalProperties(); - if (!additionalProperties.isEmpty()) { - licenseBuilder.getAdditionalPropertiesBuilder().putAllAdditionalProperties(additionalProperties).build(); - } - } - - private static Optional<String> getServerId(List<Setting> settings) { - Optional<Setting> setting = settings.stream().filter(s -> s.getKey().equals(PERMANENT_SERVER_ID)).findFirst(); - return setting.isPresent() ? Optional.of(setting.get().getValue()) : Optional.empty(); - } - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/license/ws/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/license/ws/package-info.java deleted file mode 100644 index 2b670d4ef82..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/license/ws/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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. - */ -@ParametersAreNonnullByDefault -package org.sonar.server.license.ws; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index fcbf8711124..c128441fb77 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -84,7 +84,6 @@ import org.sonar.server.issue.notification.NewIssuesNotificationDispatcher; import org.sonar.server.issue.notification.NewIssuesNotificationFactory; import org.sonar.server.issue.ws.IssueWsModule; import org.sonar.server.language.ws.LanguageWs; -import org.sonar.server.license.ws.LicensesWsModule; import org.sonar.server.measure.custom.ws.CustomMeasuresWsModule; import org.sonar.server.measure.index.ProjectsEsModule; import org.sonar.server.measure.ws.MeasuresWsModule; @@ -178,7 +177,6 @@ import org.sonar.server.rule.ws.RuleQueryFactory; import org.sonar.server.rule.ws.RuleWsSupport; import org.sonar.server.rule.ws.RulesWs; import org.sonar.server.rule.ws.TagsAction; -import org.sonar.server.serverid.ws.ServerIdWsModule; import org.sonar.server.setting.ws.SettingsWsModule; import org.sonar.server.source.HtmlSourceDecorator; import org.sonar.server.source.SourceService; @@ -473,9 +471,6 @@ public class PlatformLevel4 extends PlatformLevel { org.sonar.server.property.ws.IndexAction.class, SettingsWsModule.class, - // Licences - LicensesWsModule.class, - TypeValidationModule.class, // Project Links @@ -497,9 +492,6 @@ public class PlatformLevel4 extends PlatformLevel { HealthActionModule.class, SystemWs.class, - // Server id - ServerIdWsModule.class, - // Plugins WS PluginWSCommons.class, PluginUpdateAggregator.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/GenerateAction.java b/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/GenerateAction.java deleted file mode 100644 index c5bbb4d4619..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/GenerateAction.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.property.PropertyDto; -import org.sonar.server.platform.ServerIdGenerator; -import org.sonar.server.user.UserSession; -import org.sonarqube.ws.ServerId.GenerateWsResponse; -import org.sonarqube.ws.client.serverid.GenerateRequest; - -import static org.sonar.api.CoreProperties.ORGANISATION; -import static org.sonar.api.CoreProperties.PERMANENT_SERVER_ID; -import static org.sonar.api.CoreProperties.SERVER_ID_IP_ADDRESS; -import static org.sonar.server.ws.WsUtils.writeProtobuf; - -public class GenerateAction implements ServerIdWsAction { - public static final String PARAM_ORGANIZATION = "organization"; - public static final String PARAM_IP = "ip"; - - private static final Logger LOG = Loggers.get(GenerateAction.class); - - private final UserSession userSession; - private final ServerIdGenerator generator; - private final DbClient dbClient; - - public GenerateAction(UserSession userSession, ServerIdGenerator generator, DbClient dbClient) { - this.userSession = userSession; - this.generator = generator; - this.dbClient = dbClient; - } - - @Override - public void define(WebService.NewController controller) { - WebService.NewAction action = controller.createAction("generate") - .setDescription("Generate a server id.<br/>" + - "Requires 'System Administer' permissions") - .setSince("6.1") - .setInternal(true) - .setPost(true) - .setResponseExample(getClass().getResource("generate-example.json")) - .setHandler(this); - - action.createParam(PARAM_ORGANIZATION) - .setDescription("Organization name") - .setExampleValue("SonarSource") - .setRequired(true); - - action.createParam(PARAM_IP) - .setDescription("IP address") - .setExampleValue("10.142.20.56") - .setRequired(true); - } - - @Override - public void handle(Request request, Response response) throws Exception { - userSession.checkIsSystemAdministrator(); - - try (DbSession dbSession = dbClient.openSession(true)) { - writeProtobuf(doHandle(dbSession, toGenerateRequest(request)), request, response); - } - } - - private GenerateWsResponse doHandle(DbSession dbSession, GenerateRequest request) { - String serverId = generator.generate(request.getOrganization(), request.getIp()); - dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(PERMANENT_SERVER_ID).setValue(serverId)); - dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(ORGANISATION).setValue(request.getOrganization())); - dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(SERVER_ID_IP_ADDRESS).setValue(request.getIp())); - dbSession.commit(); - LOG.info("Generated new server ID={}", serverId); - - return GenerateWsResponse.newBuilder().setServerId(serverId).build(); - } - - private static GenerateRequest toGenerateRequest(Request request) { - return GenerateRequest.builder() - .setOrganization(request.mandatoryParam(PARAM_ORGANIZATION)) - .setIp(request.mandatoryParam(PARAM_IP)) - .build(); - } - -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWs.java b/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWs.java deleted file mode 100644 index 7036ff1ac4b..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWs.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import org.sonar.api.server.ws.WebService; - -public class ServerIdWs implements WebService { - - private final ServerIdWsAction[] actions; - - public ServerIdWs(ServerIdWsAction... actions) { - this.actions = actions; - } - - @Override - public void define(Context context) { - NewController controller = context.createController("api/server_id") - .setDescription("Get server id information and generate server id.") - .setSince("6.1"); - for (ServerIdWsAction action : actions) { - action.define(controller); - } - controller.done(); - } - -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWsAction.java deleted file mode 100644 index 82160780388..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWsAction.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import org.sonar.server.ws.WsAction; - -public interface ServerIdWsAction extends WsAction { - // Marker interface -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWsModule.java deleted file mode 100644 index da8ccad2016..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ServerIdWsModule.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import org.sonar.core.platform.Module; - -public class ServerIdWsModule extends Module { - @Override - protected void configureModule() { - add( - ServerIdWs.class, - ShowAction.class, - GenerateAction.class); - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ShowAction.java deleted file mode 100644 index cafa7da131a..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/ShowAction.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import com.google.common.collect.ImmutableSet; -import java.net.InetAddress; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.function.Function; -import javax.annotation.Nullable; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.core.util.stream.MoreCollectors; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.property.PropertyDto; -import org.sonar.server.platform.ServerIdGenerator; -import org.sonar.server.user.UserSession; - -import static java.util.stream.Collectors.toList; -import static org.sonar.api.CoreProperties.ORGANISATION; -import static org.sonar.api.CoreProperties.PERMANENT_SERVER_ID; -import static org.sonar.api.CoreProperties.SERVER_ID_IP_ADDRESS; -import static org.sonar.server.ws.WsUtils.writeProtobuf; -import static org.sonarqube.ws.ServerId.ShowWsResponse; - -public class ShowAction implements ServerIdWsAction { - - private static final Set<String> SETTINGS_KEYS = ImmutableSet.of(PERMANENT_SERVER_ID, ORGANISATION, SERVER_ID_IP_ADDRESS); - - private final UserSession userSession; - private final ServerIdGenerator serverIdGenerator; - private final DbClient dbClient; - - public ShowAction(UserSession userSession, ServerIdGenerator serverIdGenerator, DbClient dbClient) { - this.userSession = userSession; - this.serverIdGenerator = serverIdGenerator; - this.dbClient = dbClient; - } - - @Override - public void define(WebService.NewController controller) { - controller.createAction("show") - .setDescription("Get server id configuration.<br/>" + - "Requires 'System Administer' permissions") - .setSince("6.1") - .setInternal(true) - .setResponseExample(getClass().getResource("show-example.json")) - .setHandler(this); - } - - @Override - public void handle(Request request, Response response) throws Exception { - userSession.checkIsSystemAdministrator(); - try (DbSession dbSession = dbClient.openSession(true)) { - Map<String, PropertyDto> properties = dbClient.propertiesDao().selectGlobalPropertiesByKeys(dbSession, SETTINGS_KEYS).stream() - .collect(MoreCollectors.uniqueIndex(PropertyDto::getKey, Function.identity())); - writeProtobuf(doHandle(properties), request, response); - } - } - - private ShowWsResponse doHandle(Map<String, PropertyDto> properties) { - ShowWsResponse.Builder responseBuilder = ShowWsResponse.newBuilder(); - List<String> validIpAddresses = serverIdGenerator.getAvailableAddresses().stream().map(InetAddress::getHostAddress).collect(toList()); - if (!validIpAddresses.isEmpty()) { - responseBuilder.addAllValidIpAddresses(validIpAddresses); - } - - Optional<String> serverId = getSettingValue(properties.get(PERMANENT_SERVER_ID)); - if (serverId.isPresent()) { - responseBuilder.setServerId(serverId.get()); - Optional<String> organization = getSettingValue(properties.get(ORGANISATION)); - organization.ifPresent(responseBuilder::setOrganization); - Optional<String> ip = getSettingValue(properties.get(SERVER_ID_IP_ADDRESS)); - ip.ifPresent(responseBuilder::setIp); - boolean isValidServId = isValidServerId(serverId.get(), organization, ip); - if (!isValidServId) { - responseBuilder.setInvalidServerId(true); - } - } - return responseBuilder.build(); - } - - private static Optional<String> getSettingValue(@Nullable PropertyDto propertyDto) { - return propertyDto != null ? Optional.of(propertyDto.getValue()) : Optional.empty(); - } - - private boolean isValidServerId(String serverId, Optional<String> organization, Optional<String> ip) { - return organization.isPresent() - && ip.isPresent() - && serverIdGenerator.validate(organization.get(), ip.get(), serverId); - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/package-info.java deleted file mode 100644 index c0ed9846804..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/serverid/ws/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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. - */ -@ParametersAreNonnullByDefault -package org.sonar.server.serverid.ws; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/test/java/org/sonar/server/license/ws/LicensesWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/license/ws/LicensesWsModuleTest.java deleted file mode 100644 index cb01027f18b..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/license/ws/LicensesWsModuleTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.license.ws; - -import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; - -import static org.assertj.core.api.Assertions.assertThat; - -public class LicensesWsModuleTest { - @Test - public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); - new LicensesWsModule().configure(container); - assertThat(container.size()).isEqualTo(2 + 2); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/license/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/license/ws/ListActionTest.java deleted file mode 100644 index b609172e9b7..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/license/ws/ListActionTest.java +++ /dev/null @@ -1,309 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.license.ws; - -import com.google.common.collect.ImmutableMap; -import java.nio.charset.StandardCharsets; -import java.util.Collections; -import java.util.Map; -import javax.annotation.Nullable; -import org.apache.commons.codec.binary.Base64; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.config.PropertyDefinition; -import org.sonar.api.config.PropertyDefinitions; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; -import org.sonar.db.DbClient; -import org.sonar.db.DbTester; -import org.sonar.db.property.PropertyDbTester; -import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.setting.ws.SettingsFinder; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsActionTester; -import org.sonarqube.ws.Licenses; -import org.sonarqube.ws.Licenses.ListWsResponse; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; -import static org.sonar.api.CoreProperties.PERMANENT_SERVER_ID; -import static org.sonar.api.PropertyType.LICENSE; -import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto; - -public class ListActionTest { - - private static final String LICENSE_KEY_SAMPLE = "sonar.governance.license.secured"; - private static final String LICENSE_NAME_SAMPLE = "Governance"; - private static final String ORGANIZATION_SAMPLE = "SonarSource"; - private static final String SERVER_ID_SAMPLE = "12345"; - private static final String PRODUCT_SAMPLE = "governance"; - private static final String TYPE_SAMPLE = "PRODUCTION"; - private static final String EXPIRATION_SAMPLE = "2099-01-01"; - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - - private DbClient dbClient = db.getDbClient(); - private PropertyDbTester propertyDb = new PropertyDbTester(db); - private PropertyDefinitions definitions = new PropertyDefinitions(); - private SettingsFinder settingsFinder = new SettingsFinder(dbClient, definitions); - - private WsActionTester ws = new WsActionTester(new ListAction(userSession, definitions, dbClient, settingsFinder)); - - @Test - public void return_licenses() throws Exception { - logInAsSystemAdministrator(); - addServerIdSettings("12345"); - String data = createBase64License("SonarSource", "governance", "12345", "2099-01-01", "PRODUCTION", ImmutableMap.of("other", "value")); - addLicenseSetting("sonar.governance.license.secured", "Governance", data); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getKey()).isEqualTo("sonar.governance.license.secured"); - assertThat(license.getName()).isEqualTo("Governance"); - assertThat(license.getValue()).isEqualTo(data); - assertThat(license.getProduct()).isEqualTo("governance"); - assertThat(license.getOrganization()).isEqualTo("SonarSource"); - assertThat(license.getExpiration()).isEqualTo("2099-01-01"); - assertThat(license.getType()).isEqualTo("PRODUCTION"); - assertThat(license.getServerId()).isEqualTo("12345"); - assertThat(license.getAdditionalProperties().getAdditionalProperties()).containsOnly(entry("other", "value")); - - assertThat(license.hasInvalidProduct()).isFalse(); - assertThat(license.hasInvalidExpiration()).isFalse(); - assertThat(license.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_licenses_even_if_no_value_set_in_database() throws Exception { - logInAsSystemAdministrator(); - addServerIdSettings("12345"); - definitions.addComponent(PropertyDefinition.builder("sonar.governance.license.secured").type(LICENSE).build()); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getKey()).isEqualTo("sonar.governance.license.secured"); - assertThat(license.hasValue()).isFalse(); - assertThat(license.hasProduct()).isFalse(); - assertThat(license.hasOrganization()).isFalse(); - assertThat(license.hasExpiration()).isFalse(); - assertThat(license.hasType()).isFalse(); - assertThat(license.hasServerId()).isFalse(); - assertThat(license.hasAdditionalProperties()).isFalse(); - - assertThat(license.hasInvalidProduct()).isFalse(); - assertThat(license.hasInvalidExpiration()).isFalse(); - assertThat(license.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_information_when_no_licence_set() throws Exception { - logInAsSystemAdministrator(); - addServerIdSettings(SERVER_ID_SAMPLE); - addLicenseSetting(LICENSE_KEY_SAMPLE, null, toBase64("")); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getKey()).isEqualTo(LICENSE_KEY_SAMPLE); - assertThat(license.hasName()).isFalse(); - assertThat(license.getValue()).isEmpty(); - assertThat(license.hasProduct()).isFalse(); - assertThat(license.hasOrganization()).isFalse(); - assertThat(license.hasExpiration()).isFalse(); - assertThat(license.hasType()).isFalse(); - assertThat(license.hasServerId()).isFalse(); - assertThat(license.hasAdditionalProperties()).isFalse(); - - assertThat(license.hasInvalidProduct()).isTrue(); - assertThat(license.hasInvalidExpiration()).isFalse(); - assertThat(license.hasInvalidServerId()).isTrue(); - } - - @Test - public void return_license_with_bad_product() throws Exception { - logInAsSystemAdministrator(); - addServerIdSettings(SERVER_ID_SAMPLE); - addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE, - createBase64License(ORGANIZATION_SAMPLE, "Other", SERVER_ID_SAMPLE, EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap())); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getProduct()).isEqualTo("Other"); - assertThat(license.getInvalidProduct()).isTrue(); - assertThat(license.hasInvalidExpiration()).isFalse(); - assertThat(license.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_license_with_bad_server_id() throws Exception { - logInAsSystemAdministrator(); - addServerIdSettings(SERVER_ID_SAMPLE); - addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE, - createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, "Other", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap())); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getServerId()).isEqualTo("Other"); - assertThat(license.getInvalidServerId()).isTrue(); - assertThat(license.hasInvalidProduct()).isFalse(); - assertThat(license.hasInvalidExpiration()).isFalse(); - } - - @Test - public void return_bad_server_id_when_server_has_no_server_id() throws Exception { - logInAsSystemAdministrator(); - addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE, - createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, SERVER_ID_SAMPLE, EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap())); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getInvalidServerId()).isTrue(); - } - - @Test - public void does_not_return_invalid_server_id_when_all_servers_accepted_and_no_server_id_setting() throws Exception { - logInAsSystemAdministrator(); - addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE, - createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap())); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getServerId()).isEqualTo("*"); - assertThat(license.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_license_when_all_servers_are_accepted() throws Exception { - logInAsSystemAdministrator(); - addServerIdSettings(SERVER_ID_SAMPLE); - addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE, - createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap())); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getServerId()).isEqualTo("*"); - assertThat(license.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_license_when_expired() throws Exception { - logInAsSystemAdministrator(); - addServerIdSettings(SERVER_ID_SAMPLE); - addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE, - createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, SERVER_ID_SAMPLE, "2010-01-01", TYPE_SAMPLE, Collections.emptyMap())); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).hasSize(1); - Licenses.License license = result.getLicenses(0); - assertThat(license.getExpiration()).isEqualTo("2010-01-01"); - assertThat(license.getInvalidExpiration()).isTrue(); - assertThat(license.hasInvalidProduct()).isFalse(); - assertThat(license.hasInvalidServerId()).isFalse(); - } - - @Test - public void none_license_type_settings_are_not_returned() throws Exception { - logInAsSystemAdministrator(); - definitions.addComponent(PropertyDefinition.builder("foo").build()); - propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("value")); - - ListWsResponse result = executeRequest(); - - assertThat(result.getLicensesList()).isEmpty(); - } - - @Test - public void throw_ForbiddenException_if_not_system_administrator() throws Exception { - userSession.logIn().setNonSystemAdministrator(); - - expectedException.expect(ForbiddenException.class); - - executeRequest(); - } - - @Test - public void test_ws_definition() { - WebService.Action action = ws.getDef(); - assertThat(action).isNotNull(); - assertThat(action.isInternal()).isTrue(); - assertThat(action.isPost()).isFalse(); - assertThat(action.responseExampleAsString()).isNotEmpty(); - assertThat(action.params()).isEmpty(); - } - - private ListWsResponse executeRequest() { - return ws.newRequest().executeProtobuf(ListWsResponse.class); - } - - private void logInAsSystemAdministrator() { - userSession.logIn().setSystemAdministrator(); - } - - private void addLicenseSetting(String key, @Nullable String name, String value) { - definitions.addComponent(PropertyDefinition.builder(key).name(name).type(LICENSE).build()); - propertyDb.insertProperties(newGlobalPropertyDto().setKey(key).setValue(value)); - } - - private void addServerIdSettings(String serverId) { - propertyDb.insertProperties(newGlobalPropertyDto().setKey(PERMANENT_SERVER_ID).setValue(serverId)); - } - - private static String toBase64(String data) { - return Base64.encodeBase64String((data.getBytes(StandardCharsets.UTF_8))); - } - - private static String createBase64License(@Nullable String organization, @Nullable String product, @Nullable String serverId, @Nullable String expirationDate, - @Nullable String type, Map<String, String> additionalProperties) { - StringBuilder data = new StringBuilder(); - data.append("Organisation: ").append(organization).append(" \n"); - data.append("Server: ").append(serverId).append(" \n"); - data.append("Product: ").append(product).append(" \n"); - data.append("Expiration: ").append(expirationDate).append(" \n"); - data.append("Type: ").append(type).append(" \n"); - for (Map.Entry<String, String> entry : additionalProperties.entrySet()) { - data.append(entry.getKey()).append(": ").append(entry.getValue()).append(" \n"); - } - return toBase64(data.toString()); - } - -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/GenerateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/GenerateActionTest.java deleted file mode 100644 index 474b76fce26..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/GenerateActionTest.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import javax.annotation.Nullable; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.log.LogTester; -import org.sonar.api.utils.log.LoggerLevel; -import org.sonar.db.DbClient; -import org.sonar.db.DbTester; -import org.sonar.db.property.PropertyDto; -import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.platform.ServerIdGenerator; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.TestRequest; -import org.sonar.server.ws.WsActionTester; -import org.sonarqube.ws.ServerId.GenerateWsResponse; - -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.CoreProperties.ORGANISATION; -import static org.sonar.api.CoreProperties.PERMANENT_SERVER_ID; -import static org.sonar.api.CoreProperties.SERVER_ID_IP_ADDRESS; -import static org.sonar.server.serverid.ws.GenerateAction.PARAM_IP; -import static org.sonar.server.serverid.ws.GenerateAction.PARAM_ORGANIZATION; -import static org.sonar.test.JsonAssert.assertJson; - -public class GenerateActionTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - @Rule - public LogTester log = new LogTester(); - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - - private DbClient dbClient = db.getDbClient(); - private ServerIdGenerator generator = mock(ServerIdGenerator.class); - private GenerateAction underTest = new GenerateAction(userSession, generator, dbClient); - private WsActionTester ws = new WsActionTester(underTest); - - @Test - public void persist_settings() { - logInAsSystemAdministrator(); - - when(generator.generate("SonarSource", "10.51.42.255")).thenReturn("server_id"); - - GenerateWsResponse result = call("SonarSource", "10.51.42.255"); - - assertThat(result.getServerId()).isEqualTo("server_id"); - assertGlobalSetting(ORGANISATION, "SonarSource"); - assertGlobalSetting(SERVER_ID_IP_ADDRESS, "10.51.42.255"); - assertGlobalSetting(PERMANENT_SERVER_ID, "server_id"); - } - - @Test - public void json_example() { - logInAsSystemAdministrator(); - - when(generator.generate("SonarSource", "127.0.0.1")).thenReturn("1818a1eefb26f9g"); - - String result = ws.newRequest() - .setParam(PARAM_ORGANIZATION, "SonarSource") - .setParam(PARAM_IP, "127.0.0.1") - .execute().getInput(); - - assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString()); - } - - @Test - public void log_message_when_id_generated() { - logInAsSystemAdministrator(); - when(generator.generate("SonarSource", "127.0.0.1")).thenReturn("server_id"); - - call("SonarSource", "127.0.0.1"); - - assertThat(log.logs(LoggerLevel.INFO)).contains("Generated new server ID=" + "server_id"); - } - - @Test - public void definition() { - WebService.Action definition = ws.getDef(); - - assertThat(definition.key()).isEqualTo("generate"); - assertThat(definition.since()).isEqualTo("6.1"); - assertThat(definition.isInternal()).isTrue(); - assertThat(definition.isPost()).isTrue(); - assertThat(definition.responseExampleAsString()).isNotEmpty(); - assertThat(definition.params()).hasSize(2); - } - - @Test - public void throw_ForbiddenException_if_not_system_administrator() { - userSession.logIn().setNonSystemAdministrator(); - - expectedException.expect(ForbiddenException.class); - - call("SonarSource", "127.0.0.1"); - } - - @Test - public void fail_if_no_organization() { - logInAsSystemAdministrator(); - - expectedException.expect(IllegalArgumentException.class); - - call(null, "127.0.0.1"); - } - - @Test - public void fail_if_empty_organization() { - logInAsSystemAdministrator(); - - expectedException.expect(IllegalArgumentException.class); - - call("", "127.0.0.1"); - } - - @Test - public void fail_if_no_ip() { - logInAsSystemAdministrator(); - - expectedException.expect(IllegalArgumentException.class); - - call("SonarSource", null); - } - - @Test - public void fail_if_empty_ip() { - logInAsSystemAdministrator(); - - expectedException.expect(IllegalArgumentException.class); - - call("SonarSource", ""); - } - - private void assertGlobalSetting(String key, String value) { - PropertyDto result = dbClient.propertiesDao().selectGlobalProperty(key); - - assertThat(result) - .extracting(PropertyDto::getKey, PropertyDto::getValue, PropertyDto::getResourceId) - .containsExactly(key, value, null); - } - - private GenerateWsResponse call(@Nullable String organization, @Nullable String ip) { - TestRequest request = ws.newRequest() - .setMethod("POST"); - - if (organization != null) { - request.setParam(PARAM_ORGANIZATION, organization); - } - - if (ip != null) { - request.setParam(PARAM_IP, ip); - } - - return request.executeProtobuf(GenerateWsResponse.class); - } - - private void logInAsSystemAdministrator() { - userSession.logIn().setSystemAdministrator(); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ServerIdWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ServerIdWsModuleTest.java deleted file mode 100644 index 3004d9c5893..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ServerIdWsModuleTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ServerIdWsModuleTest { - @Test - public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); - new ServerIdWsModule().configure(container); - assertThat(container.size()).isEqualTo(3 + 2); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ServerIdWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ServerIdWsTest.java deleted file mode 100644 index fa31b3f16c4..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ServerIdWsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import org.junit.Test; -import org.sonar.api.server.ws.WebService; -import org.sonar.db.DbClient; -import org.sonar.server.platform.ServerIdGenerator; -import org.sonar.server.user.UserSession; -import org.sonar.server.ws.WsTester; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -public class ServerIdWsTest { - - WsTester ws = new WsTester(new ServerIdWs(new ShowAction(mock(UserSession.class), mock(ServerIdGenerator.class), mock(DbClient.class)))); - WebService.Controller underTest = ws.controller("api/server_id"); - - @Test - public void definition() { - assertThat(underTest.path()).isEqualTo("api/server_id"); - assertThat(underTest.since()).isEqualTo("6.1"); - assertThat(underTest.description()).isNotEmpty(); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ShowActionTest.java deleted file mode 100644 index 330698bc2e6..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/serverid/ws/ShowActionTest.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.serverid.ws; - -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nullable; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; -import org.sonar.db.DbClient; -import org.sonar.db.DbTester; -import org.sonar.db.property.PropertyDbTester; -import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.platform.ServerIdGenerator; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsActionTester; -import org.sonar.test.JsonAssert; -import org.sonarqube.ws.ServerId.ShowWsResponse; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto; -import static org.sonarqube.ws.MediaTypes.JSON; - -public class ShowActionTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - - private DbClient dbClient = db.getDbClient(); - private PropertyDbTester propertyDb = new PropertyDbTester(db); - private ServerIdGenerator generator = mock(ServerIdGenerator.class); - private WsActionTester ws = new WsActionTester(new ShowAction(userSession, generator, dbClient)); - - @Test - public void return_server_id_info() throws Exception { - logInAsSystemAdministrator(); - - when(generator.validate("home", "127.0.0.1", "1818a1eefb26f9g")).thenReturn(true); - setAvailableIpAdresses("192.168.1.1", "127.0.0.1"); - insertConfiguration("1818a1eefb26f9g", "home", "127.0.0.1"); - - ShowWsResponse response = executeRequest(); - - assertThat(response.getServerId()).isEqualTo("1818a1eefb26f9g"); - assertThat(response.getOrganization()).isEqualTo("home"); - assertThat(response.getIp()).isEqualTo("127.0.0.1"); - assertThat(response.getValidIpAddressesList()).containsOnly("192.168.1.1", "127.0.0.1"); - assertThat(response.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_invalid_server_id() throws Exception { - logInAsSystemAdministrator(); - when(generator.validate("home", "127.0.0.1", "1818a1eefb26f9g")).thenReturn(true); - insertConfiguration("invalid", null, null); - - ShowWsResponse response = executeRequest(); - - assertThat(response.hasInvalidServerId()).isTrue(); - assertThat(response.getServerId()).isEqualTo("invalid"); - assertThat(response.hasOrganization()).isFalse(); - assertThat(response.hasIp()).isFalse(); - assertThat(response.getValidIpAddressesList()).isEmpty(); - } - - @Test - public void return_no_server_id_info_when_no_settings_and_no_available_ips() throws Exception { - logInAsSystemAdministrator(); - - ShowWsResponse response = executeRequest(); - - assertThat(response.hasServerId()).isFalse(); - assertThat(response.hasOrganization()).isFalse(); - assertThat(response.hasIp()).isFalse(); - assertThat(response.getValidIpAddressesList()).isEmpty(); - assertThat(response.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_no_server_id_info_when_no_server_id_but_other_settings() throws Exception { - logInAsSystemAdministrator(); - insertConfiguration(null, "home", "127.0.0.1"); - - ShowWsResponse response = executeRequest(); - - assertThat(response.hasServerId()).isFalse(); - assertThat(response.hasOrganization()).isFalse(); - assertThat(response.hasIp()).isFalse(); - assertThat(response.getValidIpAddressesList()).isEmpty(); - assertThat(response.hasInvalidServerId()).isFalse(); - } - - @Test - public void return_available_ips_even_if_no_settings() throws Exception { - logInAsSystemAdministrator(); - setAvailableIpAdresses("192.168.1.1", "127.0.0.1"); - - ShowWsResponse response = executeRequest(); - - assertThat(response.hasServerId()).isFalse(); - assertThat(response.hasOrganization()).isFalse(); - assertThat(response.hasIp()).isFalse(); - assertThat(response.getValidIpAddressesList()).containsOnly("192.168.1.1", "127.0.0.1"); - assertThat(response.hasInvalidServerId()).isFalse(); - } - - @Test - public void throw_ForbiddenException_if_not_system_administrator() throws Exception { - userSession.logIn().setNonSystemAdministrator(); - - expectedException.expect(ForbiddenException.class); - expectedException.expectMessage("Insufficient privileges"); - - executeRequest(); - } - - @Test - public void test_ws_definition() { - WebService.Action action = ws.getDef(); - assertThat(action).isNotNull(); - assertThat(action.isInternal()).isTrue(); - assertThat(action.isPost()).isFalse(); - assertThat(action.responseExampleAsString()).isNotEmpty(); - assertThat(action.params()).isEmpty(); - } - - @Test - public void test_example_json_response() { - logInAsSystemAdministrator(); - when(generator.validate("home", "127.0.0.1", "1818a1eefb26f9g")).thenReturn(true); - setAvailableIpAdresses("192.168.1.1", "127.0.0.1"); - insertConfiguration("1818a1eefb26f9g", "home", "127.0.0.1"); - - String result = ws.newRequest() - .setMediaType(JSON) - .execute() - .getInput(); - - JsonAssert.assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result); - } - - private void insertConfiguration(@Nullable String serverId, @Nullable String organisation, @Nullable String ip) { - if (serverId != null) { - propertyDb.insertProperties(newGlobalPropertyDto().setKey("sonar.server_id").setValue(serverId)); - } - if (organisation != null) { - propertyDb.insertProperties(newGlobalPropertyDto().setKey("sonar.organisation").setValue(organisation)); - } - if (ip != null) { - propertyDb.insertProperties(newGlobalPropertyDto().setKey("sonar.server_id.ip_address").setValue(ip)); - } - } - - private void setAvailableIpAdresses(String... ips) { - List<InetAddress> availableAddresses = new ArrayList<>(); - for (String ip : ips) { - InetAddress inetAddress = mock(InetAddress.class); - when(inetAddress.getHostAddress()).thenReturn(ip); - availableAddresses.add(inetAddress); - } - when(generator.getAvailableAddresses()).thenReturn(availableAddresses); - } - - private ShowWsResponse executeRequest() { - return ws.newRequest() - .executeProtobuf(ShowWsResponse.class); - } - - private void logInAsSystemAdministrator() { - userSession.logIn().setSystemAdministrator(); - } -} |