7a4cbfa9ad6c3ef711cb1388be1fdc4b3a213052
[oom/platform/cert-service.git] / certServiceClient / src / main / java / org / onap / aaf / certservice / client / certification / conversion / PemArtifactsCreator.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.security.PrivateKey;
23 import java.util.List;
24 import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder;
25 import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException;
26 import org.onap.aaf.certservice.client.certification.exception.PkEncodingException;
27 import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class PemArtifactsCreator implements ArtifactsCreator {
32     private static final Logger LOGGER = LoggerFactory.getLogger(PemArtifactsCreator.class);
33
34     private static final String KEY_PEM = "key.pem";
35     private static final String KEYSTORE_PEM = "keystore.pem";
36     private static final String TRUSTSTORE_PEM = "truststore.pem";
37
38     private final CertFileWriter writer;
39     private final PrivateKeyToPemEncoder pkEncoder;
40
41     public PemArtifactsCreator(CertFileWriter writer, PrivateKeyToPemEncoder pkEncoder) {
42         this.writer = writer;
43         this.pkEncoder = pkEncoder;
44     }
45
46     @Override
47     public void create(List<String> keystoreData, List<String> truststoreData, PrivateKey privateKey)
48         throws PkEncodingException, CertFileWriterException {
49         LOGGER.debug("Attempt to create PEM private key file and saving data. File name: {}", KEY_PEM);
50         writer.saveData(pkEncoder.encodePrivateKeyToPem(privateKey).getBytes(), KEY_PEM);
51
52         LOGGER.debug("Attempt to create PEM keystore file and saving data. File name: {}", KEYSTORE_PEM);
53         writer.saveData(getDataAsBytes(keystoreData), KEYSTORE_PEM);
54
55         LOGGER.debug("Attempt to create PEM truststore file and saving data. File name: {}", TRUSTSTORE_PEM);
56         writer.saveData(getDataAsBytes(truststoreData), TRUSTSTORE_PEM);
57     }
58
59     private byte[] getDataAsBytes(List<String> data) {
60         return String.join("\n", data).getBytes();
61     }
62 }