38f7a0cd3fc631d25392b75a3db29e121c16c7ea
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
4  * ================================================================================
5  * Copyright (C) 2020 Samsung. 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.so.adapters.vevnfm.configuration;
22
23 import java.io.IOException;
24 import java.security.*;
25 import java.security.cert.CertificateException;
26 import javax.net.ssl.SSLContext;
27 import org.apache.http.client.HttpClient;
28 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
29 import org.apache.http.impl.client.HttpClients;
30 import org.apache.http.ssl.SSLContextBuilder;
31 import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider;
32 import org.onap.so.configuration.rest.HttpHeadersProvider;
33 import org.onap.so.rest.service.HttpRestServiceProvider;
34 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.context.annotation.Bean;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.core.io.Resource;
40 import org.springframework.http.client.BufferingClientHttpRequestFactory;
41 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
42 import org.springframework.web.client.RestTemplate;
43
44 @Configuration
45 public class ApplicationConfiguration {
46
47     private static final Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);
48
49     private final Resource clientKeyStore;
50     private final String clientKeyStorePassword;
51     private final Resource clientTrustStore;
52     private final String clientTrustStorePassword;
53
54     public ApplicationConfiguration(final ConfigProperties configProperties) {
55         clientKeyStore = configProperties.getClientKeyStore();
56         clientKeyStorePassword = configProperties.getClientKeyStorePassword();
57         clientTrustStore = configProperties.getClientTrustStore();
58         clientTrustStorePassword = configProperties.getClientTrustStorePassword();
59     }
60
61     @Bean
62     public AuthorizationHeadersProvider headersProvider() {
63         return new AuthorizationHeadersProvider();
64     }
65
66     @Bean
67     public HttpRestServiceProvider restProvider(final RestTemplate restTemplate,
68             final HttpHeadersProvider headersProvider) {
69         modify(restTemplate);
70         return new HttpRestServiceProviderImpl(restTemplate, headersProvider);
71     }
72
73     private void modify(final RestTemplate restTemplate) {
74
75         if (clientKeyStore == null || clientTrustStore == null) {
76             return;
77         }
78
79         try {
80             final KeyStore keystore = KeyStore.getInstance("PKCS12");
81             keystore.load(clientKeyStore.getInputStream(), clientKeyStorePassword.toCharArray());
82
83             final SSLContext sslContext = new SSLContextBuilder()
84                     .loadTrustMaterial(clientTrustStore.getURL(), clientTrustStorePassword.toCharArray())
85                     .loadKeyMaterial(keystore, clientKeyStorePassword.toCharArray()).build();
86
87             logger.info("Setting truststore: {}", clientTrustStore.getURL());
88
89             final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
90             final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
91             final HttpComponentsClientHttpRequestFactory factory =
92                     new HttpComponentsClientHttpRequestFactory(httpClient);
93
94             restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
95         } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
96                 | IOException | UnrecoverableKeyException e) {
97             logger.error("Error reading truststore, TLS connection to VNFM will fail.", e);
98         }
99     }
100 }