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