]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7983 Create WS api/settings/check_secret_key 1213/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 5 Sep 2016 16:18:48 +0000 (18:18 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 5 Sep 2016 16:34:41 +0000 (18:34 +0200)
server/sonar-server/src/main/java/org/sonar/server/setting/ws/CheckSecretKeyAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingsWsModule.java
server/sonar-server/src/main/resources/org/sonar/server/setting/ws/check_secret_key-example.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/setting/ws/CheckSecretKeyActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/setting/ws/SettingsWsModuleTest.java
sonar-ws/src/main/protobuf/ws-settings.proto

diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/CheckSecretKeyAction.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/CheckSecretKeyAction.java
new file mode 100644 (file)
index 0000000..32f475b
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.setting.ws;
+
+import org.sonar.api.config.Settings;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.Settings.CheckSecretKeyWsResponse;
+
+import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
+public class CheckSecretKeyAction implements SettingsWsAction {
+  private final Settings settings;
+  private final UserSession userSession;
+
+  public CheckSecretKeyAction(Settings settings, UserSession userSession) {
+    this.settings = settings;
+    this.userSession = userSession;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    context.createAction("check_secret_key")
+      .setDescription("Check if a secret key is available")
+      .setSince("6.1")
+      .setInternal(true)
+      .setResponseExample(getClass().getResource("check_secret_key-example.json"))
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    userSession.checkPermission(SYSTEM_ADMIN);
+
+    writeProtobuf(CheckSecretKeyWsResponse.newBuilder().setSecretKeyAvailable(settings.getEncryption().hasSecretKey()).build(), request, response);
+  }
+}
index e8bbb1d78168f8e713472d499e1f550392b47091..b81ac302d9897dd3aa9ed90ef9c312eebd5db805 100644 (file)
@@ -34,6 +34,7 @@ public class SettingsWsModule extends Module {
       ResetAction.class,
       EncryptAction.class,
       GenerateSecretKeyAction.class,
+      CheckSecretKeyAction.class,
       SettingsUpdater.class);
   }
 }
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/check_secret_key-example.json b/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/check_secret_key-example.json
new file mode 100644 (file)
index 0000000..38ce425
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "secretKeyAvailable": true
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/CheckSecretKeyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/CheckSecretKeyActionTest.java
new file mode 100644 (file)
index 0000000..00c55ea
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.setting.ws;
+
+import com.google.common.base.Throwables;
+import java.io.File;
+import java.io.IOException;
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.config.Encryption;
+import org.sonar.api.config.Settings;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.Settings.CheckSecretKeyWsResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
+import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class CheckSecretKeyActionTest {
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone().setGlobalPermissions(SYSTEM_ADMIN);
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  Settings settings = new Settings();
+  Encryption encryption = settings.getEncryption();
+
+  CheckSecretKeyAction underTest = new CheckSecretKeyAction(settings, userSession);
+
+  WsActionTester ws = new WsActionTester(underTest);
+
+  @Test
+  public void json_example() throws IOException {
+    File secretKeyFile = temporaryFolder.newFile();
+    FileUtils.writeStringToFile(secretKeyFile, "fCVFf/JHRi8Qwu5KLNva7g==");
+    encryption.setPathToSecretKey(secretKeyFile.getAbsolutePath());
+
+    String result = ws.newRequest().execute().getInput();
+
+    assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
+  }
+
+  @Test
+  public void false_when_no_secret_key() {
+    encryption.setPathToSecretKey("unknown/path/to_secret_key.txt");
+
+    CheckSecretKeyWsResponse result = call();
+
+    assertThat(result.getSecretKeyAvailable()).isFalse();
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action definition = ws.getDef();
+
+    assertThat(definition.key()).isEqualTo("check_secret_key");
+    assertThat(definition.isPost()).isFalse();
+    assertThat(definition.isInternal()).isTrue();
+    assertThat(definition.since()).isEqualTo("6.1");
+    assertThat(definition.responseExampleAsString()).isNotEmpty();
+    assertThat(definition.params()).hasSize(0);
+  }
+
+  @Test
+  public void fail_if_insufficient_permissions() {
+    expectedException.expect(ForbiddenException.class);
+
+    userSession.anonymous().setGlobalPermissions(QUALITY_PROFILE_ADMIN);
+
+    call();
+  }
+
+  private CheckSecretKeyWsResponse call() {
+    TestRequest request = ws.newRequest()
+      .setMediaType(MediaTypes.PROTOBUF)
+      .setMethod("GET");
+
+    try {
+      return CheckSecretKeyWsResponse.parseFrom(request.execute().getInputStream());
+    } catch (IOException e) {
+      throw Throwables.propagate(e);
+    }
+  }
+
+}
index 627905526f9a925f76e9480c67f1ecc8340137ac..19a40eb621b5be6d6b2e03fd12c39234efd21bf7 100644 (file)
@@ -29,6 +29,6 @@ public class SettingsWsModuleTest {
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new SettingsWsModule().configure(container);
-    assertThat(container.size()).isEqualTo(10 + 2);
+    assertThat(container.size()).isEqualTo(11 + 2);
   }
 }
index 910e57b57bd59f0aa9fce63961d72631b0b39d48..df8e36059b569f8ab43d48a74689542791da3d3c 100644 (file)
@@ -39,6 +39,11 @@ message GenerateSecretKeyWsResponse {
   optional string secretKey = 1;
 }
 
+// Response of GET api/settings/check_secret_key
+message CheckSecretKeyWsResponse {
+  optional bool secretKeyAvailable = 1;
+}
+
 message Definition {
   optional string key = 1;
   optional string name = 2;