fe2b2c1045770772094509dcb2c9d289081d7500
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.security.GeneralSecurityException;
31 import java.security.KeyStore;
32 import javax.net.ssl.SSLContext;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
35 import org.apache.http.impl.client.HttpClients;
36 import org.apache.http.ssl.SSLContextBuilder;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
38 import org.springframework.boot.web.client.RestTemplateBuilder;
39 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
40 import org.springframework.web.client.RestTemplate;
41 import reactor.core.publisher.Mono;
42
43 public class DmaaPRestTemplateFactory {
44
45     /**
46      * Function for creating RestTemplate object.
47      *
48      * @param publisherConfiguration - DMaaP publisher configuration object
49      * @return RestTemplate with correct ssl configuration
50      */
51     public Mono<RestTemplate> build(DmaapPublisherConfiguration publisherConfiguration) {
52         if (publisherConfiguration.enableDmaapCertAuth()) {
53             return createRestTemplateWithSslSetup(publisherConfiguration);
54         }
55
56         return Mono.just(new RestTemplate());
57     }
58
59     private Mono<RestTemplate> createRestTemplateWithSslSetup(DmaapPublisherConfiguration publisherConfiguration) {
60         try {
61             RestTemplateBuilder builder = new RestTemplateBuilder();
62
63             SSLContext sslContext = createSslContext(publisherConfiguration,
64                     loadPasswordFromFile(publisherConfiguration.keyStorePasswordPath()),
65                     loadPasswordFromFile(publisherConfiguration.trustStorePasswordPath()));
66
67             return Mono.just(builder
68                     .requestFactory(() -> createRequestFactory(sslContext)).build());
69
70         } catch (GeneralSecurityException | IOException e) {
71             return Mono.error(e);
72         }
73     }
74
75     private SSLContext createSslContext(DmaapPublisherConfiguration publisherConfiguration,
76                                         String keyStorePassword, String trustStorePassword)
77             throws IOException, GeneralSecurityException {
78         return new SSLContextBuilder()
79                         .loadKeyMaterial(
80                                 keyStore(publisherConfiguration.keyStorePath(), keyStorePassword),
81                                 keyStorePassword.toCharArray())
82                         .loadTrustMaterial(
83                                 getFile(publisherConfiguration.trustStorePath()), trustStorePassword.toCharArray())
84                         .build();
85     }
86
87     private HttpComponentsClientHttpRequestFactory createRequestFactory(SSLContext sslContext) {
88         SSLConnectionSocketFactory socketFactory =
89                 new SSLConnectionSocketFactory(sslContext);
90         HttpClient httpClient = HttpClients.custom()
91                 .setSSLSocketFactory(socketFactory).build();
92
93         return new HttpComponentsClientHttpRequestFactory(httpClient);
94     }
95
96     private KeyStore keyStore(String keyStoreFile, String keyStorePassword)
97             throws GeneralSecurityException, IOException {
98         KeyStore ks = KeyStore.getInstance("jks");
99         ks.load(getResource(keyStoreFile), keyStorePassword.toCharArray());
100         return ks;
101     }
102
103     private File getFile(String fileName) {
104         return new File(fileName);
105     }
106
107     private InputStream getResource(String fileName) throws FileNotFoundException {
108         return new FileInputStream(fileName);
109     }
110
111     private String loadPasswordFromFile(String path) throws IOException {
112         return new String(Files.readAllBytes(Paths.get(path)));
113     }
114
115 }