diff options
author | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2020-06-09 15:37:46 +0200 |
---|---|---|
committer | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2020-06-16 14:37:56 +0200 |
commit | b288b7ab24f33af72e9c0fedecbb9979d1b4afc7 (patch) | |
tree | 1344fef298151973e7d2626f79571f63d6a7cca2 /certServiceClient/src/main/java | |
parent | 1a890623eff92322a033943ba8e46d2e33b9c2c5 (diff) |
Refactor flow of cert files generation, based on OUTPUT_TYPE parameter
-Add artifacts creator provider (strategy pattern)
-Refactor KeystoreTruststoreCreator
-Add new exception: CertOutputTypeNotSupported
-Change Unit tests
Issue-ID: AAF-1152
Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com>
Change-Id: If2b2fa50d551e72f19319d781bfb6079d07c7b83
Diffstat (limited to 'certServiceClient/src/main/java')
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java | 16 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java | 3 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreator.java (renamed from certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorFactory.java) | 18 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java | 66 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java (renamed from certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreator.java) | 16 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.java | 35 |
6 files changed, 129 insertions, 25 deletions
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java index 1b5b8ee3..27e8a4f0 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java @@ -23,8 +23,7 @@ import org.onap.aaf.certservice.client.api.ExitableException; import org.onap.aaf.certservice.client.certification.CsrFactory; import org.onap.aaf.certservice.client.certification.KeyPairFactory; import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder; -import org.onap.aaf.certservice.client.certification.conversion.KeystoreTruststoreCreator; -import org.onap.aaf.certservice.client.certification.conversion.KeystoreTruststoreCreatorFactory; +import org.onap.aaf.certservice.client.certification.conversion.ArtifactsCreatorProvider; import org.onap.aaf.certservice.client.common.Base64Encoder; import org.onap.aaf.certservice.client.configuration.EnvsForClient; import org.onap.aaf.certservice.client.configuration.EnvsForCsr; @@ -78,12 +77,15 @@ public class CertServiceClient { base64Encoder.encode(csrFactory.createCsrInPem(keyPair)), base64Encoder.encode(pkEncoder.encodePrivateKeyToPem(keyPair.getPrivate()))); - KeystoreTruststoreCreator filesCreator = new KeystoreTruststoreCreatorFactory( - clientConfiguration.getCertsOutputPath()).create(); - filesCreator.createKeystore(certServiceData.getCertificateChain(), keyPair.getPrivate()); - filesCreator.createTruststore(certServiceData.getTrustedCertificates()); + ArtifactsCreatorProvider + .getCreator(clientConfiguration.getOutputType(), + clientConfiguration.getCertsOutputPath()) + .create(certServiceData.getCertificateChain(), + certServiceData.getTrustedCertificates(), + keyPair.getPrivate()); + } catch (ExitableException e) { - LOGGER.error("Cert Service Client fail in execution: ", e); + LOGGER.error("Cert Service Client fails in execution: ", e); appExitHandler.exit(e.applicationExitStatus()); } appExitHandler.exit(SUCCESS); 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 41217e76..00057829 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 @@ -30,7 +30,8 @@ public enum ExitStatus { 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"), - OUTPUT_TYPE_PARAMETER_VALIDATION_EXCEPTION(10, "Invalid value of the OUTPUT_TYPE parameter"); + OUTPUT_TYPE_PARAMETER_VALIDATION_EXCEPTION(10, "Invalid value of the OUTPUT_TYPE parameter"), + CERT_OUTPUT_TYPE_NOT_SUPPORTED_EXCEPTION(11, "Certificate creation type is not supported"); private final int value; private final String message; diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreator.java index 8c719535..8907c481 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreator.java @@ -16,20 +16,14 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.onap.aaf.certservice.client.certification.conversion; -public class KeystoreTruststoreCreatorFactory { - private final String outputPath; +import org.onap.aaf.certservice.client.api.ExitableException; - public KeystoreTruststoreCreatorFactory(String outputPath) { - this.outputPath = outputPath; - } +import java.security.PrivateKey; +import java.util.List; - public KeystoreTruststoreCreator create() { - return new KeystoreTruststoreCreator( - new PKCS12FilesCreator(outputPath), - new RandomPasswordGenerator(), - new PemToPKCS12Converter()); - } +public interface ArtifactsCreator { + void create(List<String> keystoreData, List<String> truststoreData, PrivateKey privateKey) + throws ExitableException; } 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 new file mode 100644 index 00000000..6fbf373b --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java @@ -0,0 +1,66 @@ +/*============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 org.onap.aaf.certservice.client.certification.exception.CertOutputTypeNotSupportedException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +public enum ArtifactsCreatorProvider { + + P12 { + @Override + ArtifactsCreator create(String outputPath) { + return new PKCS12ArtifactsCreator( + new PKCS12FilesCreator(outputPath), + new RandomPasswordGenerator(), + new PemToPKCS12Converter()); + } + }, + JKS { + @Override + ArtifactsCreator create(String outputPath) { + return null; + } + }, + PEM { + @Override + ArtifactsCreator create(String outputPath) { + return null; + } + }; + + private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactsCreatorProvider.class); + + public static ArtifactsCreator getCreator(String outputType, String outputPath) + throws CertOutputTypeNotSupportedException { + try { + LOGGER.info("Artifact creation type selected: {}", outputType); + return valueOf(outputType).create(outputPath); + } catch (IllegalArgumentException e) { + LOGGER.error("Artifact creation type: {} is not supported. Supported types: {}", + outputType, Arrays.toString(values())); + throw new CertOutputTypeNotSupportedException(e); + } + } + + abstract ArtifactsCreator create(String outputPath); +} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreator.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java index 43784609..c07dfd11 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreator.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java @@ -23,7 +23,7 @@ import java.security.PrivateKey; import java.util.List; import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; -public class KeystoreTruststoreCreator { +public class PKCS12ArtifactsCreator implements ArtifactsCreator { private static final String CERTIFICATE_ALIAS = "certificate"; private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-"; @@ -32,21 +32,27 @@ public class KeystoreTruststoreCreator { private final PemToPKCS12Converter converter; private final PKCS12FilesCreator creator; - public KeystoreTruststoreCreator(PKCS12FilesCreator creator, RandomPasswordGenerator generator, - PemToPKCS12Converter converter) { + public PKCS12ArtifactsCreator(PKCS12FilesCreator creator, RandomPasswordGenerator generator, + PemToPKCS12Converter converter) { this.generator = generator; this.converter = converter; this.creator = creator; } - public void createKeystore(List<String> data, PrivateKey privateKey) + @Override + public void create(List<String> keystoreData, List<String> truststoreData, PrivateKey privateKey) throws PemToPKCS12ConverterException { + createKeystore(keystoreData,privateKey); + createTruststore(truststoreData); + } + + private void createKeystore(List<String> data, PrivateKey privateKey) throws PemToPKCS12ConverterException { Password password = generator.generate(PASSWORD_LENGTH); creator.saveKeystoreData(converter.convertKeystore(data, password, CERTIFICATE_ALIAS, privateKey), password.getCurrentPassword()); } - public void createTruststore(List<String> data) + private void createTruststore(List<String> data) throws PemToPKCS12ConverterException { Password password = generator.generate(PASSWORD_LENGTH); creator.saveTruststoreData(converter.convertTruststore(data, password, TRUSTED_CERTIFICATE_ALIAS), diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.java new file mode 100644 index 00000000..3c9581ac --- /dev/null +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.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 CertOutputTypeNotSupportedException extends ExitableException { + private static final ExitStatus EXIT_STATUS = ExitStatus.CERT_OUTPUT_TYPE_NOT_SUPPORTED_EXCEPTION; + + public CertOutputTypeNotSupportedException(Throwable e) { + super(e); + } + + public ExitStatus applicationExitStatus() { + return EXIT_STATUS; + } +} |