Add Certification merge logic
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / truststoremerger / certification / file / TruststoreFileFactory.java
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.oom.truststoremerger.certification.file;
21
22 import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore;
23 import org.onap.oom.truststoremerger.certification.file.model.PemTruststore;
24 import org.onap.oom.truststoremerger.certification.file.model.Truststore;
25
26 import java.io.File;
27 import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException;
28 import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
29 import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException;
30 import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException;
31 import org.onap.oom.truststoremerger.certification.file.provider.CertificateStoreControllerFactory;
32 import org.onap.oom.truststoremerger.certification.file.provider.FileManager;
33 import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController;
34 import org.onap.oom.truststoremerger.certification.file.provider.PasswordReader;
35 import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController;
36
37 public class TruststoreFileFactory {
38
39     private static final String JKS_EXTENSION = ".jks";
40     private static final String P12_EXTENSION = ".p12";
41     private static final String PEM_EXTENSION = ".pem";
42     private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist";
43     private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s";
44
45     private final FileManager fileManager;
46     private final PasswordReader passwordReader;
47     private final CertificateStoreControllerFactory certificateStoreControllerFactory =
48         new CertificateStoreControllerFactory();
49
50     public TruststoreFileFactory(FileManager fileManager, PasswordReader passwordReader) {
51         this.fileManager = fileManager;
52         this.passwordReader = passwordReader;
53     }
54
55     public Truststore create(String truststoreFilePath, String truststorePasswordPath)
56         throws TruststoreFileFactoryException, PasswordReaderException, KeystoreInstanceException, LoadTruststoreException {
57         File truststoreFile = new File(truststoreFilePath);
58         if (!fileManager.checkIfFileExists(truststoreFile)) {
59             throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile));
60         }
61         return createTypedTruststore(truststoreFile, truststorePasswordPath);
62     }
63
64     private Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath)
65         throws KeystoreInstanceException, PasswordReaderException, LoadTruststoreException, TruststoreFileFactoryException {
66         String extension = fileManager.getExtension(truststoreFile);
67         switch (extension) {
68             case JKS_EXTENSION:
69                 return createJksTruststore(truststoreFile, truststorePasswordPath);
70             case P12_EXTENSION:
71                 return createP12Truststore(truststoreFile, truststorePasswordPath);
72             case PEM_EXTENSION:
73                 return createPemTruststore(truststoreFile);
74             default:
75                 throw new TruststoreFileFactoryException(
76                     String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension));
77         }
78     }
79
80     private JavaTruststore createJksTruststore(File truststoreFile, String truststorePasswordPath)
81         throws PasswordReaderException, LoadTruststoreException, KeystoreInstanceException {
82         String password = passwordReader.readPassword(new File(truststorePasswordPath));
83         JavaCertificateStoreController storeController = certificateStoreControllerFactory
84             .createLoadedJksCertificateStoreController(truststoreFile, password);
85         return new JavaTruststore(truststoreFile, storeController);
86     }
87
88     private JavaTruststore createP12Truststore(File truststoreFile, String truststorePasswordPath)
89         throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
90         String password = passwordReader.readPassword(new File(truststorePasswordPath));
91         JavaCertificateStoreController storeController = certificateStoreControllerFactory
92             .createLoadedPkcs12CertificateStoreController(truststoreFile, password);
93         return new JavaTruststore(truststoreFile, storeController);
94     }
95
96     private PemTruststore createPemTruststore(File truststoreFile) {
97         return new PemTruststore(truststoreFile, new PemCertificateController(truststoreFile));
98     }
99 }