summaryrefslogtreecommitdiffstats
path: root/certService/src
diff options
context:
space:
mode:
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-02-14 10:31:37 +0100
committerBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-02-14 10:31:37 +0100
commitca2c01591b33804be131b9112e703e17641d6c83 (patch)
treefaf8702e7ebd88d43f1bf33e40f8dc73c9bedfa4 /certService/src
parentc663e2f61287e612e351df2360306fb5a257a8bf (diff)
Handle exception thrown during base64 decoding
Issue-ID: AAF-995 Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> Change-Id: I37e47382dc998bead008c47e34e3de417312fefb
Diffstat (limited to 'certService/src')
-rw-r--r--certService/src/main/java/org/onap/aaf/certservice/certification/CsrModelFactory.java26
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java38
2 files changed, 53 insertions, 11 deletions
diff --git a/certService/src/main/java/org/onap/aaf/certservice/certification/CsrModelFactory.java b/certService/src/main/java/org/onap/aaf/certservice/certification/CsrModelFactory.java
index 6794bd6b..4abf4d04 100644
--- a/certService/src/main/java/org/onap/aaf/certservice/certification/CsrModelFactory.java
+++ b/certService/src/main/java/org/onap/aaf/certservice/certification/CsrModelFactory.java
@@ -21,6 +21,7 @@
package org.onap.aaf.certservice.certification;
import java.util.Base64;
+import java.util.Optional;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.util.io.pem.PemObject;
@@ -28,15 +29,12 @@ import org.onap.aaf.certservice.certification.exceptions.CsrDecryptionException;
import org.onap.aaf.certservice.certification.exceptions.DecryptionException;
import org.onap.aaf.certservice.certification.exceptions.KeyDecryptionException;
import org.onap.aaf.certservice.certification.model.CsrModel;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class CsrModelFactory {
- private static final Logger LOGGER = LoggerFactory.getLogger(CsrModelFactory.class);
private final PemObjectFactory pemObjectFactory
= new PemObjectFactory();
private final PKCS10CertificationRequestFactory certificationRequestFactory
@@ -45,7 +43,6 @@ public class CsrModelFactory {
public CsrModel createCsrModel(StringBase64 csr, StringBase64 privateKey)
throws DecryptionException {
- LOGGER.debug("Decoded CSR: \n{}", csr);
PKCS10CertificationRequest decodedCsr = decodeCsr(csr);
PemObject decodedPrivateKey = decodePrivateKey(privateKey);
return new CsrModel(decodedCsr, decodedPrivateKey);
@@ -53,17 +50,20 @@ public class CsrModelFactory {
private PemObject decodePrivateKey(StringBase64 privateKey)
throws KeyDecryptionException {
- return pemObjectFactory.createPemObject(privateKey.asString()).orElseThrow(
+
+ return privateKey.asString()
+ .flatMap(pemObjectFactory::createPemObject)
+ .orElseThrow(
() -> new KeyDecryptionException("Incorrect Key, decryption failed")
);
}
private PKCS10CertificationRequest decodeCsr(StringBase64 csr)
throws CsrDecryptionException {
- return pemObjectFactory.createPemObject(csr.asString())
- .flatMap(
- certificationRequestFactory::createKCS10CertificationRequest
- ).orElseThrow(
+ return csr.asString()
+ .flatMap(pemObjectFactory::createPemObject)
+ .flatMap(certificationRequestFactory::createKCS10CertificationRequest)
+ .orElseThrow(
() -> new CsrDecryptionException("Incorrect CSR, decryption failed")
);
}
@@ -76,8 +76,12 @@ public class CsrModelFactory {
this.value = value;
}
- public String asString() {
- return new String(decoder.decode(value));
+ public Optional<String> asString() {
+ try {
+ return Optional.of(new String(decoder.decode(value)));
+ } catch(RuntimeException e) {
+ return Optional.empty();
+ }
}
}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java
index 77594ed7..5f48b2bf 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java
@@ -106,4 +106,42 @@ class CsrModelFactoryTest {
assertTrue(actualMessage.contains(expectedMessage));
}
+
+ @Test
+ void shouldThrowCsrDecryptionExceptionWhenCsrIsNotInBase64Encoding() {
+ // given
+ String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
+ String wrongCsr = "Not Base 64 Csr";
+
+ // when
+ Exception exception = assertThrows(
+ CsrDecryptionException.class, () -> csrModelFactory
+ .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
+ );
+
+ String expectedMessage = "Incorrect CSR, decryption failed";
+ String actualMessage = exception.getMessage();
+
+ // then
+ assertTrue(actualMessage.contains(expectedMessage));
+ }
+
+ @Test
+ void shouldThrowKeyDecryptionExceptionWhenPKIsNotInBase64Encoding() {
+ // given
+ String encoderPK = "Not Base64 Key";
+ String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
+
+ // when
+ Exception exception = assertThrows(
+ KeyDecryptionException.class, () -> csrModelFactory
+ .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
+ );
+
+ String expectedMessage = "Incorrect Key, decryption failed";
+ String actualMessage = exception.getMessage();
+
+ // then
+ assertTrue(actualMessage.contains(expectedMessage));
+ }
}