From 26f799d695ff5a0527998f15fe6a5119e660c9df Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 5 Sep 2016 17:15:58 +0200 Subject: [PATCH] SONAR-7982 WS settings/encrypt fails properly when no secret key available --- .../server/setting/ws/EncryptAction.java | 6 +++++- .../server/setting/ws/EncryptActionTest.java | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java index b8150733d83..2615d6590a1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java @@ -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); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java index b5c7a09c69e..a9665351790 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java @@ -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) -- 2.39.5