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
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.aaf.certservice.client.certification.conversion;
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;
29 import java.security.PrivateKey;
30 import java.util.List;
32 public class PemArtifactsCreator implements ArtifactsCreator {
33 private static final Logger LOGGER = LoggerFactory.getLogger(PemArtifactsCreator.class);
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";
39 private final CertFileWriter writer;
40 private final PrivateKeyToPemEncoder pkEncoder;
42 public PemArtifactsCreator(CertFileWriter writer, PrivateKeyToPemEncoder pkEncoder) {
44 this.pkEncoder = pkEncoder;
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);
53 LOGGER.debug("Attempt to create PEM keystore file and saving data. File name: {}", KEYSTORE_PEM);
54 writer.saveData(getDataAsBytes(keystoreData), KEYSTORE_PEM);
56 LOGGER.debug("Attempt to create PEM truststore file and saving data. File name: {}", TRUSTSTORE_PEM);
57 writer.saveData(getDataAsBytes(truststoreData), TRUSTSTORE_PEM);
60 private byte[] getDataAsBytes(List<String> data) {
61 return String.join("\n", data).getBytes();