From 9518352d2bdc2db7b33c5ad305faeca77eba9656 Mon Sep 17 00:00:00 2001 From: kjaniak Date: Wed, 2 Sep 2020 13:34:19 +0200 Subject: Move path logic to configuration (merger) Issue-ID: DCAEGEN2-2253 Change-Id: I8ef6603335a837fbd2bcda9975d669f73f94db57 Signed-off-by: kjaniak --- .../oom/truststoremerger/TrustStoreMerger.java | 14 ++- .../certification/path/EnvProvider.java | 36 ------- .../certification/path/PathValidator.java | 38 ------- .../path/TruststoresPathsProvider.java | 72 ------------- .../path/TruststoresPathsProviderException.java | 30 ------ .../configuration/MergerConfiguration.java | 44 -------- .../MergerConfigurationException.java | 30 ------ .../configuration/MergerConfigurationFactory.java | 51 --------- .../configuration/MergerConfigurationProvider.java | 52 +++++++++ .../exception/MergerConfigurationException.java | 30 ++++++ .../TruststoresPathsProviderException.java | 30 ++++++ .../configuration/model/MergerConfiguration.java | 44 ++++++++ .../configuration/path/EnvProvider.java | 36 +++++++ .../configuration/path/PathValidator.java | 38 +++++++ .../path/TruststoresPathsProvider.java | 73 +++++++++++++ .../path/TruststoresPathsProviderFactory.java | 30 ++++++ .../certification/path/PathValidatorTest.java | 58 ---------- .../path/TruststoresPathsProviderTest.java | 108 ------------------- .../MergerConfigurationFactoryTest.java | 98 ----------------- .../MergerConfigurationProviderTest.java | 100 +++++++++++++++++ .../configuration/path/PathValidatorTest.java | 58 ++++++++++ .../path/TruststoresPathsProviderFactoryTest.java | 34 ++++++ .../path/TruststoresPathsProviderTest.java | 119 +++++++++++++++++++++ 23 files changed, 650 insertions(+), 573 deletions(-) delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/EnvProvider.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/PathValidator.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProvider.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfiguration.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationException.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactory.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/MergerConfiguration.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/EnvProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/PathValidatorTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderTest.java 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 c8cc84df..e2e0c62a 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java @@ -27,11 +27,10 @@ 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.certification.path.EnvProvider; -import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProvider; -import org.onap.oom.truststoremerger.configuration.MergerConfiguration; -import org.onap.oom.truststoremerger.configuration.MergerConfigurationFactory; -import org.onap.oom.truststoremerger.certification.path.PathValidator; +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.configuration.MergerConfigurationProvider; import java.util.List; import org.slf4j.Logger; @@ -75,9 +74,8 @@ class TrustStoreMerger { } private MergerConfiguration loadConfiguration() throws ExitableException { - TruststoresPathsProvider truststoresPathsProvider = new TruststoresPathsProvider(new EnvProvider(), - new PathValidator()); - MergerConfigurationFactory factory = new MergerConfigurationFactory(truststoresPathsProvider); + TruststoresPathsProvider truststoresPathsProvider = TruststoresPathsProviderFactory.create(); + MergerConfigurationProvider factory = new MergerConfigurationProvider(truststoresPathsProvider); return factory.createConfiguration(); } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/EnvProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/EnvProvider.java deleted file mode 100644 index a1998b86..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/EnvProvider.java +++ /dev/null @@ -1,36 +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.path; - -import java.util.Optional; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class EnvProvider { - - private static final Logger LOGGER = LoggerFactory.getLogger(EnvProvider.class); - - Optional 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/certification/path/PathValidator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/PathValidator.java deleted file mode 100644 index 256da490..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/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.certification.path; - -public 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"; - - public boolean isTruststorePathValid(String truststorePath) { - return isPathValid(truststorePath, TRUSTSTORE_PATH_REGEX); - } - - public 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/certification/path/TruststoresPathsProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProvider.java deleted file mode 100644 index 9d86a8b9..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProvider.java +++ /dev/null @@ -1,72 +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.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; - -public class TruststoresPathsProvider { - - private static final String DELIMITER = ":"; - private static final int NEGATIVE_SPLIT_LIMIT = -1; - - private final EnvProvider envProvider; - private final PathValidator pathValidator; - - public TruststoresPathsProvider(EnvProvider envProvider, PathValidator pathValidator) { - this.envProvider = envProvider; - this.pathValidator = pathValidator; - } - - public List getTruststores() throws TruststoresPathsProviderException { - return envProvider.getEnv(TRUSTSTORES_PATHS_ENV) - .filter(Predicate.not(String::isEmpty)) - .map(this::splitToList) - .filter(this::validateTruststores) - .orElseThrow(() -> new TruststoresPathsProviderException( - TRUSTSTORES_PATHS_ENV + " environment variable does not contain valid truststores paths")); - } - - public List 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 truststores) { - return truststores.stream() - .allMatch(pathValidator::isTruststorePathValid); - } - - private boolean validateTruststoresPasswords(List truststoresPasswords) { - return truststoresPasswords.stream() - .allMatch(pathValidator::isTruststorePasswordPathValid); - } - - private List splitToList(String stringToSplit) { - return Arrays.asList(stringToSplit.split(DELIMITER, NEGATIVE_SPLIT_LIMIT)); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderException.java deleted file mode 100644 index 1f69fe20..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderException.java +++ /dev/null @@ -1,30 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.path; - -import org.onap.oom.truststoremerger.api.ExitStatus; -import org.onap.oom.truststoremerger.api.ExitableException; - -public class TruststoresPathsProviderException extends ExitableException { - - TruststoresPathsProviderException(String message) { - super(message, ExitStatus.TRUSTSTORES_PATHS_PROVIDER_EXCEPTION); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfiguration.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfiguration.java deleted file mode 100644 index f3b7d935..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfiguration.java +++ /dev/null @@ -1,44 +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; - -import java.util.Collections; -import java.util.List; - -public class MergerConfiguration { - private final List truststoreFilePaths; - private final List truststoreFilePasswordPaths; - - public MergerConfiguration(List truststoreFilePaths, - List truststoreFilePasswordPaths) { - this.truststoreFilePaths = List.copyOf(truststoreFilePaths); - this.truststoreFilePasswordPaths = List.copyOf(truststoreFilePasswordPaths); - } - - public List getTruststoreFilePaths() { - return Collections.unmodifiableList(truststoreFilePaths); - } - - - public List getTruststoreFilePasswordPaths() { - return Collections.unmodifiableList(truststoreFilePasswordPaths); - } - -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationException.java deleted file mode 100644 index 54982f5f..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationException.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; - -import org.onap.oom.truststoremerger.api.ExitStatus; -import org.onap.oom.truststoremerger.api.ExitableException; - -public class MergerConfigurationException extends ExitableException { - - MergerConfigurationException(String message) { - super(message, ExitStatus.MERGER_CONFIGURATION_EXCEPTION); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactory.java deleted file mode 100644 index acd9a840..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactory.java +++ /dev/null @@ -1,51 +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; - -import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProvider; -import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProviderException; - -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.List; - -public class MergerConfigurationFactory { - - private final TruststoresPathsProvider pathsProvider; - - public MergerConfigurationFactory(TruststoresPathsProvider pathsProvider) { - this.pathsProvider = pathsProvider; - } - - public MergerConfiguration createConfiguration() - throws MergerConfigurationException, TruststoresPathsProviderException { - List truststores = pathsProvider.getTruststores(); - List truststoresPasswords = pathsProvider.getTruststoresPasswords(); - - if (truststores.size() != truststoresPasswords.size()) { - throw new MergerConfigurationException( - "Size of " + TRUSTSTORES_PATHS_ENV - + " does not match size of " + TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variables"); - } - - return new MergerConfiguration(truststores, truststoresPasswords); - } -} 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 new file mode 100644 index 00000000..5a40a64a --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProvider.java @@ -0,0 +1,52 @@ +/*============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; + +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.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; + +public class MergerConfigurationProvider { + + private final TruststoresPathsProvider pathsProvider; + + public MergerConfigurationProvider(TruststoresPathsProvider pathsProvider) { + this.pathsProvider = pathsProvider; + } + + public MergerConfiguration createConfiguration() + throws MergerConfigurationException, TruststoresPathsProviderException { + List truststores = pathsProvider.getTruststores(); + List truststoresPasswords = pathsProvider.getTruststoresPasswords(); + + if (truststores.size() != truststoresPasswords.size()) { + throw new MergerConfigurationException( + "Size of " + TRUSTSTORES_PATHS_ENV + + " does not match size of " + TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variables"); + } + + return new MergerConfiguration(truststores, truststoresPasswords); + } +} 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 new file mode 100644 index 00000000..90a75d91 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java @@ -0,0 +1,30 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.configuration.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class MergerConfigurationException extends ExitableException { + + public MergerConfigurationException(String message) { + super(message, 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 new file mode 100644 index 00000000..dda53e32 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java @@ -0,0 +1,30 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.configuration.exception; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +public class TruststoresPathsProviderException extends ExitableException { + + public TruststoresPathsProviderException(String message) { + super(message, ExitStatus.TRUSTSTORES_PATHS_PROVIDER_EXCEPTION); + } +} 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 new file mode 100644 index 00000000..3b807c39 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/MergerConfiguration.java @@ -0,0 +1,44 @@ +/*============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.model; + +import java.util.Collections; +import java.util.List; + +public final class MergerConfiguration { + private final List truststoreFilePaths; + private final List truststoreFilePasswordPaths; + + public MergerConfiguration(List truststoreFilePaths, + List truststoreFilePasswordPaths) { + this.truststoreFilePaths = List.copyOf(truststoreFilePaths); + this.truststoreFilePasswordPaths = List.copyOf(truststoreFilePasswordPaths); + } + + public List getTruststoreFilePaths() { + return Collections.unmodifiableList(truststoreFilePaths); + } + + + public List getTruststoreFilePasswordPaths() { + return Collections.unmodifiableList(truststoreFilePasswordPaths); + } + +} 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/EnvProvider.java new file mode 100644 index 00000000..c6fbf515 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/EnvProvider.java @@ -0,0 +1,36 @@ +/*============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 java.util.Optional; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class EnvProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(EnvProvider.class); + + Optional 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/PathValidator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java new file mode 100644 index 00000000..4240228d --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java @@ -0,0 +1,38 @@ +/*============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/TruststoresPathsProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProvider.java new file mode 100644 index 00000000..9d7e30a4 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProvider.java @@ -0,0 +1,73 @@ +/*============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.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; + +public class TruststoresPathsProvider { + + private static final String DELIMITER = ":"; + private static final int NEGATIVE_SPLIT_LIMIT = -1; + + private final EnvProvider envProvider; + private final PathValidator pathValidator; + + TruststoresPathsProvider(EnvProvider envProvider, PathValidator pathValidator) { + this.envProvider = envProvider; + this.pathValidator = pathValidator; + } + + public List getTruststores() throws TruststoresPathsProviderException { + return envProvider.getEnv(TRUSTSTORES_PATHS_ENV) + .filter(Predicate.not(String::isEmpty)) + .map(this::splitToList) + .filter(this::validateTruststores) + .orElseThrow(() -> new TruststoresPathsProviderException( + TRUSTSTORES_PATHS_ENV + " environment variable does not contain valid truststores paths")); + } + + public List 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 truststores) { + return truststores.stream() + .allMatch(pathValidator::isTruststorePathValid); + } + + private boolean validateTruststoresPasswords(List truststoresPasswords) { + return truststoresPasswords.stream() + .allMatch(pathValidator::isTruststorePasswordPathValid); + } + + private List splitToList(String stringToSplit) { + return Arrays.asList(stringToSplit.split(DELIMITER, NEGATIVE_SPLIT_LIMIT)); + } +} 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 new file mode 100644 index 00000000..7a999108 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java @@ -0,0 +1,30 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.configuration.path; + +public class TruststoresPathsProviderFactory { + + private TruststoresPathsProviderFactory() { + } + + public static TruststoresPathsProvider create() { + return new TruststoresPathsProvider(new EnvProvider(), new PathValidator()); + } +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/PathValidatorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/PathValidatorTest.java deleted file mode 100644 index a11bb232..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/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.certification.path; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import static org.assertj.core.api.Assertions.assertThat; - -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/certification/path/TruststoresPathsProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderTest.java deleted file mode 100644 index 38422d5c..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderTest.java +++ /dev/null @@ -1,108 +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.path; - -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.Optional; - -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_PATHS_ENV; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; - - -@ExtendWith(MockitoExtension.class) -class TruststoresPathsProviderTest { - - 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:"; - private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid"; - private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass"; - - @Mock - private EnvProvider envProvider; - private TruststoresPathsProvider truststoresPathsProvider; - - @BeforeEach - void setUp() { - truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator()); - } - - @Test - void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException { - mockTruststoresEnv(VALID_TRUSTSTORES); - - assertThat(truststoresPathsProvider.getTruststores()) - .contains("/opt/app/certificates/truststore.jks", - "/opt/app/certificates/truststore.pem"); - } - - @Test - void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException { - mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS); - - assertThat(truststoresPathsProvider.getTruststoresPasswords()) - .contains("/opt/app/certificates/truststore.pass", - ""); - } - - @Test - void shouldThrowExceptionWhenTruststoresEmpty() { - mockTruststoresEnv(""); - - assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststores); - } - - @Test - void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { - mockTruststoresEnv(INVALID_TRUSTSTORES); - - assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststores); - } - - @Test - void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() { - mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS); - - assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststoresPasswords); - } - - private void mockTruststoresEnv(String truststores) { - mockEnv(truststores, TRUSTSTORES_PATHS_ENV); - } - - private void mockTruststoresPasswordsEnv(String truststoresPasswords) { - mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_PATHS_ENV); - } - - private void mockEnv(String envValue, String envName) { - when(envProvider.getEnv(envName)) - .thenReturn(Optional.of(envValue)); - } -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java deleted file mode 100644 index 43b7b9e1..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java +++ /dev/null @@ -1,98 +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; - -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.certification.path.TruststoresPathsProvider; -import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProviderException; - -import java.util.ArrayList; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class MergerConfigurationFactoryTest { - - private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_"; - private static final String TRUSTSTORE_EXTENSION = ".jks"; - private static final String PASSWORD_EXTENSION = ".pass"; - - @Mock - private TruststoresPathsProvider pathsProvider; - private MergerConfigurationFactory factory; - - @BeforeEach - void setUp() { - factory = new MergerConfigurationFactory(pathsProvider); - } - - @Test - void shouldReturnConfigurationWithCorrectPaths() throws TruststoresPathsProviderException, MergerConfigurationException { - int numberOfPaths = 5; - List truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION); - List truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); - - MergerConfiguration configuration = factory.createConfiguration(); - - assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths); - assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths); - } - - @Test - void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException { - int numberOfTruststores = 5; - int numberOfTruststoresPasswords = 4; - List truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION); - List truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); - - assertThatExceptionOfType(MergerConfigurationException.class) - .isThrownBy(factory::createConfiguration); - } - - private void mockPaths(List truststores, List truststoresPasswords) throws TruststoresPathsProviderException { - mockTruststores(truststores); - mockTruststoresPasswords(truststoresPasswords); - } - - private void mockTruststores(List truststores) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststores()).thenReturn(truststores); - } - - private void mockTruststoresPasswords(List truststoresPasswords) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords); - } - - private List createListOfPathsWithExtension(int numberOfPaths, String password_extension) { - List paths = new ArrayList<>(); - while (numberOfPaths-- > 0) { - paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension); - } - return paths; - } -} 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 new file mode 100644 index 00000000..854cde79 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java @@ -0,0 +1,100 @@ +/*============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; + +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; + +@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"; + + @Mock + private TruststoresPathsProvider pathsProvider; + private MergerConfigurationProvider factory; + + @BeforeEach + void setUp() { + factory = new MergerConfigurationProvider(pathsProvider); + } + + @Test + void shouldReturnConfigurationWithCorrectPaths() throws TruststoresPathsProviderException, MergerConfigurationException { + int numberOfPaths = 5; + List truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION); + List truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION); + mockPaths(truststoresPaths, truststorePasswordPaths); + + MergerConfiguration configuration = factory.createConfiguration(); + + assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths); + assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths); + } + + @Test + void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException { + int numberOfTruststores = 5; + int numberOfTruststoresPasswords = 4; + List truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION); + List truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION); + mockPaths(truststoresPaths, truststorePasswordPaths); + + assertThatExceptionOfType(MergerConfigurationException.class) + .isThrownBy(factory::createConfiguration); + } + + private void mockPaths(List truststores, List truststoresPasswords) throws TruststoresPathsProviderException { + mockTruststores(truststores); + mockTruststoresPasswords(truststoresPasswords); + } + + private void mockTruststores(List truststores) throws TruststoresPathsProviderException { + when(pathsProvider.getTruststores()).thenReturn(truststores); + } + + private void mockTruststoresPasswords(List truststoresPasswords) throws TruststoresPathsProviderException { + when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords); + } + + private List createListOfPathsWithExtension(int numberOfPaths, String password_extension) { + List paths = new ArrayList<>(); + while (numberOfPaths-- > 0) { + paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension); + } + return paths; + } +} 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 new file mode 100644 index 00000000..b0487478 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java @@ -0,0 +1,58 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.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/TruststoresPathsProviderFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java new file mode 100644 index 00000000..efaef65f --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java @@ -0,0 +1,34 @@ +/*============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.api.Test; + +class TruststoresPathsProviderFactoryTest { + + @Test + void shouldReturnCorrectObject() { + // given, when, then + assertThat(TruststoresPathsProviderFactory.create()).isInstanceOf(TruststoresPathsProvider.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/TruststoresPathsProviderTest.java new file mode 100644 index 00000000..d2e9cdef --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderTest.java @@ -0,0 +1,119 @@ +/*============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 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 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; + + +@ExtendWith(MockitoExtension.class) +class TruststoresPathsProviderTest { + + 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:"; + private static final String VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE = "/opt/app/certificates/truststore.pass::/etc/truststore.pass"; + private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid"; + private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass"; + + @Mock + private EnvProvider envProvider; + private TruststoresPathsProvider truststoresPathsProvider; + + @BeforeEach + void setUp() { + truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator()); + } + + @Test + void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException { + mockTruststoresEnv(VALID_TRUSTSTORES); + + assertThat(truststoresPathsProvider.getTruststores()) + .containsSequence("/opt/app/certificates/truststore.jks", + "/opt/app/certificates/truststore.pem"); + } + + @Test + void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException { + mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS); + + assertThat(truststoresPathsProvider.getTruststoresPasswords()) + .containsSequence("/opt/app/certificates/truststore.pass", ""); + } + + @Test + void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() + throws TruststoresPathsProviderException { + mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE); + + assertThat(truststoresPathsProvider.getTruststoresPasswords()).containsSequence( + "/opt/app/certificates/truststore.pass", + "", + "/etc/truststore.pass" + ); + } + + @Test + void shouldThrowExceptionWhenTruststoresEmpty() { + mockTruststoresEnv(""); + + assertThatExceptionOfType(TruststoresPathsProviderException.class) + .isThrownBy(truststoresPathsProvider::getTruststores); + } + + @Test + void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { + mockTruststoresEnv(INVALID_TRUSTSTORES); + + assertThatExceptionOfType(TruststoresPathsProviderException.class) + .isThrownBy(truststoresPathsProvider::getTruststores); + } + + @Test + void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() { + mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS); + + assertThatExceptionOfType(TruststoresPathsProviderException.class) + .isThrownBy(truststoresPathsProvider::getTruststoresPasswords); + } + + private void mockTruststoresEnv(String truststores) { + mockEnv(truststores, TRUSTSTORES_PATHS_ENV); + } + + private void mockTruststoresPasswordsEnv(String truststoresPasswords) { + mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_PATHS_ENV); + } + + private void mockEnv(String envValue, String envName) { + when(envProvider.getEnv(envName)).thenReturn(Optional.of(envValue)); + } +} -- cgit 1.2.3-korg