Handle exception thrown during base64 decoding
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Fri, 14 Feb 2020 09:31:37 +0000 (10:31 +0100)
committerBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Fri, 14 Feb 2020 09:31:37 +0000 (10:31 +0100)
Issue-ID: AAF-995
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I37e47382dc998bead008c47e34e3de417312fefb

certService/src/main/java/org/onap/aaf/certservice/certification/CsrModelFactory.java
certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java

index 6794bd6..4abf4d0 100644 (file)
@@ -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();
+            }
         }
     }
 
index 77594ed..5f48b2b 100644 (file)
@@ -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));
+    }
 }