c79d6c0521554f3441d119a6884799f57d9bd3a0
[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
81         
82                 SSLContext ctx = null;
83                 
84                 try {
85
86                         SimpleDateFormat dateFormat = DateUtil.getDateFormat();
87                         config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
88                         
89                         String truststorePath = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_FILENAME);
90                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " "
91                                 + "truststore_path=" +
92                                 truststorePath);
93                         String truststorePassword = SchedulerProperties.getProperty(SchedulerProperties.VID_TRUSTSTORE_PASSWD_X);
94                         
95                         
96                         String decryptedTruststorePassword = Password.deobfuscate(truststorePassword);
97                         //logger.debug(dateFormat.format(new Date()) + " " + methodName + " decrypted_truststore_password=" + decrypted_truststore_password);
98                         
99                         File tr = new File (truststorePath);
100                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " " + methodName + " absolute "
101                                 + "truststore path=" + tr.getAbsolutePath());
102                         
103                         //String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
104                         //String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
105                         //String decrypted_keystore_password = EncryptedPropValue.decryptTriple(keystore_password);
106                         
107                     System.setProperty("javax.net.ssl.trustStore", truststorePath);
108                     System.setProperty("javax.net.ssl.trustStorePassword", decryptedTruststorePassword);
109                         HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){
110                             public boolean verify(String string,SSLSession ssls) {
111                                 return true;
112                             }
113                         });
114         
115                         //May need to make the algorithm a parameter. MSO requires TLSv1.1      or TLSv1.2
116                         ctx = SSLContext.getInstance("TLSv1.2");
117                         
118                         /* 
119                         KeyManagerFactory kmf = null;
120                         try {
121                                 kmf = KeyManagerFactory.getInstance("SunX509");
122                                 FileInputStream fin = new FileInputStream(keystore_path);
123                                 KeyStore ks = KeyStore.getInstance("PKCS12");
124                                 char[] pwd = decrypted_keystore_password.toCharArray();
125                                 ks.load(fin, pwd);
126                                 kmf.init(ks, pwd);
127                         } catch (Exception e) {
128                                 System.out.println("Error setting up kmf: exiting");
129                                 e.printStackTrace();
130                                 System.exit(1);
131                         }
132
133                         ctx.init(kmf.getKeyManagers(), null, null);
134                         */
135                         ctx.init(null, null, null);
136
137                         return ClientBuilder.newBuilder()
138                                 .sslContext(ctx)
139                                 .hostnameVerifier(new HostnameVerifier() {
140                                         @Override
141                                         public boolean verify( String s, SSLSession sslSession ) {
142                                                 return true;
143                                         }
144                                 }).withConfig(config)
145                                 .build()
146                                 .register(CustomJacksonJaxBJsonProvider.class);
147                         
148         } catch (Exception e) {
149             logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up config: exiting", e);
150             return null;
151         }
152     }
153 }