]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7982 WS settings/encrypt fails properly when no secret key available
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 5 Sep 2016 15:15:58 +0000 (17:15 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 5 Sep 2016 15:15:58 +0000 (17:15 +0200)
server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java
server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java

index b8150733d831d3b8df48bae9e7321526b4db98f6..2615d6590a184ca9802bd38e03412cfe14b7b935 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.sonar.server.setting.ws;
 
+import org.sonar.api.config.Encryption;
 import org.sonar.api.config.Settings;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
@@ -64,7 +65,10 @@ public class EncryptAction implements SettingsWsAction {
     String value = request.mandatoryParam(PARAM_VALUE);
     checkRequest(!value.isEmpty(), "Parameter '%s' must not be empty", PARAM_VALUE);
 
-    String encryptedValue = settings.getEncryption().encrypt(value);
+    Encryption encryption = settings.getEncryption();
+    checkRequest(encryption.hasSecretKey(), "No secret key available");
+
+    String encryptedValue = encryption.encrypt(value);
 
     writeProtobuf(toEncryptWsResponse(encryptedValue), request, response);
   }
index b5c7a09c69ede2a5c77630428ef4a679b9673d3d..a966535179040cb0604c603919cbb410bab56db9 100644 (file)
@@ -42,8 +42,6 @@ import org.sonarqube.ws.MediaTypes;
 import org.sonarqube.ws.Settings.EncryptWsResponse;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 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;
@@ -57,8 +55,8 @@ public class EncryptActionTest {
   @Rule
   public TemporaryFolder folder = new TemporaryFolder();
 
-  Settings settings = mock(Settings.class);
-  Encryption encryption;
+  Settings settings = new Settings();
+  Encryption encryption = settings.getEncryption();
 
   EncryptAction underTest = new EncryptAction(userSession, settings);
 
@@ -70,9 +68,7 @@ public class EncryptActionTest {
       File secretKeyFile = folder.newFile();
       FileUtils.writeStringToFile(secretKeyFile, "fCVFf/JHRi8Qwu5KLNva7g==");
 
-      encryption = new Encryption(secretKeyFile.getAbsolutePath());
-
-      when(settings.getEncryption()).thenReturn(encryption);
+      encryption.setPathToSecretKey(secretKeyFile.getAbsolutePath());
     } catch (IOException e) {
       Throwables.propagate(e);
     }
@@ -101,7 +97,6 @@ public class EncryptActionTest {
     assertThat(definition.isInternal()).isTrue();
     assertThat(definition.responseExampleAsString()).isNotEmpty();
     assertThat(definition.params()).hasSize(1);
-
   }
 
   @Test
@@ -128,6 +123,16 @@ public class EncryptActionTest {
     call("  ");
   }
 
+  @Test
+  public void fail_if_no_secret_key_available() {
+    encryption.setPathToSecretKey("unknown/path/to/secret/key");
+
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("No secret key available");
+
+    call("my value");
+  }
+
   private EncryptWsResponse call(@Nullable String value) {
     TestRequest request = ws.newRequest()
       .setMediaType(MediaTypes.PROTOBUF)