2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.aai.util;
\r
24 import java.io.FileInputStream;
\r
25 import java.security.KeyManagementException;
\r
26 import java.security.KeyStore;
\r
28 import javax.net.ssl.HostnameVerifier;
\r
29 import javax.net.ssl.HttpsURLConnection;
\r
30 import javax.net.ssl.KeyManagerFactory;
\r
31 import javax.net.ssl.SSLContext;
\r
32 import javax.net.ssl.SSLSession;
\r
33 import javax.ws.rs.client.Client;
\r
34 import javax.ws.rs.client.ClientBuilder;
\r
36 import org.eclipse.jetty.util.security.Password;
\r
37 import org.glassfish.jersey.client.ClientConfig;
\r
38 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
\r
39 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
40 import org.openecomp.portalsdk.core.util.SystemProperties;
\r
42 * The Class HttpsAuthClient.
\r
44 public class HttpsAuthClient{
\r
46 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsAuthClient.class);
\r
51 * @param certFilePath the cert file path
\r
52 * @return the client
\r
53 * @throws KeyManagementException the key management exception
\r
55 public static Client getClient(String certFilePath) throws KeyManagementException {
\r
57 ClientConfig config = new ClientConfig();
\r
58 //config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
\r
59 //config.getClasses().add(org.openecomp.aai.util.CustomJacksonJaxBJsonProvider.class);
\r
63 config.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, Boolean.TRUE );
\r
65 config.connectorProvider(new HttpUrlConnectorProvider().useSetMethodWorkaround());
\r
66 String truststore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_FILENAME);
\r
67 String truststore_password = SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_PASSWD_X);
\r
68 String decrypted_truststore_password = Password.deobfuscate(truststore_password);
\r
70 boolean useClientCert = false;
\r
72 String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
\r
73 String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
\r
74 String decrypted_keystore_password = Password.deobfuscate(keystore_password);
\r
76 String clientCert = SystemProperties.getProperty(AAIProperties.AAI_USE_CLIENT_CERT);
\r
78 if (clientCert != null &&
\r
79 SystemProperties.getProperty(AAIProperties.AAI_USE_CLIENT_CERT).equalsIgnoreCase("true")) {
\r
80 useClientCert = true;
\r
83 System.setProperty("javax.net.ssl.trustStore", truststore_path);
\r
84 System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
\r
85 HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
\r
86 public boolean verify(String string,SSLSession ssls) {
\r
91 final SSLContext ctx = SSLContext.getInstance("TLS");
\r
93 KeyManagerFactory kmf = null;
\r
94 if (useClientCert) {
\r
97 kmf = KeyManagerFactory.getInstance("SunX509");
\r
98 FileInputStream fin = new FileInputStream(keystore_path);
\r
99 KeyStore ks = KeyStore.getInstance("PKCS12");
\r
100 char[] pwd = decrypted_keystore_password.toCharArray();
\r
103 } catch (Exception e) {
\r
104 //System.out.println("Error setting up kmf: exiting");
\r
105 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up kmf: exiting");
\r
106 e.printStackTrace();
\r
109 ctx.init(kmf.getKeyManagers(), null, null);
\r
111 return ClientBuilder.newBuilder()
\r
113 .hostnameVerifier(new HostnameVerifier() {
\r
115 public boolean verify( String s, SSLSession sslSession ) {
\r
118 }).withConfig(config)
\r
120 .register(org.openecomp.aai.util.CustomJacksonJaxBJsonProvider.class);
\r
122 return ClientBuilder.newBuilder()
\r
123 .hostnameVerifier(new HostnameVerifier() {
\r
125 public boolean verify( String s, SSLSession sslSession ) {
\r
128 }).withConfig(config)
\r
130 .register(org.openecomp.aai.util.CustomJacksonJaxBJsonProvider.class);
\r
132 } catch (Exception e) {
\r
133 logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
\r
134 //System.out.println("Error setting up config: exiting");
\r
135 e.printStackTrace();
\r