From 212038b654728b79aa647e08da2562484c63c883 Mon Sep 17 00:00:00 2001 From: Joanna Jeremicz Date: Wed, 17 Jun 2020 10:48:20 +0200 Subject: Add PEM artifacts creation with unit tests Issue-ID: AAF-1152 Change-Id: I95afd62330f3111f916507d628d142262ff951cc Signed-off-by: Joanna Jeremicz --- certServiceClient/README.md | 1 + .../aaf/certservice/client/api/ExitStatus.java | 3 +- .../conversion/ArtifactsCreatorProvider.java | 37 +++---- .../conversion/PKCS12ArtifactsCreator.java | 40 ++++++-- .../conversion/PKCS12FilesCreator.java | 73 -------------- .../conversion/PemArtifactsCreator.java | 62 ++++++++++++ .../exception/CertFileWriterException.java | 35 +++++++ .../certification/writer/CertFileWriter.java | 48 +++++++++ .../factory/AbstractConfigurationFactory.java | 2 +- .../conversion/ArtifactsCreatorProviderTest.java | 17 +++- .../conversion/PKCS12ArtifactsCreatorTest.java | 24 +++-- .../conversion/PKCS12FilesCreatorTest.java | 111 --------------------- .../conversion/PemArtifactsCreatorTest.java | 57 +++++++++++ .../certification/writer/CertFileWriterTest.java | 81 +++++++++++++++ 14 files changed, 362 insertions(+), 229 deletions(-) delete mode 100644 certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java create mode 100644 certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreator.java create mode 100644 certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertFileWriterException.java create mode 100644 certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java delete mode 100644 certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreatorTest.java create mode 100644 certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java create mode 100644 certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java diff --git a/certServiceClient/README.md b/certServiceClient/README.md index 849db4f1..56c33bdf 100644 --- a/certServiceClient/README.md +++ b/certServiceClient/README.md @@ -71,3 +71,4 @@ docker logs aaf-certservice-client 7 Fail in PKCS12 conversion 8 Fail in Private Key to PEM Encoding 9 Wrong TLS configuration +10 File could not be created diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java index 78ecc778..6e91fe84 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java @@ -29,7 +29,8 @@ public enum ExitStatus { HTTP_CLIENT_EXCEPTION(6,"Internal HTTP Client connection problem"), PKCS12_CONVERSION_EXCEPTION(7,"Fail in PKCS12 conversion"), PK_TO_PEM_ENCODING_EXCEPTION(8,"Fail in Private Key to PEM Encoding"), - TLS_CONFIGURATION_EXCEPTION(9, "Invalid TLS configuration"); + TLS_CONFIGURATION_EXCEPTION(9, "Invalid TLS configuration"), + FILE_CREATION_EXCEPTION(10, "File could not be created"); private final int value; private final String message; diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java index 4ed86a4d..dd4df73b 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java @@ -18,42 +18,37 @@ */ package org.onap.aaf.certservice.client.certification.conversion; +import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder; +import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; + public enum ArtifactsCreatorProvider { - P12("P12") { + P12 { @Override - ArtifactsCreator create(String outputPath) { + ArtifactsCreator create(String destPath) { return new PKCS12ArtifactsCreator( - new PKCS12FilesCreator(outputPath), + new CertFileWriter(destPath), new RandomPasswordGenerator(), new PemToPKCS12Converter()); } }, - JKS("JKS") { + JKS { @Override - ArtifactsCreator create(String outputPath) { + ArtifactsCreator create(String destPath) { return null; } }, - PEM("PEM") { + PEM { @Override - ArtifactsCreator create(String outputPath) { - return null; + ArtifactsCreator create(String destPath) { + return new PemArtifactsCreator( + new CertFileWriter(destPath), + new PrivateKeyToPemEncoder()); } }; - private final String name; - - ArtifactsCreatorProvider(String name) { - this.name = name; - } - - public static ArtifactsCreator getCreator(String outputType, String outputPath) { - return valueOf(outputType).create(outputPath); - } - - public String getName() { - return name; + public static ArtifactsCreator getCreator(String outputType, String destPath) { + return valueOf(outputType).create(destPath); } - abstract ArtifactsCreator create(String outputPath); + abstract ArtifactsCreator create(String destPath); } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java index c07dfd11..c1e7c1c8 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java @@ -21,41 +21,61 @@ package org.onap.aaf.certservice.client.certification.conversion; import java.security.PrivateKey; import java.util.List; +import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException; import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; +import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PKCS12ArtifactsCreator implements ArtifactsCreator { + private static final Logger LOGGER = LoggerFactory.getLogger(PKCS12ArtifactsCreator.class); private static final String CERTIFICATE_ALIAS = "certificate"; private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-"; private static final int PASSWORD_LENGTH = 24; + private static final String KEYSTORE_P12 = "keystore.p12"; + private static final String KEYSTORE_PASS = "keystore.pass"; + private static final String TRUSTSTORE_P12 = "truststore.p12"; + private static final String TRUSTSTORE_PASS = "truststore.pass"; private final RandomPasswordGenerator generator; private final PemToPKCS12Converter converter; - private final PKCS12FilesCreator creator; + private final CertFileWriter writer; - public PKCS12ArtifactsCreator(PKCS12FilesCreator creator, RandomPasswordGenerator generator, + public PKCS12ArtifactsCreator(CertFileWriter writer, RandomPasswordGenerator generator, PemToPKCS12Converter converter) { this.generator = generator; this.converter = converter; - this.creator = creator; + this.writer = writer; } @Override - public void create(List keystoreData, List truststoreData, PrivateKey privateKey) throws PemToPKCS12ConverterException { + public void create(List keystoreData, List truststoreData, PrivateKey privateKey) + throws PemToPKCS12ConverterException, CertFileWriterException { createKeystore(keystoreData,privateKey); createTruststore(truststoreData); } private void createKeystore(List data, PrivateKey privateKey) - throws PemToPKCS12ConverterException { + throws PemToPKCS12ConverterException, CertFileWriterException { Password password = generator.generate(PASSWORD_LENGTH); - creator.saveKeystoreData(converter.convertKeystore(data, password, CERTIFICATE_ALIAS, privateKey), - password.getCurrentPassword()); + + LOGGER.debug("Attempt to create PKCS12 keystore files and saving data. File names: {}, {}", KEYSTORE_P12, KEYSTORE_PASS); + + writer.saveData(converter.convertKeystore(data, password, CERTIFICATE_ALIAS, privateKey), KEYSTORE_P12); + writer.saveData(getPasswordAsBytes(password), KEYSTORE_PASS); } private void createTruststore(List data) - throws PemToPKCS12ConverterException { + throws PemToPKCS12ConverterException, CertFileWriterException { Password password = generator.generate(PASSWORD_LENGTH); - creator.saveTruststoreData(converter.convertTruststore(data, password, TRUSTED_CERTIFICATE_ALIAS), - password.getCurrentPassword()); + + LOGGER.debug("Attempt to create PKCS12 truststore files and saving data. File names: {}, {}", TRUSTSTORE_P12, TRUSTSTORE_PASS); + + writer.saveData(converter.convertTruststore(data, password, TRUSTED_CERTIFICATE_ALIAS), TRUSTSTORE_P12); + writer.saveData(getPasswordAsBytes(password), TRUSTSTORE_PASS); + } + + private byte[] getPasswordAsBytes(Password password) { + return password.getCurrentPassword().getBytes(); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java deleted file mode 100644 index 9b0cfb78..00000000 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreator.java +++ /dev/null @@ -1,73 +0,0 @@ -/*============LICENSE_START======================================================= - * aaf-certservice-client - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.aaf.certservice.client.certification.conversion; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Path; - -import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -class PKCS12FilesCreator { - - private static final Logger LOGGER = LoggerFactory.getLogger(PKCS12FilesCreator.class); - private static final String KEYSTORE_JKS = "keystore.jks"; - private static final String KEYSTORE_PASS = "keystore.pass"; - private static final String TRUSTSTORE_JKS = "truststore.jks"; - private static final String TRUSTSTORE_PASS = "truststore.pass"; - private final String keystoreJksPath; - private final String keystorePassPath; - private final String truststoreJksPath; - private final String truststorePassPath; - - - PKCS12FilesCreator(String path) { - keystoreJksPath = Path.of(path, KEYSTORE_JKS).toString(); - keystorePassPath = Path.of(path, KEYSTORE_PASS).toString(); - truststoreJksPath = Path.of(path, TRUSTSTORE_JKS).toString(); - truststorePassPath = Path.of(path, TRUSTSTORE_PASS).toString(); - } - - void saveKeystoreData(byte[] keystoreData, String keystorePassword) throws PemToPKCS12ConverterException { - LOGGER.debug("Attempt to create PKCS12 keystore files and saving data. Keystore path: {}", keystoreJksPath); - - saveDataToLocation(keystoreData, keystoreJksPath); - saveDataToLocation(keystorePassword.getBytes(), keystorePassPath); - } - - void saveTruststoreData(byte[] truststoreData, String truststorePassword) - throws PemToPKCS12ConverterException { - LOGGER.debug("Attempt to create PKCS12 truststore files and saving data. Truststore path: {}", truststoreJksPath); - - saveDataToLocation(truststoreData, truststoreJksPath); - saveDataToLocation(truststorePassword.getBytes(), truststorePassPath); - } - - private void saveDataToLocation(byte[] data, String path) throws PemToPKCS12ConverterException { - try (FileOutputStream fos = new FileOutputStream(path)) { - fos.write(data); - } catch (IOException e) { - LOGGER.error("PKCS12 files creation failed, exception message: {}", e.getMessage()); - throw new PemToPKCS12ConverterException(e); - } - } -} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreator.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreator.java new file mode 100644 index 00000000..7a4cbfa9 --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreator.java @@ -0,0 +1,62 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.client.certification.conversion; + +import java.security.PrivateKey; +import java.util.List; +import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder; +import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException; +import org.onap.aaf.certservice.client.certification.exception.PkEncodingException; +import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PemArtifactsCreator implements ArtifactsCreator { + private static final Logger LOGGER = LoggerFactory.getLogger(PemArtifactsCreator.class); + + private static final String KEY_PEM = "key.pem"; + private static final String KEYSTORE_PEM = "keystore.pem"; + private static final String TRUSTSTORE_PEM = "truststore.pem"; + + private final CertFileWriter writer; + private final PrivateKeyToPemEncoder pkEncoder; + + public PemArtifactsCreator(CertFileWriter writer, PrivateKeyToPemEncoder pkEncoder) { + this.writer = writer; + this.pkEncoder = pkEncoder; + } + + @Override + public void create(List keystoreData, List truststoreData, PrivateKey privateKey) + throws PkEncodingException, CertFileWriterException { + LOGGER.debug("Attempt to create PEM private key file and saving data. File name: {}", KEY_PEM); + writer.saveData(pkEncoder.encodePrivateKeyToPem(privateKey).getBytes(), KEY_PEM); + + LOGGER.debug("Attempt to create PEM keystore file and saving data. File name: {}", KEYSTORE_PEM); + writer.saveData(getDataAsBytes(keystoreData), KEYSTORE_PEM); + + LOGGER.debug("Attempt to create PEM truststore file and saving data. File name: {}", TRUSTSTORE_PEM); + writer.saveData(getDataAsBytes(truststoreData), TRUSTSTORE_PEM); + } + + private byte[] getDataAsBytes(List data) { + return String.join("\n", data).getBytes(); + } +} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertFileWriterException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertFileWriterException.java new file mode 100644 index 00000000..e723ca11 --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertFileWriterException.java @@ -0,0 +1,35 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.client.certification.exception; + +import org.onap.aaf.certservice.client.api.ExitStatus; +import org.onap.aaf.certservice.client.api.ExitableException; + +public class CertFileWriterException extends ExitableException { + + public CertFileWriterException(Throwable e) { + super(e); + } + + @Override + public ExitStatus applicationExitStatus() { + return ExitStatus.FILE_CREATION_EXCEPTION; + } +} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java new file mode 100644 index 00000000..400c0b72 --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriter.java @@ -0,0 +1,48 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.client.certification.writer; + +import java.io.FileOutputStream; +import java.io.IOException; + +import java.nio.file.Path; +import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CertFileWriter { + + private static final Logger LOGGER = LoggerFactory.getLogger(CertFileWriter.class); + private final String destPath; + + public CertFileWriter(String destPath) { + this.destPath = destPath; + } + +public void saveData(byte[] data, String filename) throws CertFileWriterException { + LOGGER.debug("Attempt to save file {} in path {}", filename, destPath); + try (FileOutputStream outputStream = new FileOutputStream(Path.of(destPath, filename).toString())) { + outputStream.write(data); + } catch (IOException e) { + LOGGER.error("File creation failed, exception message: {}", e.getMessage()); + throw new CertFileWriterException(e); + } + } +} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java index 70faa6b4..a54c9263 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java @@ -59,7 +59,7 @@ public abstract class AbstractConfigurationFactory public boolean isOutputTypeValid(String outputType) { return Arrays.stream(ArtifactsCreatorProvider.values()) - .map(ArtifactsCreatorProvider::getName) + .map(ArtifactsCreatorProvider::toString) .anyMatch(name -> name.equals(outputType)); } diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java index be00003b..133d90d2 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java @@ -26,16 +26,27 @@ import static org.assertj.core.api.Assertions.assertThat; class ArtifactsCreatorProviderTest { - private static final String STRATEGY_P12 = "P12"; + private static final String P12 = "P12"; + private static final String PEM = "PEM"; private static final String TEST_PATH = "testPath"; @Test - void getStrategyOfStringShouldReturnCorrectCreator(){ + void artifactsProviderShouldReturnP12Creator(){ // when ArtifactsCreator artifactsCreator = - ArtifactsCreatorProvider.getCreator(STRATEGY_P12, TEST_PATH); + ArtifactsCreatorProvider.getCreator(P12, TEST_PATH); // then assertThat(artifactsCreator).isInstanceOf(PKCS12ArtifactsCreator.class); } + + @Test + void artifactsProviderShouldReturnPemCreator(){ + + // when + ArtifactsCreator artifactsCreator = + ArtifactsCreatorProvider.getCreator(PEM, TEST_PATH); + // then + assertThat(artifactsCreator).isInstanceOf(PemArtifactsCreator.class); + } } diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java index 13ac0a6e..4a690e5f 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java @@ -29,7 +29,9 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException; import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; +import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; class PKCS12ArtifactsCreatorTest { @@ -43,7 +45,7 @@ class PKCS12ArtifactsCreatorTest { private static final byte[] SAMPLE_KEYSTORE_BYTES = "this is a keystore test".getBytes(); private static final byte[] SAMPLE_TRUSTSTORE_BYTES = "this is a truststore test".getBytes(); - private PKCS12FilesCreator filesCreator; + private CertFileWriter certFileWriter; private RandomPasswordGenerator passwordGenerator; private PemToPKCS12Converter converter; private PrivateKey privateKey; @@ -52,17 +54,20 @@ class PKCS12ArtifactsCreatorTest { @BeforeEach void setUp() { - filesCreator = mock(PKCS12FilesCreator.class); + certFileWriter = mock(CertFileWriter.class); passwordGenerator = mock(RandomPasswordGenerator.class); converter = mock(PemToPKCS12Converter.class); privateKey = mock(PrivateKey.class); - artifactCreator = new PKCS12ArtifactsCreator(filesCreator, passwordGenerator, converter); + artifactCreator = new PKCS12ArtifactsCreator(certFileWriter, passwordGenerator, converter); } @Test - void generateArtifactsShouldCallConverterAndFilesCreatorMethods() throws PemToPKCS12ConverterException { + void artifactsCreatorShouldCauseCallOfConvertAndDataSaveMethods() + throws PemToPKCS12ConverterException, CertFileWriterException { // given mockPasswordGeneratorAndPKSC12Converter(); + final String keystoreP12 = "keystore.p12"; + final String keystorePass = "keystore.pass"; //when artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); @@ -70,16 +75,17 @@ class PKCS12ArtifactsCreatorTest { // then verify(converter, times(1)) .convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey); - verify(filesCreator, times(1)) - .saveKeystoreData(SAMPLE_KEYSTORE_BYTES, SAMPLE_PASSWORD.getCurrentPassword()); + verify(certFileWriter, times(1)) + .saveData(SAMPLE_KEYSTORE_BYTES, keystoreP12); + verify(certFileWriter, times(1)) + .saveData(SAMPLE_PASSWORD.getCurrentPassword().getBytes(), keystorePass); verify(converter, times(1)) .convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS); - verify(filesCreator, times(1)) - .saveTruststoreData(SAMPLE_TRUSTSTORE_BYTES, SAMPLE_PASSWORD.getCurrentPassword()); } @Test - void generateArtifactsMethodShouldCallPasswordGeneratorTwice() throws PemToPKCS12ConverterException { + void artifactsCreatorShouldCallPasswordGeneratorTwice() + throws PemToPKCS12ConverterException, CertFileWriterException { // given mockPasswordGeneratorAndPKSC12Converter(); diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreatorTest.java deleted file mode 100644 index 8e6e03c6..00000000 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreatorTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/*============LICENSE_START======================================================= - * aaf-certservice-client - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.aaf.certservice.client.certification.conversion; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; - -class PKCS12FilesCreatorTest { - - private static final String RESOURCES_PATH = "src/test/resources"; - private static final String OUTPUT_PATH = RESOURCES_PATH + "/generatedFiles/"; - private static final String KEYSTORE_PATH = OUTPUT_PATH + "keystore.jks"; - private static final String KEYSTORE_PASS_PATH = OUTPUT_PATH + "keystore.pass"; - private static final String TRUSTSTORE_PATH = OUTPUT_PATH + "truststore.jks"; - private static final String TRUSTSTORE_PASS_PATH = OUTPUT_PATH + "truststore.pass"; - private static final String ERROR_MESSAGE = "java.io.FileNotFoundException: src/test/resources/generatedFiles/thisPathDoesNotExist/keystore.jks (No such file or directory)"; - - private File outputDirectory = new File(OUTPUT_PATH); - - @BeforeEach - void createDirectory() { - outputDirectory.mkdir(); - } - - @AfterEach - void cleanUpFiles() { - List.of(outputDirectory.listFiles()).forEach(f -> f.delete()); - outputDirectory.delete(); - } - - @Test - void saveKeystoreDataShouldCreateFilesWithDataInGivenLocation() throws PemToPKCS12ConverterException, IOException { - // given - final byte[] data = new byte[]{-128, 1, 127}; - final String password = "onap123"; - File keystore = new File(KEYSTORE_PATH); - File keystorePass = new File(KEYSTORE_PASS_PATH); - PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH); - - // when - filesCreator.saveKeystoreData(data, password); - - // then - assertTrue(keystore.exists()); - assertTrue(keystorePass.exists()); - assertArrayEquals(data, Files.readAllBytes(Path.of(KEYSTORE_PATH))); - assertEquals(password, Files.readString(Path.of(KEYSTORE_PASS_PATH), StandardCharsets.UTF_8)); - } - - @Test - void saveTruststoreDataShouldCreateFilesWithDataInGivenLocation() - throws PemToPKCS12ConverterException, IOException { - // given - final byte[] data = new byte[]{-128, 1, 2, 3, 127}; - final String password = "nokia321"; - File truststore = new File(TRUSTSTORE_PATH); - File truststorePass = new File(TRUSTSTORE_PASS_PATH); - PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH); - - // when - filesCreator.saveTruststoreData(data, password); - - // then - assertTrue(truststore.exists()); - assertTrue(truststorePass.exists()); - assertArrayEquals(data, Files.readAllBytes(Path.of(TRUSTSTORE_PATH))); - assertEquals(password, Files.readString(Path.of(TRUSTSTORE_PASS_PATH), StandardCharsets.UTF_8)); - } - - @Test - void saveKeystoreDataShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() { - // given - final byte[] data = new byte[]{-128, 1, 2, 3, 0}; - final String password = "123aikon"; - PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH + "thisPathDoesNotExist/"); - - // when then - assertThatThrownBy(() -> filesCreator.saveKeystoreData(data, password)) - .isInstanceOf(PemToPKCS12ConverterException.class).hasMessage(ERROR_MESSAGE); - } -} \ No newline at end of file diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java new file mode 100644 index 00000000..9963d245 --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java @@ -0,0 +1,57 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.client.certification.conversion; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.security.PrivateKey; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.api.ExitableException; +import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder; +import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; + +class PemArtifactsCreatorTest { + private final String KEYSTORE_PEM = "keystore.pem"; + private final String TRUSTSTORE_PEM = "truststore.pem"; + private final String KEY_PEM = "key.pem"; + private final String KEY = "my private key"; + private CertFileWriter certFileWriter = mock(CertFileWriter.class); + private PrivateKey privateKey = mock(PrivateKey.class); + private PrivateKeyToPemEncoder pkEncoder = mock(PrivateKeyToPemEncoder.class); + + @Test + void pemArtifactsCreatorShouldCallRequiredMethods() throws ExitableException { + // given + final PemArtifactsCreator creator = new PemArtifactsCreator(certFileWriter, pkEncoder); + + // when + when(pkEncoder.encodePrivateKeyToPem(privateKey)).thenReturn(KEY); + creator.create(List.of("one", "two"), List.of("three", "four"), privateKey); + + // then + verify(certFileWriter, times(1)).saveData("one\ntwo".getBytes(), KEYSTORE_PEM); + verify(certFileWriter, times(1)).saveData("three\nfour".getBytes(), TRUSTSTORE_PEM); + verify(certFileWriter, times(1)).saveData(KEY.getBytes(), KEY_PEM); + } +} diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java new file mode 100644 index 00000000..443f5627 --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java @@ -0,0 +1,81 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aaf.certservice.client.certification.writer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException; + +class CertFileWriterTest { + + private static final String RESOURCES_PATH = "src/test/resources"; + private static final String OUTPUT_PATH = RESOURCES_PATH + "/generatedFiles/"; + private static final String TRUSTSTORE_P12 = "truststore.p12"; + private static final String ERROR_MESSAGE = "java.io.FileNotFoundException: src/test/resources/generatedFiles/thisPathDoesNotExist/truststore.p12 (No such file or directory)"; + + private File outputDirectory = new File(OUTPUT_PATH); + + @BeforeEach + void createDirectory() { + outputDirectory.mkdir(); + } + + @AfterEach + void cleanUpFiles() { + List.of(outputDirectory.listFiles()).forEach(f -> f.delete()); + outputDirectory.delete(); + } + + @Test + void certFileWriterShouldCreateFilesWithDataInGivenLocation() + throws IOException, CertFileWriterException { + // given + final byte[] data = new byte[]{-128, 1, 2, 3, 127}; + File truststore = new File(OUTPUT_PATH + TRUSTSTORE_P12); + CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH); + + // when + certFileWriter.saveData(data, TRUSTSTORE_P12); + + // then + assertThat(truststore.exists()).isTrue(); + assertThat(Files.readAllBytes(Path.of(OUTPUT_PATH + TRUSTSTORE_P12))).isEqualTo(data); + } + + @Test + void certFileWriterShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() { + // given + final byte[] data = new byte[]{-128, 1, 2, 3, 0}; + CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH + "thisPathDoesNotExist/"); + + // when then + assertThatThrownBy(() -> certFileWriter.saveData(data, TRUSTSTORE_P12)) + .isInstanceOf(CertFileWriterException.class).hasMessage(ERROR_MESSAGE); + } +} -- cgit 1.2.3-korg