diff options
author | 2020-09-02 14:35:32 +0200 | |
---|---|---|
committer | 2020-09-09 14:03:01 +0200 | |
commit | a9fd6d0a175e647ac36932ce77b91f9e54e97084 (patch) | |
tree | 2c9c150c07162770be7629b0bd665185cff7f9b0 /trustStoreMerger | |
parent | 4014c7482b233bba9e344b9b3fbe6b7641ebdcfd (diff) |
Refactor truststore merger logic
- Merge PemTruststore and JavaTruststore into Truststore
- Rename controller classes to be consistent
- Remove duplicated methods
Issue-ID: DCAEGEN2-2253
Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Change-Id: I3270cd9811e5eaf360ceea0d0ca99be1155eaf00
Diffstat (limited to 'trustStoreMerger')
43 files changed, 714 insertions, 943 deletions
diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java index babd32e2..7f53331f 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java @@ -22,12 +22,9 @@ package org.onap.oom.truststoremerger; import java.util.List; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory; -import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider; -import org.onap.oom.truststoremerger.certification.file.model.Truststore; -import org.onap.oom.truststoremerger.certification.file.provider.FileManager; -import org.onap.oom.truststoremerger.certification.file.provider.PasswordReader; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.TruststoreFilesProvider; +import org.onap.oom.truststoremerger.merger.model.Truststore; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; import org.onap.oom.truststoremerger.configuration.MergerConfigurationProvider; import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration; import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; @@ -69,8 +66,9 @@ class TrustStoreMerger { baseFile.createBackup(); for (int i = SECOND_TRUSTSTORE_INDEX; i < truststoreFilesList.size(); i++) { - List<CertificateWithAlias> certificateWrappers = truststoreFilesList.get(i).getCertificates(); - baseFile.addCertificate(certificateWrappers); + Truststore truststore = truststoreFilesList.get(i); + List<CertificateWithAlias> certificateWrappers = truststore.getCertificates(); + baseFile.addCertificates(certificateWrappers); } baseFile.saveFile(); @@ -87,13 +85,9 @@ class TrustStoreMerger { return factory.createConfiguration(); } - private List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException { - TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), - new PasswordReader()); - TruststoreFilesListProvider truststoreFilesListProvider = new TruststoreFilesListProvider( - truststoreFileFactory); - return truststoreFilesListProvider - .getTruststoreFilesList( + private static List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException { + return TruststoreFilesProvider + .getTruststoreFiles( configuration.getTruststoreFilePaths(), configuration.getTruststoreFilePasswordPaths() ); diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java index 68c5d13c..75756aa1 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/CertificateConstants.java @@ -21,8 +21,8 @@ package org.onap.oom.truststoremerger.api; public class CertificateConstants { - public static final String JKS_INSTANCE = "JKS"; - public static final String PKCS12_INSTANCE = "PKCS12"; + public static final String JKS_TYPE = "JKS"; + public static final String PKCS12_TYPE = "PKCS12"; public static final String X_509_CERTIFICATE = "X.509"; public static final String BOUNCY_CASTLE_PROVIDER = "BC"; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileFactory.java deleted file mode 100644 index d93409b6..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileFactory.java +++ /dev/null @@ -1,99 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.file; - -import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore; -import org.onap.oom.truststoremerger.certification.file.model.PemTruststore; -import org.onap.oom.truststoremerger.certification.file.model.Truststore; - -import java.io.File; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; -import org.onap.oom.truststoremerger.certification.file.provider.CertificateStoreControllerFactory; -import org.onap.oom.truststoremerger.certification.file.provider.FileManager; -import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController; -import org.onap.oom.truststoremerger.certification.file.provider.PasswordReader; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; - -public class TruststoreFileFactory { - - private static final String JKS_EXTENSION = ".jks"; - private static final String P12_EXTENSION = ".p12"; - private static final String PEM_EXTENSION = ".pem"; - private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist"; - private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s"; - - private final FileManager fileManager; - private final PasswordReader passwordReader; - private final CertificateStoreControllerFactory certificateStoreControllerFactory = - new CertificateStoreControllerFactory(); - - public TruststoreFileFactory(FileManager fileManager, PasswordReader passwordReader) { - this.fileManager = fileManager; - this.passwordReader = passwordReader; - } - - public Truststore create(String truststoreFilePath, String truststorePasswordPath) - throws TruststoreFileFactoryException, PasswordReaderException, KeystoreInstanceException, LoadTruststoreException { - File truststoreFile = new File(truststoreFilePath); - if (!fileManager.checkIfFileExists(truststoreFile)) { - throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile)); - } - return createTypedTruststore(truststoreFile, truststorePasswordPath); - } - - private Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath) - throws KeystoreInstanceException, PasswordReaderException, LoadTruststoreException, TruststoreFileFactoryException { - String extension = fileManager.getExtension(truststoreFile); - switch (extension) { - case JKS_EXTENSION: - return createJksTruststore(truststoreFile, truststorePasswordPath); - case P12_EXTENSION: - return createP12Truststore(truststoreFile, truststorePasswordPath); - case PEM_EXTENSION: - return createPemTruststore(truststoreFile); - default: - throw new TruststoreFileFactoryException( - String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension)); - } - } - - private JavaTruststore createJksTruststore(File truststoreFile, String truststorePasswordPath) - throws PasswordReaderException, LoadTruststoreException, KeystoreInstanceException { - String password = passwordReader.readPassword(new File(truststorePasswordPath)); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedJksCertificateStoreController(truststoreFile, password); - return new JavaTruststore(truststoreFile, storeController); - } - - private JavaTruststore createP12Truststore(File truststoreFile, String truststorePasswordPath) - throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { - String password = passwordReader.readPassword(new File(truststorePasswordPath)); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedPkcs12CertificateStoreController(truststoreFile, password); - return new JavaTruststore(truststoreFile, storeController); - } - - private PemTruststore createPemTruststore(File truststoreFile) { - return new PemTruststore(truststoreFile, new PemCertificateController(truststoreFile)); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststore.java deleted file mode 100644 index d46fba1e..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststore.java +++ /dev/null @@ -1,58 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.file.model; - -import java.io.File; -import java.util.List; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; -import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class JavaTruststore extends Truststore { - - private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststore.class); - private final JavaCertificateStoreController storeController; - - public JavaTruststore(File truststoreFile, JavaCertificateStoreController storeController) { - super(truststoreFile); - this.storeController = storeController; - } - - @Override - public List<CertificateWithAlias> getCertificates() throws ExitableException { - LOGGER.debug("Attempt ro read certificates from file: {} ", this.getFile().getPath()); - return storeController.getNotEmptyCertificateList(); - } - - @Override - public void addCertificate(List<CertificateWithAlias> certificates) throws ExitableException { - LOGGER.debug("Attempt to add certificates for saving to file"); - storeController.addCertificates(certificates); - } - - @Override - public void saveFile() throws WriteTruststoreFileException { - LOGGER.debug("Attempt to save file: {}", this.getFile().getPath()); - storeController.saveFile(); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststore.java deleted file mode 100644 index 36195267..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststore.java +++ /dev/null @@ -1,58 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.file.model; - -import java.io.File; -import java.util.List; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PemTruststore extends Truststore { - - private static final Logger LOGGER = LoggerFactory.getLogger(PemTruststore.class); - private final PemCertificateController pemCertificateController; - - public PemTruststore(File truststoreFile, PemCertificateController pemCertificateController) { - super(truststoreFile); - this.pemCertificateController = pemCertificateController; - } - - @Override - public List<CertificateWithAlias> getCertificates() throws ExitableException { - LOGGER.debug("Attempt ro read certificates from file: {}", this.getFile().getPath()); - return pemCertificateController.getNotEmptyCertificateList(); - } - - @Override - public void addCertificate(List<CertificateWithAlias> certificates) throws ExitableException { - LOGGER.debug("Attempt to add certificates for saving to file"); - pemCertificateController.addCertificates(certificates); - } - - @Override - public void saveFile() throws ExitableException { - LOGGER.debug("Attempt to save file: {}", this.getFile().getPath()); - pemCertificateController.saveFile(); - } - -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateStoreControllerFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateStoreControllerFactory.java deleted file mode 100644 index 66e2aed2..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateStoreControllerFactory.java +++ /dev/null @@ -1,61 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.file.provider; - -import static org.onap.oom.truststoremerger.api.CertificateConstants.JKS_INSTANCE; -import static org.onap.oom.truststoremerger.api.CertificateConstants.PKCS12_INSTANCE; - -import java.io.File; -import java.security.KeyStore; -import java.security.KeyStoreException; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class CertificateStoreControllerFactory { - - private static final Logger LOGGER = LoggerFactory.getLogger(CertificateStoreControllerFactory.class); - - public JavaCertificateStoreController createLoadedJksCertificateStoreController(File certFile, String certPassword) - throws LoadTruststoreException, KeystoreInstanceException { - return createLoadedCertificateStoreController(certFile, certPassword, JKS_INSTANCE); - } - - public JavaCertificateStoreController createLoadedPkcs12CertificateStoreController(File certFile, String certPassword) - throws KeystoreInstanceException, LoadTruststoreException { - return createLoadedCertificateStoreController(certFile, certPassword, PKCS12_INSTANCE); - } - - private JavaCertificateStoreController createLoadedCertificateStoreController(File certFile, String certPassword, - String instanceType) - throws LoadTruststoreException, KeystoreInstanceException { - try { - JavaCertificateStoreController javaCertificateStoreController = new JavaCertificateStoreController( - KeyStore.getInstance(instanceType), certFile, certPassword); - javaCertificateStoreController.loadFile(); - return javaCertificateStoreController; - } catch (KeyStoreException e) { - LOGGER.error("Cannot initialize Java Keystore instance"); - throw new KeystoreInstanceException(e); - } - } -} - diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/Truststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java index 153805a7..9187393e 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/model/Truststore.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/BackupCreator.java @@ -17,46 +17,32 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.model; +package org.onap.oom.truststoremerger.common; import java.io.File; import java.io.FileOutputStream; import java.nio.file.Files; -import java.util.List; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class Truststore { +public final class BackupCreator { - private static final Logger LOGGER = LoggerFactory.getLogger(Truststore.class); + private static final Logger LOGGER = LoggerFactory.getLogger(BackupCreator.class); private static final String BACKUP_EXTENSION = ".bak"; - private final File file; - Truststore(File file) { - this.file = file; + private BackupCreator() { } - public abstract List<CertificateWithAlias> getCertificates() throws ExitableException; - - public abstract void addCertificate(List<CertificateWithAlias> certificates) throws ExitableException; - - public abstract void saveFile() throws ExitableException; - - public File getFile() { - return file; - } - - public void createBackup() throws CreateBackupException { + public static void createBackup(File file) throws CreateBackupException { LOGGER.debug("Create backup of file: {}", file.getPath()); String backupFilePath = file.getAbsolutePath() + BACKUP_EXTENSION; try (FileOutputStream fileOutputStream = new FileOutputStream(backupFilePath)) { Files.copy(file.toPath(), fileOutputStream); } catch (Exception e) { - LOGGER.error("Cannot create backup of file: {} ", getFile().getPath()); + LOGGER.error("Cannot create backup of file: {} ", file.getPath()); throw new CreateBackupException(e); } + LOGGER.debug("Backup was successfully created in: {}", backupFilePath); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/ExtensionResolver.java index 12029ade..af792c48 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/ExtensionResolver.java @@ -17,15 +17,17 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import java.io.File; -public class FileManager { +public final class ExtensionResolver { private static final int INDEX_NOT_FOUND = -1; - public String getExtension(File file) { + private ExtensionResolver() {} + + public static String get(File file) { int extStartIndex = file.getName().lastIndexOf("."); if (extStartIndex == INDEX_NOT_FOUND) { return ""; @@ -33,7 +35,7 @@ public class FileManager { return file.getName().substring(extStartIndex).toLowerCase(); } - public boolean checkIfFileExists(File file) { + public static boolean checkIfFileExists(File file) { return file.exists(); } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java index d7da53b0..d84be5ac 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/common/PasswordReader.java @@ -17,17 +17,21 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import java.io.File; import java.io.IOException; import java.nio.file.Files; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; + +public final class PasswordReader { -public class PasswordReader { private static final String COULD_NOT_READ_PASSWORD_FROM_FILE_MSG_TEMPLATE = "Could not read password from file: %s"; - public String readPassword(File file) throws PasswordReaderException { + private PasswordReader() { + } + + public static String readPassword(File file) throws PasswordReaderException { try { return Files.readString(file.toPath()); } catch (IOException e) { diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java index 90a75d91..4bdfd9f4 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java @@ -24,7 +24,7 @@ import org.onap.oom.truststoremerger.api.ExitableException; public class MergerConfigurationException extends ExitableException { - public MergerConfigurationException(String message) { - super(message, ExitStatus.MERGER_CONFIGURATION_EXCEPTION); + public MergerConfigurationException(String errorMessage) { + super(errorMessage, ExitStatus.MERGER_CONFIGURATION_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java index dda53e32..6089d314 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java @@ -24,7 +24,7 @@ import org.onap.oom.truststoremerger.api.ExitableException; public class TruststoresPathsProviderException extends ExitableException { - public TruststoresPathsProviderException(String message) { - super(message, ExitStatus.TRUSTSTORES_PATHS_PROVIDER_EXCEPTION); + public TruststoresPathsProviderException(String errorMessage) { + super(errorMessage, ExitStatus.TRUSTSTORES_PATHS_PROVIDER_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFilesListProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProvider.java index 92e3c2a8..9108fb69 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFilesListProvider.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProvider.java @@ -17,37 +17,35 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file; - -import org.onap.oom.truststoremerger.certification.file.model.Truststore; +package org.onap.oom.truststoremerger.merger; import java.util.ArrayList; import java.util.List; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreFileFactoryException; +import org.onap.oom.truststoremerger.merger.model.TruststoreFactory; +import org.onap.oom.truststoremerger.merger.model.Truststore; -public class TruststoreFilesListProvider { +public class TruststoreFilesProvider { - private final TruststoreFileFactory truststoreFileFactory; - public TruststoreFilesListProvider(TruststoreFileFactory truststoreFileFactory) { - this.truststoreFileFactory = truststoreFileFactory; + private TruststoreFilesProvider() { } - public List<Truststore> getTruststoreFilesList(List<String> truststoreFilePaths, + public static List<Truststore> getTruststoreFiles(List<String> truststoreFilePaths, List<String> truststoreFilePasswordPaths) throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { - List<Truststore> truststoreFilesList = new ArrayList<>(); + List<Truststore> truststoreFiles = new ArrayList<>(); for (int i = 0; i < truststoreFilePaths.size(); i++) { String truststorePath = truststoreFilePaths.get(i); String passwordPath = truststoreFilePasswordPaths.get(i); - Truststore truststore = truststoreFileFactory.create(truststorePath, passwordPath); - truststoreFilesList.add(truststore); + Truststore truststore = TruststoreFactory.create(truststorePath, passwordPath); + truststoreFiles.add(truststore); } - return truststoreFilesList; + return truststoreFiles; } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/AliasConflictException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java index a4102d9f..71df3a45 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/AliasConflictException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/AliasConflictException.java @@ -17,15 +17,15 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class AliasConflictException extends ExitableException { - public AliasConflictException(String message) { - super(message, ExitStatus.ALIAS_CONFLICT_EXCEPTION); + public AliasConflictException(String errorMessage) { + super(errorMessage, ExitStatus.ALIAS_CONFLICT_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/CreateBackupException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java index a21f7013..f655a9f5 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/CreateBackupException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/CreateBackupException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class CreateBackupException extends ExitableException { - public CreateBackupException(Exception e) { - super(e, ExitStatus.CREATE_BACKUP_EXCEPTION); + public CreateBackupException(Exception cause) { + super(cause, ExitStatus.CREATE_BACKUP_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/KeystoreInstanceException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java index c5bcc3ca..99a955d9 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/KeystoreInstanceException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/KeystoreInstanceException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class KeystoreInstanceException extends ExitableException { - public KeystoreInstanceException(Throwable e) { - super(e, ExitStatus.KEYSTORE_INSTANCE_EXCEPTION); + public KeystoreInstanceException(Exception cause) { + super(cause, ExitStatus.KEYSTORE_INSTANCE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/LoadTruststoreException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java index b8bb53fa..810bf556 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/LoadTruststoreException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/LoadTruststoreException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class LoadTruststoreException extends ExitableException { - public LoadTruststoreException(Throwable e) { - super(e, ExitStatus.TRUSTSTORE_LOAD_FILE_EXCEPTION); + public LoadTruststoreException(Exception cause) { + super(cause, ExitStatus.TRUSTSTORE_LOAD_FILE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/MissingTruststoreException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java index c502d6b6..9065c9d3 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/MissingTruststoreException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/MissingTruststoreException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class MissingTruststoreException extends ExitableException { - public MissingTruststoreException(String message) { - super(message, ExitStatus.MISSING_TRUSTSTORE_EXCEPTION); + public MissingTruststoreException(String errorMessage) { + super(errorMessage, ExitStatus.MISSING_TRUSTSTORE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/PasswordReaderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java index d601d229..9f9d2e27 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/PasswordReaderException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/PasswordReaderException.java @@ -17,13 +17,13 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class PasswordReaderException extends ExitableException { - public PasswordReaderException(String message) { - super(message, ExitStatus.PASSWORD_READER_EXCEPTION); + public PasswordReaderException(String errorMessage) { + super(errorMessage, ExitStatus.PASSWORD_READER_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreDataOperationException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java index cf848f79..c18cb006 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreDataOperationException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreDataOperationException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class TruststoreDataOperationException extends ExitableException { - public TruststoreDataOperationException(Exception e) { - super(e, ExitStatus.TRUSTSTORE_DATA_OPERATION_EXCEPTION); + public TruststoreDataOperationException(Exception cause) { + super(cause, ExitStatus.TRUSTSTORE_DATA_OPERATION_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreFileFactoryException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java index 18349fd4..f802a9d3 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/TruststoreFileFactoryException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/TruststoreFileFactoryException.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class TruststoreFileFactoryException extends ExitableException { - public TruststoreFileFactoryException(String message) { - super(message, ExitStatus.TRUSTSTORE_FILE_FACTORY_EXCEPTION); + public TruststoreFileFactoryException(String errorMessage) { + super(errorMessage, ExitStatus.TRUSTSTORE_FILE_FACTORY_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/WriteTruststoreFileException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java index a5e02b3c..fe368868 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/exception/WriteTruststoreFileException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/exception/WriteTruststoreFileException.java @@ -18,14 +18,14 @@ */ -package org.onap.oom.truststoremerger.certification.file.exception; +package org.onap.oom.truststoremerger.merger.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; public class WriteTruststoreFileException extends ExitableException { - public WriteTruststoreFileException(Exception e) { - super(e, ExitStatus.WRITE_TRUSTSTORE_FILE_EXCEPTION); + public WriteTruststoreFileException(Exception cause) { + super(cause, ExitStatus.WRITE_TRUSTSTORE_FILE_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java index a4b129c9..e3a03996 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststore.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; import java.io.File; import java.io.FileInputStream; @@ -28,33 +28,40 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.exception.AliasConflictException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; -import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory; +import org.onap.oom.truststoremerger.merger.exception.AliasConflictException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException; +import org.onap.oom.truststoremerger.merger.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAliasFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class JavaCertificateStoreController implements CertificateController { +public final class JavaTruststore extends Truststore { - private static final Logger LOGGER = LoggerFactory.getLogger(JavaCertificateStoreController.class); + private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststore.class); private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); private final KeyStore keyStore; - private final File storeFile; private final String password; - public JavaCertificateStoreController(KeyStore keyStore, File storeFile, String password) { + private JavaTruststore(KeyStore keyStore, File storeFile, String password) { + super(storeFile); this.keyStore = keyStore; - this.storeFile = storeFile; this.password = password; } - public List<CertificateWithAlias> getNotEmptyCertificateList() throws ExitableException { + public static JavaTruststore createWithLoadingFile(KeyStore keyStore, File storeFile, String password) + throws LoadTruststoreException { + JavaTruststore javaTruststore = new JavaTruststore(keyStore, storeFile, password); + javaTruststore.loadFile(); + return javaTruststore; + } + + public List<CertificateWithAlias> getCertificates() throws ExitableException { + LOGGER.debug("Attempt to read certificates from file: {}", storeFile.getPath()); List<String> aliases = getTruststoreAliases(); if (aliases.isEmpty()) { throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); @@ -64,6 +71,7 @@ public class JavaCertificateStoreController implements CertificateController { public void addCertificates(List<CertificateWithAlias> certificatesWithAliases) throws ExitableException { + LOGGER.debug("Attempt to add certificates for saving to file"); if (getTruststoreAliases().isEmpty()) { throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); } @@ -73,7 +81,8 @@ public class JavaCertificateStoreController implements CertificateController { } public void saveFile() throws WriteTruststoreFileException { - try (FileOutputStream outputStream = new FileOutputStream(this.storeFile)) { + LOGGER.debug("Attempt to save file: {}", storeFile.getPath()); + try (FileOutputStream outputStream = new FileOutputStream(storeFile)) { keyStore.store(outputStream, this.password.toCharArray()); } catch (Exception e) { LOGGER.error("Cannot write truststore file"); @@ -81,11 +90,11 @@ public class JavaCertificateStoreController implements CertificateController { } } - public void loadFile() throws LoadTruststoreException { + private void loadFile() throws LoadTruststoreException { try { - keyStore.load(new FileInputStream(this.storeFile), this.password.toCharArray()); + keyStore.load(new FileInputStream(storeFile), this.password.toCharArray()); } catch (Exception e) { - LOGGER.error("Cannot load file: {}", this.storeFile.getPath()); + LOGGER.error("Cannot load file: {}", storeFile.getPath()); throw new LoadTruststoreException(e); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreFactory.java new file mode 100644 index 00000000..d40cfb1a --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreFactory.java @@ -0,0 +1,51 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.merger.model; + +import java.io.File; +import java.security.KeyStore; +import java.security.KeyStoreException; +import org.onap.oom.truststoremerger.common.PasswordReader; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JavaTruststoreFactory { + + private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststoreFactory.class); + + private JavaTruststoreFactory() { + } + + public static Truststore create(File certFile, String truststorePasswordPath, String keystoreType) + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + String password = PasswordReader.readPassword(new File(truststorePasswordPath)); + try { + return JavaTruststore + .createWithLoadingFile(KeyStore.getInstance(keystoreType), certFile, password); + } catch (KeyStoreException e) { + LOGGER.error("Cannot initialize Java Keystore instance"); + throw new KeystoreInstanceException(e); + } + } +} + diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateController.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/PemTruststore.java index 9ff42b87..d04a01b1 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateController.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/PemTruststore.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; import static org.onap.oom.truststoremerger.api.CertificateConstants.BOUNCY_CASTLE_PROVIDER; import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; @@ -37,30 +37,30 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator; import org.bouncycastle.util.io.pem.PemObjectGenerator; import org.bouncycastle.util.io.pem.PemWriter; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; -import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException; +import org.onap.oom.truststoremerger.merger.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAliasFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class PemCertificateController implements CertificateController { +public class PemTruststore extends Truststore { - private static final Logger LOGGER = LoggerFactory.getLogger(PemCertificateController.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PemTruststore.class); private static final boolean APPEND_TO_FILE = true; private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory(); private final List<CertificateWithAlias> certificatesToBeSaved = new ArrayList<>(); - private final File file; - public PemCertificateController(File file) { - this.file = file; + public PemTruststore(File storeFile) { + super(storeFile); } - public List<CertificateWithAlias> getNotEmptyCertificateList() + public List<CertificateWithAlias> getCertificates() throws TruststoreDataOperationException, MissingTruststoreException { + LOGGER.debug("Attempt to read certificates from file: {}", storeFile.getPath()); if (isFileWithoutPemCertificate()) { throw new MissingTruststoreException("File does not contain any certificate"); } @@ -70,14 +70,16 @@ public class PemCertificateController implements CertificateController { public void addCertificates(List<CertificateWithAlias> certificates) throws TruststoreDataOperationException, MissingTruststoreException { + LOGGER.debug("Attempt to add certificates for saving to file"); if (isFileWithoutPemCertificate()) { - LOGGER.error("File does not contain any certificate. File path: {} ", this.file.getPath()); + LOGGER.error("File does not contain any certificate. File path: {} ", storeFile.getPath()); throw new MissingTruststoreException("File does not contain any certificate"); } certificatesToBeSaved.addAll(certificates); } public void saveFile() throws WriteTruststoreFileException, TruststoreDataOperationException { + LOGGER.debug("Attempt to save file: {}", storeFile.getPath()); List<Certificate> certificates = certificatesToBeSaved.stream() .map(CertificateWithAlias::getCertificate) .collect(Collectors.toList()); @@ -104,19 +106,17 @@ public class PemCertificateController implements CertificateController { return sw.toString(); } - private List<Certificate> extractCertificatesFromFile() throws TruststoreDataOperationException { - try (FileInputStream inputStream = new FileInputStream(this.file)) { + try (FileInputStream inputStream = new FileInputStream(storeFile)) { Security.addProvider(new BouncyCastleProvider()); CertificateFactory factory = CertificateFactory.getInstance(X_509_CERTIFICATE, BOUNCY_CASTLE_PROVIDER); return new ArrayList<>(factory.generateCertificates(inputStream)); } catch (Exception e) { - LOGGER.error("Cannot read certificates from file: {}", this.file.getPath()); + LOGGER.error("Cannot read certificates from file: {}", storeFile.getPath()); throw new TruststoreDataOperationException(e); } } - private List<PemObjectGenerator> transformToPemGenerators(List<Certificate> certificates) throws TruststoreDataOperationException { List<PemObjectGenerator> generators = new ArrayList<>(); @@ -145,7 +145,7 @@ public class PemCertificateController implements CertificateController { private void appendToFile(String certificatesAsString) throws WriteTruststoreFileException { try { - FileOutputStream fileOutputStream = new FileOutputStream(this.file, APPEND_TO_FILE); + FileOutputStream fileOutputStream = new FileOutputStream(storeFile, APPEND_TO_FILE); fileOutputStream.write(certificatesAsString.getBytes()); } catch (Exception e) { LOGGER.error("Cannot write certificates to file"); diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateController.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java index f2ed2c45..2c3acf49 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/CertificateController.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/Truststore.java @@ -17,17 +17,30 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; +import java.io.File; import java.util.List; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.common.BackupCreator; -public interface CertificateController { +public abstract class Truststore { - List<CertificateWithAlias> getNotEmptyCertificateList() throws ExitableException; + final File storeFile; - void addCertificates(List<CertificateWithAlias> certificates) throws ExitableException; + public Truststore(File storeFile) { + this.storeFile = storeFile; + } - void saveFile() throws ExitableException; + public void createBackup() throws CreateBackupException { + BackupCreator.createBackup(storeFile); + } + + public abstract List<CertificateWithAlias> getCertificates() throws ExitableException; + + public abstract void addCertificates(List<CertificateWithAlias> certificates) throws ExitableException; + + public abstract void saveFile() throws ExitableException; } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactory.java new file mode 100644 index 00000000..7e4b71e3 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactory.java @@ -0,0 +1,69 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.merger.model; + +import static org.onap.oom.truststoremerger.api.CertificateConstants.JKS_TYPE; +import static org.onap.oom.truststoremerger.api.CertificateConstants.PKCS12_TYPE; + +import java.io.File; +import org.onap.oom.truststoremerger.common.ExtensionResolver; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreFileFactoryException; + +public class TruststoreFactory { + + private static final String JKS_EXTENSION = ".jks"; + private static final String P12_EXTENSION = ".p12"; + private static final String PEM_EXTENSION = ".pem"; + private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist"; + private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s"; + + + private TruststoreFactory() { + } + + public static Truststore create(String truststoreFilePath, String truststorePasswordPath) + throws TruststoreFileFactoryException, PasswordReaderException, KeystoreInstanceException, LoadTruststoreException { + File truststoreFile = new File(truststoreFilePath); + if (!ExtensionResolver.checkIfFileExists(truststoreFile)) { + throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile)); + } + return createTypedTruststore(truststoreFile, truststorePasswordPath); + } + + private static Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath) + throws KeystoreInstanceException, PasswordReaderException, LoadTruststoreException, TruststoreFileFactoryException { + String extension = ExtensionResolver.get(truststoreFile); + switch (extension) { + case JKS_EXTENSION: + return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, JKS_TYPE); + case P12_EXTENSION: + return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, PKCS12_TYPE); + case PEM_EXTENSION: + return new PemTruststore(truststoreFile); + default: + throw new TruststoreFileFactoryException( + String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension)); + } + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAlias.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAlias.java index decc3977..990a1c66 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAlias.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAlias.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import java.security.cert.Certificate; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAliasFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java index 0889650e..ce3c0c47 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/CertificateWithAliasFactory.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/CertificateWithAliasFactory.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import java.security.cert.Certificate; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGenerator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java index 56faa1f6..b812fcb3 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGenerator.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGenerator.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import java.util.concurrent.atomic.AtomicInteger; 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<CertificateWithAlias> 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<CertificateWithAlias> p12certificates = TestCertificateProvider.getSampleP12Truststore() - .getCertificates(); - List<CertificateWithAlias> 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<CertificateWithAlias> 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<CertificateWithAlias> jksTruststoreCertificates = TestCertificateProvider - .getSampleJksTruststoreFileWithUniqueAlias() - .getCertificates(); - List<CertificateWithAlias> 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<CertificateWithAlias> 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<CertificateWithAlias> jksTruststoreCertificates = TestCertificateProvider - .getSampleJksTruststoreFileWithUniqueAlias().getCertificates(); - List<CertificateWithAlias> p12TruststoreCertificates = TestCertificateProvider.getSampleP12Truststore() - .getCertificates(); - - //when - tmpPemTruststoreFile.addCertificate(jksTruststoreCertificates); - tmpPemTruststoreFile.addCertificate(p12TruststoreCertificates); - tmpPemTruststoreFile.saveFile(); - - PemTruststore tmpPemTruststoreSaved = TestCertificateProvider.getTmpPemTruststoreFile(); - List<CertificateWithAlias> 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<CertificateWithAlias> certificate = pemTruststore.getCertificates(); - //then - - assertThat(certificate).hasSize(EXPECTED_ONE); - } - - @Test - void shouldThrowExceptionWhenCannotSaveFile() throws IOException, ExitableException { - //given - PemTruststore tmpPemTruststoreFile = TestCertificateProvider.createTmpPemTruststoreFile(); - List<CertificateWithAlias> 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<CertificateWithAlias> 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/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<String> truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH); - List<String> truststorePasswordPaths = Arrays.asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH); - List<Truststore> 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/model/TruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.java index eea1f9c7..b81eb36e 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/TruststoreTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.java @@ -17,44 +17,35 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.model; +package org.onap.oom.truststoremerger.common; + +import static org.assertj.core.api.Assertions.assertThat; 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.AfterEach; 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; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.model.TestCertificateProvider; -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"; +public class BackupCreatorTest { + public static final String BAK_EXTENSION = ".bak"; @Test - void createBackupShouldCreateFileWithExtension() throws CreateBackupException { + void shouldCreateBackupProvidedFile() throws CreateBackupException { //given - File pemFile = new File(PEM_FILE_PATH); - Truststore truststore = new PemTruststore(pemFile, new PemCertificateController(pemFile)); + File fileToBackup = new File(TestCertificateProvider.PEM_FILE_PATH); + String backupFilePath = fileToBackup.getPath() + BAK_EXTENSION; //when - truststore.createBackup(); - + BackupCreator.createBackup(fileToBackup); //then - File backupFile = new File(PEM_BACKUP_FILE_PATH); - assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue(); - assertThat(backupFile.isFile()).isTrue(); + assertThat(fileToBackup.equals(new File(backupFilePath))); } - - @AfterAll - static void removeBackupFile() throws IOException { - Files.deleteIfExists(Paths.get(PEM_BACKUP_FILE_PATH)); + @AfterEach + void removeTemporaryFiles() throws IOException { + TestCertificateProvider.removeTemporaryFiles(); } } 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/common/ExtensionResolverTest.java index c649ba68..e59a7671 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java @@ -18,7 +18,7 @@ */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -27,9 +27,7 @@ import java.io.File; import static org.assertj.core.api.Assertions.assertThat; -class FileManagerTest { - - private FileManager fileManager = new FileManager(); +class ExtensionResolverTest { @ParameterizedTest @CsvSource(value = { @@ -40,7 +38,7 @@ class FileManagerTest { "opt/app/truststore:''", }, delimiter = ':') void shouldReturnCorrectExtension(String filePath, String expectedExtension) { - String extension = fileManager.getExtension(new File(filePath)); + String extension = ExtensionResolver.get(new File(filePath)); assertThat(extension).isEqualTo(expectedExtension); } 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/common/PasswordReaderTest.java index 40eda4dd..a43951bd 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/PasswordReaderTest.java @@ -17,12 +17,12 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import org.junit.jupiter.api.Test; import java.io.File; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -31,15 +31,13 @@ class PasswordReaderTest { @Test void shouldReturnCorrectPasswordFromFile() throws PasswordReaderException { - PasswordReader passwordReader = new PasswordReader(); - String fileData = passwordReader.readPassword(new File("src/test/resources/truststore-jks.pass")); + 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"))); + .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<String> truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH); + List<String> truststorePasswordPaths = Arrays + .asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH); + + //when + List<Truststore> 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/certification/file/provider/JavaCertificateStoreControllerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java index 8ee77ef2..99b8e623 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreControllerTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java @@ -17,43 +17,40 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; 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 { +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 - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); - List<CertificateWithAlias> jksTruststoreCertificates = getSampleJksTruststoreFile().getCertificates(); + Truststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); + + List<CertificateWithAlias> certificateFromJks = TestCertificateProvider + .getSampleJksTruststoreFile().getCertificates(); //when //then assertThatExceptionOfType(AliasConflictException.class) - .isThrownBy(() -> p12Truststore.addCertificate(jksTruststoreCertificates)); + .isThrownBy(() -> p12Truststore.addCertificates(certificateFromJks)); } - @Test void throwExceptionWhenFileNotContainsTruststoreEntry() throws ExitableException { //given - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Keystore(); + Truststore p12Truststore = TestCertificateProvider.getSampleP12Keystore(); //when//then assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(p12Truststore::getCertificates); + .isThrownBy(() -> p12Truststore.getCertificates()); } } 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/merger/model/PemTruststoreTest.java index 080fcca3..505a6826 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateControllerTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/PemTruststoreTest.java @@ -17,50 +17,70 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +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.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; +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 PemCertificateControllerTest { +class PemTruststoreTest { + + private static final int EXPECTED_ONE = 1; @Test - void getNotEmptyCertificateListShouldThrowExceptionWhenFileNotContainsCertificate() { + void getCertificatesShouldThrowExceptionWhenFileNotContainsCertificate() { //given - File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile); + File emptyPemFile = TestCertificateProvider.getEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(emptyPemFile); //when//then assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(pemCertificateController::getNotEmptyCertificateList); + .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 - PemTruststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); + Truststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); + List<CertificateWithAlias> wrappedCertificates = pemTruststore.getCertificates(); - File notEmptyPemFile = pemTruststore.getFile(); List<Certificate> certificateList = unWrapCertificate(wrappedCertificates); - PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile); - String expected = TestCertificateProvider.getExpectedPemCertificateAsString(); + File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile); //when - String certificateTransformed = pemCertificateController.transformToStringInPemFormat(certificateList); + String certificateTransformed = pemCertificate.transformToStringInPemFormat(certificateList); //then + String expected = TestCertificateProvider.getExpectedPemCertificateAsString(); assertThat(certificateTransformed).isEqualTo(expected); } @@ -68,21 +88,34 @@ class PemCertificateControllerTest { void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate() throws TruststoreDataOperationException { //given - File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile); + File emptyPemFile = TestCertificateProvider.getEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(emptyPemFile); //when//then - assertThat(pemCertificateController.isFileWithoutPemCertificate()).isTrue(); + assertThat(pemCertificate.isFileWithoutPemCertificate()).isTrue(); } @Test void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate() throws TruststoreDataOperationException { //given - File notEmptyPemFile = TestCertificateProvider.getSamplePemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile); + File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile); //when//then - assertThat(pemCertificateController.isFileWithoutPemCertificate()).isFalse(); + assertThat(pemCertificate.isFileWithoutPemCertificate()).isFalse(); + } + + @Test + void privateKeyIsSkippedWhileReadingCertificates() throws ExitableException { + //given + File pemTruststoreFile = TestCertificateProvider.getPemWithPrivateKeyFile(); + PemTruststore pemCertificate = new PemTruststore(pemTruststoreFile); + + //when + List<CertificateWithAlias> certificate = pemCertificate.getCertificates(); + + //then + assertThat(certificate).hasSize(EXPECTED_ONE); } private List<Certificate> unWrapCertificate(List<CertificateWithAlias> certificateWithAliases) { diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java index b5893230..abd77d19 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java @@ -17,7 +17,10 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file; +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; @@ -25,26 +28,22 @@ 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; +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 class TestCertificateProvider { +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 = "88y9v5D8H3SG6bZWRVHDfOAo"; + 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 = "Foh49MJNYI7S_pEzE9gvUDSu"; + 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 = "EOyuFbuYDyq_EhpboM72RHua"; + 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"; @@ -52,99 +51,95 @@ public class TestCertificateProvider { 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 final String PEM_FILE_PATH = "src/test/resources/truststore.pem"; + public static final String PEM_BACKUP_FILE_PATH = "src/test/resources/truststore.pem.bak"; - public static JavaTruststore getSampleP12Truststore() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD); + private TestCertificateProvider() { } - public static JavaTruststore getSampleP12Keystore() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD); + public static Truststore getSampleP12Truststore() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE); } - 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 Truststore getSampleP12Keystore() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD_PATH, PKCS12_TYPE); } - 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 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 PemTruststore getSamplePemTruststoreFile() { + public static Truststore 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 { + 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 PemTruststore createTmpPemTruststoreFile() throws IOException { + 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 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 Truststore getSampleJksTruststoreFile() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE); } - public static JavaTruststore getSampleJksTruststoreFileWithUniqueAlias() - throws LoadTruststoreException, KeystoreInstanceException { - return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, - SAMPLE_JKS_TRUSTSTORE_PASSWORD); + 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 JavaTruststore createTmpJksTruststoreFileWithUniqAlias() - throws IOException, LoadTruststoreException, KeystoreInstanceException { + public static Truststore createTmpJksTruststoreFileWithUniqAlias() + throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH); - return createJksTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); + 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 JavaTruststore createJksTruststoreInstance(String filePath, String password) - throws LoadTruststoreException, KeystoreInstanceException { + private static Truststore createJavaTruststore(String filePath, String password, String instanceType) + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { File certFile = getFile(filePath); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedJksCertificateStoreController(certFile, password); - return new JavaTruststore(certFile, storeController); + return JavaTruststoreFactory.create(certFile, password, instanceType); } - private static PemTruststore getPemTruststoreInstance(String tmpPemTruststoreFilePath) { + private static Truststore getPemTruststoreInstance( + String tmpPemTruststoreFilePath) { File file = getFile(tmpPemTruststoreFilePath); - return new PemTruststore(file, new PemCertificateController(file)); + return new PemTruststore(file); } private static void copyFile(String sourcePath, String destPath) throws IOException { 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/merger/model/TruststoreFactoryTest.java index b2063cc3..eb6c847a 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFactoryTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactoryTest.java @@ -18,25 +18,18 @@ */ -package org.onap.oom.truststoremerger.certification.file.provider; +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.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; +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 { @@ -50,66 +43,66 @@ class TruststoreFactoryTest { 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)); + //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 { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_P12_PATH, - TRUSTSTORE_P12_PASS_PATH); - assertThat(truststore).isInstanceOf(JavaTruststore.class); + //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 { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_PEM_PATH, - EMPTY_PASS_PATH); - assertThat(truststore).isInstanceOf(PemTruststore.class); + //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( - () -> truststoreFileFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH) + () -> TruststoreFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH) ); } @Test void shouldThrowExceptionForInvalidJksPassPath() { assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( - () -> truststoreFileFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH) + () -> TruststoreFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH) ); } @Test void shouldThrowExceptionForUnknownTruststoreExtension() { assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( - () -> truststoreFileFactory.create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH) + () -> TruststoreFactory + .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) + () -> 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<CertificateWithAlias> 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<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore() + .getCertificates(); + + List<CertificateWithAlias> 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<CertificateWithAlias> certificateFromJks = TestCertificateProvider + .getSampleJksTruststoreFileWithUniqueAlias().getCertificates(); + + List<CertificateWithAlias> 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<CertificateWithAlias> certificateFromJks = TestCertificateProvider + .getSampleJksTruststoreFileWithUniqueAlias().getCertificates(); + + List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore() + .getCertificates(); + + //when + + pemTruststore.addCertificates(certificateFromJks); + + pemTruststore.addCertificates(certificateFromP12); + + pemTruststore.saveFile(); + + //then + + List<CertificateWithAlias> 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<CertificateWithAlias> 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<Arguments> 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/certification/file/provider/entry/PemAliasGeneratorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java index f05cbc86..4ff3e750 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import static org.assertj.core.api.Assertions.assertThat; 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 |