2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.vid.aai.util;
25 import org.apache.http.conn.ssl.DefaultHostnameVerifier;
26 import org.apache.http.conn.ssl.NoopHostnameVerifier;
27 import org.glassfish.jersey.client.ClientConfig;
28 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.vid.aai.exceptions.HttpClientBuilderException;
31 import org.onap.vid.properties.Features;
32 import org.togglz.core.manager.FeatureManager;
34 import javax.net.ssl.HostnameVerifier;
35 import javax.net.ssl.HttpsURLConnection;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.ClientBuilder;
38 import java.io.IOException;
39 import java.nio.file.FileSystems;
40 import java.security.GeneralSecurityException;
42 import static org.onap.vid.aai.util.HttpClientMode.WITH_KEYSTORE;
45 * The Class HttpsAuthClient.
47 public class HttpsAuthClient {
49 private static final String SSL_TRUST_STORE = "javax.net.ssl.trustStore";
50 private static final String SSL_TRUST_STORE_PASS_WORD = "javax.net.ssl.trustStorePassword";
52 private final SystemPropertyHelper systemPropertyHelper;
53 private final SSLContextProvider sslContextProvider;
55 public HttpsAuthClient(String certFilePath, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider, FeatureManager featureManager) {
56 this.certFilePath = certFilePath;
57 this.systemPropertyHelper = systemPropertyHelper;
58 this.sslContextProvider = sslContextProvider;
59 this.featureManager = featureManager;
62 private final String certFilePath;
64 FeatureManager featureManager;
67 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(org.onap.vid.aai.util.HttpsAuthClient.class);
75 public Client getClient(HttpClientMode mode) throws GeneralSecurityException, IOException {
76 ClientConfig config = prepareClientConfig(mode);
79 setSystemProperties();
81 optionallyVerifyHostname();
83 return systemPropertyHelper.isClientCertEnabled() ?
84 getTrustedClient(config, getKeystorePath(), systemPropertyHelper.getDecryptedKeystorePassword(), mode)
85 : getUntrustedClient(config);
87 } catch (Exception e) {
88 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config", e);
94 private void optionallyVerifyHostname() {
95 HttpsURLConnection.setDefaultHostnameVerifier(getHostnameVerifier());
98 private Client getUntrustedClient(ClientConfig config) {
99 return ClientBuilder.newBuilder().withConfig(config).build().register(CustomJacksonJaxBJsonProvider.class);
102 private Client getTrustedClient(ClientConfig config, String keystorePath, String keystorePassword, HttpClientMode httpClientMode) throws HttpClientBuilderException {
103 return ClientBuilder.newBuilder()
104 .sslContext(sslContextProvider.getSslContext(keystorePath, keystorePassword, httpClientMode))
105 .hostnameVerifier(getHostnameVerifier())
108 .register(CustomJacksonJaxBJsonProvider.class);
111 protected HostnameVerifier getHostnameVerifier() {
112 if(featureManager.isActive(Features.FLAG_EXP_USE_DEFAULT_HOST_NAME_VERIFIER)){
113 return new DefaultHostnameVerifier();
116 return new NoopHostnameVerifier();
119 private String getKeystorePath() {
120 return getCertificatesPath() + FileSystems.getDefault().getSeparator() + systemPropertyHelper.getAAIKeystoreFilename();
123 private void setSystemProperties() {
124 System.setProperty(SSL_TRUST_STORE, getCertificatesPath() + FileSystems.getDefault().getSeparator() +
125 systemPropertyHelper.getAAITruststoreFilename().orElse(""));
126 System.setProperty(SSL_TRUST_STORE_PASS_WORD, systemPropertyHelper.getDecryptedTruststorePassword());
129 private ClientConfig prepareClientConfig(HttpClientMode mode) {
130 ClientConfig config = new ClientConfig();
131 if (mode.equals(WITH_KEYSTORE)) {
132 config.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, Boolean.TRUE);
133 config.connectorProvider(new HttpUrlConnectorProvider().useSetMethodWorkaround());
138 private String getCertificatesPath() {