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;
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);
}
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;
@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);
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);
}
assertThat(definition.isInternal()).isTrue();
assertThat(definition.responseExampleAsString()).isNotEmpty();
assertThat(definition.params()).hasSize(1);
-
}
@Test
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)