2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 - 2019 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;
54 private final String certFilePath;
55 FeatureManager featureManager;
58 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(org.onap.vid.aai.util.HttpsAuthClient.class);
61 public HttpsAuthClient(String certFilePath, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider, FeatureManager featureManager) {
62 this.certFilePath = certFilePath;
63 this.systemPropertyHelper = systemPropertyHelper;
64 this.sslContextProvider = sslContextProvider;
65 this.featureManager = featureManager;
74 public Client getClient(HttpClientMode mode) throws GeneralSecurityException, IOException {
75 ClientConfig config = prepareClientConfig(mode);
78 setSystemProperties();
80 optionallyVerifyHostname();
82 return systemPropertyHelper.isClientCertEnabled() ?
83 getTrustedClient(config, getKeystorePath(), systemPropertyHelper.getDecryptedKeystorePassword(), mode)
84 : getUntrustedClient(config);
86 } catch (Exception e) {
87 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config", e);
93 private void optionallyVerifyHostname() {
94 HttpsURLConnection.setDefaultHostnameVerifier(getHostnameVerifier());
97 private Client getUntrustedClient(ClientConfig config) {
98 return ClientBuilder.newBuilder().withConfig(config).build().register(CustomJacksonJaxBJsonProvider.class);
101 private Client getTrustedClient(ClientConfig config, String keystorePath, String keystorePassword, HttpClientMode httpClientMode) throws HttpClientBuilderException {
102 return ClientBuilder.newBuilder()
103 .sslContext(sslContextProvider.getSslContext(keystorePath, keystorePassword, httpClientMode))
104 .hostnameVerifier(getHostnameVerifier())
107 .register(CustomJacksonJaxBJsonProvider.class);
110 protected HostnameVerifier getHostnameVerifier() {
111 if(featureManager.isActive(Features.FLAG_EXP_USE_DEFAULT_HOST_NAME_VERIFIER)){
112 return new DefaultHostnameVerifier();
115 return new NoopHostnameVerifier();
118 protected String getKeystorePath() {
119 return systemPropertyHelper.getAAIKeystoreFilename().map(this::getCertificatesPathOf).orElse("");
122 private void setSystemProperties() {
123 System.setProperty(SSL_TRUST_STORE,
124 systemPropertyHelper.getAAITruststoreFilename().map(this::getCertificatesPathOf).orElse(""));
125 System.setProperty(SSL_TRUST_STORE_PASS_WORD, systemPropertyHelper.getDecryptedTruststorePassword());
128 private ClientConfig prepareClientConfig(HttpClientMode mode) {
129 ClientConfig config = new ClientConfig();
130 if (mode.equals(WITH_KEYSTORE)) {
131 config.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, Boolean.TRUE);
132 config.connectorProvider(new HttpUrlConnectorProvider().useSetMethodWorkaround());
137 private String getCertificatesPathOf(String fileName) {
138 if (fileName.contains("/") || fileName.contains("\\")) {
141 return certFilePath + FileSystems.getDefault().getSeparator() + fileName;