diff options
author | 2020-09-02 14:35:32 +0200 | |
---|---|---|
committer | 2020-09-09 14:03:01 +0200 | |
commit | a9fd6d0a175e647ac36932ce77b91f9e54e97084 (patch) | |
tree | 2c9c150c07162770be7629b0bd665185cff7f9b0 /trustStoreMerger/src/main | |
parent | 4014c7482b233bba9e344b9b3fbe6b7641ebdcfd (diff) |
Refactor truststore merger logic
- Merge PemTruststore and JavaTruststore into Truststore
- Rename controller classes to be consistent
- Remove duplicated methods
Issue-ID: DCAEGEN2-2253
Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Change-Id: I3270cd9811e5eaf360ceea0d0ca99be1155eaf00
Diffstat (limited to 'trustStoreMerger/src/main')
29 files changed, 265 insertions, 415 deletions
diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java index babd32e2..7f53331f 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java @@ -22,12 +22,9 @@ package org.onap.oom.truststoremerger; import java.util.List; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory; -import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider; -import org.onap.oom.truststoremerger.certification.file.model.Truststore; -import org.onap.oom.truststoremerger.certification.file.provider.FileManager; -import org.onap.oom.truststoremerger.certification.file.provider.PasswordReader; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.TruststoreFilesProvider; +import org.onap.oom.truststoremerger.merger.model.Truststore; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; import org.onap.oom.truststoremerger.configuration.MergerConfigurationProvider; import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration; import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; @@ -69,8 +66,9 @@ class TrustStoreMerger { baseFile.createBackup(); for (int i = SECOND_TRUSTSTORE_INDEX; i < truststoreFilesList.size(); i++) { - List<CertificateWithAlias> certificateWrappers = truststoreFilesList.get(i).getCertificates(); - baseFile.addCertificate(certificateWrappers); + Truststore truststore = truststoreFilesList.get(i); + List<CertificateWithAlias> certificateWrappers = truststore.getCertificates(); + baseFile.addCertificates(certificateWrappers); } baseFile.saveFile(); @@ -87,13 +85,9 @@ class TrustStoreMerger { return factory.createConfiguration(); } - private List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException { - TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), - new PasswordReader()); - TruststoreFilesListProvider truststoreFilesListProvider = new TruststoreFilesListProvider( - truststoreFileFactory); - return truststoreFilesListProvider - .getTruststoreFilesList( + private static List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException { + return TruststoreFilesProvider + .getTruststoreFiles( configuration.getTruststoreFilePaths(), configuration.getTruststoreFilePasswordPaths() ); diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java index 68c5d13c..75756aa1 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java @@ -21,8 +21,8 @@ package org.onap.oom.truststoremerger.api; public class CertificateConstants { - public static final String JKS_INSTANCE = "JKS"; - public static final String PKCS12_INSTANCE = "PKCS12"; + public static final String JKS_TYPE = "JKS"; + public static final String PKCS12_TYPE = "PKCS12"; public static final String X_509_CERTIFICATE = "X.509"; public static final String BOUNCY_CASTLE_PROVIDER = "BC"; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileFactory.java deleted file mode 100644 index d93409b6..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileFactory.java +++ /dev/null @@ -1,99 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * 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.oom.truststoremerger.certification.file; - -import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore; -import org.onap.oom.truststoremerger.certification.file.model.PemTruststore; -import org.onap.oom.truststoremerger.certification.file.model.Truststore; - -import java.io.File; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; -import org.onap.oom.truststoremerger.certification.file.provider.CertificateStoreControllerFactory; -import org.onap.oom.truststoremerger.certification.file.provider.FileManager; -import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController; -import org.onap.oom.truststoremerger.certification.file.provider.PasswordReader; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; - -public class TruststoreFileFactory { - - private static final String JKS_EXTENSION = ".jks"; - private static final String P12_EXTENSION = ".p12"; - private static final String PEM_EXTENSION = ".pem"; - private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist"; - private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s"; - - private final FileManager fileManager; - private final PasswordReader passwordReader; - private final CertificateStoreControllerFactory certificateStoreControllerFactory = - new CertificateStoreControllerFactory(); - - public TruststoreFileFactory(FileManager fileManager, PasswordReader passwordReader) { - this.fileManager = fileManager; - this.passwordReader = passwordReader; - } - - public Truststore create(String truststoreFilePath, String truststorePasswordPath) - throws TruststoreFileFactoryException, PasswordReaderException, KeystoreInstanceException, LoadTruststoreException { - File truststoreFile = new File(truststoreFilePath); - if (!fileManager.checkIfFileExists(truststoreFile)) { - throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile)); - } - return createTypedTruststore(truststoreFile, truststorePasswordPath); - } - - private Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath) - throws KeystoreInstanceException, PasswordReaderException, LoadTruststoreException, TruststoreFileFactoryException { - String extension = fileManager.getExtension(truststoreFile); - switch (extension) { - case JKS_EXTENSION: - return createJksTruststore(truststoreFile, truststorePasswordPath); - case P12_EXTENSION: - return createP12Truststore(truststoreFile, truststorePasswordPath); - case PEM_EXTENSION: - return createPemTruststore(truststoreFile); - default: - throw new TruststoreFileFactoryException( - String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension)); - } - } - - private JavaTruststore createJksTruststore(File truststoreFile, String truststorePasswordPath) - throws PasswordReaderException, LoadTruststoreException, KeystoreInstanceException { - String password = passwordReader.readPassword(new File(truststorePasswordPath)); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedJksCertificateStoreController(truststoreFile, password); - return new JavaTruststore(truststoreFile, storeController); - } - - private JavaTruststore createP12Truststore(File truststoreFile, String truststorePasswordPath) - throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { - String password = passwordReader.readPassword(new File(truststorePasswordPath)); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedPkcs12CertificateStoreController(truststoreFile, password); - return new JavaTruststore(truststoreFile, storeController); - } - - private PemTruststore createPemTruststore(File truststoreFile) { - return new PemTruststore(truststoreFile, new PemCertificateController(truststoreFile)); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststore.java deleted file mode 100644 index d46fba1e..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststore.java +++ /dev/null @@ -1,58 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * 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.oom.truststoremerger.certification.file.model; - -import java.io.File; -import java.util.List; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; -import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class JavaTruststore extends Truststore { - - private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststore.class); - private final JavaCertificateStoreController storeController; - - public JavaTruststore(File truststoreFile, JavaCertificateStoreController storeController) { - super(truststoreFile); - this.storeController = storeController; - } - - @Override - public List<CertificateWithAlias> getCertificates() throws ExitableException { - LOGGER.debug("Attempt ro read certificates from file: {} ", this.getFile().getPath()); - return storeController.getNotEmptyCertificateList(); - } - - @Override - public void addCertificate(List<CertificateWithAlias> certificates) throws ExitableException { - LOGGER.debug("Attempt to add certificates for saving to file"); - storeController.addCertificates(certificates); - } - - @Override - public void saveFile() throws WriteTruststoreFileException { - LOGGER.debug("Attempt to save file: {}", this.getFile().getPath()); - storeController.saveFile(); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststore.java deleted file mode 100644 index 36195267..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststore.java +++ /dev/null @@ -1,58 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * 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.oom.truststoremerger.certification.file.model; - -import java.io.File; -import java.util.List; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PemTruststore extends Truststore { - - private static final Logger LOGGER = LoggerFactory.getLogger(PemTruststore.class); - private final PemCertificateController pemCertificateController; - - public PemTruststore(File truststoreFile, PemCertificateController pemCertificateController) { - super(truststoreFile); - this.pemCertificateController = pemCertificateController; - } - - @Override - public List<CertificateWithAlias> getCertificates() throws ExitableException { - LOGGER.debug("Attempt ro read certificates from file: {}", this.getFile().getPath()); - return pemCertificateController.getNotEmptyCertificateList(); - } - - @Override - public void addCertificate(List<CertificateWithAlias> certificates) throws ExitableException { - LOGGER.debug("Attempt to add certificates for saving to file"); - pemCertificateController.addCertificates(certificates); - } - - @Override - public void saveFile() throws ExitableException { - LOGGER.debug("Attempt to save file: {}", this.getFile().getPath()); - pemCertificateController.saveFile(); - } - -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateStoreControllerFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateStoreControllerFactory.java deleted file mode 100644 index 66e2aed2..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateStoreControllerFactory.java +++ /dev/null @@ -1,61 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * 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.oom.truststoremerger.certification.file.provider; - -import static org.onap.oom.truststoremerger.api.CertificateConstants.JKS_INSTANCE; -import static org.onap.oom.truststoremerger.api.CertificateConstants.PKCS12_INSTANCE; - -import java.io.File; -import java.security.KeyStore; -import java.security.KeyStoreException; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class CertificateStoreControllerFactory { - - private static final Logger LOGGER = LoggerFactory.getLogger(CertificateStoreControllerFactory.class); - - public JavaCertificateStoreController createLoadedJksCertificateStoreController(File certFile, String certPassword) - throws LoadTruststoreException, KeystoreInstanceException { - return createLoadedCertificateStoreController(certFile, certPassword, JKS_INSTANCE); - } - - public JavaCertificateStoreController createLoadedPkcs12CertificateStoreController(File certFile, String certPassword) - throws KeystoreInstanceException, LoadTruststoreException { - return createLoadedCertificateStoreController(certFile, certPassword, PKCS12_INSTANCE); - } - - private JavaCertificateStoreController createLoadedCertificateStoreController(File certFile, String certPassword, - String instanceType) - throws LoadTruststoreException, KeystoreInstanceException { - try { - JavaCertificateStoreController javaCertificateStoreController = new JavaCertificateStoreController( - KeyStore.getInstance(instanceType), certFile, certPassword); - javaCertificateStoreController.loadFile(); - return javaCertificateStoreController; - } catch (KeyStoreException e) { - LOGGER.error("Cannot initialize Java Keystore instance"); - throw new KeystoreInstanceException(e); - } - } -} - diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/Truststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java index 153805a7..9187393e 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/Truststore.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java @@ -17,46 +17,32 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.model; +package org.onap.oom.truststoremerger.common; import java.io.File; import java.io.FileOutputStream; import java.nio.file.Files; -import java.util.List; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class Truststore { +public final class BackupCreator { - private static final Logger LOGGER = LoggerFactory.getLogger(Truststore.class); + private static final Logger LOGGER = LoggerFactory.getLogger(BackupCreator.class); private static final String BACKUP_EXTENSION = ".bak"; - private final File file; - Truststore(File file) { - this.file = file; + private BackupCreator() { } - public abstract List<CertificateWithAlias> getCertificates() throws ExitableException; - - public abstract void addCertificate(List<CertificateWithAlias> certificates) throws ExitableException; - - public abstract void saveFile() throws ExitableException; - - public File getFile() { - return file; - } - - public void createBackup() throws CreateBackupException { + public static void createBackup(File file) throws CreateBackupException { LOGGER.debug("Create backup of file: {}", file.getPath()); String backupFilePath = file.getAbsolutePath() + BACKUP_EXTENSION; try (FileOutputStream fileOutputStream = new FileOutputStream(backupFilePath)) { Files.copy(file.toPath(), fileOutputStream); } catch (Exception e) { - LOGGER.error("Cannot create backup of file: {} ", getFile().getPath()); + LOGGER.error("Cannot create backup of file: {} ", file.getPath()); throw new CreateBackupException(e); } + LOGGER.debug("Backup was successfully created in: {}", backupFilePath); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/ExtensionResolver.java index 12029ade..af792c48 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/ExtensionResolver.java @@ -17,15 +17,17 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import java.io.File; -public class FileManager { +public final class ExtensionResolver { private static final int INDEX_NOT_FOUND = -1; - public String getExtension(File file) { + private ExtensionResolver() {} + + public static String get(File file) { int extStartIndex = file.getName().lastIndexOf("."); if (extStartIndex == INDEX_NOT_FOUND) { return ""; @@ -33,7 +35,7 @@ public class FileManager { return file.getName().substring(extStartIndex).toLowerCase(); } - public boolean checkIfFileExists(File file) { + public static boolean checkIfFileExists(File file) { return file.exists(); } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java index d7da53b0..d84be5ac 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java @@ -17,17 +17,21 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import java.io.File; import java.io.IOException; import java.nio.file.Files; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; + +public final class PasswordReader { -public class PasswordReader { private static final String COULD_NOT_READ_PASSWORD_FROM_FILE_MSG_TEMPLATE = "Could not read password from file: %s"; - public String readPassword(File file) throws PasswordReaderException { + private PasswordReader() { + } + + public static String readPassword(File file) throws PasswordReaderException { try { return Files.readString(file.toPath()); } catch (IOException e) { diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java index 90a75d91..4bdfd9f4 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java @@ -24,7 +24,7 @@ import org.onap.oom.truststoremerger.api.ExitableException; public class MergerConfigurationException extends ExitableException { - public MergerConfigurationException(String message) { - super(message, ExitStatus.MERGER_CONFIGURATION_EXCEPTION); + public MergerConfigurationException(String errorMessage) { + super(errorMessage, ExitStatus.MERGER_CONFIGURATION_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java index dda53e32..6089d314 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java @@ -24,7 +24,7 @@ import org.onap.oom.truststoremerger.api.ExitableException; public class TruststoresPathsProviderException extends ExitableException { - public TruststoresPathsProviderException(String message) { - super(message, ExitStatus.TRUSTSTORES_PATHS_PROVIDER_EXCEPTION); + public TruststoresPathsProviderException(String errorMessage) { + super(errorMessage, ExitStatus.TRUSTSTORES_PATHS_PROVIDER_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFilesListProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProvider.java index 92e3c2a8..9108fb69 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFilesListProvider.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProvider.java @@ -17,37 +17,35 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file; - -import org.onap.oom.truststoremerger.certification.file.model.Truststore; +package org.onap.oom.truststoremerger.merger; import java.util.ArrayList; import java.util.List; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreFileFactoryException; +import org.onap.oom.truststoremerger.merger.model.TruststoreFactory; +import org.onap.oom.truststoremerger.merger.model.Truststore; -public class TruststoreFilesListProvider { +public class TruststoreFilesProvider { - private final TruststoreFileFactory truststoreFileFactory; - public TruststoreFilesListProvider(TruststoreFileFactory truststoreFileFactory) { - this.truststoreFileFactory = truststoreFileFactory; + private TruststoreFilesProvider() { } - public List<Truststore> getTruststoreFilesList(List<String> truststoreFilePaths, + public static List<Truststore> getTruststoreFiles(List<String> truststoreFilePaths, List<String> truststoreFilePasswordPaths) throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { - List<Truststore> truststoreFilesList = new ArrayList<>(); + List<Truststore> truststoreFiles = new ArrayList<>(); for (int i = 0; i < truststoreFilePaths.size(); i++) { String truststorePath = truststoreFilePaths.get(i); String passwordPath = truststoreFilePasswordPaths.get(i); - Truststore truststore = truststoreFileFactory.create(truststorePath, passwordPath); - truststoreFilesList.add(truststore); + Truststore truststore = TruststoreFactory.create(truststorePath, passwordPath); + truststoreFiles.add(truststore); } - return truststoreFilesList; + return truststoreFiles; } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/AliasConflictException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java index a4102d9f..71df3a45 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/AliasConflictException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java @@ -17,15 +17,15 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class AliasConflictException extends ExitableException { - public AliasConflictException(String message) { - super(message, ExitStatus.ALIAS_CONFLICT_EXCEPTION); + public AliasConflictException(String errorMessage) { + super(errorMessage, ExitStatus.ALIAS_CONFLICT_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/CreateBackupException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java index a21f7013..f655a9f5 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/CreateBackupException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class CreateBackupException extends ExitableException { - public CreateBackupException(Exception e) { - super(e, ExitStatus.CREATE_BACKUP_EXCEPTION); + public CreateBackupException(Exception cause) { + super(cause, ExitStatus.CREATE_BACKUP_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/KeystoreInstanceException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java index c5bcc3ca..99a955d9 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/KeystoreInstanceException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class KeystoreInstanceException extends ExitableException { - public KeystoreInstanceException(Throwable e) { - super(e, ExitStatus.KEYSTORE_INSTANCE_EXCEPTION); + public KeystoreInstanceException(Exception cause) { + super(cause, ExitStatus.KEYSTORE_INSTANCE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/LoadTruststoreException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java index b8bb53fa..810bf556 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/LoadTruststoreException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class LoadTruststoreException extends ExitableException { - public LoadTruststoreException(Throwable e) { - super(e, ExitStatus.TRUSTSTORE_LOAD_FILE_EXCEPTION); + public LoadTruststoreException(Exception cause) { + super(cause, ExitStatus.TRUSTSTORE_LOAD_FILE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/MissingTruststoreException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java index c502d6b6..9065c9d3 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/MissingTruststoreException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class MissingTruststoreException extends ExitableException { - public MissingTruststoreException(String message) { - super(message, ExitStatus.MISSING_TRUSTSTORE_EXCEPTION); + public MissingTruststoreException(String errorMessage) { + super(errorMessage, ExitStatus.MISSING_TRUSTSTORE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/PasswordReaderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java index d601d229..9f9d2e27 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/PasswordReaderException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java @@ -17,13 +17,13 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class PasswordReaderException extends ExitableException { - public PasswordReaderException(String message) { - super(message, ExitStatus.PASSWORD_READER_EXCEPTION); + public PasswordReaderException(String errorMessage) { + super(errorMessage, ExitStatus.PASSWORD_READER_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreDataOperationException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java index cf848f79..c18cb006 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreDataOperationException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class TruststoreDataOperationException extends ExitableException { - public TruststoreDataOperationException(Exception e) { - super(e, ExitStatus.TRUSTSTORE_DATA_OPERATION_EXCEPTION); + public TruststoreDataOperationException(Exception cause) { + super(cause, ExitStatus.TRUSTSTORE_DATA_OPERATION_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreFileFactoryException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java index 18349fd4..f802a9d3 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreFileFactoryException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class TruststoreFileFactoryException extends ExitableException { - public TruststoreFileFactoryException(String message) { - super(message, ExitStatus.TRUSTSTORE_FILE_FACTORY_EXCEPTION); + public TruststoreFileFactoryException(String errorMessage) { + super(errorMessage, ExitStatus.TRUSTSTORE_FILE_FACTORY_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/WriteTruststoreFileException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java index a5e02b3c..fe368868 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/WriteTruststoreFileException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java @@ -18,14 +18,14 @@ */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class WriteTruststoreFileException extends ExitableException { - public WriteTruststoreFileException(Exception e) { - super(e, ExitStatus.WRITE_TRUSTSTORE_FILE_EXCEPTION); + public WriteTruststoreFileException(Exception cause) { + super(cause, ExitStatus.WRITE_TRUSTSTORE_FILE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java index a4b129c9..e3a03996 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; import java.io.File; import java.io.FileInputStream; @@ -28,33 +28,40 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.exception.AliasConflictException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; -import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory; +import org.onap.oom.truststoremerger.merger.exception.AliasConflictException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException; +import org.onap.oom.truststoremerger.merger.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAliasFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class JavaCertificateStoreController implements CertificateController { +public final class JavaTruststore extends Truststore { - private static final Logger LOGGER = LoggerFactory.getLogger(JavaCertificateStoreController.class); + private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststore.class); private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); private final KeyStore keyStore; - private final File storeFile; private final String password; - public JavaCertificateStoreController(KeyStore keyStore, File storeFile, String password) { + private JavaTruststore(KeyStore keyStore, File storeFile, String password) { + super(storeFile); this.keyStore = keyStore; - this.storeFile = storeFile; this.password = password; } - public List<CertificateWithAlias> getNotEmptyCertificateList() throws ExitableException { + public static JavaTruststore createWithLoadingFile(KeyStore keyStore, File storeFile, String password) + throws LoadTruststoreException { + JavaTruststore javaTruststore = new JavaTruststore(keyStore, storeFile, password); + javaTruststore.loadFile(); + return javaTruststore; + } + + public List<CertificateWithAlias> getCertificates() throws ExitableException { + LOGGER.debug("Attempt to read certificates from file: {}", storeFile.getPath()); List<String> aliases = getTruststoreAliases(); if (aliases.isEmpty()) { throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); @@ -64,6 +71,7 @@ public class JavaCertificateStoreController implements CertificateController { public void addCertificates(List<CertificateWithAlias> certificatesWithAliases) throws ExitableException { + LOGGER.debug("Attempt to add certificates for saving to file"); if (getTruststoreAliases().isEmpty()) { throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); } @@ -73,7 +81,8 @@ public class JavaCertificateStoreController implements CertificateController { } public void saveFile() throws WriteTruststoreFileException { - try (FileOutputStream outputStream = new FileOutputStream(this.storeFile)) { + LOGGER.debug("Attempt to save file: {}", storeFile.getPath()); + try (FileOutputStream outputStream = new FileOutputStream(storeFile)) { keyStore.store(outputStream, this.password.toCharArray()); } catch (Exception e) { LOGGER.error("Cannot write truststore file"); @@ -81,11 +90,11 @@ public class JavaCertificateStoreController implements CertificateController { } } - public void loadFile() throws LoadTruststoreException { + private void loadFile() throws LoadTruststoreException { try { - keyStore.load(new FileInputStream(this.storeFile), this.password.toCharArray()); + keyStore.load(new FileInputStream(storeFile), this.password.toCharArray()); } catch (Exception e) { - LOGGER.error("Cannot load file: {}", this.storeFile.getPath()); + LOGGER.error("Cannot load file: {}", storeFile.getPath()); throw new LoadTruststoreException(e); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreFactory.java new file mode 100644 index 00000000..d40cfb1a --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreFactory.java @@ -0,0 +1,51 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * 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.oom.truststoremerger.merger.model; + +import java.io.File; +import java.security.KeyStore; +import java.security.KeyStoreException; +import org.onap.oom.truststoremerger.common.PasswordReader; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JavaTruststoreFactory { + + private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststoreFactory.class); + + private JavaTruststoreFactory() { + } + + public static Truststore create(File certFile, String truststorePasswordPath, String keystoreType) + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + String password = PasswordReader.readPassword(new File(truststorePasswordPath)); + try { + return JavaTruststore + .createWithLoadingFile(KeyStore.getInstance(keystoreType), certFile, password); + } catch (KeyStoreException e) { + LOGGER.error("Cannot initialize Java Keystore instance"); + throw new KeystoreInstanceException(e); + } + } +} + diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateController.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/PemTruststore.java index 9ff42b87..d04a01b1 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateController.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/PemTruststore.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; import static org.onap.oom.truststoremerger.api.CertificateConstants.BOUNCY_CASTLE_PROVIDER; import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; @@ -37,30 +37,30 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator; import org.bouncycastle.util.io.pem.PemObjectGenerator; import org.bouncycastle.util.io.pem.PemWriter; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; -import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException; +import org.onap.oom.truststoremerger.merger.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAliasFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class PemCertificateController implements CertificateController { +public class PemTruststore extends Truststore { - private static final Logger LOGGER = LoggerFactory.getLogger(PemCertificateController.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PemTruststore.class); private static final boolean APPEND_TO_FILE = true; private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); private final List<CertificateWithAlias> certificatesToBeSaved = new ArrayList<>(); - private final File file; - public PemCertificateController(File file) { - this.file = file; + public PemTruststore(File storeFile) { + super(storeFile); } - public List<CertificateWithAlias> getNotEmptyCertificateList() + public List<CertificateWithAlias> getCertificates() throws TruststoreDataOperationException, MissingTruststoreException { + LOGGER.debug("Attempt to read certificates from file: {}", storeFile.getPath()); if (isFileWithoutPemCertificate()) { throw new MissingTruststoreException("File does not contain any certificate"); } @@ -70,14 +70,16 @@ public class PemCertificateController implements CertificateController { public void addCertificates(List<CertificateWithAlias> certificates) throws TruststoreDataOperationException, MissingTruststoreException { + LOGGER.debug("Attempt to add certificates for saving to file"); if (isFileWithoutPemCertificate()) { - LOGGER.error("File does not contain any certificate. File path: {} ", this.file.getPath()); + LOGGER.error("File does not contain any certificate. File path: {} ", storeFile.getPath()); throw new MissingTruststoreException("File does not contain any certificate"); } certificatesToBeSaved.addAll(certificates); } public void saveFile() throws WriteTruststoreFileException, TruststoreDataOperationException { + LOGGER.debug("Attempt to save file: {}", storeFile.getPath()); List<Certificate> certificates = certificatesToBeSaved.stream() .map(CertificateWithAlias::getCertificate) .collect(Collectors.toList()); @@ -104,19 +106,17 @@ public class PemCertificateController implements CertificateController { return sw.toString(); } - private List<Certificate> extractCertificatesFromFile() throws TruststoreDataOperationException { - try (FileInputStream inputStream = new FileInputStream(this.file)) { + try (FileInputStream inputStream = new FileInputStream(storeFile)) { Security.addProvider(new BouncyCastleProvider()); CertificateFactory factory = CertificateFactory.getInstance(X_509_CERTIFICATE, BOUNCY_CASTLE_PROVIDER); return new ArrayList<>(factory.generateCertificates(inputStream)); } catch (Exception e) { - LOGGER.error("Cannot read certificates from file: {}", this.file.getPath()); + LOGGER.error("Cannot read certificates from file: {}", storeFile.getPath()); throw new TruststoreDataOperationException(e); } } - private List<PemObjectGenerator> transformToPemGenerators(List<Certificate> certificates) throws TruststoreDataOperationException { List<PemObjectGenerator> generators = new ArrayList<>(); @@ -145,7 +145,7 @@ public class PemCertificateController implements CertificateController { private void appendToFile(String certificatesAsString) throws WriteTruststoreFileException { try { - FileOutputStream fileOutputStream = new FileOutputStream(this.file, APPEND_TO_FILE); + FileOutputStream fileOutputStream = new FileOutputStream(storeFile, APPEND_TO_FILE); fileOutputStream.write(certificatesAsString.getBytes()); } catch (Exception e) { LOGGER.error("Cannot write certificates to file"); diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateController.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java index f2ed2c45..2c3acf49 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateController.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java @@ -17,17 +17,30 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; +import java.io.File; import java.util.List; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.common.BackupCreator; -public interface CertificateController { +public abstract class Truststore { - List<CertificateWithAlias> getNotEmptyCertificateList() throws ExitableException; + final File storeFile; - void addCertificates(List<CertificateWithAlias> certificates) throws ExitableException; + public Truststore(File storeFile) { + this.storeFile = storeFile; + } - void saveFile() throws ExitableException; + public void createBackup() throws CreateBackupException { + BackupCreator.createBackup(storeFile); + } + + public abstract List<CertificateWithAlias> getCertificates() throws ExitableException; + + public abstract void addCertificates(List<CertificateWithAlias> certificates) throws ExitableException; + + public abstract void saveFile() throws ExitableException; } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactory.java new file mode 100644 index 00000000..7e4b71e3 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactory.java @@ -0,0 +1,69 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * 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.oom.truststoremerger.merger.model; + +import static org.onap.oom.truststoremerger.api.CertificateConstants.JKS_TYPE; +import static org.onap.oom.truststoremerger.api.CertificateConstants.PKCS12_TYPE; + +import java.io.File; +import org.onap.oom.truststoremerger.common.ExtensionResolver; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreFileFactoryException; + +public class TruststoreFactory { + + private static final String JKS_EXTENSION = ".jks"; + private static final String P12_EXTENSION = ".p12"; + private static final String PEM_EXTENSION = ".pem"; + private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist"; + private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s"; + + + private TruststoreFactory() { + } + + public static Truststore create(String truststoreFilePath, String truststorePasswordPath) + throws TruststoreFileFactoryException, PasswordReaderException, KeystoreInstanceException, LoadTruststoreException { + File truststoreFile = new File(truststoreFilePath); + if (!ExtensionResolver.checkIfFileExists(truststoreFile)) { + throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile)); + } + return createTypedTruststore(truststoreFile, truststorePasswordPath); + } + + private static Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath) + throws KeystoreInstanceException, PasswordReaderException, LoadTruststoreException, TruststoreFileFactoryException { + String extension = ExtensionResolver.get(truststoreFile); + switch (extension) { + case JKS_EXTENSION: + return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, JKS_TYPE); + case P12_EXTENSION: + return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, PKCS12_TYPE); + case PEM_EXTENSION: + return new PemTruststore(truststoreFile); + default: + throw new TruststoreFileFactoryException( + String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension)); + } + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAlias.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAlias.java index decc3977..990a1c66 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAlias.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAlias.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import java.security.cert.Certificate; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAliasFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java index 0889650e..ce3c0c47 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAliasFactory.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import java.security.cert.Certificate; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGenerator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java index 56faa1f6..b812fcb3 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGenerator.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import java.util.concurrent.atomic.AtomicInteger; |