1 /*============LICENSE_START=======================================================
2 * oom-truststore-merger
3 * ================================================================================
4 * Copyright (C) 2020 Nokia. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.oom.certservice.postprocessor.merger.model;
22 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.JKS_TYPE;
23 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.PKCS12_TYPE;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.nio.file.StandardCopyOption;
31 import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
32 import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
33 import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
35 public final class TestCertificateProvider {
37 public static final String SAMPLE_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-p12.p12";
38 public static final String SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH = "src/test/resources/truststore-p12.pass";
39 public static final String TMP_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-p12.p12";
41 public static final String SAMPLE_P12_KEYSTORE_FILE_PATH = "src/test/resources/keystore.p12";
42 public static final String SAMPLE_P12_KEYSTORE_PASSWORD_PATH = "src/test/resources/keystore.pass";
44 public static final String SAMPLE_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-jks.jks";
45 public static final String SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH = "src/test/resources/truststore-jks-uniq.jks";
46 public static final String SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH = "src/test/resources/truststore-jks.pass";
47 public static final String TMP_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-jks.jks";
49 public static final String SAMPLE_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore.pem";
50 public static final String EMPTY_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/empty-truststore.pem";
51 public static final String TMP_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore.pem";
52 public static final String SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH = "src/test/resources/truststore-with-private-key.pem";
54 public static final String PEM_FILE_PATH = "src/test/resources/truststore.pem";
55 public static final String PEM_BACKUP_FILE_PATH = "src/test/resources/truststore.pem.bak";
57 private TestCertificateProvider() {
60 public static Truststore getSampleP12Truststore()
61 throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
62 return createJavaTruststore(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE);
65 public static Truststore getSampleP12Keystore()
66 throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
67 return createJavaTruststore(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD_PATH, PKCS12_TYPE);
70 public static Truststore createTmpP12TruststoreFile()
71 throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
72 copyFile(SAMPLE_P12_TRUSTSTORE_FILE_PATH, TMP_P12_TRUSTSTORE_FILE_PATH);
73 return createJavaTruststore(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE);
76 public static Truststore getSamplePemTruststoreFile() {
77 return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
80 public static Truststore createEmptyTmpPemTruststoreFile()
82 copyFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
83 return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
86 public static Truststore createTmpPemTruststoreFile()
88 copyFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
89 return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
92 public static String getExpectedPemCertificateAsString() throws IOException {
93 Path samplePemFilePath = Paths.get(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
94 return Files.readString(samplePemFilePath);
97 public static Truststore getSampleJksTruststoreFile()
98 throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
99 return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE);
102 public static Truststore getSampleJksTruststoreFileWithUniqueAlias()
103 throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
104 return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH,
108 public static Truststore createTmpJksTruststoreFileWithUniqAlias()
109 throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
110 copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH);
111 return createJavaTruststore(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE);
114 public static File getEmptyPemFile() {
115 return getFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH);
118 public static File getNotEmptyPemFile() {
119 return getFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
122 public static File getPemWithPrivateKeyFile() {
123 return getFile(SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH);
126 public static void removeTemporaryFiles() throws IOException {
127 Files.deleteIfExists(Paths.get(TMP_PEM_TRUSTSTORE_FILE_PATH));
128 Files.deleteIfExists(Paths.get(TMP_JKS_TRUSTSTORE_FILE_PATH));
129 Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH));
130 Files.deleteIfExists(Paths.get(PEM_BACKUP_FILE_PATH));
133 private static Truststore createJavaTruststore(String filePath, String password, String instanceType)
134 throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
135 File certFile = getFile(filePath);
136 return JavaTruststoreFactory.create(certFile, password, instanceType);
139 private static Truststore getPemTruststoreInstance(
140 String tmpPemTruststoreFilePath) {
141 File file = getFile(tmpPemTruststoreFilePath);
142 return new PemTruststore(file);
145 private static void copyFile(String sourcePath, String destPath) throws IOException {
146 Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING);
149 private static File getFile(String path) {
150 return new File(path);