Merge "merge 2 TestNg transformers"
[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 - 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
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         private final String certFilePath;
55         FeatureManager featureManager;
56
57     /** The logger. */
58     static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(org.onap.vid.aai.util.HttpsAuthClient.class);
59         
60
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;
66     }
67
68
69     /**
70      * Gets the client.
71      *
72      * @return the client
73      */
74     public Client getClient(HttpClientMode mode) throws GeneralSecurityException, IOException {
75         ClientConfig config = prepareClientConfig(mode);
76
77         try {
78             setSystemProperties();
79
80             optionallyVerifyHostname();
81
82             return systemPropertyHelper.isClientCertEnabled() ?
83                     getTrustedClient(config, getKeystorePath(), systemPropertyHelper.getDecryptedKeystorePassword(), mode)
84                     : getUntrustedClient(config);
85
86         } catch (Exception e) {
87             logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config", e);
88             throw e;
89         }
90
91     }
92
93     private void optionallyVerifyHostname() {
94         HttpsURLConnection.setDefaultHostnameVerifier(getHostnameVerifier());
95     }
96
97     private Client getUntrustedClient(ClientConfig config) {
98         return ClientBuilder.newBuilder().withConfig(config).build().register(CustomJacksonJaxBJsonProvider.class);
99     }
100
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())
105                 .withConfig(config)
106                 .build()
107                 .register(CustomJacksonJaxBJsonProvider.class);
108     }
109
110     protected HostnameVerifier getHostnameVerifier() {
111         if(featureManager.isActive(Features.FLAG_EXP_USE_DEFAULT_HOST_NAME_VERIFIER)){
112             return new DefaultHostnameVerifier();
113         }
114
115         return new NoopHostnameVerifier();
116     }
117
118     protected String getKeystorePath() {
119         return systemPropertyHelper.getAAIKeystoreFilename().map(this::getCertificatesPathOf).orElse("");
120     }
121
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());
126     }
127
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());
133         }
134         return config;
135     }
136
137     private String getCertificatesPathOf(String fileName) {
138         if (fileName.contains("/") || fileName.contains("\\")) {
139             return fileName;
140         }
141         return certFilePath + FileSystems.getDefault().getSeparator() + fileName;
142     }
143
144 }