Move ArtifcatsCreationProvider one level higher
[oom/platform/cert-service.git] / certServiceClient / src / main / java / org / onap / aaf / certservice / client / CertServiceClient.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;
21
22 import org.onap.aaf.certservice.client.api.ExitableException;
23 import org.onap.aaf.certservice.client.certification.ArtifactsCreatorProvider;
24 import org.onap.aaf.certservice.client.certification.CsrFactory;
25 import org.onap.aaf.certservice.client.certification.KeyPairFactory;
26 import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder;
27 import org.onap.aaf.certservice.client.common.Base64Encoder;
28 import org.onap.aaf.certservice.client.configuration.EnvsForClient;
29 import org.onap.aaf.certservice.client.configuration.EnvsForCsr;
30 import org.onap.aaf.certservice.client.configuration.EnvsForTls;
31 import org.onap.aaf.certservice.client.configuration.factory.ClientConfigurationFactory;
32 import org.onap.aaf.certservice.client.configuration.factory.CsrConfigurationFactory;
33 import org.onap.aaf.certservice.client.configuration.factory.SslContextFactory;
34 import org.onap.aaf.certservice.client.configuration.model.ClientConfiguration;
35 import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration;
36 import org.onap.aaf.certservice.client.httpclient.CloseableHttpsClientProvider;
37 import org.onap.aaf.certservice.client.httpclient.HttpClient;
38 import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import javax.net.ssl.SSLContext;
43 import java.security.KeyPair;
44
45 import static org.onap.aaf.certservice.client.api.ExitStatus.SUCCESS;
46 import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE;
47 import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM;
48
49 public class CertServiceClient {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(CertServiceClient.class);
52
53     private AppExitHandler appExitHandler;
54
55     public CertServiceClient(AppExitHandler appExitHandler) {
56         this.appExitHandler = appExitHandler;
57     }
58
59     public void run() {
60         KeyPairFactory keyPairFactory = new KeyPairFactory(RSA_ENCRYPTION_ALGORITHM, KEY_SIZE);
61         PrivateKeyToPemEncoder pkEncoder = new PrivateKeyToPemEncoder();
62         Base64Encoder base64Encoder = new Base64Encoder();
63         try {
64             ClientConfiguration clientConfiguration = new ClientConfigurationFactory(new EnvsForClient()).create();
65             CsrConfiguration csrConfiguration = new CsrConfigurationFactory(new EnvsForCsr()).create();
66             KeyPair keyPair = keyPairFactory.create();
67             CsrFactory csrFactory = new CsrFactory(csrConfiguration);
68             SSLContext sslContext = new SslContextFactory(new EnvsForTls()).create();
69
70             CloseableHttpsClientProvider provider = new CloseableHttpsClientProvider(
71                     sslContext, clientConfiguration.getRequestTimeout());
72             HttpClient httpClient = new HttpClient(provider, clientConfiguration.getUrlToCertService());
73
74             CertServiceResponse certServiceData =
75                     httpClient.retrieveCertServiceData(
76                             clientConfiguration.getCaName(),
77                             base64Encoder.encode(csrFactory.createCsrInPem(keyPair)),
78                             base64Encoder.encode(pkEncoder.encodePrivateKeyToPem(keyPair.getPrivate())));
79
80             ArtifactsCreatorProvider
81                     .get(clientConfiguration.getOutputType(),
82                             clientConfiguration.getCertsOutputPath())
83                     .create(certServiceData.getCertificateChain(),
84                             certServiceData.getTrustedCertificates(),
85                             keyPair.getPrivate());
86
87         } catch (ExitableException e) {
88             LOGGER.error("Cert Service Client fails in execution: ", e);
89             appExitHandler.exit(e.applicationExitStatus());
90         }
91         appExitHandler.exit(SUCCESS);
92     }
93 }