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