diff options
author | Remigiusz Janeczek <remigiusz.janeczek@nokia.com> | 2020-12-02 09:14:51 +0100 |
---|---|---|
committer | Remigiusz Janeczek <remigiusz.janeczek@nokia.com> | 2020-12-02 09:26:54 +0100 |
commit | c435e2cb9c2aa02315e5ca5df9515b056dc681e8 (patch) | |
tree | db64a4437d0f1007f91557f11728551bf3bbe631 /certServiceClient/src | |
parent | b9afa7d1d25f63a68dcb03fda523cd968323bddc (diff) |
[OOM CERT-SERVICE-CLIENT] Fix null pointer when sans empty2.3.1
Issue-ID: OOM-2632
Signed-off-by: Remigiusz Janeczek <remigiusz.janeczek@nokia.com>
Change-Id: I5e05eb2923b96313cb1d37eb844862289d6acae1
Diffstat (limited to 'certServiceClient/src')
2 files changed, 42 insertions, 11 deletions
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); } } |