Automation adds INFO.yaml
[oom/platform/cert-service.git] / certServiceClient / src / main / java / org / onap / aaf / certservice / client / certification / conversion / ConvertedArtifactsCreator.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.exception.CertFileWriterException;
23 import org.onap.aaf.certservice.client.certification.exception.PemConversionException;
24 import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.security.PrivateKey;
29 import java.util.List;
30
31 public class ConvertedArtifactsCreator implements ArtifactsCreator {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(ConvertedArtifactsCreator.class);
34     private static final String CERTIFICATE_ALIAS = "certificate";
35     private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-";
36     private static final int PASSWORD_LENGTH = 24;
37     private static final String PASS_EXT = "pass";
38     private static final String KEYSTORE = "keystore";
39     private static final String TRUSTSTORE = "truststore";
40
41     private final String fileExtension;
42     private final RandomPasswordGenerator passwordGenerator;
43     private final PemConverter converter;
44     private final CertFileWriter fileWriter;
45
46     ConvertedArtifactsCreator(CertFileWriter fileWriter, RandomPasswordGenerator passwordGenerator,
47                               PemConverter converter, String fileExtension) {
48         this.passwordGenerator = passwordGenerator;
49         this.converter = converter;
50         this.fileWriter = fileWriter;
51         this.fileExtension = fileExtension;
52     }
53
54     @Override
55     public void create(List<String> keystoreData, List<String> truststoreData, PrivateKey privateKey)
56             throws PemConversionException, CertFileWriterException {
57         createKeystore(keystoreData, privateKey);
58         createTruststore(truststoreData);
59     }
60
61     private void createKeystore(List<String> data, PrivateKey privateKey)
62             throws PemConversionException, CertFileWriterException {
63         Password password = passwordGenerator.generate(PASSWORD_LENGTH);
64         String keystoreArtifactName = getFilenameWithExtension(KEYSTORE, fileExtension);
65         String keystorePass = getFilenameWithExtension(KEYSTORE, PASS_EXT);
66
67         LOGGER.debug("Attempt to create keystore files and saving data. File names: {}, {}", keystoreArtifactName, keystorePass);
68
69         fileWriter.saveData(converter.convertKeystore(data, password, CERTIFICATE_ALIAS, privateKey), keystoreArtifactName);
70         fileWriter.saveData(getPasswordAsBytes(password), keystorePass);
71     }
72
73     private void createTruststore(List<String> data)
74             throws PemConversionException, CertFileWriterException {
75         Password password = passwordGenerator.generate(PASSWORD_LENGTH);
76         String truststoreArtifactName = getFilenameWithExtension(TRUSTSTORE, fileExtension);
77         String truststorePass = getFilenameWithExtension(TRUSTSTORE, PASS_EXT);
78
79         LOGGER.debug("Attempt to create truststore files and saving data. File names: {}, {}", truststoreArtifactName, truststorePass);
80
81         fileWriter.saveData(converter.convertTruststore(data, password, TRUSTED_CERTIFICATE_ALIAS), truststoreArtifactName);
82         fileWriter.saveData(getPasswordAsBytes(password), truststorePass);
83     }
84
85     private byte[] getPasswordAsBytes(Password password) {
86         return password.getCurrentPassword().getBytes();
87     }
88
89     private String getFilenameWithExtension(String filename, String extension) {
90         return String.format("%s.%s", filename, extension);
91     }
92 }