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;
22 import org.onap.aaf.certservice.client.api.ExitableException;
23 import org.onap.aaf.certservice.client.certification.CsrFactory;
24 import org.onap.aaf.certservice.client.certification.KeyPairFactory;
25 import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder;
26 import org.onap.aaf.certservice.client.certification.conversion.KeystoreTruststoreCreator;
27 import org.onap.aaf.certservice.client.certification.conversion.KeystoreTruststoreCreatorFactory;
28 import org.onap.aaf.certservice.client.common.Base64Encoder;
29 import org.onap.aaf.certservice.client.configuration.EnvsForClient;
30 import org.onap.aaf.certservice.client.configuration.EnvsForCsr;
31 import org.onap.aaf.certservice.client.configuration.EnvsForTls;
32 import org.onap.aaf.certservice.client.configuration.factory.ClientConfigurationFactory;
33 import org.onap.aaf.certservice.client.configuration.factory.CsrConfigurationFactory;
34 import org.onap.aaf.certservice.client.configuration.factory.SslContextFactory;
35 import org.onap.aaf.certservice.client.configuration.model.ClientConfiguration;
36 import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration;
37 import org.onap.aaf.certservice.client.httpclient.CloseableHttpsClientProvider;
38 import org.onap.aaf.certservice.client.httpclient.HttpClient;
39 import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import javax.net.ssl.SSLContext;
44 import java.security.KeyPair;
46 import static org.onap.aaf.certservice.client.api.ExitStatus.SUCCESS;
47 import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE;
48 import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM;
50 public class CertServiceClient {
52 private static final Logger LOGGER = LoggerFactory.getLogger(CertServiceClient.class);
54 private AppExitHandler appExitHandler;
56 public CertServiceClient(AppExitHandler appExitHandler) {
57 this.appExitHandler = appExitHandler;
61 KeyPairFactory keyPairFactory = new KeyPairFactory(RSA_ENCRYPTION_ALGORITHM, KEY_SIZE);
62 PrivateKeyToPemEncoder pkEncoder = new PrivateKeyToPemEncoder();
63 Base64Encoder base64Encoder = new Base64Encoder();
65 ClientConfiguration clientConfiguration = new ClientConfigurationFactory(new EnvsForClient()).create();
66 CsrConfiguration csrConfiguration = new CsrConfigurationFactory(new EnvsForCsr()).create();
67 KeyPair keyPair = keyPairFactory.create();
68 CsrFactory csrFactory = new CsrFactory(csrConfiguration);
69 SSLContext sslContext = new SslContextFactory(new EnvsForTls()).create();
71 CloseableHttpsClientProvider provider = new CloseableHttpsClientProvider(
72 sslContext, clientConfiguration.getRequestTimeout());
73 HttpClient httpClient = new HttpClient(provider, clientConfiguration.getUrlToCertService());
75 CertServiceResponse certServiceData =
76 httpClient.retrieveCertServiceData(
77 clientConfiguration.getCaName(),
78 base64Encoder.encode(csrFactory.createCsrInPem(keyPair)),
79 base64Encoder.encode(pkEncoder.encodePrivateKeyToPem(keyPair.getPrivate())));
81 KeystoreTruststoreCreator filesCreator = new KeystoreTruststoreCreatorFactory(
82 clientConfiguration.getCertsOutputPath()).create();
83 filesCreator.createKeystore(certServiceData.getCertificateChain(), keyPair.getPrivate());
84 filesCreator.createTruststore(certServiceData.getTrustedCertificates());
85 } catch (ExitableException e) {
86 LOGGER.error("Cert Service Client fail in execution: ", e);
87 appExitHandler.exit(e.applicationExitStatus());
89 appExitHandler.exit(SUCCESS);