Improve exceptions logging in certservice client
[oom/platform/cert-service.git] / certServiceClient / src / main / java / org / onap / aaf / certservice / client / certification / conversion / PKCS12FilesCreator.java
1 /*============LICENSE_START=======================================================
2  * aaf-certservice-client
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.aaf.certservice.client.certification.conversion;
21
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.nio.file.Path;
25
26 import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 class PKCS12FilesCreator {
31
32     private static final String KEYSTORE_JKS = "keystore.jks";
33     private static final String KEYSTORE_PASS = "keystore.pass";
34     private static final String TRUSTSTORE_JKS = "truststore.jks";
35     private static final String TRUSTSTORE_PASS = "truststore.pass";
36     private final String keystoreJksPath;
37     private final String keystorePassPath;
38     private final String truststoreJksPath;
39     private final String truststorePassPath;
40     private final Logger LOGGER = LoggerFactory.getLogger(PKCS12FilesCreator.class);
41
42
43     PKCS12FilesCreator(String path) {
44         keystoreJksPath = Path.of(path, KEYSTORE_JKS).toString();
45         keystorePassPath = Path.of(path, KEYSTORE_PASS).toString();
46         truststoreJksPath = Path.of(path, TRUSTSTORE_JKS).toString();
47         truststorePassPath = Path.of(path, TRUSTSTORE_PASS).toString();
48     }
49
50     void saveKeystoreData(byte[] keystoreData, String keystorePassword) throws PemToPKCS12ConverterException {
51         LOGGER.debug("Creating PKCS12 keystore files and saving data. Keystore path: {}", keystoreJksPath);
52
53         saveDataToLocation(keystoreData, keystoreJksPath);
54         saveDataToLocation(keystorePassword.getBytes(), keystorePassPath);
55     }
56
57     void saveTruststoreData(byte[] truststoreData, String truststorePassword)
58         throws PemToPKCS12ConverterException {
59         LOGGER.debug("Creating PKCS12 truststore files and saving data. Truststore path: {}", truststoreJksPath);
60
61         saveDataToLocation(truststoreData, truststoreJksPath);
62         saveDataToLocation(truststorePassword.getBytes(), truststorePassPath);
63     }
64
65     private void saveDataToLocation(byte[] data, String path) throws PemToPKCS12ConverterException {
66         try (FileOutputStream fos = new FileOutputStream(path)) {
67             fos.write(data);
68         } catch (IOException e) {
69             LOGGER.error("PKCS12 files creation failed, exception message: {}", e.getMessage());
70             throw new PemToPKCS12ConverterException(e);
71         }
72     }
73 }