Add too weak custom encryption exception (#1119)

* Add too weak custom encryption exception
This commit is contained in:
Joël
2018-08-28 19:45:58 +02:00
committed by Ryan Baxter
parent 0395ad06cb
commit 4ee026b03d
3 changed files with 23 additions and 8 deletions

View File

@@ -163,11 +163,13 @@ public class EncryptionController {
}
private void checkEncryptorInstalled(String name, String profiles) {
if (this.encryptor == null
|| this.encryptor.locate(this.helper.getEncryptorKeys(name, profiles, ""))
.encrypt("FOO").equals("FOO")) {
if (this.encryptor == null) {
throw new KeyNotInstalledException();
}
if (this.encryptor.locate(this.helper.getEncryptorKeys(name, profiles, ""))
.encrypt("FOO").equals("FOO")) {
throw new EncryptionTooWeakException();
}
}
private String stripFormData(String data, MediaType type, boolean cipher) {
@@ -218,6 +220,15 @@ public class EncryptionController {
body.put("description", "No key was installed for encryption service");
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(EncryptionTooWeakException.class)
@ResponseBody
public ResponseEntity<Map<String, Object>> encryptionTooWeak() {
Map<String, Object> body = new HashMap<String, Object>();
body.put("status", "INVALID");
body.put("description", "The encryption algorithm is not strong enough");
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(InvalidCipherException.class)
@ResponseBody
@@ -238,6 +249,10 @@ class KeyNotInstalledException extends RuntimeException {
class KeyNotAvailableException extends RuntimeException {
}
@SuppressWarnings("serial")
class EncryptionTooWeakException extends RuntimeException {
}
@SuppressWarnings("serial")
class InvalidCipherException extends RuntimeException {
}
}

View File

@@ -35,7 +35,7 @@ public class EncryptionControllerMultiTextEncryptorTests {
encrypted, TEXT_PLAIN));
}
@Test(expected = KeyNotInstalledException.class)
@Test(expected = EncryptionTooWeakException.class)
public void shouldNotEncryptUsingNoOp() {
// given
String application = "unknown";
@@ -46,7 +46,7 @@ public class EncryptionControllerMultiTextEncryptorTests {
// then exception is thrown
}
@Test(expected = KeyNotInstalledException.class)
@Test(expected = EncryptionTooWeakException.class)
public void shouldNotDecryptUsingNoOp() {
// given
String application = "unknown";

View File

@@ -45,12 +45,12 @@ public class EncryptionControllerTests {
private EncryptionController controller = new EncryptionController(
new SingleTextEncryptorLocator(Encryptors.noOpText()));
@Test(expected = KeyNotInstalledException.class)
@Test(expected = EncryptionTooWeakException.class)
public void cannotDecryptWithoutKey() {
this.controller.decrypt("foo", MediaType.TEXT_PLAIN);
}
@Test(expected = KeyNotInstalledException.class)
@Test(expected = EncryptionTooWeakException.class)
public void cannotDecryptWithNoopEncryptor() {
this.controller.decrypt("foo", MediaType.TEXT_PLAIN);
}