Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / util / HttpsAuthClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.vid.aai.util;
23
24
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;
33
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;
41
42 import static org.onap.vid.aai.util.HttpClientMode.WITH_KEYSTORE;
43
44 /**
45  * The Class HttpsAuthClient.
46  */
47 public class HttpsAuthClient {
48
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";
51
52     private final SystemPropertyHelper systemPropertyHelper;
53     private final SSLContextProvider sslContextProvider;
54
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;
60     }
61
62     private final String certFilePath;
63
64     FeatureManager featureManager;
65
66     /** The logger. */
67     static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(org.onap.vid.aai.util.HttpsAuthClient.class);
68
69
70     /**
71      * Gets the client.
72      *
73      * @return the client
74      */
75     public Client getClient(HttpClientMode mode) throws GeneralSecurityException, IOException {
76         ClientConfig config = prepareClientConfig(mode);
77
78         try {
79             setSystemProperties();
80
81             optionallyVerifyHostname();
82
83             return systemPropertyHelper.isClientCertEnabled() ?
84                     getTrustedClient(config, getKeystorePath(), systemPropertyHelper.getDecryptedKeystorePassword(), mode)
85                     : getUntrustedClient(config);
86
87         } catch (Exception e) {
88             logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config", e);
89             throw e;
90         }
91
92     }
93
94     private void optionallyVerifyHostname() {
95         HttpsURLConnection.setDefaultHostnameVerifier(getHostnameVerifier());
96     }
97
98     private Client getUntrustedClient(ClientConfig config) {
99         return ClientBuilder.newBuilder().withConfig(config).build().register(CustomJacksonJaxBJsonProvider.class);
100     }
101
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())
106                 .withConfig(config)
107                 .build()
108                 .register(CustomJacksonJaxBJsonProvider.class);
109     }
110
111     protected HostnameVerifier getHostnameVerifier() {
112         if(featureManager.isActive(Features.FLAG_EXP_USE_DEFAULT_HOST_NAME_VERIFIER)){
113             return new DefaultHostnameVerifier();
114         }
115
116         return new NoopHostnameVerifier();
117     }
118
119     private String getKeystorePath() {
120         return getCertificatesPath() + FileSystems.getDefault().getSeparator() + systemPropertyHelper.getAAIKeystoreFilename();
121     }
122
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());
127     }
128
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());
134         }
135         return config;
136     }
137
138     private String getCertificatesPath() {
139         return certFilePath;
140     }
141
142 }