org.onap migration
[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  * ================================================================================
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.aai.util;
22
23
24 import java.io.FileInputStream;
25 import java.security.KeyManagementException;
26 import java.security.KeyStore;
27
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;
35
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;
41 /**
42  * The Class HttpsAuthClient.
43  */
44 public class HttpsAuthClient{
45         /** The logger. */
46         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsAuthClient.class);
47         
48         /**
49          * Gets the client.
50          *
51          * @param certFilePath the cert file path
52          * @return the client
53          * @throws KeyManagementException the key management exception
54          */
55         public static Client getClient(String certFilePath) throws KeyManagementException {
56
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);
60
61                 try {
62                         
63                     config.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, Boolean.TRUE );
64                         
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);
69                         
70                         boolean useClientCert = false;
71                         
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);
75                         
76                         String clientCert = SystemProperties.getProperty(org.onap.vid.aai.util.AAIProperties.AAI_USE_CLIENT_CERT);
77                         
78                         if (clientCert != null && 
79                                         SystemProperties.getProperty(org.onap.vid.aai.util.AAIProperties.AAI_USE_CLIENT_CERT).equalsIgnoreCase("true")) {
80                                 useClientCert = true;
81                         }
82                         
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) {
87                                 return true;
88                             }
89                         });
90         
91                         final SSLContext ctx = SSLContext.getInstance("TLS");
92                         
93                         KeyManagerFactory kmf = null;
94                         if (useClientCert) {
95                         
96                                 try {
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();
101                                         ks.load(fin, pwd);
102                                         kmf.init(ks, pwd);
103                                 } catch (Exception e) {
104                                         //System.out.println("Error setting up kmf: exiting");
105                                         logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up kmf: exiting");
106                                         e.printStackTrace();
107                                         return null;
108                                 }
109                                 ctx.init(kmf.getKeyManagers(), null, null);
110                         
111                                 return ClientBuilder.newBuilder()
112                                                 .sslContext(ctx)
113                                                 .hostnameVerifier(new HostnameVerifier() {
114                                                         @Override
115                                                         public boolean verify( String s, SSLSession sslSession ) {
116                                                                 return true;
117                                                         }
118                                                 }).withConfig(config)
119                                                 .build()
120                                                 .register(org.onap.vid.aai.util.CustomJacksonJaxBJsonProvider.class);
121                         } else { 
122                                 return ClientBuilder.newBuilder()
123                                                 .hostnameVerifier(new HostnameVerifier() {
124                                                         @Override
125                                                         public boolean verify( String s, SSLSession sslSession ) {
126                                                                 return true;
127                                                         }
128                                                 }).withConfig(config)
129                                                 .build()
130                                                 .register(org.onap.vid.aai.util.CustomJacksonJaxBJsonProvider.class);
131                         }
132                 } catch (Exception e) {
133                         logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
134                         //System.out.println("Error setting up config: exiting");
135                         e.printStackTrace();
136                         System.exit(1);
137                         return null;
138                 }
139         }
140 }