From c435e2cb9c2aa02315e5ca5df9515b056dc681e8 Mon Sep 17 00:00:00 2001 From: Remigiusz Janeczek Date: Wed, 2 Dec 2020 09:14:51 +0100 Subject: [OOM CERT-SERVICE-CLIENT] Fix null pointer when sans empty Issue-ID: OOM-2632 Signed-off-by: Remigiusz Janeczek Change-Id: I5e05eb2923b96313cb1d37eb844862289d6acae1 --- certService/pom.xml | 4 +- certService/version.properties | 2 +- certServiceClient/pom.xml | 4 +- .../client/certification/CsrFactory.java | 3 +- .../client/certification/CsrFactoryTest.java | 50 +++++++++++++++++----- certServiceClient/version.properties | 2 +- certServiceK8sExternalProvider/pom.xml | 2 +- certServicePostProcessor/pom.xml | 4 +- pom.xml | 2 +- version.properties | 2 +- 10 files changed, 53 insertions(+), 22 deletions(-) diff --git a/certService/pom.xml b/certService/pom.xml index e3e98174..e6a8672e 100644 --- a/certService/pom.xml +++ b/certService/pom.xml @@ -18,10 +18,10 @@ org.onap.oom.platform.cert-service oom-certservice - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT oom-certservice-api - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT oom-certservice-api OOM Certification Service Api jar diff --git a/certService/version.properties b/certService/version.properties index 8d40756c..f1c5779d 100644 --- a/certService/version.properties +++ b/certService/version.properties @@ -1,6 +1,6 @@ major=2 minor=3 -patch=0 +patch=1 base_version=${major}.${minor}.${patch} release_version=${base_version} snapshot_version=${base_version}-SNAPSHOT diff --git a/certServiceClient/pom.xml b/certServiceClient/pom.xml index d0cb5bd1..b1b2af92 100644 --- a/certServiceClient/pom.xml +++ b/certServiceClient/pom.xml @@ -18,12 +18,12 @@ oom-certservice org.onap.oom.platform.cert-service - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT 4.0.0 oom-certservice-client - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT oom-certservice-client OOM Certification Service Api Client jar diff --git a/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java b/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java index 1215e699..4612854d 100644 --- a/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java +++ b/certServiceClient/src/main/java/org/onap/oom/certservice/client/certification/CsrFactory.java @@ -34,6 +34,7 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import javax.security.auth.x500.X500Principal; +import org.apache.commons.collections.CollectionUtils; import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; import org.bouncycastle.asn1.x509.Extension; import org.bouncycastle.asn1.x509.Extensions; @@ -98,7 +99,7 @@ public class CsrFactory { JcaPKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subject, keyPair.getPublic()); - if (!configuration.getSans().isEmpty()) { + if (!CollectionUtils.isEmpty(configuration.getSans())) { builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, generateSansExtension()); } diff --git a/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java b/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java index ab9fc921..27c20e0e 100644 --- a/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java +++ b/certServiceClient/src/test/java/org/onap/oom/certservice/client/certification/CsrFactoryTest.java @@ -35,27 +35,57 @@ import org.onap.oom.certservice.client.configuration.model.San; class CsrFactoryTest { - CsrConfiguration config = mock(CsrConfiguration.class); + private CsrConfiguration config = mock(CsrConfiguration.class); @Test void createEncodedCsr_shouldSucceedWhenAllFieldsAreSetCorrectly() throws KeyPairGenerationException, CsrGenerationException { - KeyPair keyPair = - new KeyPairFactory(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM, - EncryptionAlgorithmConstants.KEY_SIZE).create(); - San san1 = new San("onapexample.com", GeneralName.dNSName); - San san2 = new San("onapexample.com.pl", GeneralName.dNSName); + KeyPair keyPair = createKeyPair(); + + mockRequiredConfigFields(); + mockOptionalConfigFields(); + + assertThat(new CsrFactory(config).createCsrInPem(keyPair)).isNotEmpty(); + } + @Test + void createEncodedCsr_shouldSucceedWhenRequiredFieldsAreSetCorrectly() + throws KeyPairGenerationException, CsrGenerationException { + + KeyPair keyPair = createKeyPair(); + + mockRequiredConfigFields(); + mockOptionalConfigFieldsEmpty(); + + assertThat(new CsrFactory(config).createCsrInPem(keyPair)).isNotEmpty(); + } + + private KeyPair createKeyPair() { + return new KeyPairFactory(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM, + EncryptionAlgorithmConstants.KEY_SIZE).create(); + } + + private void mockRequiredConfigFields() { when(config.getCommonName()).thenReturn("onap.org"); - when(config.getSans()).thenReturn(List.of(san1, san2)); + when(config.getOrganization()).thenReturn("Linux-Foundation"); when(config.getCountry()).thenReturn("US"); + when(config.getState()).thenReturn("California"); + } + + private void mockOptionalConfigFields() { + San san1 = new San("onapexample.com", GeneralName.dNSName); + San san2 = new San("onapexample.com.pl", GeneralName.dNSName); + when(config.getLocation()).thenReturn("San-Francisco"); - when(config.getOrganization()).thenReturn("Linux-Foundation"); + when(config.getSans()).thenReturn(List.of(san1, san2)); when(config.getOrganizationUnit()).thenReturn("ONAP"); - when(config.getState()).thenReturn("California"); + } - assertThat(new CsrFactory(config).createCsrInPem(keyPair)).isNotEmpty(); + private void mockOptionalConfigFieldsEmpty() { + when(config.getLocation()).thenReturn(null); + when(config.getSans()).thenReturn(null); + when(config.getOrganizationUnit()).thenReturn(null); } } diff --git a/certServiceClient/version.properties b/certServiceClient/version.properties index 8d40756c..f1c5779d 100644 --- a/certServiceClient/version.properties +++ b/certServiceClient/version.properties @@ -1,6 +1,6 @@ major=2 minor=3 -patch=0 +patch=1 base_version=${major}.${minor}.${patch} release_version=${base_version} snapshot_version=${base_version}-SNAPSHOT diff --git a/certServiceK8sExternalProvider/pom.xml b/certServiceK8sExternalProvider/pom.xml index 22c4757e..fe1d657f 100644 --- a/certServiceK8sExternalProvider/pom.xml +++ b/certServiceK8sExternalProvider/pom.xml @@ -5,7 +5,7 @@ oom-certservice org.onap.oom.platform.cert-service - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT 4.0.0 diff --git a/certServicePostProcessor/pom.xml b/certServicePostProcessor/pom.xml index bd79d85d..66444649 100644 --- a/certServicePostProcessor/pom.xml +++ b/certServicePostProcessor/pom.xml @@ -5,12 +5,12 @@ oom-certservice org.onap.oom.platform.cert-service - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT 4.0.0 oom-certservice-post-processor - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT oom-certservice-post-processor An application which conducts certificate post-processing like: merging truststores, copying keystores. jar diff --git a/pom.xml b/pom.xml index a1738a1c..7dee166c 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ org.onap.oom.platform.cert-service oom-certservice - 2.3.0-SNAPSHOT + 2.3.1-SNAPSHOT oom-certservice OOM Certification Service pom diff --git a/version.properties b/version.properties index 8d40756c..f1c5779d 100644 --- a/version.properties +++ b/version.properties @@ -1,6 +1,6 @@ major=2 minor=3 -patch=0 +patch=1 base_version=${major}.${minor}.${patch} release_version=${base_version} snapshot_version=${base_version}-SNAPSHOT -- cgit 1.2.3-korg