From eddd3bd7b3d7fbb7cc052eebf3059589f1715233 Mon Sep 17 00:00:00 2001 From: Remigiusz Janeczek Date: Mon, 10 Aug 2020 11:39:43 +0200 Subject: Add TruststoreFile provider Move certification.file classes to certification.path package Issue-ID: DCAEGEN2-2253 Signed-off-by: Remigiusz Janeczek Change-Id: I3098ac443b940031506732216f2bedffa3143adb --- .../oom/truststoremerger/TrustStoreMerger.java | 24 ++++- .../onap/oom/truststoremerger/api/ExitStatus.java | 4 +- .../certification/file/EnvProvider.java | 29 ------ .../certification/file/JksTruststore.java | 37 +++++++ .../certification/file/P12Truststore.java | 38 +++++++ .../certification/file/PathValidator.java | 38 ------- .../certification/file/PemTruststore.java | 37 +++++++ .../certification/file/TruststoreFile.java | 38 +++++++ .../file/TruststoreFileWithPassword.java | 35 +++++++ .../file/TruststoresPathsProvider.java | 72 ------------- .../file/TruststoresPathsProviderException.java | 30 ------ .../certification/file/provider/FileManager.java | 39 +++++++ .../file/provider/PasswordReader.java | 36 +++++++ .../file/provider/PasswordReaderException.java | 29 ++++++ .../file/provider/TruststoreFileFactory.java | 84 +++++++++++++++ .../provider/TruststoreFileFactoryException.java | 30 ++++++ .../file/provider/TruststoreFilesListProvider.java | 50 +++++++++ .../certification/path/EnvProvider.java | 29 ++++++ .../certification/path/PathValidator.java | 38 +++++++ .../path/TruststoresPathsProvider.java | 72 +++++++++++++ .../path/TruststoresPathsProviderException.java | 30 ++++++ .../configuration/MergerConfigurationFactory.java | 4 +- .../certification/file/PathValidatorTest.java | 58 ----------- .../file/TruststoresPathsProviderTest.java | 108 ------------------- .../file/provider/FileManagerTest.java | 46 +++++++++ .../file/provider/PasswordReaderTest.java | 44 ++++++++ .../file/provider/TruststoreFileFactoryTest.java | 114 +++++++++++++++++++++ .../provider/TruststoreFilesListProviderTest.java | 90 ++++++++++++++++ .../certification/path/PathValidatorTest.java | 58 +++++++++++ .../path/TruststoresPathsProviderTest.java | 108 +++++++++++++++++++ .../MergerConfigurationFactoryTest.java | 4 +- .../src/test/resources/truststore-jks.jks | Bin 0 -> 1285 bytes .../src/test/resources/truststore-jks.pass | 1 + .../src/test/resources/truststore-p12.p12 | Bin 0 -> 1530 bytes .../src/test/resources/truststore-p12.pass | 1 + trustStoreMerger/src/test/resources/truststore.pem | 28 +++++ 36 files changed, 1140 insertions(+), 343 deletions(-) delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/EnvProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/JksTruststore.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/P12Truststore.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/PathValidator.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/PemTruststore.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFile.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileWithPassword.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProvider.java delete mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProviderException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactory.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryException.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/EnvProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/PathValidator.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProvider.java create mode 100644 trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderException.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/PathValidatorTest.java delete mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProviderTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/PathValidatorTest.java create mode 100644 trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderTest.java create mode 100644 trustStoreMerger/src/test/resources/truststore-jks.jks create mode 100644 trustStoreMerger/src/test/resources/truststore-jks.pass create mode 100644 trustStoreMerger/src/test/resources/truststore-p12.p12 create mode 100644 trustStoreMerger/src/test/resources/truststore-p12.pass create mode 100644 trustStoreMerger/src/test/resources/truststore.pem 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 3a3b9b6e..98c67ba8 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java @@ -21,11 +21,18 @@ package org.onap.oom.truststoremerger; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.EnvProvider; -import org.onap.oom.truststoremerger.certification.file.TruststoresPathsProvider; +import org.onap.oom.truststoremerger.certification.file.TruststoreFile; +import org.onap.oom.truststoremerger.certification.file.provider.FileManager; +import org.onap.oom.truststoremerger.certification.file.provider.PasswordReader; +import org.onap.oom.truststoremerger.certification.file.provider.TruststoreFileFactory; +import org.onap.oom.truststoremerger.certification.file.provider.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.file.PathValidator; +import org.onap.oom.truststoremerger.certification.path.PathValidator; + +import java.util.List; class TrustStoreMerger { @@ -46,6 +53,7 @@ class TrustStoreMerger { private void mergeTruststores() throws ExitableException { MergerConfiguration configuration = loadConfiguration(); + List truststoreFilesList = getTruststoreFilesList(configuration); } private MergerConfiguration loadConfiguration() throws ExitableException { @@ -53,4 +61,14 @@ class TrustStoreMerger { MergerConfigurationFactory factory = new MergerConfigurationFactory(truststoresPathsProvider); return factory.createConfiguration(); } + + private List getTruststoreFilesList(MergerConfiguration configuration) throws ExitableException { + TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); + TruststoreFilesListProvider truststoreFilesListProvider = new TruststoreFilesListProvider(truststoreFileFactory); + return truststoreFilesListProvider + .getTruststoreFilesList( + configuration.getTruststoreFilePaths(), + configuration.getTruststoreFilePasswordPaths() + ); + } } 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 ae145f7e..d0c3b2f0 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 @@ -23,7 +23,9 @@ public enum ExitStatus { SUCCESS(0, "Success"), TRUSTSTORES_PATHS_PROVIDER_EXCEPTION(1, "Invalid paths in environment variables"), - MERGER_CONFIGURATION_EXCEPTION(2, "Invalid merger configuration"); + MERGER_CONFIGURATION_EXCEPTION(2, "Invalid merger configuration"), + TRUSTSTORE_FILE_FACTORY_EXCEPTION(3, "Invalid truststore file-password pair"), + PASSWORD_READER_EXCEPTION(4, "Cannot read password from file"); private final int value; private final String message; diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/EnvProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/EnvProvider.java deleted file mode 100644 index f64edc89..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/EnvProvider.java +++ /dev/null @@ -1,29 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.file; - -import java.util.Optional; - -public class EnvProvider { - - Optional getEnv(String name) { - return Optional.ofNullable(System.getenv(name)); - } -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/JksTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/JksTruststore.java new file mode 100644 index 00000000..b977daee --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/JksTruststore.java @@ -0,0 +1,37 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file; + +import java.io.File; +import java.security.cert.Certificate; +import java.util.Collections; +import java.util.List; + +public class JksTruststore extends TruststoreFileWithPassword { + + public JksTruststore(File truststoreFile, String password) { + super(truststoreFile, password); + } + + @Override + public List getCertificates() { + return Collections.emptyList(); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/P12Truststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/P12Truststore.java new file mode 100644 index 00000000..8527cce5 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/P12Truststore.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.certification.file; + +import java.io.File; +import java.security.cert.Certificate; +import java.util.Collections; +import java.util.List; + +public class P12Truststore extends TruststoreFileWithPassword { + + public P12Truststore(File truststoreFile, String password) { + super(truststoreFile, password); + } + + @Override + public List getCertificates() { + return Collections.emptyList(); + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/PathValidator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/PathValidator.java deleted file mode 100644 index 05b80f14..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/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.file; - -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/file/PemTruststore.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/PemTruststore.java new file mode 100644 index 00000000..ca2ac85d --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/PemTruststore.java @@ -0,0 +1,37 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file; + +import java.io.File; +import java.security.cert.Certificate; +import java.util.Collections; +import java.util.List; + +public class PemTruststore extends TruststoreFile { + + public PemTruststore(File truststoreFile) { + super(truststoreFile); + } + + @Override + public List getCertificates() { + return Collections.emptyList(); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFile.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFile.java new file mode 100644 index 00000000..88b1b5a8 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFile.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.certification.file; + +import java.io.File; +import java.security.cert.Certificate; +import java.util.List; + +public abstract class TruststoreFile { + private File truststoreFile; + + TruststoreFile(File truststoreFile) { + this.truststoreFile = truststoreFile; + } + + public abstract List getCertificates(); + + public File getTruststoreFile() { + return truststoreFile; + }; +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileWithPassword.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileWithPassword.java new file mode 100644 index 00000000..484f2d4f --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoreFileWithPassword.java @@ -0,0 +1,35 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file; + +import java.io.File; + +public abstract class TruststoreFileWithPassword extends TruststoreFile { + private String password; + + TruststoreFileWithPassword(File truststoreFile, String password) { + super(truststoreFile); + this.password = password; + } + + public String getPassword(){ + return password; + }; +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProvider.java deleted file mode 100644 index e23a1add..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/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.file; - -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_ENV; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_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 EnvProvider envProvider; - private PathValidator pathValidator; - - public TruststoresPathsProvider(EnvProvider envProvider, PathValidator pathValidator) { - this.envProvider = envProvider; - this.pathValidator = pathValidator; - } - - public List getTruststores() throws TruststoresPathsProviderException { - return envProvider.getEnv(TRUSTSTORES_ENV) - .filter(Predicate.not(String::isEmpty)) - .map(this::splitToList) - .filter(this::validateTruststores) - .orElseThrow(() -> new TruststoresPathsProviderException( - TRUSTSTORES_ENV + " environment variable does not contain valid truststores paths")); - } - - public List getTruststoresPasswords() throws TruststoresPathsProviderException { - return envProvider.getEnv(TRUSTSTORES_PASSWORDS_ENV) - .map(this::splitToList) - .filter(this::validateTruststoresPasswords) - .orElseThrow(() -> new TruststoresPathsProviderException( - TRUSTSTORES_PASSWORDS_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/file/TruststoresPathsProviderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProviderException.java deleted file mode 100644 index 5f491c36..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/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.file; - -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/certification/file/provider/FileManager.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java new file mode 100644 index 00000000..901c13ab --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/FileManager.java @@ -0,0 +1,39 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file.provider; + +import java.io.File; + +public class FileManager { + private static final int NOT_FOUND_INDEX=-1; + + String getExtension(File file) { + int extStartIndex = file.getName().lastIndexOf("."); + if (extStartIndex == NOT_FOUND_INDEX) { + return ""; + } + return file.getName().substring(extStartIndex); + } + + boolean checkIfFileExists(File file){ + return file.exists(); + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.java new file mode 100644 index 00000000..db42f3bd --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReader.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.certification.file.provider; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class PasswordReader { + private static final String COULD_NOT_READ_PASSWORD_FROM_FILE_MSG_TEMPLATE = "Could not read password from file: %s"; + + String readPassword(File file) throws PasswordReaderException { + try { + return Files.readString(file.toPath()); + } catch (IOException e) { + throw new PasswordReaderException(String.format(COULD_NOT_READ_PASSWORD_FROM_FILE_MSG_TEMPLATE, file)); + } + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderException.java new file mode 100644 index 00000000..2928f0c5 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderException.java @@ -0,0 +1,29 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file.provider; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +class PasswordReaderException extends ExitableException { + PasswordReaderException(String message) { + super(message, ExitStatus.PASSWORD_READER_EXCEPTION); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactory.java new file mode 100644 index 00000000..e63e7c33 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactory.java @@ -0,0 +1,84 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file.provider; + +import org.onap.oom.truststoremerger.certification.file.JksTruststore; +import org.onap.oom.truststoremerger.certification.file.P12Truststore; +import org.onap.oom.truststoremerger.certification.file.PemTruststore; +import org.onap.oom.truststoremerger.certification.file.TruststoreFile; + +import java.io.File; + +public class TruststoreFileFactory { + + private static final String JKS_EXTENSION = ".jks"; + private static final String P12_EXTENSION = ".p12"; + private static final String PEM_EXTENSION = ".pem"; + private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist"; + private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s"; + + private final FileManager fileManager; + private final PasswordReader passwordReader; + + public TruststoreFileFactory(FileManager fileManager, PasswordReader passwordReader) { + this.fileManager = fileManager; + this.passwordReader = passwordReader; + } + + TruststoreFile create(String truststoreFilePath, String truststorePasswordPath) + throws TruststoreFileFactoryException, PasswordReaderException { + File truststoreFile = new File(truststoreFilePath); + if (!fileManager.checkIfFileExists(truststoreFile)) { + throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile)); + } + return createTypedTruststore(truststoreFile, truststorePasswordPath); + } + + private TruststoreFile createTypedTruststore(File truststoreFile, String truststorePasswordPath) + throws PasswordReaderException, TruststoreFileFactoryException { + String extension = fileManager.getExtension(truststoreFile); + switch (extension) { + case JKS_EXTENSION: + return createJksTruststore(truststoreFile, truststorePasswordPath); + case P12_EXTENSION: + return createP12Truststore(truststoreFile, truststorePasswordPath); + case PEM_EXTENSION: + return createPemTruststore(truststoreFile); + default: + throw new TruststoreFileFactoryException(String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension)); + } + } + + private JksTruststore createJksTruststore(File truststoreFile, String truststorePasswordPath) + throws PasswordReaderException { + String password = passwordReader.readPassword(new File(truststorePasswordPath)); + return new JksTruststore(truststoreFile, password); + } + + private P12Truststore createP12Truststore(File truststoreFile, String truststorePasswordPath) + throws PasswordReaderException { + String password = passwordReader.readPassword(new File(truststorePasswordPath)); + return new P12Truststore(truststoreFile, password); + } + + private PemTruststore createPemTruststore(File truststoreFile) { + return new PemTruststore(truststoreFile); + } +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryException.java new file mode 100644 index 00000000..43342c83 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryException.java @@ -0,0 +1,30 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file.provider; + +import org.onap.oom.truststoremerger.api.ExitStatus; +import org.onap.oom.truststoremerger.api.ExitableException; + +class TruststoreFileFactoryException extends ExitableException { + TruststoreFileFactoryException(String message) { + super(message, ExitStatus.TRUSTSTORE_FILE_FACTORY_EXCEPTION); + } + +} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProvider.java new file mode 100644 index 00000000..2f5356d5 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProvider.java @@ -0,0 +1,50 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.file.provider; + +import org.onap.oom.truststoremerger.certification.file.TruststoreFile; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class TruststoreFilesListProvider { + + private final TruststoreFileFactory truststoreFileFactory; + + public TruststoreFilesListProvider(TruststoreFileFactory truststoreFileFactory) { + this.truststoreFileFactory = truststoreFileFactory; + } + + public List getTruststoreFilesList(List truststoreFilePaths, + List truststoreFilePasswordPaths) + throws PasswordReaderException, TruststoreFileFactoryException { + List truststoreFilesList = new ArrayList<>(); + for (int i = 0; i < truststoreFilePaths.size(); i++) { + String truststorePath = truststoreFilePaths.get(i); + String passwordPath = truststoreFilePasswordPaths.get(i); + + TruststoreFile truststoreFile = truststoreFileFactory.create(truststorePath, passwordPath); + truststoreFilesList.add(truststoreFile); + } + + return truststoreFilesList; + } +} 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 new file mode 100644 index 00000000..4bb763da --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/EnvProvider.java @@ -0,0 +1,29 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.certification.path; + +import java.util.Optional; + +public class EnvProvider { + + Optional getEnv(String name) { + 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 new file mode 100644 index 00000000..256da490 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/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.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 new file mode 100644 index 00000000..f8e85d49 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProvider.java @@ -0,0 +1,72 @@ +/*============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_ENV; +import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_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 EnvProvider envProvider; + private PathValidator pathValidator; + + public TruststoresPathsProvider(EnvProvider envProvider, PathValidator pathValidator) { + this.envProvider = envProvider; + this.pathValidator = pathValidator; + } + + public List getTruststores() throws TruststoresPathsProviderException { + return envProvider.getEnv(TRUSTSTORES_ENV) + .filter(Predicate.not(String::isEmpty)) + .map(this::splitToList) + .filter(this::validateTruststores) + .orElseThrow(() -> new TruststoresPathsProviderException( + TRUSTSTORES_ENV + " environment variable does not contain valid truststores paths")); + } + + public List getTruststoresPasswords() throws TruststoresPathsProviderException { + return envProvider.getEnv(TRUSTSTORES_PASSWORDS_ENV) + .map(this::splitToList) + .filter(this::validateTruststoresPasswords) + .orElseThrow(() -> new TruststoresPathsProviderException( + TRUSTSTORES_PASSWORDS_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 new file mode 100644 index 00000000..1f69fe20 --- /dev/null +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/path/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.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/MergerConfigurationFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactory.java index fa0b8cde..7a2fdc10 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactory.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactory.java @@ -19,8 +19,8 @@ package org.onap.oom.truststoremerger.configuration; -import org.onap.oom.truststoremerger.certification.file.TruststoresPathsProvider; -import org.onap.oom.truststoremerger.certification.file.TruststoresPathsProviderException; +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_ENV; import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_ENV; diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/PathValidatorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/PathValidatorTest.java deleted file mode 100644 index 1c455764..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/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.file; - -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/file/TruststoresPathsProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProviderTest.java deleted file mode 100644 index 6b017709..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/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.file; - -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_ENV; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_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_ENV); - } - - private void mockTruststoresPasswordsEnv(String truststoresPasswords) { - mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_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/certification/file/provider/FileManagerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java new file mode 100644 index 00000000..d348dd7e --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.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.certification.file.provider; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.io.File; + +import static org.assertj.core.api.Assertions.assertThat; + +class FileManagerTest { + + private FileManager fileManager = new FileManager(); + + @ParameterizedTest + @CsvSource(value = { + "opt/app/truststore.jks:.jks", + "opt/app/truststore.p12:.p12", + "opt/app/truststore.pem:.pem", + "opt/app/truststore:''", + }, delimiter = ':') + void shouldReturnCorrectExtension(String filePath, String expectedExtension){ + String extension = fileManager.getExtension(new File(filePath)); + assertThat(extension).isEqualTo(expectedExtension); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java new file mode 100644 index 00000000..712935ac --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.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.certification.file.provider; + +import org.junit.jupiter.api.Test; + +import java.io.File; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +class PasswordReaderTest { + + @Test + void shouldReturnCorrectPasswordFromFile() throws PasswordReaderException { + PasswordReader passwordReader = new PasswordReader(); + String fileData = passwordReader.readPassword(new File("src/test/resources/truststore-jks.pass")); + assertThat(fileData).isEqualTo("EOyuFbuYDyq_EhpboM72RHua"); + } + + @Test + void shouldThrowExceptionForNonExistingFile() { + PasswordReader passwordReader = new PasswordReader(); + assertThatExceptionOfType(PasswordReaderException.class) + .isThrownBy(() -> passwordReader.readPassword(new File("src/test/resources/non-esisting-file.pass"))); + } +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryTest.java new file mode 100644 index 00000000..f00b2bc4 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFileFactoryTest.java @@ -0,0 +1,114 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + + +package org.onap.oom.truststoremerger.certification.file.provider; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.oom.truststoremerger.certification.file.JksTruststore; +import org.onap.oom.truststoremerger.certification.file.P12Truststore; +import org.onap.oom.truststoremerger.certification.file.PemTruststore; +import org.onap.oom.truststoremerger.certification.file.TruststoreFile; + +import java.io.File; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +@ExtendWith(MockitoExtension.class) +class TruststoreFileFactoryTest { + + private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks"; + private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass"; + private static final String TRUSTSTORE_JKS_PASS = "EOyuFbuYDyq_EhpboM72RHua"; + private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12"; + private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass"; + private static final String TRUSTSTORE_P12_PASS = "88y9v5D8H3SG6bZWRVHDfOAo"; + private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem"; + private static final String EMPTY_PASS_PATH = ""; + private static final String TRUSTSTORE_UNKNOWN_EXTENSION_PATH = "src/test/resources/truststore-jks.unknown"; + private static final String NON_EXISTING_TRUSTSTORE_PATH = "src/test/resources/non-existing-truststore.jks"; + + private TruststoreFileFactory truststoreFileFactory; + + @BeforeEach + void setUp() { + truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); + } + + @Test + void shouldReturnCorrectJksTruststoreForJksFile() throws TruststoreFileFactoryException, PasswordReaderException { + TruststoreFile truststore = truststoreFileFactory + .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH); + assertThat(truststore).isInstanceOf(JksTruststore.class); + JksTruststore jksTruststore = (JksTruststore) truststore; + assertThat(jksTruststore.getPassword()).isEqualTo(TRUSTSTORE_JKS_PASS); + assertThat(jksTruststore.getTruststoreFile()).isEqualTo(new File(TRUSTSTORE_JKS_PATH)); + } + + @Test + void shouldReturnCorrectP12TruststoreForP12File() throws TruststoreFileFactoryException, PasswordReaderException { + TruststoreFile truststore = truststoreFileFactory + .create(TRUSTSTORE_P12_PATH, + TRUSTSTORE_P12_PASS_PATH); + assertThat(truststore).isInstanceOf(P12Truststore.class); + P12Truststore jksTruststore = (P12Truststore) truststore; + assertThat(jksTruststore.getPassword()).isEqualTo(TRUSTSTORE_P12_PASS); + } + + @Test + void shouldReturnCorrectPemTruststoreForPemFile() throws TruststoreFileFactoryException, PasswordReaderException { + TruststoreFile truststore = truststoreFileFactory + .create(TRUSTSTORE_PEM_PATH, + EMPTY_PASS_PATH); + assertThat(truststore).isInstanceOf(PemTruststore.class); + } + + @Test + void shouldThrowExceptionForInvalidP12PassPath() { + assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( + () -> truststoreFileFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH) + ); + } + + @Test + void shouldThrowExceptionForInvalidJksPassPath() { + assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( + () -> truststoreFileFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH) + ); + } + + @Test + void shouldThrowExceptionForUnknownTruststoreExtension() { + assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( + () -> truststoreFileFactory.create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH) + ); + } + + @Test + void shouldThrowExceptionForNonExistingTruststoreFile() { + assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( + () -> truststoreFileFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH) + ); + } + +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java new file mode 100644 index 00000000..034e1b32 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java @@ -0,0 +1,90 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + + +package org.onap.oom.truststoremerger.certification.file.provider; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.oom.truststoremerger.certification.file.JksTruststore; +import org.onap.oom.truststoremerger.certification.file.P12Truststore; +import org.onap.oom.truststoremerger.certification.file.PemTruststore; +import org.onap.oom.truststoremerger.certification.file.TruststoreFile; +import org.onap.oom.truststoremerger.certification.file.TruststoreFileWithPassword; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class TruststoreFilesListProviderTest { + + private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks"; + private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass"; + private static final String TRUSTSTORE_JKS_PASS = "EOyuFbuYDyq_EhpboM72RHua"; + private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12"; + private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass"; + private static final String TRUSTSTORE_P12_PASS = "88y9v5D8H3SG6bZWRVHDfOAo"; + private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem"; + private static final String EMPTY_PASS_PATH = ""; + + private TruststoreFilesListProvider truststoreFilesListProvider; + + @BeforeEach + void setUp() { + TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); + truststoreFilesListProvider = new TruststoreFilesListProvider(truststoreFileFactory); + } + + @Test + void shouldReturnTruststoreFilesList() throws PasswordReaderException, TruststoreFileFactoryException { + List truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH); + List truststorePasswordPaths = Arrays.asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH); + List truststoreFilesList = truststoreFilesListProvider.getTruststoreFilesList(truststorePaths, truststorePasswordPaths); + assertThat(truststoreFilesList.size()).isEqualTo(3); + assertCorrectJksTruststore(truststoreFilesList.get(0), TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS); + assertCorrectP12Truststore(truststoreFilesList.get(1), TRUSTSTORE_P12_PATH, TRUSTSTORE_P12_PASS); + assertCorrectPemTruststore(truststoreFilesList.get(2), TRUSTSTORE_PEM_PATH); + } + + private void assertCorrectJksTruststore(TruststoreFile truststoreFile, String truststorePath, String truststorePass) { + assertCorrectTypeAndTruststorePath(truststoreFile, truststorePath, JksTruststore.class); + assertContainsCorrectPassword(truststoreFile, truststorePass); + } + + private void assertCorrectP12Truststore(TruststoreFile truststoreFile, String truststorePath, String truststorePass) { + assertCorrectTypeAndTruststorePath(truststoreFile, truststorePath, P12Truststore.class); + assertContainsCorrectPassword(truststoreFile, truststorePass); + } + + private void assertCorrectPemTruststore(TruststoreFile truststoreFile, String truststorePath) { + assertCorrectTypeAndTruststorePath(truststoreFile, truststorePath, PemTruststore.class); + } + + private void assertCorrectTypeAndTruststorePath(TruststoreFile truststoreFile, String truststorePath, Class truststoreType) { + assertThat(truststoreFile).isInstanceOf(truststoreType); + assertThat(truststoreFile.getTruststoreFile()).isEqualTo(new File(truststorePath)); + } + + private void assertContainsCorrectPassword(TruststoreFile truststoreFile, String truststorePass) { + TruststoreFileWithPassword truststoreFileWithPassword = (TruststoreFileWithPassword) truststoreFile; + assertThat(truststoreFileWithPassword.getPassword()).isEqualTo(truststorePass); + } +} 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 new file mode 100644 index 00000000..a11bb232 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/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.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 new file mode 100644 index 00000000..945a1077 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/path/TruststoresPathsProviderTest.java @@ -0,0 +1,108 @@ +/*============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_ENV; +import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_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_ENV); + } + + private void mockTruststoresPasswordsEnv(String truststoresPasswords) { + mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_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 index 336ba5da..43b7b9e1 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java @@ -24,8 +24,8 @@ 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.file.TruststoresPathsProvider; -import org.onap.oom.truststoremerger.certification.file.TruststoresPathsProviderException; +import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProvider; +import org.onap.oom.truststoremerger.certification.path.TruststoresPathsProviderException; import java.util.ArrayList; import java.util.List; diff --git a/trustStoreMerger/src/test/resources/truststore-jks.jks b/trustStoreMerger/src/test/resources/truststore-jks.jks new file mode 100644 index 00000000..38229811 Binary files /dev/null and b/trustStoreMerger/src/test/resources/truststore-jks.jks differ diff --git a/trustStoreMerger/src/test/resources/truststore-jks.pass b/trustStoreMerger/src/test/resources/truststore-jks.pass new file mode 100644 index 00000000..7426fd4d --- /dev/null +++ b/trustStoreMerger/src/test/resources/truststore-jks.pass @@ -0,0 +1 @@ +EOyuFbuYDyq_EhpboM72RHua \ No newline at end of file diff --git a/trustStoreMerger/src/test/resources/truststore-p12.p12 b/trustStoreMerger/src/test/resources/truststore-p12.p12 new file mode 100644 index 00000000..0fa8aecc Binary files /dev/null and b/trustStoreMerger/src/test/resources/truststore-p12.p12 differ diff --git a/trustStoreMerger/src/test/resources/truststore-p12.pass b/trustStoreMerger/src/test/resources/truststore-p12.pass new file mode 100644 index 00000000..86cc5aac --- /dev/null +++ b/trustStoreMerger/src/test/resources/truststore-p12.pass @@ -0,0 +1 @@ +88y9v5D8H3SG6bZWRVHDfOAo \ No newline at end of file diff --git a/trustStoreMerger/src/test/resources/truststore.pem b/trustStoreMerger/src/test/resources/truststore.pem new file mode 100644 index 00000000..3268e3a6 --- /dev/null +++ b/trustStoreMerger/src/test/resources/truststore.pem @@ -0,0 +1,28 @@ +-----BEGIN CERTIFICATE----- +MIIEszCCAxugAwIBAgIUE+27eIlr12tQ+AMxkJTf2Y+ycOEwDQYJKoZIhvcNAQEL +BQAwYTEjMCEGCgmSJomT8ixkAQEME2MtMDRjYmE2YjhhMDQ5ODEyNGQxFTATBgNV +BAMMDE1hbmFnZW1lbnRDQTEjMCEGA1UECgwaRUpCQ0EgQ29udGFpbmVyIFF1aWNr +c3RhcnQwHhcNMjAwNzA4MTIzODU4WhcNMzAwNzA4MTIzODU4WjBhMSMwIQYKCZIm +iZPyLGQBAQwTYy0wNGNiYTZiOGEwNDk4MTI0ZDEVMBMGA1UEAwwMTWFuYWdlbWVu +dENBMSMwIQYDVQQKDBpFSkJDQSBDb250YWluZXIgUXVpY2tzdGFydDCCAaIwDQYJ +KoZIhvcNAQEBBQADggGPADCCAYoCggGBALTlx22Ld87VO5QgkD7OJvx81a8xLRWt +b4cqmLSBRKw+jTjX4fHCtLh98hXNtYXJ9nxPa2t8MKR/I00Wf1razX1IYN9H/diV +uICjyMxDyK6nwEMpqaWiQgOQx1N4TjNhr19ULTbyFLQMVfXy1OrTsfoWQ2omvRxN +LIoVKwPHd92KG6iqJDZU14ErfA6UtypDV+4rOKQBh0JrfFI/KxKFKRH3e0oDxD8c +PIOUpYVccVv/4Gbc0ZRs8KK0uPZN73LlQccYzPrSk/VAUeuZ52Wqk6dNrq5FHSCe +EwPbx6aqgLwhTLlYAJqmYuDsGU9ZL09buCVKim1pjZiPaoaYAvv3KHdjEKAu9NxF +dezd4JZ24hqYCA7EGnKgyjHxA0SiD/B8f+aBdRGDZbMlH1gKFKivjuHSfPwRv6Op +p8ykEzk3yp0RcqSflVPg0mj+LPViYo/loLLOLybFFR7BetyFieN5QV7BKRyfc7Qi +Se6Idh1nLIrYR9ek8BDkEE9u/JiTT0gP3QIDAQABo2MwYTAPBgNVHRMBAf8EBTAD +AQH/MB8GA1UdIwQYMBaAFDYtHGSe9lYaC9+WnNT91wuiMlkjMB0GA1UdDgQWBBQ2 +LRxknvZWGgvflpzU/dcLojJZIzAOBgNVHQ8BAf8EBAMCAYYwDQYJKoZIhvcNAQEL +BQADggGBAIcLj76GVhYSuVaWMMCVlVl8rHhYYufT9z2X7G/0D1G655/dAeAJLltL +S4T7SZI44XKfVH4ztc4TO6OEMLZzslcfDzv/tUzL4EOsXtBTpsK9JgHP2lzCE+aj +a7uxn5SGWlu0YmT/++2d+QYaVVAjqalal8NsppOYCh8GB84TXbQjOMWcR9YBozZf +DSy3/vDNMuggZfdEOMMP57M10NoOKor+8eMGB42k4NR+G2npYHZ4uh1Ifk+eoTAh +o5O0iz3+/8eMTkLavqpnfzBhWHfRTI8wUu6zgm+QI+tsqhPePRuwauD8r79JBnPW +0gayZI5jIWTwvufpweKMgLyQbiGVUDtsr2c43kJ6XHoEf0ACUzbJKtGDD3Y7H/G1 +5Q7hBWbQwhUpiVeRnofS9jHQPWu0Ueq4/784hy+yPWotBIeIWEy4KzKTS+GaRDm0 +OSYtta/BdU0iZO/PzzTC5yIzwrsaq+5Idp16mub7mCAW0B36x0Phmr0DQWpZwxmX +9envV9HcJw== +-----END CERTIFICATE----- -- cgit 1.2.3-korg