]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8716 fix check of permissions in api/license
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 2 Feb 2017 12:35:18 +0000 (13:35 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 7 Feb 2017 13:22:39 +0000 (14:22 +0100)
server/sonar-server/src/main/java/org/sonar/server/license/ws/ListAction.java
server/sonar-server/src/main/java/org/sonar/server/serverid/ws/GenerateAction.java
server/sonar-server/src/test/java/org/sonar/server/license/ws/ListActionTest.java

index 39cf1b3d560fa37c9b4ed1e509bdff8fb24121d1..cf3db6826d5acd36a632a494d2356b5e4d66e84f 100644 (file)
@@ -48,7 +48,6 @@ 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.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.core.util.stream.Collectors.uniqueIndex;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 import static org.sonarqube.ws.client.license.LicensesWsParameters.ACTION_LIST;
@@ -82,13 +81,10 @@ public class ListAction implements WsAction {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    userSession.checkPermission(SYSTEM_ADMIN);
+    userSession.checkIsRoot();
 
-    DbSession dbSession = dbClient.openSession(true);
-    try {
+    try (DbSession dbSession = dbClient.openSession(true)) {
       writeProtobuf(doHandle(dbSession), request, response);
-    } finally {
-      dbClient.closeSession(dbSession);
     }
   }
 
index 4b6b50242ce18aac06d92ef2af34c57b89189b3d..fd1dc0dd132d94942e8ac7edd15f668f56f5474c 100644 (file)
@@ -79,7 +79,7 @@ public class GenerateAction implements ServerIdWsAction {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    userSession.checkPermission(SYSTEM_ADMIN);
+    userSession.checkIsRoot();
 
     DbSession dbSession = dbClient.openSession(true);
     try {
index e2610ed579732ad6598398d0b80057c0d7520337..7d5b43ba476118f12437d7fd050de7d7c9930a15 100644 (file)
@@ -34,7 +34,6 @@ 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.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.property.PropertyDbTester;
@@ -51,7 +50,6 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Java6Assertions.entry;
 import static org.sonar.api.CoreProperties.PERMANENT_SERVER_ID;
 import static org.sonar.api.PropertyType.LICENSE;
-import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
 
 public class ListActionTest {
@@ -72,16 +70,16 @@ public class ListActionTest {
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
-  DbClient dbClient = db.getDbClient();
-  PropertyDbTester propertyDb = new PropertyDbTester(db);
-  PropertyDefinitions definitions = new PropertyDefinitions();
-  SettingsFinder settingsFinder = new SettingsFinder(dbClient, definitions);
+  private DbClient dbClient = db.getDbClient();
+  private PropertyDbTester propertyDb = new PropertyDbTester(db);
+  private PropertyDefinitions definitions = new PropertyDefinitions();
+  private SettingsFinder settingsFinder = new SettingsFinder(dbClient, definitions);
 
-  WsActionTester ws = new WsActionTester(new ListAction(userSession, definitions, dbClient, settingsFinder));
+  private WsActionTester ws = new WsActionTester(new ListAction(userSession, definitions, dbClient, settingsFinder));
 
   @Test
   public void return_licenses() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addServerIdSettings("12345");
     String data = createBase64License("SonarSource", "governance", "12345", "2099-01-01", "PRODUCTION", ImmutableMap.of("other", "value"));
     addLicenseSetting("sonar.governance.license.secured", "Governance", data);
@@ -107,7 +105,7 @@ public class ListActionTest {
 
   @Test
   public void return_licenses_even_if_no_value_set_in_database() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addServerIdSettings("12345");
     definitions.addComponent(PropertyDefinition.builder("sonar.governance.license.secured").type(LICENSE).build());
 
@@ -131,7 +129,7 @@ public class ListActionTest {
 
   @Test
   public void return_information_when_no_licence_set() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addServerIdSettings(SERVER_ID_SAMPLE);
     addLicenseSetting(LICENSE_KEY_SAMPLE, null, toBase64(""));
 
@@ -156,7 +154,7 @@ public class ListActionTest {
 
   @Test
   public void return_license_with_bad_product() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addServerIdSettings(SERVER_ID_SAMPLE);
     addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
       createBase64License(ORGANIZATION_SAMPLE, "Other", SERVER_ID_SAMPLE, EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
@@ -173,7 +171,7 @@ public class ListActionTest {
 
   @Test
   public void return_license_with_bad_server_id() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addServerIdSettings(SERVER_ID_SAMPLE);
     addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
       createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, "Other", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
@@ -190,7 +188,7 @@ public class ListActionTest {
 
   @Test
   public void return_bad_server_id_when_server_has_no_server_id() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
       createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, SERVER_ID_SAMPLE, EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
 
@@ -203,7 +201,7 @@ public class ListActionTest {
 
   @Test
   public void does_not_return_invalid_server_id_when_all_servers_accepted_and_no_server_id_setting() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
       createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
 
@@ -217,7 +215,7 @@ public class ListActionTest {
 
   @Test
   public void return_license_when_all_servers_are_accepted() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     addServerIdSettings(SERVER_ID_SAMPLE);
     addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
       createBase64License(ORGANIZATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
@@ -232,7 +230,7 @@ public class ListActionTest {
 
   @Test
   public void return_license_when_expired() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     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()));
@@ -249,7 +247,7 @@ public class ListActionTest {
 
   @Test
   public void none_license_type_settings_are_not_returned() throws Exception {
-    setUserAsSystemAdmin();
+    logInAsRoot();
     definitions.addComponent(PropertyDefinition.builder("foo").build());
     propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("value"));
 
@@ -259,9 +257,8 @@ public class ListActionTest {
   }
 
   @Test
-  public void fail_when_not_system_admin() throws Exception {
-    userSession.logIn("not-admin").setGlobalPermissions(GlobalPermissions.QUALITY_GATE_ADMIN);
-    definitions.addComponent(PropertyDefinition.builder("foo").build());
+  public void throw_ForbiddenException_if_not_root() throws Exception {
+    userSession.logIn().setNonRoot();
 
     expectedException.expect(ForbiddenException.class);
 
@@ -288,8 +285,8 @@ public class ListActionTest {
     }
   }
 
-  private void setUserAsSystemAdmin() {
-    userSession.logIn("admin").setGlobalPermissions(SYSTEM_ADMIN);
+  private void logInAsRoot() {
+    userSession.logIn().setRoot();
   }
 
   private void addLicenseSetting(String key, @Nullable String name, String value) {