From a9fd6d0a175e647ac36932ce77b91f9e54e97084 Mon Sep 17 00:00:00 2001 From: Piotr Marcinkiewicz Date: Wed, 2 Sep 2020 14:35:32 +0200 Subject: 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 Change-Id: I3270cd9811e5eaf360ceea0d0ca99be1155eaf00 --- .../oom/truststoremerger/TrustStoreMerger.java | 24 +-- .../truststoremerger/api/CertificateConstants.java | 4 +- .../certification/file/TruststoreFileFactory.java | 99 ---------- .../file/TruststoreFilesListProvider.java | 53 ------ .../file/exception/AliasConflictException.java | 31 ---- .../file/exception/CreateBackupException.java | 30 --- .../file/exception/KeystoreInstanceException.java | 30 --- .../file/exception/LoadTruststoreException.java | 30 --- .../file/exception/MissingTruststoreException.java | 30 --- .../file/exception/PasswordReaderException.java | 29 --- .../TruststoreDataOperationException.java | 30 --- .../exception/TruststoreFileFactoryException.java | 30 --- .../exception/WriteTruststoreFileException.java | 31 ---- .../certification/file/model/JavaTruststore.java | 58 ------ .../certification/file/model/PemTruststore.java | 58 ------ .../certification/file/model/Truststore.java | 62 ------- .../file/provider/CertificateController.java | 33 ---- .../CertificateStoreControllerFactory.java | 61 ------ .../certification/file/provider/FileManager.java | 40 ---- .../provider/JavaCertificateStoreController.java | 156 ---------------- .../file/provider/PasswordReader.java | 37 ---- .../file/provider/PemCertificateController.java | 155 ---------------- .../file/provider/entry/CertificateWithAlias.java | 41 ----- .../entry/CertificateWithAliasFactory.java | 33 ---- .../file/provider/entry/PemAliasGenerator.java | 42 ----- .../oom/truststoremerger/common/BackupCreator.java | 48 +++++ .../truststoremerger/common/ExtensionResolver.java | 42 +++++ .../truststoremerger/common/PasswordReader.java | 41 +++++ .../exception/MergerConfigurationException.java | 4 +- .../TruststoresPathsProviderException.java | 4 +- .../merger/TruststoreFilesProvider.java | 51 ++++++ .../merger/exception/AliasConflictException.java | 31 ++++ .../merger/exception/CreateBackupException.java | 30 +++ .../exception/KeystoreInstanceException.java | 30 +++ .../merger/exception/LoadTruststoreException.java | 30 +++ .../exception/MissingTruststoreException.java | 30 +++ .../merger/exception/PasswordReaderException.java | 29 +++ .../TruststoreDataOperationException.java | 30 +++ .../exception/TruststoreFileFactoryException.java | 30 +++ .../exception/WriteTruststoreFileException.java | 31 ++++ .../merger/model/JavaTruststore.java | 165 +++++++++++++++++ .../merger/model/JavaTruststoreFactory.java | 51 ++++++ .../merger/model/PemTruststore.java | 155 ++++++++++++++++ .../truststoremerger/merger/model/Truststore.java | 46 +++++ .../merger/model/TruststoreFactory.java | 69 +++++++ .../model/certificate/CertificateWithAlias.java | 41 +++++ .../certificate/CertificateWithAliasFactory.java | 33 ++++ .../model/certificate/PemAliasGenerator.java | 42 +++++ .../file/TestCertificateProvider.java | 157 ---------------- .../file/model/JavaTruststoreTest.java | 116 ------------ .../file/model/PemTruststoreTest.java | 149 --------------- .../certification/file/model/TruststoreTest.java | 60 ------ .../file/provider/FileManagerTest.java | 47 ----- .../JavaCertificateStoreControllerTest.java | 59 ------ .../file/provider/PasswordReaderTest.java | 45 ----- .../provider/PemCertificateControllerTest.java | 95 ---------- .../file/provider/TruststoreFactoryTest.java | 116 ------------ .../provider/TruststoreFilesListProviderTest.java | 87 --------- .../file/provider/entry/PemAliasGeneratorTest.java | 58 ------ .../truststoremerger/common/BackupCreatorTest.java | 51 ++++++ .../common/ExtensionResolverTest.java | 45 +++++ .../common/PasswordReaderTest.java | 43 +++++ .../merger/TruststoreFilesProviderTest.java | 63 +++++++ .../merger/model/JavaTruststoreTest.java | 56 ++++++ .../merger/model/PemTruststoreTest.java | 128 +++++++++++++ .../merger/model/TestCertificateProvider.java | 152 +++++++++++++++ .../merger/model/TruststoreFactoryTest.java | 109 +++++++++++ .../merger/model/TruststoreTest.java | 204 +++++++++++++++++++++ .../model/certificate/PemAliasGeneratorTest.java | 58 ++++++ trustStoreMerger/src/test/resources/keystore.pass | 1 + 70 files changed, 1980 insertions(+), 2209 deletions(-) delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileFactory.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFilesListProvider.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/AliasConflictException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/CreateBackupException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/KeystoreInstanceException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/LoadTruststoreException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/MissingTruststoreException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/PasswordReaderException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreDataOperationException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreFileFactoryException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/WriteTruststoreFileException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststore.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststore.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/Truststore.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateController.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateStoreControllerFactory.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateController.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAlias.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAliasFactory.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGenerator.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/ExtensionResolver.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreFactory.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/PemTruststore.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactory.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAlias.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststoreTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststoreTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/TruststoreTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreControllerTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateControllerTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFactoryTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/PasswordReaderTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProviderTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/PemTruststoreTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactoryTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java create mode 100644 trustStoreMerger/src/test/resources/keystore.pass (limited to 'trustStoreMerger/src') 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 certificateWrappers = truststoreFilesList.get(i).getCertificates(); - baseFile.addCertificate(certificateWrappers); + Truststore truststore = truststoreFilesList.get(i); + List certificateWrappers = truststore.getCertificates(); + baseFile.addCertificates(certificateWrappers); } baseFile.saveFile(); @@ -87,13 +85,9 @@ class TrustStoreMerger { return factory.createConfiguration(); } - private List getTruststoreFiles(MergerConfiguration configuration) throws ExitableException { - TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), - new PasswordReader()); - TruststoreFilesListProvider truststoreFilesListProvider = new TruststoreFilesListProvider( - truststoreFileFactory); - return truststoreFilesListProvider - .getTruststoreFilesList( + private static List 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/TruststoreFilesListProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFilesListProvider.java deleted file mode 100644 index 92e3c2a8..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFilesListProvider.java +++ /dev/null @@ -1,53 +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.Truststore; - -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; - -public class TruststoreFilesListProvider { - - private final TruststoreFileFactory truststoreFileFactory; - - public TruststoreFilesListProvider(TruststoreFileFactory truststoreFileFactory) { - this.truststoreFileFactory = truststoreFileFactory; - } - - public List getTruststoreFilesList(List truststoreFilePaths, - List truststoreFilePasswordPaths) - throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { - List truststoreFilesList = 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); - } - - return truststoreFilesList; - } -} 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/certification/file/exception/AliasConflictException.java deleted file mode 100644 index a4102d9f..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/AliasConflictException.java +++ /dev/null @@ -1,31 +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.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); - } - -} 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/certification/file/exception/CreateBackupException.java deleted file mode 100644 index a21f7013..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/CreateBackupException.java +++ /dev/null @@ -1,30 +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.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); - } -} 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/certification/file/exception/KeystoreInstanceException.java deleted file mode 100644 index c5bcc3ca..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/KeystoreInstanceException.java +++ /dev/null @@ -1,30 +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.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); - } -} 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/certification/file/exception/LoadTruststoreException.java deleted file mode 100644 index b8bb53fa..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/LoadTruststoreException.java +++ /dev/null @@ -1,30 +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.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); - } -} 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/certification/file/exception/MissingTruststoreException.java deleted file mode 100644 index c502d6b6..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/MissingTruststoreException.java +++ /dev/null @@ -1,30 +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.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); - } -} 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/certification/file/exception/PasswordReaderException.java deleted file mode 100644 index d601d229..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/PasswordReaderException.java +++ /dev/null @@ -1,29 +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.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); - } -} 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/certification/file/exception/TruststoreDataOperationException.java deleted file mode 100644 index cf848f79..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreDataOperationException.java +++ /dev/null @@ -1,30 +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.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); - } -} 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/certification/file/exception/TruststoreFileFactoryException.java deleted file mode 100644 index 18349fd4..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreFileFactoryException.java +++ /dev/null @@ -1,30 +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.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); - } - -} 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/certification/file/exception/WriteTruststoreFileException.java deleted file mode 100644 index a5e02b3c..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/WriteTruststoreFileException.java +++ /dev/null @@ -1,31 +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.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); - } -} 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 getCertificates() throws ExitableException { - LOGGER.debug("Attempt ro read certificates from file: {} ", this.getFile().getPath()); - return storeController.getNotEmptyCertificateList(); - } - - @Override - public void addCertificate(List 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 getCertificates() throws ExitableException { - LOGGER.debug("Attempt ro read certificates from file: {}", this.getFile().getPath()); - return pemCertificateController.getNotEmptyCertificateList(); - } - - @Override - public void addCertificate(List 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/model/Truststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/Truststore.java deleted file mode 100644 index 153805a7..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/Truststore.java +++ /dev/null @@ -1,62 +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.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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public abstract class Truststore { - - private static final Logger LOGGER = LoggerFactory.getLogger(Truststore.class); - private static final String BACKUP_EXTENSION = ".bak"; - private final File file; - - Truststore(File file) { - this.file = file; - } - - public abstract List getCertificates() throws ExitableException; - - public abstract void addCertificate(List certificates) throws ExitableException; - - public abstract void saveFile() throws ExitableException; - - public File getFile() { - return file; - } - - public void createBackup() 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()); - throw new CreateBackupException(e); - } - } -} 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/certification/file/provider/CertificateController.java deleted file mode 100644 index f2ed2c45..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateController.java +++ /dev/null @@ -1,33 +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 java.util.List; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; - -public interface CertificateController { - - List getNotEmptyCertificateList() throws ExitableException; - - void addCertificates(List certificates) throws ExitableException; - - void saveFile() throws ExitableException; -} 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/provider/FileManager.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java deleted file mode 100644 index 12029ade..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java +++ /dev/null @@ -1,40 +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 java.io.File; - -public class FileManager { - - private static final int INDEX_NOT_FOUND = -1; - - public String getExtension(File file) { - int extStartIndex = file.getName().lastIndexOf("."); - if (extStartIndex == INDEX_NOT_FOUND) { - return ""; - } - return file.getName().substring(extStartIndex).toLowerCase(); - } - - public boolean checkIfFileExists(File file) { - return file.exists(); - } - -} 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/certification/file/provider/JavaCertificateStoreController.java deleted file mode 100644 index a4b129c9..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java +++ /dev/null @@ -1,156 +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 java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.security.KeyStore; -import java.security.KeyStoreException; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class JavaCertificateStoreController implements CertificateController { - - private static final Logger LOGGER = LoggerFactory.getLogger(JavaCertificateStoreController.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) { - this.keyStore = keyStore; - this.storeFile = storeFile; - this.password = password; - } - - public List getNotEmptyCertificateList() throws ExitableException { - List aliases = getTruststoreAliases(); - if (aliases.isEmpty()) { - throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); - } - return getWrappedCertificates(aliases); - } - - public void addCertificates(List certificatesWithAliases) - throws ExitableException { - if (getTruststoreAliases().isEmpty()) { - throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); - } - for (CertificateWithAlias certificate : certificatesWithAliases) { - addCertificate(certificate); - } - } - - public void saveFile() throws WriteTruststoreFileException { - try (FileOutputStream outputStream = new FileOutputStream(this.storeFile)) { - keyStore.store(outputStream, this.password.toCharArray()); - } catch (Exception e) { - LOGGER.error("Cannot write truststore file"); - throw new WriteTruststoreFileException(e); - } - } - - public void loadFile() throws LoadTruststoreException { - try { - keyStore.load(new FileInputStream(this.storeFile), this.password.toCharArray()); - } catch (Exception e) { - LOGGER.error("Cannot load file: {}", this.storeFile.getPath()); - throw new LoadTruststoreException(e); - } - } - - private void addCertificate(CertificateWithAlias certificate) - throws TruststoreDataOperationException, AliasConflictException { - if (hasAliasConflict(certificate)) { - LOGGER.error("Alias conflict detected"); - throw new AliasConflictException("Alias conflict detected. Alias conflicted: " + certificate.getAlias()); - } - try { - keyStore.setCertificateEntry(certificate.getAlias(), certificate.getCertificate()); - } catch (KeyStoreException e) { - LOGGER.error("Cannot merge certificate with alias: {}", certificate.getAlias()); - throw new TruststoreDataOperationException(e); - } - } - - private boolean hasAliasConflict(CertificateWithAlias certificate) throws TruststoreDataOperationException { - try { - return keyStore.containsAlias(certificate.getAlias()); - } catch (KeyStoreException e) { - LOGGER.error("Cannot check alias conflict"); - throw new TruststoreDataOperationException(e); - } - } - - private List getWrappedCertificates(List aliases) - throws TruststoreDataOperationException { - - List certificateWrapped = new ArrayList<>(); - - for (String alias : aliases) { - certificateWrapped.add(createWrappedCertificate(alias)); - } - return certificateWrapped; - } - - private CertificateWithAlias createWrappedCertificate(String alias) throws TruststoreDataOperationException { - try { - return factory.createCertificateWithAlias(keyStore.getCertificate(alias), alias); - } catch (KeyStoreException e) { - LOGGER.warn("Cannot get certificate with alias: {} ", alias); - throw new TruststoreDataOperationException(e); - } - } - - private List getTruststoreAliases() throws TruststoreDataOperationException { - try { - List aliases = Collections.list(keyStore.aliases()); - return getFilteredAlias(aliases); - } catch (KeyStoreException e) { - LOGGER.warn("Cannot read truststore aliases"); - throw new TruststoreDataOperationException(e); - } - } - - private List getFilteredAlias(List aliases) throws KeyStoreException { - List filteredAlias = new ArrayList<>(); - for (String alias : aliases) { - if (keyStore.isCertificateEntry(alias)) { - filteredAlias.add(alias); - } - } - return filteredAlias; - } - -} 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/certification/file/provider/PasswordReader.java deleted file mode 100644 index d7da53b0..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java +++ /dev/null @@ -1,37 +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 java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; - -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 { - try { - return Files.readString(file.toPath()); - } catch (IOException e) { - throw new PasswordReaderException(String.format(COULD_NOT_READ_PASSWORD_FROM_FILE_MSG_TEMPLATE, file)); - } - } -} 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/certification/file/provider/PemCertificateController.java deleted file mode 100644 index 9ff42b87..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateController.java +++ /dev/null @@ -1,155 +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.BOUNCY_CASTLE_PROVIDER; -import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.StringWriter; -import java.security.Security; -import java.security.cert.Certificate; -import java.security.cert.CertificateFactory; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PemCertificateController implements CertificateController { - - private static final Logger LOGGER = LoggerFactory.getLogger(PemCertificateController.class); - - private static final boolean APPEND_TO_FILE = true; - - private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); - private final List certificatesToBeSaved = new ArrayList<>(); - private final File file; - - public PemCertificateController(File file) { - this.file = file; - } - - public List getNotEmptyCertificateList() - throws TruststoreDataOperationException, MissingTruststoreException { - if (isFileWithoutPemCertificate()) { - throw new MissingTruststoreException("File does not contain any certificate"); - } - List extractedCertificate = extractCertificatesFromFile(); - return wrapCertificates(extractedCertificate); - } - - public void addCertificates(List certificates) - throws TruststoreDataOperationException, MissingTruststoreException { - if (isFileWithoutPemCertificate()) { - LOGGER.error("File does not contain any certificate. File path: {} ", this.file.getPath()); - throw new MissingTruststoreException("File does not contain any certificate"); - } - certificatesToBeSaved.addAll(certificates); - } - - public void saveFile() throws WriteTruststoreFileException, TruststoreDataOperationException { - List certificates = certificatesToBeSaved.stream() - .map(CertificateWithAlias::getCertificate) - .collect(Collectors.toList()); - String certificatesAsString = transformToStringInPemFormat(certificates); - appendToFile(certificatesAsString); - } - - boolean isFileWithoutPemCertificate() throws TruststoreDataOperationException { - List certificateList = extractCertificatesFromFile(); - return certificateList.isEmpty(); - } - - String transformToStringInPemFormat(List certificates) throws TruststoreDataOperationException { - StringWriter sw = new StringWriter(); - List generators = transformToPemGenerators(certificates); - try (PemWriter pemWriter = new PemWriter(sw)) { - for (PemObjectGenerator generator : generators) { - pemWriter.writeObject(generator); - } - } catch (IOException e) { - LOGGER.error("Cannot convert certificates to PEM format"); - throw new TruststoreDataOperationException(e); - } - return sw.toString(); - } - - - private List extractCertificatesFromFile() throws TruststoreDataOperationException { - try (FileInputStream inputStream = new FileInputStream(this.file)) { - 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()); - throw new TruststoreDataOperationException(e); - } - } - - - private List transformToPemGenerators(List certificates) - throws TruststoreDataOperationException { - List generators = new ArrayList<>(); - for (Certificate certificate : certificates) { - PemObjectGenerator generator = createPemGenerator(certificate); - generators.add(generator); - } - return generators; - } - - private JcaMiscPEMGenerator createPemGenerator(Certificate certificate) - throws TruststoreDataOperationException { - try { - return new JcaMiscPEMGenerator(certificate); - } catch (IOException e) { - LOGGER.error("Cannot convert Certificate Object to PemGenerator Object"); - throw new TruststoreDataOperationException(e); - } - } - - private List wrapCertificates(List rawCertificates) { - return rawCertificates.stream() - .map(factory::createPemCertificate) - .collect(Collectors.toList()); - } - - private void appendToFile(String certificatesAsString) throws WriteTruststoreFileException { - try { - FileOutputStream fileOutputStream = new FileOutputStream(this.file, APPEND_TO_FILE); - fileOutputStream.write(certificatesAsString.getBytes()); - } catch (Exception e) { - LOGGER.error("Cannot write certificates to file"); - throw new WriteTruststoreFileException(e); - } - } -} 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/certification/file/provider/entry/CertificateWithAlias.java deleted file mode 100644 index decc3977..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAlias.java +++ /dev/null @@ -1,41 +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.entry; - -import java.security.cert.Certificate; - -public class CertificateWithAlias { - - private final Certificate certificate; - private final String alias; - - public CertificateWithAlias(Certificate certificate, String alias) { - this.certificate = certificate; - this.alias = alias; - } - - public String getAlias() { - return this.alias; - } - - public Certificate getCertificate() { - return this.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/certification/file/provider/entry/CertificateWithAliasFactory.java deleted file mode 100644 index 0889650e..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAliasFactory.java +++ /dev/null @@ -1,33 +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.entry; - -import java.security.cert.Certificate; - -public class CertificateWithAliasFactory { - - public CertificateWithAlias createCertificateWithAlias(Certificate certificate, String alias) { - return new CertificateWithAlias(certificate, alias); - } - - public CertificateWithAlias createPemCertificate(Certificate certificate) { - return new CertificateWithAlias(certificate, PemAliasGenerator.getInstance().getAlias()); - } -} 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/certification/file/provider/entry/PemAliasGenerator.java deleted file mode 100644 index 56faa1f6..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGenerator.java +++ /dev/null @@ -1,42 +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.entry; - - -import java.util.concurrent.atomic.AtomicInteger; - -public class PemAliasGenerator { - - private static final String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; - private static final PemAliasGenerator INSTANCE = new PemAliasGenerator(); - private static AtomicInteger counter = new AtomicInteger(0); - - private PemAliasGenerator() { - } - - public static PemAliasGenerator getInstance() { - return INSTANCE; - } - - public String getAlias() { - - return PREFIX_ALIAS_NAME + counter.getAndIncrement(); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java new file mode 100644 index 00000000..9187393e --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java @@ -0,0 +1,48 @@ +/*============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.common; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.file.Files; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class BackupCreator { + + private static final Logger LOGGER = LoggerFactory.getLogger(BackupCreator.class); + private static final String BACKUP_EXTENSION = ".bak"; + + private BackupCreator() { + } + + 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: {} ", 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/common/ExtensionResolver.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/ExtensionResolver.java new file mode 100644 index 00000000..af792c48 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/ExtensionResolver.java @@ -0,0 +1,42 @@ +/*============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.common; + +import java.io.File; + +public final class ExtensionResolver { + + private static final int INDEX_NOT_FOUND = -1; + + private ExtensionResolver() {} + + public static String get(File file) { + int extStartIndex = file.getName().lastIndexOf("."); + if (extStartIndex == INDEX_NOT_FOUND) { + return ""; + } + return file.getName().substring(extStartIndex).toLowerCase(); + } + + public static boolean checkIfFileExists(File file) { + return file.exists(); + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java new file mode 100644 index 00000000..d84be5ac --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java @@ -0,0 +1,41 @@ +/*============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.common; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; + +public final class PasswordReader { + + private static final String COULD_NOT_READ_PASSWORD_FROM_FILE_MSG_TEMPLATE = "Could not read password from file: %s"; + + private PasswordReader() { + } + + public static String readPassword(File file) throws PasswordReaderException { + try { + return Files.readString(file.toPath()); + } catch (IOException e) { + throw new PasswordReaderException(String.format(COULD_NOT_READ_PASSWORD_FROM_FILE_MSG_TEMPLATE, file)); + } + } +} 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/merger/TruststoreFilesProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProvider.java new file mode 100644 index 00000000..9108fb69 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProvider.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; + +import java.util.ArrayList; +import java.util.List; +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 TruststoreFilesProvider { + + + private TruststoreFilesProvider() { + } + + public static List getTruststoreFiles(List truststoreFilePaths, + List truststoreFilePasswordPaths) + throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { + List truststoreFiles = new ArrayList<>(); + for (int i = 0; i < truststoreFilePaths.size(); i++) { + String truststorePath = truststoreFilePaths.get(i); + String passwordPath = truststoreFilePasswordPaths.get(i); + + Truststore truststore = TruststoreFactory.create(truststorePath, passwordPath); + truststoreFiles.add(truststore); + } + + return truststoreFiles; + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java new file mode 100644 index 00000000..71df3a45 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java @@ -0,0 +1,31 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class AliasConflictException extends ExitableException { + + public AliasConflictException(String errorMessage) { + super(errorMessage, ExitStatus.ALIAS_CONFLICT_EXCEPTION); + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java new file mode 100644 index 00000000..f655a9f5 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java @@ -0,0 +1,30 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class CreateBackupException extends ExitableException { + + public CreateBackupException(Exception cause) { + super(cause, ExitStatus.CREATE_BACKUP_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java new file mode 100644 index 00000000..99a955d9 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java @@ -0,0 +1,30 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class KeystoreInstanceException extends ExitableException { + + public KeystoreInstanceException(Exception cause) { + super(cause, ExitStatus.KEYSTORE_INSTANCE_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java new file mode 100644 index 00000000..810bf556 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java @@ -0,0 +1,30 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class LoadTruststoreException extends ExitableException { + + public LoadTruststoreException(Exception cause) { + super(cause, ExitStatus.TRUSTSTORE_LOAD_FILE_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java new file mode 100644 index 00000000..9065c9d3 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java @@ -0,0 +1,30 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class MissingTruststoreException extends ExitableException { + + public MissingTruststoreException(String errorMessage) { + super(errorMessage, ExitStatus.MISSING_TRUSTSTORE_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java new file mode 100644 index 00000000..9f9d2e27 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java @@ -0,0 +1,29 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class PasswordReaderException extends ExitableException { + public PasswordReaderException(String errorMessage) { + super(errorMessage, ExitStatus.PASSWORD_READER_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java new file mode 100644 index 00000000..c18cb006 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java @@ -0,0 +1,30 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class TruststoreDataOperationException extends ExitableException { + + public TruststoreDataOperationException(Exception cause) { + super(cause, ExitStatus.TRUSTSTORE_DATA_OPERATION_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java new file mode 100644 index 00000000..f802a9d3 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java @@ -0,0 +1,30 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class TruststoreFileFactoryException extends ExitableException { + public TruststoreFileFactoryException(String errorMessage) { + super(errorMessage, ExitStatus.TRUSTSTORE_FILE_FACTORY_EXCEPTION); + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java new file mode 100644 index 00000000..fe368868 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java @@ -0,0 +1,31 @@ +/*============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.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class WriteTruststoreFileException extends ExitableException { + + public WriteTruststoreFileException(Exception cause) { + super(cause, ExitStatus.WRITE_TRUSTSTORE_FILE_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java new file mode 100644 index 00000000..e3a03996 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java @@ -0,0 +1,165 @@ +/*============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.io.FileInputStream; +import java.io.FileOutputStream; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.onap.oom.truststoremerger.api.ExitableException; +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 final class JavaTruststore extends Truststore { + + private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststore.class); + + private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); + private final KeyStore keyStore; + private final String password; + + + private JavaTruststore(KeyStore keyStore, File storeFile, String password) { + super(storeFile); + this.keyStore = keyStore; + this.password = password; + } + + 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 getCertificates() throws ExitableException { + LOGGER.debug("Attempt to read certificates from file: {}", storeFile.getPath()); + List aliases = getTruststoreAliases(); + if (aliases.isEmpty()) { + throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); + } + return getWrappedCertificates(aliases); + } + + public void addCertificates(List 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()); + } + for (CertificateWithAlias certificate : certificatesWithAliases) { + addCertificate(certificate); + } + } + + public void saveFile() throws WriteTruststoreFileException { + 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"); + throw new WriteTruststoreFileException(e); + } + } + + private void loadFile() throws LoadTruststoreException { + try { + keyStore.load(new FileInputStream(storeFile), this.password.toCharArray()); + } catch (Exception e) { + LOGGER.error("Cannot load file: {}", storeFile.getPath()); + throw new LoadTruststoreException(e); + } + } + + private void addCertificate(CertificateWithAlias certificate) + throws TruststoreDataOperationException, AliasConflictException { + if (hasAliasConflict(certificate)) { + LOGGER.error("Alias conflict detected"); + throw new AliasConflictException("Alias conflict detected. Alias conflicted: " + certificate.getAlias()); + } + try { + keyStore.setCertificateEntry(certificate.getAlias(), certificate.getCertificate()); + } catch (KeyStoreException e) { + LOGGER.error("Cannot merge certificate with alias: {}", certificate.getAlias()); + throw new TruststoreDataOperationException(e); + } + } + + private boolean hasAliasConflict(CertificateWithAlias certificate) throws TruststoreDataOperationException { + try { + return keyStore.containsAlias(certificate.getAlias()); + } catch (KeyStoreException e) { + LOGGER.error("Cannot check alias conflict"); + throw new TruststoreDataOperationException(e); + } + } + + private List getWrappedCertificates(List aliases) + throws TruststoreDataOperationException { + + List certificateWrapped = new ArrayList<>(); + + for (String alias : aliases) { + certificateWrapped.add(createWrappedCertificate(alias)); + } + return certificateWrapped; + } + + private CertificateWithAlias createWrappedCertificate(String alias) throws TruststoreDataOperationException { + try { + return factory.createCertificateWithAlias(keyStore.getCertificate(alias), alias); + } catch (KeyStoreException e) { + LOGGER.warn("Cannot get certificate with alias: {} ", alias); + throw new TruststoreDataOperationException(e); + } + } + + private List getTruststoreAliases() throws TruststoreDataOperationException { + try { + List aliases = Collections.list(keyStore.aliases()); + return getFilteredAlias(aliases); + } catch (KeyStoreException e) { + LOGGER.warn("Cannot read truststore aliases"); + throw new TruststoreDataOperationException(e); + } + } + + private List getFilteredAlias(List aliases) throws KeyStoreException { + List filteredAlias = new ArrayList<>(); + for (String alias : aliases) { + if (keyStore.isCertificateEntry(alias)) { + filteredAlias.add(alias); + } + } + return filteredAlias; + } + +} 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/merger/model/PemTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/PemTruststore.java new file mode 100644 index 00000000..d04a01b1 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/PemTruststore.java @@ -0,0 +1,155 @@ +/*============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.BOUNCY_CASTLE_PROVIDER; +import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.security.Security; +import java.security.cert.Certificate; +import java.security.cert.CertificateFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +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.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 PemTruststore extends Truststore { + + 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 certificatesToBeSaved = new ArrayList<>(); + + public PemTruststore(File storeFile) { + super(storeFile); + } + + public List 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"); + } + List extractedCertificate = extractCertificatesFromFile(); + return wrapCertificates(extractedCertificate); + } + + public void addCertificates(List 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: {} ", 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 certificates = certificatesToBeSaved.stream() + .map(CertificateWithAlias::getCertificate) + .collect(Collectors.toList()); + String certificatesAsString = transformToStringInPemFormat(certificates); + appendToFile(certificatesAsString); + } + + boolean isFileWithoutPemCertificate() throws TruststoreDataOperationException { + List certificateList = extractCertificatesFromFile(); + return certificateList.isEmpty(); + } + + String transformToStringInPemFormat(List certificates) throws TruststoreDataOperationException { + StringWriter sw = new StringWriter(); + List generators = transformToPemGenerators(certificates); + try (PemWriter pemWriter = new PemWriter(sw)) { + for (PemObjectGenerator generator : generators) { + pemWriter.writeObject(generator); + } + } catch (IOException e) { + LOGGER.error("Cannot convert certificates to PEM format"); + throw new TruststoreDataOperationException(e); + } + return sw.toString(); + } + + private List extractCertificatesFromFile() throws TruststoreDataOperationException { + 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: {}", storeFile.getPath()); + throw new TruststoreDataOperationException(e); + } + } + + private List transformToPemGenerators(List certificates) + throws TruststoreDataOperationException { + List generators = new ArrayList<>(); + for (Certificate certificate : certificates) { + PemObjectGenerator generator = createPemGenerator(certificate); + generators.add(generator); + } + return generators; + } + + private JcaMiscPEMGenerator createPemGenerator(Certificate certificate) + throws TruststoreDataOperationException { + try { + return new JcaMiscPEMGenerator(certificate); + } catch (IOException e) { + LOGGER.error("Cannot convert Certificate Object to PemGenerator Object"); + throw new TruststoreDataOperationException(e); + } + } + + private List wrapCertificates(List rawCertificates) { + return rawCertificates.stream() + .map(factory::createPemCertificate) + .collect(Collectors.toList()); + } + + private void appendToFile(String certificatesAsString) throws WriteTruststoreFileException { + try { + FileOutputStream fileOutputStream = new FileOutputStream(storeFile, APPEND_TO_FILE); + fileOutputStream.write(certificatesAsString.getBytes()); + } catch (Exception e) { + LOGGER.error("Cannot write certificates to file"); + throw new WriteTruststoreFileException(e); + } + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java new file mode 100644 index 00000000..2c3acf49 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java @@ -0,0 +1,46 @@ +/*============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.util.List; +import org.onap.oom.truststoremerger.api.ExitableException; +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 abstract class Truststore { + + final File storeFile; + + public Truststore(File storeFile) { + this.storeFile = storeFile; + } + + public void createBackup() throws CreateBackupException { + BackupCreator.createBackup(storeFile); + } + + public abstract List getCertificates() throws ExitableException; + + public abstract void addCertificates(List 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/merger/model/certificate/CertificateWithAlias.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAlias.java new file mode 100644 index 00000000..990a1c66 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAlias.java @@ -0,0 +1,41 @@ +/*============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.certificate; + +import java.security.cert.Certificate; + +public class CertificateWithAlias { + + private final Certificate certificate; + private final String alias; + + public CertificateWithAlias(Certificate certificate, String alias) { + this.certificate = certificate; + this.alias = alias; + } + + public String getAlias() { + return this.alias; + } + + public Certificate getCertificate() { + return this.certificate; + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java new file mode 100644 index 00000000..ce3c0c47 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java @@ -0,0 +1,33 @@ +/*============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.certificate; + +import java.security.cert.Certificate; + +public class CertificateWithAliasFactory { + + public CertificateWithAlias createCertificateWithAlias(Certificate certificate, String alias) { + return new CertificateWithAlias(certificate, alias); + } + + public CertificateWithAlias createPemCertificate(Certificate certificate) { + return new CertificateWithAlias(certificate, PemAliasGenerator.getInstance().getAlias()); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java new file mode 100644 index 00000000..b812fcb3 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java @@ -0,0 +1,42 @@ +/*============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.certificate; + + +import java.util.concurrent.atomic.AtomicInteger; + +public class PemAliasGenerator { + + private static final String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; + private static final PemAliasGenerator INSTANCE = new PemAliasGenerator(); + private static AtomicInteger counter = new AtomicInteger(0); + + private PemAliasGenerator() { + } + + public static PemAliasGenerator getInstance() { + return INSTANCE; + } + + public String getAlias() { + + return PREFIX_ALIAS_NAME + counter.getAndIncrement(); + } +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java deleted file mode 100644 index b5893230..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java +++ /dev/null @@ -1,157 +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 java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -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.model.JavaTruststore; -import org.onap.oom.truststoremerger.certification.file.model.PemTruststore; -import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController; -import org.onap.oom.truststoremerger.certification.file.provider.CertificateStoreControllerFactory; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; - -public class TestCertificateProvider { - - public static final String SAMPLE_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-p12.p12"; - public static final String SAMPLE_P12_TRUSTSTORE_PASSWORD = "88y9v5D8H3SG6bZWRVHDfOAo"; - public static final String TMP_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-p12.p12"; - - public static final String SAMPLE_P12_KEYSTORE_FILE_PATH = "src/test/resources/keystore.p12"; - public static final String SAMPLE_P12_KEYSTORE_PASSWORD = "Foh49MJNYI7S_pEzE9gvUDSu"; - - public static final String SAMPLE_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-jks.jks"; - public static final String SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH = "src/test/resources/truststore-jks-uniq.jks"; - public static final String SAMPLE_JKS_TRUSTSTORE_PASSWORD = "EOyuFbuYDyq_EhpboM72RHua"; - public static final String TMP_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-jks.jks"; - - public static final String SAMPLE_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore.pem"; - public static final String EMPTY_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/empty-truststore.pem"; - public static final String TMP_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore.pem"; - public static final String SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH = "src/test/resources/truststore-with-private-key.pem"; - - private static final CertificateStoreControllerFactory certificateStoreControllerFactory = new CertificateStoreControllerFactory(); - - public static JavaTruststore getSampleP12Truststore() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD); - } - - public static JavaTruststore getSampleP12Keystore() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD); - } - - public static JavaTruststore createTmpP12TruststoreFile() - throws IOException, LoadTruststoreException, KeystoreInstanceException { - copyFile(SAMPLE_P12_TRUSTSTORE_FILE_PATH, TMP_P12_TRUSTSTORE_FILE_PATH); - return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD); - } - - public static JavaTruststore getTmpP12TruststoreFile() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD); - } - - private static JavaTruststore createP12TruststoreInstance(String filePath, String password) - throws LoadTruststoreException, KeystoreInstanceException { - File certFile = getFile(filePath); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedPkcs12CertificateStoreController(certFile, password); - return new JavaTruststore(certFile, storeController); - } - - public static PemTruststore getSamplePemTruststoreFile() { - return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_FILE_PATH); - } - - public static PemTruststore getEmptyPemTruststoreFile() { - return getPemTruststoreInstance(EMPTY_PEM_TRUSTSTORE_FILE_PATH); - } - - public static PemTruststore createEmptyTmpPemTruststoreFile() throws IOException { - copyFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH); - return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH); - } - - public static PemTruststore createTmpPemTruststoreFile() throws IOException { - copyFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH); - return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH); - } - - public static PemTruststore getTmpPemTruststoreFile() { - return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH); - } - - public static PemTruststore getPemWithPrivateKeyTruststoreFile() { - return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH); - } - - public static String getExpectedPemCertificateAsString() throws IOException { - Path samplePemFilePath = Paths.get(SAMPLE_PEM_TRUSTSTORE_FILE_PATH); - return Files.readString(samplePemFilePath); - } - - public static JavaTruststore getSampleJksTruststoreFile() - throws LoadTruststoreException, KeystoreInstanceException { - return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); - } - - public static JavaTruststore getSampleJksTruststoreFileWithUniqueAlias() - throws LoadTruststoreException, KeystoreInstanceException { - return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, - SAMPLE_JKS_TRUSTSTORE_PASSWORD); - } - - public static JavaTruststore createTmpJksTruststoreFileWithUniqAlias() - throws IOException, LoadTruststoreException, KeystoreInstanceException { - copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH); - return createJksTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); - } - - public static void removeTemporaryFiles() throws IOException { - Files.deleteIfExists(Paths.get(TMP_PEM_TRUSTSTORE_FILE_PATH)); - Files.deleteIfExists(Paths.get(TMP_JKS_TRUSTSTORE_FILE_PATH)); - Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH)); - } - - private static JavaTruststore createJksTruststoreInstance(String filePath, String password) - throws LoadTruststoreException, KeystoreInstanceException { - File certFile = getFile(filePath); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedJksCertificateStoreController(certFile, password); - return new JavaTruststore(certFile, storeController); - } - - private static PemTruststore getPemTruststoreInstance(String tmpPemTruststoreFilePath) { - File file = getFile(tmpPemTruststoreFilePath); - return new PemTruststore(file, new PemCertificateController(file)); - } - - private static void copyFile(String sourcePath, String destPath) throws IOException { - Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING); - } - - private static File getFile(String path) { - return new File(path); - } -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststoreTest.java deleted file mode 100644 index eccf36bc..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststoreTest.java +++ /dev/null @@ -1,116 +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 static org.assertj.core.api.Assertions.assertThat; -import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; - -import java.io.IOException; -import java.security.cert.Certificate; -import java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; - - -class JavaTruststoreTest { - public static final int FIRST_ELEMENT = 0; - private static final int EXPECTED_ONE = 1; - public static final int EXPECTED_THREE = 3; - - @Test - void jksTruststoreShouldReadCertificatesFromFile() throws ExitableException { - - //given - JavaTruststore jksTruststoreFile = TestCertificateProvider.getSampleJksTruststoreFile(); - - //when - List certificates = jksTruststoreFile.getCertificates(); - Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate(); - - //then - assertThat(certificates).hasSize(EXPECTED_ONE); - assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); - } - - @Test - void jksTruststoreShouldAddDifferentCertificates() throws Exception { - - //given - JavaTruststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias(); - List p12certificates = TestCertificateProvider.getSampleP12Truststore() - .getCertificates(); - List pemCertificates = TestCertificateProvider.getSamplePemTruststoreFile() - .getCertificates(); - - //when - jksTruststore.addCertificate(p12certificates); - jksTruststore.addCertificate(pemCertificates); - - //then - assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE); - - } - - @Test - void p12TruststoreShouldReadCertificatesFromFile() throws ExitableException { - //given - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); - - //when - List certificatesWithAliases = p12Truststore.getCertificates(); - Certificate certificate = certificatesWithAliases.get(FIRST_ELEMENT).getCertificate(); - - //then - assertThat(certificatesWithAliases).hasSize(EXPECTED_ONE); - assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); - } - - - @Test - void p12TruststoreShouldAddDifferentCertificates() throws Exception { - //given - JavaTruststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile(); - List jksTruststoreCertificates = TestCertificateProvider - .getSampleJksTruststoreFileWithUniqueAlias() - .getCertificates(); - List pemTruststoreCertificates = TestCertificateProvider.getSamplePemTruststoreFile() - .getCertificates(); - - //when - p12Truststore.addCertificate(jksTruststoreCertificates); - p12Truststore.addCertificate(pemTruststoreCertificates); - p12Truststore.saveFile(); - - - //then - JavaTruststore p12TruststoreSaved = TestCertificateProvider.getTmpP12TruststoreFile(); - assertThat(p12TruststoreSaved.getCertificates()).hasSize(EXPECTED_THREE); - } - - - - @AfterAll - static void removeTemporaryFiles() throws IOException { - TestCertificateProvider.removeTemporaryFiles(); - } -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststoreTest.java deleted file mode 100644 index e7ffa093..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststoreTest.java +++ /dev/null @@ -1,149 +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 static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; - -import java.io.IOException; -import java.security.cert.Certificate; -import java.security.cert.CertificateEncodingException; -import java.util.ArrayList; -import java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; -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.entry.CertificateWithAliasFactory; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; -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; - -class PemTruststoreTest { - - public static final int EXPECTED_ONE = 1; - public static final int EXPECTED_THREE = 3; - public static final int FIRST_ELEMENT = 0; - - private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); - - @Test - void pemTruststoreShouldReadCertificatesFromFile() throws ExitableException { - - //given - PemTruststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); - - //when - List certificates = pemTruststore.getCertificates(); - Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate(); - //then - - assertThat(certificates).hasSize(EXPECTED_ONE); - assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); - } - - @Test - void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException { - - //given - PemTruststore tmpPemTruststoreFile = TestCertificateProvider.createTmpPemTruststoreFile(); - List jksTruststoreCertificates = TestCertificateProvider - .getSampleJksTruststoreFileWithUniqueAlias().getCertificates(); - List p12TruststoreCertificates = TestCertificateProvider.getSampleP12Truststore() - .getCertificates(); - - //when - tmpPemTruststoreFile.addCertificate(jksTruststoreCertificates); - tmpPemTruststoreFile.addCertificate(p12TruststoreCertificates); - tmpPemTruststoreFile.saveFile(); - - PemTruststore tmpPemTruststoreSaved = TestCertificateProvider.getTmpPemTruststoreFile(); - List addedCertificates = tmpPemTruststoreSaved.getCertificates(); - Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate(); - - //then - assertThat(addedCertificates).hasSize(EXPECTED_THREE); - assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); - - } - - @Test - void privateKeyIsSkippedWhileReadingCertificates() throws ExitableException { - //given - PemTruststore pemTruststore = TestCertificateProvider.getPemWithPrivateKeyTruststoreFile(); - - //when - List certificate = pemTruststore.getCertificates(); - //then - - assertThat(certificate).hasSize(EXPECTED_ONE); - } - - @Test - void shouldThrowExceptionWhenCannotSaveFile() throws IOException, ExitableException { - //given - PemTruststore tmpPemTruststoreFile = TestCertificateProvider.createTmpPemTruststoreFile(); - List pemTruststoreCertificates = - TestCertificateProvider.getSamplePemTruststoreFile().getCertificates(); - //when - tmpPemTruststoreFile.addCertificate(pemTruststoreCertificates); - tmpPemTruststoreFile.getFile().setWritable(false); - //then - assertThatExceptionOfType(WriteTruststoreFileException.class) - .isThrownBy(tmpPemTruststoreFile::saveFile); - - } - - @Test - void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException { - //given - PemTruststore tmpPemTruststoreFile = TestCertificateProvider.createEmptyTmpPemTruststoreFile(); - //when//then - assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(tmpPemTruststoreFile::getCertificates); - } - - @Test - void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception { - //given - PemTruststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile(); - Certificate certificate = mock(Certificate.class); - - when(certificate.getEncoded()).thenThrow(new CertificateEncodingException()); - - List certificatesWithAliases = new ArrayList<>(); - certificatesWithAliases.add(factory.createPemCertificate(certificate)); - pemTruststore.addCertificate(certificatesWithAliases); - - //when //then - assertThatExceptionOfType(TruststoreDataOperationException.class) - .isThrownBy(pemTruststore::saveFile); - } - - @AfterAll - static void removeTemporaryFiles() throws IOException { - TestCertificateProvider.removeTemporaryFiles(); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/TruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/TruststoreTest.java deleted file mode 100644 index eea1f9c7..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/TruststoreTest.java +++ /dev/null @@ -1,60 +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.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.certification.file.exception.CreateBackupException; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; - -import static org.assertj.core.api.Assertions.assertThat; - -class TruststoreTest { - - private static final String PEM_FILE_PATH = "src/test/resources/truststore.pem"; - private static final String PEM_BACKUP_FILE_PATH = "src/test/resources/truststore.pem.bak"; - private static final String BACKUP_EXTENSION = ".bak"; - - - @Test - void createBackupShouldCreateFileWithExtension() throws CreateBackupException { - //given - File pemFile = new File(PEM_FILE_PATH); - Truststore truststore = new PemTruststore(pemFile, new PemCertificateController(pemFile)); - //when - truststore.createBackup(); - - //then - File backupFile = new File(PEM_BACKUP_FILE_PATH); - assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue(); - assertThat(backupFile.isFile()).isTrue(); - } - - - @AfterAll - static void removeBackupFile() throws IOException { - Files.deleteIfExists(Paths.get(PEM_BACKUP_FILE_PATH)); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java deleted file mode 100644 index c649ba68..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java +++ /dev/null @@ -1,47 +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 org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - -import java.io.File; - -import static org.assertj.core.api.Assertions.assertThat; - -class FileManagerTest { - - private FileManager fileManager = new FileManager(); - - @ParameterizedTest - @CsvSource(value = { - "opt/app/truststore.jks:.jks", - "opt/app/truststore.p12:.p12", - "opt/app/truststore.pem:.pem", - "opt/app/truststore.PEM:.pem", - "opt/app/truststore:''", - }, delimiter = ':') - void shouldReturnCorrectExtension(String filePath, String expectedExtension) { - String extension = fileManager.getExtension(new File(filePath)); - assertThat(extension).isEqualTo(expectedExtension); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreControllerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreControllerTest.java deleted file mode 100644 index 8ee77ef2..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreControllerTest.java +++ /dev/null @@ -1,59 +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.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.onap.oom.truststoremerger.certification.file.TestCertificateProvider.getSampleJksTruststoreFile; - -import java.util.List; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; -import org.onap.oom.truststoremerger.certification.file.exception.AliasConflictException; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore; - -class JavaCertificateStoreControllerTest { - - - @Test - void throwExceptionWhenAliasConflictDetected() throws Exception { - //given - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); - List jksTruststoreCertificates = getSampleJksTruststoreFile().getCertificates(); - - //when //then - assertThatExceptionOfType(AliasConflictException.class) - .isThrownBy(() -> p12Truststore.addCertificate(jksTruststoreCertificates)); - } - - - @Test - void throwExceptionWhenFileNotContainsTruststoreEntry() throws ExitableException { - //given - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Keystore(); - - //when//then - assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(p12Truststore::getCertificates); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java deleted file mode 100644 index 40eda4dd..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java +++ /dev/null @@ -1,45 +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 org.junit.jupiter.api.Test; - -import java.io.File; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; - -class PasswordReaderTest { - - @Test - void shouldReturnCorrectPasswordFromFile() throws PasswordReaderException { - PasswordReader passwordReader = new PasswordReader(); - String fileData = passwordReader.readPassword(new File("src/test/resources/truststore-jks.pass")); - assertThat(fileData).isEqualTo("EOyuFbuYDyq_EhpboM72RHua"); - } - - @Test - void shouldThrowExceptionForNonExistingFile() { - PasswordReader passwordReader = new PasswordReader(); - assertThatExceptionOfType(PasswordReaderException.class) - .isThrownBy(() -> passwordReader.readPassword(new File("src/test/resources/non-esisting-file.pass"))); - } -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateControllerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateControllerTest.java deleted file mode 100644 index 080fcca3..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateControllerTest.java +++ /dev/null @@ -1,95 +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.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; - -import java.io.File; -import java.io.IOException; -import java.security.cert.Certificate; -import java.util.List; -import java.util.stream.Collectors; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; -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.model.PemTruststore; - -class PemCertificateControllerTest { - - @Test - void getNotEmptyCertificateListShouldThrowExceptionWhenFileNotContainsCertificate() { - //given - File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile); - //when//then - assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(pemCertificateController::getNotEmptyCertificateList); - } - - @Test - void transformToStringInPemFormatShouldCorrectlyTransform() throws ExitableException, IOException { - //given - PemTruststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); - List wrappedCertificates = pemTruststore.getCertificates(); - File notEmptyPemFile = pemTruststore.getFile(); - List certificateList = unWrapCertificate(wrappedCertificates); - PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile); - String expected = TestCertificateProvider.getExpectedPemCertificateAsString(); - - //when - String certificateTransformed = pemCertificateController.transformToStringInPemFormat(certificateList); - - //then - assertThat(certificateTransformed).isEqualTo(expected); - } - - @Test - void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate() - throws TruststoreDataOperationException { - //given - File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile); - //when//then - assertThat(pemCertificateController.isFileWithoutPemCertificate()).isTrue(); - } - - @Test - void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate() - throws TruststoreDataOperationException { - //given - File notEmptyPemFile = TestCertificateProvider.getSamplePemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile); - - //when//then - assertThat(pemCertificateController.isFileWithoutPemCertificate()).isFalse(); - } - - private List unWrapCertificate(List certificateWithAliases) { - return certificateWithAliases - .stream() - .map(CertificateWithAlias::getCertificate) - .collect(Collectors.toList()); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFactoryTest.java deleted file mode 100644 index b2063cc3..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFactoryTest.java +++ /dev/null @@ -1,116 +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 org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; -import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory; -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 org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; - -import java.io.File; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; - -@ExtendWith(MockitoExtension.class) -class TruststoreFactoryTest { - - private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks"; - private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass"; - private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12"; - private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass"; - private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem"; - private static final String EMPTY_PASS_PATH = ""; - private static final String TRUSTSTORE_UNKNOWN_EXTENSION_PATH = "src/test/resources/truststore-jks.unknown"; - private static final String NON_EXISTING_TRUSTSTORE_PATH = "src/test/resources/non-existing-truststore.jks"; - - private TruststoreFileFactory truststoreFileFactory; - - @BeforeEach - void setUp() { - truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); - } - - @Test - void shouldReturnCorrectJksTruststoreForJksFile() - throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH); - assertThat(truststore).isInstanceOf(JavaTruststore.class); - JavaTruststore jksTruststore = (JavaTruststore) truststore; - assertThat(jksTruststore.getFile()).isEqualTo(new File(TRUSTSTORE_JKS_PATH)); - } - - @Test - void shouldReturnCorrectP12TruststoreForP12File() - throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_P12_PATH, - TRUSTSTORE_P12_PASS_PATH); - assertThat(truststore).isInstanceOf(JavaTruststore.class); - } - - @Test - void shouldReturnCorrectPemTruststoreForPemFile() - throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_PEM_PATH, - EMPTY_PASS_PATH); - assertThat(truststore).isInstanceOf(PemTruststore.class); - } - - @Test - void shouldThrowExceptionForInvalidP12PassPath() { - assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( - () -> truststoreFileFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH) - ); - } - - @Test - void shouldThrowExceptionForInvalidJksPassPath() { - assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( - () -> truststoreFileFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH) - ); - } - - @Test - void shouldThrowExceptionForUnknownTruststoreExtension() { - assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( - () -> truststoreFileFactory.create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH) - ); - } - - @Test - void shouldThrowExceptionForNonExistingTruststoreFile() { - assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( - () -> truststoreFileFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH) - ); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java deleted file mode 100644 index 0dadcfef..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java +++ /dev/null @@ -1,87 +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 org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory; -import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider; -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 org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; - -import java.io.File; -import java.util.Arrays; -import java.util.List; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; - -import static org.assertj.core.api.Assertions.assertThat; - -class TruststoreFilesListProviderTest { - - private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks"; - private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass"; - private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12"; - private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass"; - private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem"; - private static final String EMPTY_PASS_PATH = ""; - - private TruststoreFilesListProvider truststoreFilesListProvider; - - @BeforeEach - void setUp() { - TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); - truststoreFilesListProvider = new TruststoreFilesListProvider(truststoreFileFactory); - } - - @Test - void shouldReturnTruststoreFilesList() - throws TruststoreFileFactoryException, PasswordReaderException, LoadTruststoreException, KeystoreInstanceException { - List truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH); - List truststorePasswordPaths = Arrays.asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH); - List truststoreFilesList = truststoreFilesListProvider.getTruststoreFilesList(truststorePaths, truststorePasswordPaths); - assertThat(truststoreFilesList.size()).isEqualTo(3); - assertCorrectJksTruststore(truststoreFilesList.get(0), TRUSTSTORE_JKS_PATH); - assertCorrectP12Truststore(truststoreFilesList.get(1), TRUSTSTORE_P12_PATH); - assertCorrectPemTruststore(truststoreFilesList.get(2), TRUSTSTORE_PEM_PATH); - } - - private void assertCorrectJksTruststore(Truststore truststore, String truststorePath) { - assertCorrectTypeAndTruststorePath(truststore, truststorePath, JavaTruststore.class); - } - - private void assertCorrectP12Truststore(Truststore truststore, String truststorePath) { - assertCorrectTypeAndTruststorePath(truststore, truststorePath, JavaTruststore.class); - } - - private void assertCorrectPemTruststore(Truststore truststore, String truststorePath) { - assertCorrectTypeAndTruststorePath(truststore, truststorePath, PemTruststore.class); - } - - private void assertCorrectTypeAndTruststorePath(Truststore truststore, String truststorePath, Class truststoreType) { - assertThat(truststore).isInstanceOf(truststoreType); - assertThat(truststore.getFile()).isEqualTo(new File(truststorePath)); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java deleted file mode 100644 index f05cbc86..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.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.provider.entry; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.HashSet; -import java.util.Set; -import org.junit.jupiter.api.Test; - -class PemAliasGeneratorTest { - - private static final String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; - static final int GENERATED_ALIASES_NUMBER = 100; - - @Test - void aliasHasPemPrefix() { - //given - PemAliasGenerator pemAliasGenerator = PemAliasGenerator.getInstance(); - //when - String alias = pemAliasGenerator.getAlias(); - //then - assertThat(alias.contains(PREFIX_ALIAS_NAME)).isTrue(); - } - - @Test - void generatedAliasesHaveUniqNames() { - //given - PemAliasGenerator pemAliasGenerator = PemAliasGenerator.getInstance(); - Set aliases = new HashSet<>(); - - //when - for (int i = 0; i < GENERATED_ALIASES_NUMBER; i++) { - aliases.add(pemAliasGenerator.getAlias()); - } - - //then - assertThat(aliases).hasSize(GENERATED_ALIASES_NUMBER); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.java new file mode 100644 index 00000000..b81eb36e --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.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.common; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.model.TestCertificateProvider; + +public class BackupCreatorTest { + + public static final String BAK_EXTENSION = ".bak"; + + @Test + void shouldCreateBackupProvidedFile() throws CreateBackupException { + //given + File fileToBackup = new File(TestCertificateProvider.PEM_FILE_PATH); + String backupFilePath = fileToBackup.getPath() + BAK_EXTENSION; + //when + BackupCreator.createBackup(fileToBackup); + //then + assertThat(fileToBackup.equals(new File(backupFilePath))); + } + + @AfterEach + void removeTemporaryFiles() throws IOException { + TestCertificateProvider.removeTemporaryFiles(); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java new file mode 100644 index 00000000..e59a7671 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java @@ -0,0 +1,45 @@ +/*============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.common; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.io.File; + +import static org.assertj.core.api.Assertions.assertThat; + +class ExtensionResolverTest { + + @ParameterizedTest + @CsvSource(value = { + "opt/app/truststore.jks:.jks", + "opt/app/truststore.p12:.p12", + "opt/app/truststore.pem:.pem", + "opt/app/truststore.PEM:.pem", + "opt/app/truststore:''", + }, delimiter = ':') + void shouldReturnCorrectExtension(String filePath, String expectedExtension) { + String extension = ExtensionResolver.get(new File(filePath)); + assertThat(extension).isEqualTo(expectedExtension); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/PasswordReaderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/PasswordReaderTest.java new file mode 100644 index 00000000..a43951bd --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/PasswordReaderTest.java @@ -0,0 +1,43 @@ +/*============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.common; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +class PasswordReaderTest { + + @Test + void shouldReturnCorrectPasswordFromFile() throws PasswordReaderException { + String fileData = PasswordReader.readPassword(new File("src/test/resources/truststore-jks.pass")); + assertThat(fileData).isEqualTo("EOyuFbuYDyq_EhpboM72RHua"); + } + + @Test + void shouldThrowExceptionForNonExistingFile() { + assertThatExceptionOfType(PasswordReaderException.class) + .isThrownBy(() -> PasswordReader.readPassword(new File("src/test/resources/non-esisting-file.pass"))); + } +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProviderTest.java new file mode 100644 index 00000000..9fc00a42 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProviderTest.java @@ -0,0 +1,63 @@ +/*============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; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; +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.Truststore; +import org.onap.oom.truststoremerger.merger.model.TruststoreFactory; + +class TruststoreFilesProviderTest { + + private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks"; + private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass"; + private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12"; + private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass"; + private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem"; + private static final String EMPTY_PASS_PATH = ""; + + @Test + void shouldReturnTruststoreFilesList() + throws TruststoreFileFactoryException, PasswordReaderException, LoadTruststoreException, KeystoreInstanceException { + //given + List truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH); + List truststorePasswordPaths = Arrays + .asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH); + + //when + List truststoreFilesList = TruststoreFilesProvider + .getTruststoreFiles(truststorePaths, truststorePasswordPaths); + + //then + assertThat(truststoreFilesList.size()).isEqualTo(3); + TruststoreFactory.create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH); + TruststoreFactory.create(TRUSTSTORE_P12_PATH, TRUSTSTORE_P12_PASS_PATH); + TruststoreFactory.create(TRUSTSTORE_PEM_PATH, EMPTY_PASS_PATH); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java new file mode 100644 index 00000000..99b8e623 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java @@ -0,0 +1,56 @@ +/*============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.assertj.core.api.Assertions.assertThatExceptionOfType; + +import java.util.List; +import org.junit.jupiter.api.Test; +import org.onap.oom.truststoremerger.api.ExitableException; +import org.onap.oom.truststoremerger.merger.exception.AliasConflictException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; + +class JavaTruststoreTest { + + @Test + void throwExceptionWhenAliasConflictDetected() throws Exception { + //given + Truststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); + + List certificateFromJks = TestCertificateProvider + .getSampleJksTruststoreFile().getCertificates(); + + //when //then + assertThatExceptionOfType(AliasConflictException.class) + .isThrownBy(() -> p12Truststore.addCertificates(certificateFromJks)); + } + + @Test + void throwExceptionWhenFileNotContainsTruststoreEntry() throws ExitableException { + //given + Truststore p12Truststore = TestCertificateProvider.getSampleP12Keystore(); + + //when//then + assertThatExceptionOfType(MissingTruststoreException.class) + .isThrownBy(() -> p12Truststore.getCertificates()); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/PemTruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/PemTruststoreTest.java new file mode 100644 index 00000000..505a6826 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/PemTruststoreTest.java @@ -0,0 +1,128 @@ +/*============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.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.mock; + +import java.io.File; +import java.io.IOException; +import java.security.KeyStore; +import java.security.KeyStoreSpi; +import java.security.cert.Certificate; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.jupiter.api.Test; +import org.onap.oom.truststoremerger.api.ExitableException; +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; + +class PemTruststoreTest { + + private static final int EXPECTED_ONE = 1; + + @Test + void getCertificatesShouldThrowExceptionWhenFileNotContainsCertificate() { + //given + File emptyPemFile = TestCertificateProvider.getEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(emptyPemFile); + //when//then + assertThatExceptionOfType(MissingTruststoreException.class) + .isThrownBy(pemCertificate::getCertificates); + } + + @Test + void shouldThrowExceptionWhenCannotSaveFile() { + //given + KeyStoreSpi keyStoreSpi = mock(KeyStoreSpi.class); + KeyStore keyStore = new KeyStore(keyStoreSpi, null, "") { + }; + File pemFile = TestCertificateProvider.getEmptyPemFile(); + pemFile.setWritable(false); + PemTruststore pem = new PemTruststore(pemFile); + + //when. then + assertThatExceptionOfType(WriteTruststoreFileException.class) + .isThrownBy(pem::saveFile); + } + + @Test + void transformToStringInPemFormatShouldCorrectlyTransform() throws ExitableException, IOException { + //given + Truststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); + + List wrappedCertificates = pemTruststore.getCertificates(); + List certificateList = unWrapCertificate(wrappedCertificates); + File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile); + + //when + String certificateTransformed = pemCertificate.transformToStringInPemFormat(certificateList); + + //then + String expected = TestCertificateProvider.getExpectedPemCertificateAsString(); + assertThat(certificateTransformed).isEqualTo(expected); + } + + @Test + void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate() + throws TruststoreDataOperationException { + //given + File emptyPemFile = TestCertificateProvider.getEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(emptyPemFile); + //when//then + assertThat(pemCertificate.isFileWithoutPemCertificate()).isTrue(); + } + + @Test + void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate() + throws TruststoreDataOperationException { + //given + File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile); + + //when//then + assertThat(pemCertificate.isFileWithoutPemCertificate()).isFalse(); + } + + @Test + void privateKeyIsSkippedWhileReadingCertificates() throws ExitableException { + //given + File pemTruststoreFile = TestCertificateProvider.getPemWithPrivateKeyFile(); + PemTruststore pemCertificate = new PemTruststore(pemTruststoreFile); + + //when + List certificate = pemCertificate.getCertificates(); + + //then + assertThat(certificate).hasSize(EXPECTED_ONE); + } + + private List unWrapCertificate(List certificateWithAliases) { + return certificateWithAliases + .stream() + .map(CertificateWithAlias::getCertificate) + .collect(Collectors.toList()); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java new file mode 100644 index 00000000..abd77d19 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java @@ -0,0 +1,152 @@ +/*============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 java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; + +public final class TestCertificateProvider { + + public static final String SAMPLE_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-p12.p12"; + public static final String SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH = "src/test/resources/truststore-p12.pass"; + public static final String TMP_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-p12.p12"; + + public static final String SAMPLE_P12_KEYSTORE_FILE_PATH = "src/test/resources/keystore.p12"; + public static final String SAMPLE_P12_KEYSTORE_PASSWORD_PATH = "src/test/resources/keystore.pass"; + + public static final String SAMPLE_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-jks.jks"; + public static final String SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH = "src/test/resources/truststore-jks-uniq.jks"; + public static final String SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH = "src/test/resources/truststore-jks.pass"; + public static final String TMP_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-jks.jks"; + + public static final String SAMPLE_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore.pem"; + public static final String EMPTY_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/empty-truststore.pem"; + public static final String TMP_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore.pem"; + public static final String SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH = "src/test/resources/truststore-with-private-key.pem"; + + public static final String PEM_FILE_PATH = "src/test/resources/truststore.pem"; + public static final String PEM_BACKUP_FILE_PATH = "src/test/resources/truststore.pem.bak"; + + private TestCertificateProvider() { + } + + public static Truststore getSampleP12Truststore() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE); + } + + public static Truststore getSampleP12Keystore() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD_PATH, PKCS12_TYPE); + } + + public static Truststore createTmpP12TruststoreFile() + throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + copyFile(SAMPLE_P12_TRUSTSTORE_FILE_PATH, TMP_P12_TRUSTSTORE_FILE_PATH); + return createJavaTruststore(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE); + } + + public static Truststore getSamplePemTruststoreFile() { + return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_FILE_PATH); + } + + public static Truststore createEmptyTmpPemTruststoreFile() + throws IOException { + copyFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH); + return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH); + } + + public static Truststore createTmpPemTruststoreFile() + throws IOException { + copyFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH); + return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH); + } + + public static String getExpectedPemCertificateAsString() throws IOException { + Path samplePemFilePath = Paths.get(SAMPLE_PEM_TRUSTSTORE_FILE_PATH); + return Files.readString(samplePemFilePath); + } + + public static Truststore getSampleJksTruststoreFile() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE); + } + + public static Truststore getSampleJksTruststoreFileWithUniqueAlias() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, + JKS_TYPE); + } + + public static Truststore createTmpJksTruststoreFileWithUniqAlias() + throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH); + return createJavaTruststore(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE); + } + + public static File getEmptyPemFile() { + return getFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH); + } + + public static File getNotEmptyPemFile() { + return getFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH); + } + + public static File getPemWithPrivateKeyFile() { + return getFile(SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH); + } + + public static void removeTemporaryFiles() throws IOException { + Files.deleteIfExists(Paths.get(TMP_PEM_TRUSTSTORE_FILE_PATH)); + Files.deleteIfExists(Paths.get(TMP_JKS_TRUSTSTORE_FILE_PATH)); + Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH)); + Files.deleteIfExists(Paths.get(PEM_BACKUP_FILE_PATH)); + } + + private static Truststore createJavaTruststore(String filePath, String password, String instanceType) + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + File certFile = getFile(filePath); + return JavaTruststoreFactory.create(certFile, password, instanceType); + } + + private static Truststore getPemTruststoreInstance( + String tmpPemTruststoreFilePath) { + File file = getFile(tmpPemTruststoreFilePath); + return new PemTruststore(file); + } + + private static void copyFile(String sourcePath, String destPath) throws IOException { + Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING); + } + + private static File getFile(String path) { + return new File(path); + } +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactoryTest.java new file mode 100644 index 00000000..eb6c847a --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactoryTest.java @@ -0,0 +1,109 @@ +/*============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.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +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; + +@ExtendWith(MockitoExtension.class) +class TruststoreFactoryTest { + + private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks"; + private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass"; + private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12"; + private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass"; + private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem"; + private static final String EMPTY_PASS_PATH = ""; + private static final String TRUSTSTORE_UNKNOWN_EXTENSION_PATH = "src/test/resources/truststore-jks.unknown"; + private static final String NON_EXISTING_TRUSTSTORE_PATH = "src/test/resources/non-existing-truststore.jks"; + + @Test + void shouldReturnCorrectJksTruststoreForJksFile() + throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { + //given, when + Truststore truststore = TruststoreFactory + .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH); + + //then + assertThat(truststore).isInstanceOf(Truststore.class); + } + + @Test + void shouldReturnCorrectP12TruststoreForP12File() + throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { + //given, when + Truststore truststore = TruststoreFactory + .create(TRUSTSTORE_P12_PATH, TRUSTSTORE_P12_PASS_PATH); + + //then + assertThat(truststore).isInstanceOf(Truststore.class); + } + + @Test + void shouldReturnCorrectPemTruststoreForPemFile() + throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { + //given, when + Truststore truststore = TruststoreFactory + .create(TRUSTSTORE_PEM_PATH, + EMPTY_PASS_PATH); + + //then + assertThat(truststore).isInstanceOf(Truststore.class); + } + + @Test + void shouldThrowExceptionForInvalidP12PassPath() { + assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( + () -> TruststoreFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH) + ); + } + + @Test + void shouldThrowExceptionForInvalidJksPassPath() { + assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( + () -> TruststoreFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH) + ); + } + + @Test + void shouldThrowExceptionForUnknownTruststoreExtension() { + assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( + () -> TruststoreFactory + .create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH) + ); + } + + @Test + void shouldThrowExceptionForNonExistingTruststoreFile() { + assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( + () -> TruststoreFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH) + ); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreTest.java new file mode 100644 index 00000000..6c294834 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreTest.java @@ -0,0 +1,204 @@ +/*============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.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; + +import java.io.File; +import java.io.IOException; +import java.security.cert.Certificate; +import java.security.cert.CertificateEncodingException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.onap.oom.truststoremerger.api.ExitableException; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAliasFactory; + +class TruststoreTest { + + private static final String BACKUP_EXTENSION = ".bak"; + + private static final int EXPECTED_ONE = 1; + public static final int EXPECTED_THREE = 3; + public static final int FIRST_ELEMENT = 0; + + private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); + + @Test + void createBackupShouldCreateFileWithExtension() throws CreateBackupException { + //given + File pemFile = new File(TestCertificateProvider.PEM_FILE_PATH); + Truststore truststore = new PemTruststore(pemFile); + //when + truststore.createBackup(); + + //then + File backupFile = new File(TestCertificateProvider.PEM_BACKUP_FILE_PATH); + assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue(); + assertThat(backupFile.isFile()).isTrue(); + } + + @ParameterizedTest + @MethodSource("truststoreProvider") + void truststoreShouldReadCertificatesFromFile(Truststore truststore) throws ExitableException { + //when + + List certificates = truststore.getCertificates(); + Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate(); + + //then + assertThat(certificates).hasSize(EXPECTED_ONE); + assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); + } + + @Test + void jksTruststoreShouldAddDifferentCertificates() throws Exception { + //given + Truststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias(); + + List certificateFromP12 = TestCertificateProvider.getSampleP12Truststore() + .getCertificates(); + + List certificateFromPem = TestCertificateProvider + .getSamplePemTruststoreFile().getCertificates(); + + //when + + jksTruststore.addCertificates(certificateFromP12); + + jksTruststore.addCertificates(certificateFromPem); + + jksTruststore.saveFile(); + + //then + + assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE); + } + + @Test + void p12TruststoreShouldAddDifferentCertificates() throws Exception { + //given + Truststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile(); + + List certificateFromJks = TestCertificateProvider + .getSampleJksTruststoreFileWithUniqueAlias().getCertificates(); + + List certificateFromPem = TestCertificateProvider + .getSamplePemTruststoreFile().getCertificates(); + + //when + + p12Truststore.addCertificates(certificateFromJks); + p12Truststore.addCertificates(certificateFromPem); + p12Truststore.saveFile(); + + //then + + assertThat(p12Truststore.getCertificates()).hasSize(EXPECTED_THREE); + } + + @Test + void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException { + //given + Truststore pemTruststore = TestCertificateProvider + .createTmpPemTruststoreFile(); + + List certificateFromJks = TestCertificateProvider + .getSampleJksTruststoreFileWithUniqueAlias().getCertificates(); + + List certificateFromP12 = TestCertificateProvider.getSampleP12Truststore() + .getCertificates(); + + //when + + pemTruststore.addCertificates(certificateFromJks); + + pemTruststore.addCertificates(certificateFromP12); + + pemTruststore.saveFile(); + + //then + + List addedCertificates = pemTruststore.getCertificates(); + Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate(); + + assertThat(pemTruststore.getCertificates()).hasSize(EXPECTED_THREE); + assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); + } + + @Test + void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException { + //given + Truststore tmpPemTruststoreFile = TestCertificateProvider + .createEmptyTmpPemTruststoreFile(); + //when//then + assertThatExceptionOfType(MissingTruststoreException.class) + .isThrownBy(() -> tmpPemTruststoreFile.getCertificates()); + } + + @Test + void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception { + //given + Truststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile(); + Certificate certificate = mock(Certificate.class); + + when(certificate.getEncoded()).thenThrow(new CertificateEncodingException()); + + List certificateFromPem = new ArrayList<>(); + certificateFromPem.add(factory.createPemCertificate(certificate)); + + pemTruststore.addCertificates(certificateFromPem); + + //when //then + assertThatExceptionOfType(TruststoreDataOperationException.class) + .isThrownBy(() -> pemTruststore.saveFile()); + } + + @AfterEach + void removeTemporaryFiles() throws IOException { + TestCertificateProvider.removeTemporaryFiles(); + } + + private static Stream truststoreProvider() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return Stream.of( + Arguments.of(TestCertificateProvider.getSampleJksTruststoreFile()), + Arguments.of(TestCertificateProvider.getSampleP12Truststore()), + Arguments.of(TestCertificateProvider.getSamplePemTruststoreFile()) + ); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java new file mode 100644 index 00000000..4ff3e750 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java @@ -0,0 +1,58 @@ +/*============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.certificate; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashSet; +import java.util.Set; +import org.junit.jupiter.api.Test; + +class PemAliasGeneratorTest { + + private static final String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; + static final int GENERATED_ALIASES_NUMBER = 100; + + @Test + void aliasHasPemPrefix() { + //given + PemAliasGenerator pemAliasGenerator = PemAliasGenerator.getInstance(); + //when + String alias = pemAliasGenerator.getAlias(); + //then + assertThat(alias.contains(PREFIX_ALIAS_NAME)).isTrue(); + } + + @Test + void generatedAliasesHaveUniqNames() { + //given + PemAliasGenerator pemAliasGenerator = PemAliasGenerator.getInstance(); + Set aliases = new HashSet<>(); + + //when + for (int i = 0; i < GENERATED_ALIASES_NUMBER; i++) { + aliases.add(pemAliasGenerator.getAlias()); + } + + //then + assertThat(aliases).hasSize(GENERATED_ALIASES_NUMBER); + } + +} diff --git a/trustStoreMerger/src/test/resources/keystore.pass b/trustStoreMerger/src/test/resources/keystore.pass new file mode 100644 index 00000000..665ff8e6 --- /dev/null +++ b/trustStoreMerger/src/test/resources/keystore.pass @@ -0,0 +1 @@ +Foh49MJNYI7S_pEzE9gvUDSu \ No newline at end of file -- cgit 1.2.3-korg