2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.vevnfm.configuration;
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;
45 public class ApplicationConfiguration {
47 private static final Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);
49 private final Resource clientKeyStore;
50 private final String clientKeyStorePassword;
51 private final Resource clientTrustStore;
52 private final String clientTrustStorePassword;
54 public ApplicationConfiguration(final ConfigProperties configProperties) {
55 clientKeyStore = configProperties.getClientKeyStore();
56 clientKeyStorePassword = configProperties.getClientKeyStorePassword();
57 clientTrustStore = configProperties.getClientTrustStore();
58 clientTrustStorePassword = configProperties.getClientTrustStorePassword();
62 public AuthorizationHeadersProvider headersProvider() {
63 return new AuthorizationHeadersProvider();
67 public HttpRestServiceProvider restProvider(final RestTemplate restTemplate,
68 final HttpHeadersProvider headersProvider) {
70 return new HttpRestServiceProviderImpl(restTemplate, headersProvider);
73 private void modify(final RestTemplate restTemplate) {
75 if (clientKeyStore == null || clientTrustStore == null) {
80 final KeyStore keystore = KeyStore.getInstance("PKCS12");
81 keystore.load(clientKeyStore.getInputStream(), clientKeyStorePassword.toCharArray());
83 final SSLContext sslContext = new SSLContextBuilder()
84 .loadTrustMaterial(clientTrustStore.getURL(), clientTrustStorePassword.toCharArray())
85 .loadKeyMaterial(keystore, clientKeyStorePassword.toCharArray()).build();
87 logger.info("Setting truststore: {}", clientTrustStore.getURL());
89 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
90 final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
91 final HttpComponentsClientHttpRequestFactory factory =
92 new HttpComponentsClientHttpRequestFactory(httpClient);
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);