2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
23 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
24 import java.io.IOException;
25 import java.security.KeyManagementException;
26 import java.security.KeyStore;
27 import java.security.KeyStoreException;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.UnrecoverableKeyException;
30 import java.security.cert.CertificateException;
31 import javax.net.ssl.SSLContext;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.ssl.SSLContextBuilder;
36 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
37 import org.onap.so.configuration.rest.HttpHeadersProvider;
38 import org.onap.so.rest.service.HttpRestServiceProvider;
39 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Qualifier;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.core.io.Resource;
48 import org.springframework.http.client.BufferingClientHttpRequestFactory;
49 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
50 import org.springframework.web.client.RestTemplate;
53 * Provides {@link org.onap.so.rest.service.VnfmAdapterServiceProvider} configuration for
54 * {@link VnfmAdapterCreateVnfTask}
56 * @author waqas.ikram@est.tech
59 public class VnfmAdapterCreateVnfTaskConfiguration {
61 private static final Logger logger = LoggerFactory.getLogger(VnfmAdapterCreateVnfTaskConfiguration.class);
63 @Value("${rest.http.client.configuration.ssl.trustStore:#{null}}")
64 private Resource trustStore;
66 @Value("${rest.http.client.configuration.ssl.trustStorePassword:#{null}}")
67 private String trustStorePassword;
69 @Value("${rest.http.client.configuration.ssl.keyStore:#{null}}")
70 private Resource keyStoreResource;
72 @Value("${rest.http.client.configuration.ssl.keyStorePassword:#{null}}")
73 private String keyStorePassword;
76 public HttpRestServiceProvider databaseHttpRestServiceProvider(
77 @Qualifier(CONFIGURABLE_REST_TEMPLATE) @Autowired final RestTemplate restTemplate,
78 @Autowired final VnfmBasicHttpConfigProvider etsiVnfmAdapter) {
79 if (trustStore != null) {
80 setTrustStore(restTemplate);
82 return getHttpRestServiceProvider(restTemplate, new BasicHttpHeadersProvider(etsiVnfmAdapter.getAuth()));
85 private void setTrustStore(final RestTemplate restTemplate) {
86 SSLContext sslContext;
88 if (keyStoreResource != null) {
89 KeyStore keystore = KeyStore.getInstance("pkcs12");
90 keystore.load(keyStoreResource.getInputStream(), keyStorePassword.toCharArray());
92 new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
93 .loadKeyMaterial(keystore, keyStorePassword.toCharArray()).build();
95 sslContext = new SSLContextBuilder()
96 .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();
98 logger.info("Setting truststore: {}", trustStore.getURL());
99 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
100 final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
101 final HttpComponentsClientHttpRequestFactory factory =
102 new HttpComponentsClientHttpRequestFactory(httpClient);
103 restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
104 } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException
105 | IOException | UnrecoverableKeyException exception) {
106 logger.error("Error reading truststore, TLS connection to VNFM will fail.", exception);
110 private HttpRestServiceProvider getHttpRestServiceProvider(final RestTemplate restTemplate,
111 final HttpHeadersProvider httpHeadersProvider) {
112 return new HttpRestServiceProviderImpl(restTemplate, httpHeadersProvider);