diff options
author | 2020-09-08 13:06:20 +0000 | |
---|---|---|
committer | 2020-09-08 13:06:20 +0000 | |
commit | 4014c7482b233bba9e344b9b3fbe6b7641ebdcfd (patch) | |
tree | b4ebd9797d79bfe3d6a37057badd20f8e36db697 /trustStoreMerger | |
parent | 45cf2f1b28f94dc5e1a9b88870aec372ac1e572b (diff) | |
parent | 0d7863b1fdbdb7d25610ec33460c2442a60fef45 (diff) |
Merge "Add of config for copier"
Diffstat (limited to 'trustStoreMerger')
19 files changed, 503 insertions, 259 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 e2e0c62a..babd32e2 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java @@ -19,20 +19,20 @@ 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.provider.entry.CertificateWithAlias; +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.TruststoreFileFactory; -import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider; -import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProviderFactory; -import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProvider; -import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration; +import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; import org.onap.oom.truststoremerger.configuration.MergerConfigurationProvider; - -import java.util.List; +import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReaderFactory; +import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,12 +55,15 @@ class TrustStoreMerger { } catch (ExitableException e) { LOGGER.error("Truststore Merger fails in execution: ", e); appExitHandler.exit(e.applicationExitStatus()); + } catch (Exception e) { + LOGGER.error("Truststore Merger fails in execution: ", e); + appExitHandler.exit(ExitStatus.UNEXPECTED_EXCEPTION); } } private void mergeTruststores() throws ExitableException { MergerConfiguration configuration = loadConfiguration(); - List<Truststore> truststoreFilesList = getTruststoreFilesList(configuration); + List<Truststore> truststoreFilesList = getTruststoreFiles(configuration); Truststore baseFile = truststoreFilesList.get(FIRST_TRUSTSTORE_INDEX); baseFile.createBackup(); @@ -74,12 +77,17 @@ class TrustStoreMerger { } private MergerConfiguration loadConfiguration() throws ExitableException { - TruststoresPathsProvider truststoresPathsProvider = TruststoresPathsProviderFactory.create(); - MergerConfigurationProvider factory = new MergerConfigurationProvider(truststoresPathsProvider); + DelimitedPathsReaderFactory readerFactory = new DelimitedPathsReaderFactory(new EnvProvider()); + DelimitedPathsReader certificatesPathsReader = readerFactory.createCertificatePathsReader(); + DelimitedPathsReader passwordsPathsReader = readerFactory.createPasswordPathsReader(); + DelimitedPathsReader copierPathsReader = readerFactory.createKeystoreCopierPathsReader(); + MergerConfigurationProvider factory = new MergerConfigurationProvider(certificatesPathsReader, + passwordsPathsReader, + copierPathsReader); return factory.createConfiguration(); } - private List<Truststore> getTruststoreFilesList(MergerConfiguration configuration) throws ExitableException { + private List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException { TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); TruststoreFilesListProvider truststoreFilesListProvider = new TruststoreFilesListProvider( diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java index b9111bf9..d3e246ae 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java @@ -32,7 +32,8 @@ public enum ExitStatus { TRUSTSTORE_DATA_OPERATION_EXCEPTION(8, "Cannot operate on truststore data"), MISSING_TRUSTSTORE_EXCEPTION(9, "Missing truststore certificates in provided file"), ALIAS_CONFLICT_EXCEPTION(10, "Alias conflict detected"), - WRITE_TRUSTSTORE_FILE_EXCEPTION(11, "Cannot save truststore file"); + WRITE_TRUSTSTORE_FILE_EXCEPTION(11, "Cannot save truststore file"), + UNEXPECTED_EXCEPTION(99, "Application exited abnormally"); private final int value; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java index 1c20fd38..a4b129c9 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java @@ -28,13 +28,13 @@ 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.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory; import org.onap.oom.truststoremerger.certification.file.exception.AliasConflictException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; +import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,7 +55,7 @@ public class JavaCertificateStoreController implements CertificateController { } public List<CertificateWithAlias> getNotEmptyCertificateList() throws ExitableException { - List<String> aliases = getTruststoreAliasesList(); + List<String> aliases = getTruststoreAliases(); if (aliases.isEmpty()) { throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); } @@ -64,7 +64,7 @@ public class JavaCertificateStoreController implements CertificateController { public void addCertificates(List<CertificateWithAlias> certificatesWithAliases) throws ExitableException { - if (getTruststoreAliasesList().isEmpty()){ + if (getTruststoreAliases().isEmpty()) { throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath()); } for (CertificateWithAlias certificate : certificatesWithAliases) { @@ -133,7 +133,7 @@ public class JavaCertificateStoreController implements CertificateController { } } - private List<String> getTruststoreAliasesList() throws TruststoreDataOperationException { + private List<String> getTruststoreAliases() throws TruststoreDataOperationException { try { List<String> aliases = Collections.list(keyStore.aliases()); return getFilteredAlias(aliases); diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ConfigurationEnvs.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/ConfigurationEnvs.java index f6f8bbba..fa72e4b0 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ConfigurationEnvs.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/ConfigurationEnvs.java @@ -17,10 +17,17 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.api; +package org.onap.oom.truststoremerger.configuration; -public class ConfigurationEnvs { +public final class ConfigurationEnvs { + + private ConfigurationEnvs() { + + } public static final String TRUSTSTORES_PATHS_ENV = "TRUSTSTORES_PATHS"; public static final String TRUSTSTORES_PASSWORDS_PATHS_ENV = "TRUSTSTORES_PASSWORDS_PATHS"; + public static final String KEYSTORE_SOURCE_PATHS_ENV = "KEYSTORE_SOURCE_PATHS"; + public static final String KEYSTORE_DESTINATION_PATHS_ENV = "KEYSTORE_DESTINATION_PATHS"; + } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProvider.java index 5a40a64a..5cbf8b20 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProvider.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProvider.java @@ -19,34 +19,53 @@ package org.onap.oom.truststoremerger.configuration; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_DESTINATION_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_SOURCE_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; import java.util.List; import org.onap.oom.truststoremerger.configuration.exception.MergerConfigurationException; import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration; -import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProvider; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; public class MergerConfigurationProvider { - private final TruststoresPathsProvider pathsProvider; + private final DelimitedPathsReader truststoresPathsReader; + private final DelimitedPathsReader truststoresPasswordsPathsReader; + private final DelimitedPathsReader copierPathsReader; - public MergerConfigurationProvider(TruststoresPathsProvider pathsProvider) { - this.pathsProvider = pathsProvider; + public MergerConfigurationProvider(DelimitedPathsReader truststoresPathsReader, + DelimitedPathsReader truststoresPasswordsPathsReader, DelimitedPathsReader copierPathsReader) { + this.truststoresPathsReader = truststoresPathsReader; + this.truststoresPasswordsPathsReader = truststoresPasswordsPathsReader; + this.copierPathsReader = copierPathsReader; } public MergerConfiguration createConfiguration() throws MergerConfigurationException, TruststoresPathsProviderException { - List<String> truststores = pathsProvider.getTruststores(); - List<String> truststoresPasswords = pathsProvider.getTruststoresPasswords(); + List<String> truststoresPaths = truststoresPathsReader.get(TRUSTSTORES_PATHS_ENV); + List<String> truststoresPasswordsPaths = truststoresPasswordsPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV); + List<String> sourceKeystorePaths = copierPathsReader.get(KEYSTORE_SOURCE_PATHS_ENV); + List<String> destinationKeystorePaths = copierPathsReader.get(KEYSTORE_DESTINATION_PATHS_ENV); - if (truststores.size() != truststoresPasswords.size()) { + ensureSameSize(truststoresPaths, truststoresPasswordsPaths, TRUSTSTORES_PATHS_ENV, + TRUSTSTORES_PASSWORDS_PATHS_ENV); + ensureSameSize(sourceKeystorePaths, destinationKeystorePaths, KEYSTORE_SOURCE_PATHS_ENV, + KEYSTORE_DESTINATION_PATHS_ENV); + + return new MergerConfiguration(truststoresPaths, truststoresPasswordsPaths, sourceKeystorePaths, + destinationKeystorePaths); + } + + private void ensureSameSize(List<String> firstList, List<String> secondList, String firstListEnvName, + String secondListEnvName) + throws MergerConfigurationException { + if (firstList.size() != secondList.size()) { throw new MergerConfigurationException( - "Size of " + TRUSTSTORES_PATHS_ENV - + " does not match size of " + TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variables"); + "Size of " + firstListEnvName + + " does not match size of " + secondListEnvName + " environment variables"); } - - return new MergerConfiguration(truststores, truststoresPasswords); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/MergerConfiguration.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/MergerConfiguration.java index 3b807c39..0809ab5a 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/MergerConfiguration.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/MergerConfiguration.java @@ -25,20 +25,32 @@ import java.util.List; public final class MergerConfiguration { private final List<String> truststoreFilePaths; private final List<String> truststoreFilePasswordPaths; + private final List<String> sourceKeystorePaths; + private final List<String> destinationKeystorePaths; public MergerConfiguration(List<String> truststoreFilePaths, - List<String> truststoreFilePasswordPaths) { + List<String> truststoreFilePasswordPaths, List<String> sourceKeystorePaths, + List<String> destinationKeystorePaths) { this.truststoreFilePaths = List.copyOf(truststoreFilePaths); this.truststoreFilePasswordPaths = List.copyOf(truststoreFilePasswordPaths); + this.sourceKeystorePaths = List.copyOf(sourceKeystorePaths); + this.destinationKeystorePaths = List.copyOf(destinationKeystorePaths); } public List<String> getTruststoreFilePaths() { return Collections.unmodifiableList(truststoreFilePaths); } - public List<String> getTruststoreFilePasswordPaths() { return Collections.unmodifiableList(truststoreFilePasswordPaths); } + + public List<String> getDestinationKeystorePaths() { + return Collections.unmodifiableList(destinationKeystorePaths); + } + + public List<String> getSourceKeystorePaths() { + return Collections.unmodifiableList(sourceKeystorePaths); + } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReader.java index 9d7e30a4..e5de4d08 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProvider.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReader.java @@ -19,52 +19,36 @@ package org.onap.oom.truststoremerger.configuration.path; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; - import java.util.Arrays; import java.util.List; import java.util.function.Predicate; import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; +import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; -public class TruststoresPathsProvider { +public class DelimitedPathsReader { private static final String DELIMITER = ":"; private static final int NEGATIVE_SPLIT_LIMIT = -1; private final EnvProvider envProvider; - private final PathValidator pathValidator; + private final Predicate<List<String>> pathsValidator; - TruststoresPathsProvider(EnvProvider envProvider, PathValidator pathValidator) { + DelimitedPathsReader(EnvProvider envProvider, Predicate<List<String>> pathsValidator) { this.envProvider = envProvider; - this.pathValidator = pathValidator; + this.pathsValidator = pathsValidator; } - public List<String> getTruststores() throws TruststoresPathsProviderException { - return envProvider.getEnv(TRUSTSTORES_PATHS_ENV) - .filter(Predicate.not(String::isEmpty)) + public List<String> get(String envName) throws TruststoresPathsProviderException { + return envProvider.getEnv(envName) + .filter(this::hasValue) .map(this::splitToList) - .filter(this::validateTruststores) + .filter(pathsValidator) .orElseThrow(() -> new TruststoresPathsProviderException( - TRUSTSTORES_PATHS_ENV + " environment variable does not contain valid truststores paths")); - } - - public List<String> getTruststoresPasswords() throws TruststoresPathsProviderException { - return envProvider.getEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV) - .map(this::splitToList) - .filter(this::validateTruststoresPasswords) - .orElseThrow(() -> new TruststoresPathsProviderException( - TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variable does not contain valid passwords paths")); - } - - private boolean validateTruststores(List<String> truststores) { - return truststores.stream() - .allMatch(pathValidator::isTruststorePathValid); + envName + " environment variable does not contain valid paths")); } - private boolean validateTruststoresPasswords(List<String> truststoresPasswords) { - return truststoresPasswords.stream() - .allMatch(pathValidator::isTruststorePasswordPathValid); + private boolean hasValue(String envValue) { + return !envValue.isEmpty(); } private List<String> splitToList(String stringToSplit) { diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactory.java new file mode 100644 index 00000000..b2a64f97 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactory.java @@ -0,0 +1,46 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.configuration.path; + +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPathsToCopy; + +import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; + +public final class DelimitedPathsReaderFactory { + private final EnvProvider envProvider; + + public DelimitedPathsReaderFactory(EnvProvider envProvider) { + this.envProvider = envProvider; + } + + public DelimitedPathsReader createPasswordPathsReader() { + return new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); + } + + public DelimitedPathsReader createCertificatePathsReader() { + return new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); + } + + public DelimitedPathsReader createKeystoreCopierPathsReader() { + return new DelimitedPathsReader(envProvider, doesItContainValidPathsToCopy()); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java deleted file mode 100644 index 4240228d..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java +++ /dev/null @@ -1,38 +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.configuration.path; - -class PathValidator { - - private static final String TRUSTSTORE_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.(pem|jks|p12)"; - private static final String TRUSTSTORE_PASSWORD_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.pass"; - - boolean isTruststorePathValid(String truststorePath) { - return isPathValid(truststorePath, TRUSTSTORE_PATH_REGEX); - } - - boolean isTruststorePasswordPathValid(String truststorePasswordPath) { - return truststorePasswordPath.isEmpty() || isPathValid(truststorePasswordPath, TRUSTSTORE_PASSWORD_PATH_REGEX); - } - - private boolean isPathValid(String path, String regex) { - return path.matches(regex); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java deleted file mode 100644 index 7a999108..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java +++ /dev/null @@ -1,30 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.configuration.path; - -public class TruststoresPathsProviderFactory { - - private TruststoresPathsProviderFactory() { - } - - public static TruststoresPathsProvider create() { - return new TruststoresPathsProvider(new EnvProvider(), new PathValidator()); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/EnvProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/env/EnvProvider.java index c6fbf515..1dd127d0 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/EnvProvider.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/env/EnvProvider.java @@ -17,18 +17,18 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.configuration.path; +package org.onap.oom.truststoremerger.configuration.path.env; import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class EnvProvider { +public class EnvProvider { private static final Logger LOGGER = LoggerFactory.getLogger(EnvProvider.class); - Optional<String> getEnv(String name) { + public Optional<String> getEnv(String name) { String value = System.getenv(name); LOGGER.info("Read variable: {} , value: {}", name, value); return Optional.ofNullable(System.getenv(name)); diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java new file mode 100644 index 00000000..f4cfa60c --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java @@ -0,0 +1,57 @@ +/*============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.configuration.path.validation; + +import java.util.List; +import java.util.function.Predicate; + +public final class ValidationFunctions { + + private static final String CERTIFICATE_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.(pem|jks|p12)"; + private static final String TRUSTSTORE_PASSWORD_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.pass"; + + private ValidationFunctions() { + } + + public static Predicate<List<String>> doesItContainValidPasswordPaths() { + return paths -> paths.stream().allMatch(ValidationFunctions::isCertificatePasswordPathValid); + } + + public static Predicate<List<String>> doesItContainValidCertificatesPaths() { + return paths -> paths.stream().allMatch(ValidationFunctions::isCertificatePathValid); + } + + public static Predicate<List<String>> doesItContainValidPathsToCopy() { + return paths -> paths.stream().allMatch(path -> + doesMatch(path, TRUSTSTORE_PASSWORD_PATH_REGEX) || isCertificatePathValid(path)); + } + + private static boolean isCertificatePathValid(String path) { + return doesMatch(path, CERTIFICATE_PATH_REGEX); + } + + private static boolean isCertificatePasswordPathValid(String path) { + return path.isEmpty() || doesMatch(path, TRUSTSTORE_PASSWORD_PATH_REGEX); + } + + private static boolean doesMatch(String path, String regex) { + return path.matches(regex); + } +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java index c971ca17..b5893230 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java @@ -113,19 +113,19 @@ public class TestCertificateProvider { public static JavaTruststore getSampleJksTruststoreFile() throws LoadTruststoreException, KeystoreInstanceException { - return createJKSTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); + return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); } public static JavaTruststore getSampleJksTruststoreFileWithUniqueAlias() throws LoadTruststoreException, KeystoreInstanceException { - return createJKSTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, + return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); } public static JavaTruststore createTmpJksTruststoreFileWithUniqAlias() throws IOException, LoadTruststoreException, KeystoreInstanceException { copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH); - return createJKSTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); + return createJksTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); } public static void removeTemporaryFiles() throws IOException { @@ -134,7 +134,7 @@ public class TestCertificateProvider { Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH)); } - private static JavaTruststore createJKSTruststoreInstance(String filePath, String password) + private static JavaTruststore createJksTruststoreInstance(String filePath, String password) throws LoadTruststoreException, KeystoreInstanceException { File certFile = getFile(filePath); JavaCertificateStoreController storeController = certificateStoreControllerFactory diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java index 0897de29..f05cbc86 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/certification/file/provider/entry/PemAliasGeneratorTest.java @@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test; class PemAliasGeneratorTest { - private final static String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; + private static final String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; static final int GENERATED_ALIASES_NUMBER = 100; @Test diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java index 854cde79..3df9bfdb 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java @@ -19,82 +19,154 @@ package org.onap.oom.truststoremerger.configuration; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.when; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_DESTINATION_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_SOURCE_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV; + +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.ArrayList; -import java.util.List; import org.onap.oom.truststoremerger.configuration.exception.MergerConfigurationException; import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration; -import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProvider; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.mockito.Mockito.when; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; @ExtendWith(MockitoExtension.class) class MergerConfigurationProviderTest { private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_"; - private static final String TRUSTSTORE_EXTENSION = ".jks"; - private static final String PASSWORD_EXTENSION = ".pass"; + private static final String KEYSTORE_PATH = "/opt/app/keystore_"; + private static final String ANOTHER_KEYSTORE_PATH = "/opt/app/external/keystore_"; + private static final String JKS_EXTENSION = ".jks"; + private static final String PEM_EXTENSION = ".pem"; + private static final String PASS_EXTENSION = ".pass"; @Mock - private TruststoresPathsProvider pathsProvider; + private DelimitedPathsReader certificatesPathsProvider; + @Mock + private DelimitedPathsReader passwordsPathsProvider; + @Mock + private DelimitedPathsReader copierPathsReader; private MergerConfigurationProvider factory; @BeforeEach void setUp() { - factory = new MergerConfigurationProvider(pathsProvider); + factory = new MergerConfigurationProvider(certificatesPathsProvider, passwordsPathsProvider, copierPathsReader); } @Test - void shouldReturnConfigurationWithCorrectPaths() throws TruststoresPathsProviderException, MergerConfigurationException { + void shouldReturnConfigurationWithCorrectPaths() + throws TruststoresPathsProviderException, MergerConfigurationException { int numberOfPaths = 5; - List<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION); - List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); + List<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, JKS_EXTENSION); + List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfPaths, PEM_EXTENSION); + List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + numberOfPaths, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); MergerConfiguration configuration = factory.createConfiguration(); assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths); assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths); + assertThat(configuration.getSourceKeystorePaths()).containsAll(sourceKeystoresPairPaths); + assertThat(configuration.getDestinationKeystorePaths()).containsAll(destKeystoresPairPaths); } @Test - void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException { - int numberOfTruststores = 5; + void shouldThrowExceptionWhenTruststoresLengthDifferentThanTruststoresPasswordsLength() + throws TruststoresPathsProviderException { + int numberOfCertificates = 5; int numberOfTruststoresPasswords = 4; - List<String> truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION); - List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); + List<String> truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION); + List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); assertThatExceptionOfType(MergerConfigurationException.class) - .isThrownBy(factory::createConfiguration); + .isThrownBy(factory::createConfiguration); } - private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException { + @Test + void shouldThrowExceptionWhenSourceLengthDifferentThanDestinationLength() + throws TruststoresPathsProviderException { + int numberOfCertificates = 5; + int anotherNumberOfCertificates = 1; + List<String> truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION); + List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfCertificates, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + anotherNumberOfCertificates, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); + + assertThatExceptionOfType(MergerConfigurationException.class) + .isThrownBy(factory::createConfiguration); + } + + private void mockTruststorePaths(List<String> truststores, List<String> truststoresPasswords) + throws TruststoresPathsProviderException { mockTruststores(truststores); mockTruststoresPasswords(truststoresPasswords); } + private void mockKeystorePaths(List<String> sourceKeystoresPairPaths, List<String> destKeystoresPairPaths) + throws TruststoresPathsProviderException { + mockKeystoreCopierSourcePaths(sourceKeystoresPairPaths); + mockKeystoreCopierDestinationPaths(destKeystoresPairPaths); + } + private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststores()).thenReturn(truststores); + when(certificatesPathsProvider.get(TRUSTSTORES_PATHS_ENV)).thenReturn(truststores); } private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords); + when(passwordsPathsProvider.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).thenReturn(truststoresPasswords); + } + + private void mockKeystoreCopierSourcePaths(List<String> paths) throws TruststoresPathsProviderException { + when(copierPathsReader.get(KEYSTORE_SOURCE_PATHS_ENV)).thenReturn(paths); } - private List<String> createListOfPathsWithExtension(int numberOfPaths, String password_extension) { + private void mockKeystoreCopierDestinationPaths(List<String> paths) throws TruststoresPathsProviderException { + when(copierPathsReader.get(KEYSTORE_DESTINATION_PATHS_ENV)).thenReturn(paths); + } + + private List<String> createListOfPathsWithExtension(int numberOfPaths, String passwordExtension) { + List<String> paths = new ArrayList<>(); + while (numberOfPaths-- > 0) { + paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + passwordExtension); + } + return paths; + } + + private List<String> createListOfKeystorePairsPathsWithExtension(String path, int numberOfPaths, + String certExtension) { List<String> paths = new ArrayList<>(); + String passExtension = certExtension.equalsIgnoreCase(".pem") ? certExtension : ".pass"; while (numberOfPaths-- > 0) { - paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension); + paths.add(path + numberOfPaths + certExtension); + paths.add(ANOTHER_KEYSTORE_PATH + numberOfPaths + passExtension); } return paths; } + } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactoryTest.java index efaef65f..5a7e9cd0 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactoryTest.java @@ -22,13 +22,18 @@ package org.onap.oom.truststoremerger.configuration.path; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; +import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; -class TruststoresPathsProviderFactoryTest { +class DelimitedPathsReaderFactoryTest { @Test - void shouldReturnCorrectObject() { - // given, when, then - assertThat(TruststoresPathsProviderFactory.create()).isInstanceOf(TruststoresPathsProvider.class); + void shouldReturnObjectOfDelimitedPathsReaderType() { + // given + DelimitedPathsReaderFactory readerFactory = new DelimitedPathsReaderFactory(new EnvProvider()); + //when, then + assertThat(readerFactory.createPasswordPathsReader()).isInstanceOf(DelimitedPathsReader.class); + assertThat(readerFactory.createCertificatePathsReader()).isInstanceOf(DelimitedPathsReader.class); + assertThat(readerFactory.createKeystoreCopierPathsReader()).isInstanceOf(DelimitedPathsReader.class); } } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderTest.java index d2e9cdef..408a7d6d 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderTest.java @@ -22,20 +22,21 @@ package org.onap.oom.truststoremerger.configuration.path; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.when; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths; import java.util.Optional; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; - +import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; @ExtendWith(MockitoExtension.class) -class TruststoresPathsProviderTest { +class DelimitedPathsReaderTest { private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem"; private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:"; @@ -45,36 +46,62 @@ class TruststoresPathsProviderTest { @Mock private EnvProvider envProvider; - private TruststoresPathsProvider truststoresPathsProvider; - - @BeforeEach - void setUp() { - truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator()); - } + private DelimitedPathsReader delimitedPathsReader; @Test void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); mockTruststoresEnv(VALID_TRUSTSTORES); - assertThat(truststoresPathsProvider.getTruststores()) + // when, then + assertThat(delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV)) .containsSequence("/opt/app/certificates/truststore.jks", "/opt/app/certificates/truststore.pem"); } @Test + void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); + mockTruststoresEnv(""); + + // when, then + assertThatExceptionOfType(TruststoresPathsProviderException.class) + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV)); + } + + @Test + void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); + mockTruststoresEnv(INVALID_TRUSTSTORES); + + // when, then + assertThatExceptionOfType(TruststoresPathsProviderException.class) + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV)); + } + + @Test void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS); - assertThat(truststoresPathsProvider.getTruststoresPasswords()) + // when, then + assertThat(delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)) .containsSequence("/opt/app/certificates/truststore.pass", ""); } @Test void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() throws TruststoresPathsProviderException { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE); - assertThat(truststoresPathsProvider.getTruststoresPasswords()).containsSequence( + // when, then + assertThat(delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).containsSequence( "/opt/app/certificates/truststore.pass", "", "/etc/truststore.pass" @@ -82,38 +109,36 @@ class TruststoresPathsProviderTest { } @Test - void shouldThrowExceptionWhenTruststoresEmpty() { - mockTruststoresEnv(""); - - assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststores); - } - - @Test - void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { - mockTruststoresEnv(INVALID_TRUSTSTORES); + void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); + mockTruststoresPasswordsEnv(""); + // when, then assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststores); + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)); } @Test void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS); + // when, then assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststoresPasswords); + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)); } private void mockTruststoresEnv(String truststores) { - mockEnv(truststores, TRUSTSTORES_PATHS_ENV); + mockEnv(TRUSTSTORES_PATHS_ENV, truststores); } private void mockTruststoresPasswordsEnv(String truststoresPasswords) { - mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_PATHS_ENV); + mockEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV, truststoresPasswords); } - private void mockEnv(String envValue, String envName) { - when(envProvider.getEnv(envName)).thenReturn(Optional.of(envValue)); + private void mockEnv(String envName, String truststores) { + when(envProvider.getEnv(envName)).thenReturn(Optional.of(truststores)); } } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java deleted file mode 100644 index b0487478..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.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.configuration.path; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -class PathValidatorTest { - - private final PathValidator validator = new PathValidator(); - - @ParameterizedTest() - @ValueSource(strings = {"/opt/app/truststore.pem", "/opt/app/truststore.jks", - "/opt/app/truststore.p12", "/truststore.pem"}) - void shouldAcceptValidTruststorePaths(String path) { - assertThat(validator.isTruststorePathValid(path)).isTrue(); - } - - @ParameterizedTest() - @ValueSource(strings = {"/opt/app/truststore.pass", "/opt/app/truststore.invalid", "/", - "truststore", "opt/app/truststore.p12", "/?.pem", "/.pem"}) - void shouldRejectInvalidTruststorePaths(String path) { - assertThat(validator.isTruststorePathValid(path)).isFalse(); - } - - @ParameterizedTest() - @ValueSource(strings = {"", "/opt/app/truststore.pass", "/truststore.pass"}) - void shouldAcceptValidTruststorePasswordPaths(String path) { - assertThat(validator.isTruststorePasswordPathValid(path)).isTrue(); - } - - @ParameterizedTest() - @ValueSource(strings = {"/opt/app/truststore.pem", "/opt/app/truststore.jks", - "/opt/app/truststore.p12", "/", "truststore", "opt/app/truststore.p12", "/?.pass", "/.pass"}) - void shouldRejectInvalidTruststorePasswordPaths(String path) { - assertThat(validator.isTruststorePasswordPathValid(path)).isFalse(); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctionsTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctionsTest.java new file mode 100644 index 00000000..3c3760f2 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctionsTest.java @@ -0,0 +1,134 @@ +/*============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.configuration.path.validation; + + +import static org.assertj.core.api.Assertions.assertThat; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +class ValidationFunctionsTest { + + @Test + void shouldValidateWithSuccessCorrectCertificatesPaths() { + // given + List<String> certPaths = Arrays.asList("/opt/app/certificates/truststore.p12"); + // when + boolean result = doesItContainValidCertificatesPaths().test(certPaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithFailureCertificatesPathsWithOneEmptyPath() { + // given + List<String> certPaths = Arrays.asList("/opt/app/certificates/truststore.p12", ""); + // when + boolean result = doesItContainValidCertificatesPaths().test(certPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithFailureCertificatesPathsWithOnePathWhichHasIncorrectExtension() { + // given + List<String> certPaths = Arrays.asList("/opt/app/certificates/truststore.txt", "/opt/cert.p12"); + // when + boolean result = doesItContainValidCertificatesPaths().test(certPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithSuccessCertificatesPasswordPaths() { + // given + List<String> passwordPaths = Arrays.asList("/opt/app/certificates/truststore.pass", ""); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithSuccessCertificatePasswordsPathsWhichContainsEmptyPathsInTheMiddle() { + // given + List<String> passwordPaths = Arrays.asList("/opt/app/certificates/truststore.pass", "", "/etc/truststore.pass"); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithFailureCertificatesPasswordsPathsWithIncorrectExtension() { + // given + List<String> passwordPaths = Arrays.asList("/pass.txt"); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithFailureCertificatesPasswordPathsWithMissingPrecedingSlash() { + // given + List<String> passwordPaths = Arrays.asList("jks.pass"); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithSuccessSourcePathsToCopyFiles() { + // given + List<String> sourcePaths = Arrays.asList("/opt/dcae/cacert/external/keystore.p12", + "/opt/dcae/cacert/external/keystore.pass"); + // when + boolean result = ValidationFunctions.doesItContainValidPathsToCopy().test(sourcePaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithSuccessDestinationPathsToCopyFiles() { + // given + List<String> sourcePaths = Arrays.asList("/opt/dcae/cacert/cert.p12","/opt/dcae/cacert/p12.pass"); + // when + boolean result = ValidationFunctions.doesItContainValidPathsToCopy().test(sourcePaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithFailureDestinationPathsWithIncorrectExtension() { + // given + List<String> sourcePaths = Arrays.asList("/opt/dcae/cacert/cert.txt","/opt/dcae/cacert/p12.other"); + // when + boolean result = ValidationFunctions.doesItContainValidPathsToCopy().test(sourcePaths); + // then + assertThat(result).isFalse(); + } + +} |