Merge "Fix sql injection vulnerability"
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / scheduler / client / HttpsBasicClient.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  * 
39  */
40
41 package org.onap.portalapp.portal.scheduler.client;
42
43 import java.io.File;
44 import java.text.SimpleDateFormat;
45 import java.util.Date;
46
47 import javax.net.ssl.HostnameVerifier;
48 import javax.net.ssl.HttpsURLConnection;
49 import javax.net.ssl.SSLContext;
50 import javax.net.ssl.SSLSession;
51 import javax.ws.rs.client.Client;
52 import javax.ws.rs.client.ClientBuilder;
53
54 import org.eclipse.jetty.util.security.Password;
55 import org.glassfish.jersey.client.ClientConfig;
56 import org.glassfish.jersey.client.ClientProperties;
57 import org.onap.portalapp.portal.scheduler.SchedulerProperties;
58 import org.onap.portalapp.portal.scheduler.util.CustomJacksonJaxBJsonProvider;
59 import org.onap.portalapp.util.DateUtil;
60 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
61
62  /**
63   *  General SSL client using the VID tomcat keystore. It doesn't use client certificates.
64   */
65  
66 public class HttpsBasicClient{
67         
68         /** The logger. */
69         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsBasicClient.class);
70
71         /**
72          * Retrieve an SSL client.
73          *
74          * @return Client The SSL client
75          * @throws Exception the exception
76          */
77         public static Client getClient() throws Exception {
78                 String methodName = "getClient";
79                 ClientConfig config = new ClientConfig();
80                 //config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
81                 //config.getClasses().add(org.onap.aai.util.CustomJacksonJaxBJsonProvider.class);
82         
83                 SSLContext ctx = null;
84                 
85                 try {
86
87                         SimpleDateFormat dateFormat = DateUtil.getDateFormat();
88                         config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
89                         
90                         String truststore_path = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_FILENAME);
91                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " "
92                                 + "truststore_path=" +
93                                 truststore_path);
94                         String truststore_password = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_PASSWD_X);
95                         
96                         
97                         String decrypted_truststore_password = Password.deobfuscate(truststore_password);
98                         //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
99                         
100                         File tr = new File (truststore_path);
101                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute "
102                                 + "truststore path=" + tr.getAbsolutePath());
103                         
104                         //String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
105                         //String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
106                         //String decrypted_keystore_password = EncryptedPropValue.decryptTriple(keystore_password);
107                         
108                     System.setProperty("javax.net.ssl.trustStore", truststore_path);
109                     System.setProperty("javax.net.ssl.trustStorePassword", decrypted_truststore_password);
110                         HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
111                             public boolean verify(String string,SSLSession ssls) {
112                                 return true;
113                             }
114                         });
115         
116                         //May need to make the algorithm a parameter. MSO requires TLSv1.1      or TLSv1.2
117                         ctx = SSLContext.getInstance("TLSv1.2");
118                         
119                         /* 
120                         KeyManagerFactory kmf = null;
121                         try {
122                                 kmf = KeyManagerFactory.getInstance("SunX509");
123                                 FileInputStream fin = new FileInputStream(keystore_path);
124                                 KeyStore ks = KeyStore.getInstance("PKCS12");
125                                 char[] pwd = decrypted_keystore_password.toCharArray();
126                                 ks.load(fin, pwd);
127                                 kmf.init(ks, pwd);
128                         } catch (Exception e) {
129                                 System.out.println("Error setting up kmf: exiting");
130                                 e.printStackTrace();
131                                 System.exit(1);
132                         }
133
134                         ctx.init(kmf.getKeyManagers(), null, null);
135                         */
136                         ctx.init(null, null, null);
137                         //config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, 
138                         //                                                      new HTTPSProperties( , ctx));
139                         
140                         return ClientBuilder.newBuilder()
141                                 .sslContext(ctx)
142                                 .hostnameVerifier(new HostnameVerifier() {
143                                         @Override
144                                         public boolean verify( String s, SSLSession sslSession ) {
145                                                 return true;
146                                         }
147                                 }).withConfig(config)
148                                 .build()
149                                 .register(CustomJacksonJaxBJsonProvider.class);
150                         
151                 } catch (Exception e) {
152                         logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting");
153                         //System.out.println("Error setting up config: exiting");
154                         e.printStackTrace();
155                         return null;
156                 }
157                         
158                 //Client client = ClientBuilder.newClient(config);
159                 // uncomment this line to get more logging for the request/response
160                 // client.addFilter(new LoggingFilter(System.out));
161                 
162                 //return client;
163         }
164 }