Refine Helm Client Charts
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / TestConnectivity.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.cadi.aaf;
23
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.net.HttpURLConnection;
27 import java.net.InetSocketAddress;
28 import java.net.Socket;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
34
35 import org.onap.aaf.cadi.Access.Level;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.Locator;
38 import org.onap.aaf.cadi.Locator.Item;
39 import org.onap.aaf.cadi.LocatorException;
40 import org.onap.aaf.cadi.PropAccess;
41 import org.onap.aaf.cadi.SecuritySetter;
42 import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
43 import org.onap.aaf.cadi.client.Future;
44 import org.onap.aaf.cadi.config.Config;
45 import org.onap.aaf.cadi.config.SecurityInfoC;
46 import org.onap.aaf.cadi.http.HBasicAuthSS;
47 import org.onap.aaf.cadi.http.HClient;
48 import org.onap.aaf.cadi.http.HX509SS;
49 import org.onap.aaf.cadi.locator.SingleEndpointLocator;
50 import org.onap.aaf.cadi.oauth.HRenewingTokenSS;
51 import org.onap.aaf.cadi.util.FixURIinfo;
52 import org.onap.aaf.misc.env.APIException;
53
54 public class TestConnectivity {
55     
56     public static void main(String[] args) {
57         if (args.length<1) {
58             System.out.println("Usage: ConnectivityTester <cadi_prop_files> [<AAF FQDN (i.e. aaf.dev.att.com)>]");
59         } else {
60             print(true,"START OF CONNECTIVITY TESTS",new Date().toString(),System.getProperty("user.name"),
61                     "Note: All API Calls are /authz/perms/user/<AppID/Alias of the caller>");
62
63             if (!args[0].contains(Config.CADI_PROP_FILES+'=')) {
64                 args[0]=Config.CADI_PROP_FILES+'='+args[0];
65             }
66
67             PropAccess access = new PropAccess(args);
68             String aaflocate;
69             if (args.length>1) {
70                 aaflocate = "https://" + args[1];
71                 access.setProperty(Config.AAF_LOCATE_URL, "https://" + args[1]);
72             } else {
73                 aaflocate = access.getProperty(Config.AAF_LOCATE_URL);
74                 if (aaflocate==null) {
75                     print(true,"Properties must contain ",Config.AAF_LOCATE_URL);
76                 }
77             }
78             
79             try {
80                 SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
81                 
82                 List<SecuritySetter<HttpURLConnection>> lss = loadSetters(access,si);
83                 /////////
84                 String directAAFURL = access.getProperty(Config.AAF_URL,null);
85                 if(directAAFURL!=null && !directAAFURL.contains("AAF_LOCATE")) {
86                     print(true,"Test Connections by non-located aaf_url");
87                     Locator<URI> locator = new SingleEndpointLocator(directAAFURL);
88                     connectTest(locator,new URI(directAAFURL));
89                     
90                     SecuritySetter<HttpURLConnection> ss = si.defSS;
91                     permTest(locator,ss);
92                 } else {
93                         /////////
94                         print(true,"Test Connections driven by AAFLocator");
95                         URI serviceURI = uri(access,"service");
96         
97                         for (URI uri : new URI[] {
98                                 serviceURI,
99                                 uri(access,"token"),
100                                 uri(access,"introspect"),
101                                 uri(access,"cm"),
102                                 uri(access,"gui"),
103                                 uri(access,"fs"),
104                                 uri(access,"hello")
105                         }) {
106                             Locator<URI> locator = new AAFLocator(si, uri);
107                             try {
108                                 connectTest(locator, uri);
109                             } catch (Exception e) {
110                                 e.printStackTrace();
111                                 System.err.flush();
112                             }
113                         }
114
115                         /////////
116                         print(true,"Test Service for Perms driven by AAFLocator");
117                         Locator<URI> locator = new AAFLocator(si,serviceURI);
118                         for (SecuritySetter<HttpURLConnection> ss : lss) {
119                             permTest(locator,ss);
120                         }
121
122                         //////////
123                         print(true,"Test essential BasicAuth Service call, driven by AAFLocator");
124                         boolean hasBath=false;
125                         for (SecuritySetter<HttpURLConnection> ss : lss) {
126                             if (ss instanceof HBasicAuthSS) {
127                                 hasBath=true;
128                                 basicAuthTest(new AAFLocator(si, serviceURI),ss);
129                             }
130                         }
131                         if(!hasBath) {
132                                 System.out.println("No User/Password to test");
133                         }
134                 }
135                 
136             } catch (Exception e) {
137                 e.printStackTrace(System.err);
138             } finally {
139                 print(true,"END OF TESTS");
140             }
141         }
142     }
143     
144     private static URI uri(PropAccess access, String ms) throws URISyntaxException {
145                 String aaf_root_ns = access.getProperty(Config.AAF_ROOT_NS,Config.AAF_ROOT_NS_DEF);
146                 String aaf_api_version = access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
147                 String aaf_locate_url = access.getProperty(Config.AAF_LOCATE_URL,Defaults.AAF_LOCATE_CONST);
148                 String aaf_container = access.getProperty(Config.AAF_LOCATOR_CONTAINER,null);
149                 if(aaf_container!=null) {
150                         String ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+'.'+aaf_container,null);
151                         if(ns==null) {
152                                 ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,null);
153                         }
154                         if(ns!=null) {
155                                 aaf_root_ns=ns + '.' + aaf_root_ns;
156                         }
157                 }
158                 if("cm".equals(ms) && "2.0".equals(aaf_api_version)) {
159                         ms = "certman";
160                 }
161                 return new URI(aaf_locate_url + "/locate/" + aaf_root_ns + '.' + ms + ':' + aaf_api_version);
162         }
163
164         private static List<SecuritySetter<HttpURLConnection>> loadSetters(PropAccess access, SecurityInfoC<HttpURLConnection> si)  {
165         print(true,"Load Security Setters from Configuration Information");
166         String user = access.getProperty(Config.AAF_APPID);
167
168         ArrayList<SecuritySetter<HttpURLConnection>> lss = new ArrayList<>();
169         
170
171         try {
172             HBasicAuthSS hbass = new HBasicAuthSS(si,true);
173             if (hbass==null || hbass.getID()==null) {
174                 access.log(Level.INFO, "BasicAuth Information is not available in configuration, BasicAuth tests will not be conducted... Continuing");
175             } else {
176                 access.log(Level.INFO, "BasicAuth Information found with ID",hbass.getID(),".  BasicAuth tests will be performed.");
177                 lss.add(hbass);
178             }
179         } catch (Exception e) {
180             access.log(Level.INFO, "BasicAuth Security Setter constructor threw exception: \"",e.getMessage(),"\". BasicAuth tests will not be performed");
181         }
182
183         try {
184             HX509SS hxss = new HX509SS(user,si);
185             if (hxss==null || hxss.getID()==null) {
186                 access.log(Level.INFO, "X509 (Client certificate) Information is not available in configuration, X509 tests will not be conducted... Continuing");
187             } else {
188                 access.log(Level.INFO, "X509 (Client certificate) Information found with ID",hxss.getID(),".  X509 tests will be performed.");
189                 lss.add(hxss);
190             }
191         } catch (Exception e) {
192             access.log(Level.INFO, "X509 (Client certificate) Security Setter constructor threw exception: \"",e.getMessage(),"\". X509 tests will not be performed");
193         }
194
195         String tokenURL = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL);
196         String locateURL=access.getProperty(Config.AAF_LOCATE_URL);
197         if (tokenURL==null || (tokenURL.contains("/locate/") && locateURL!=null)) {
198             tokenURL=Config.OAUTH2_TOKEN_URL_DEF;
199         }
200         
201
202         try {
203             HRenewingTokenSS hrtss = new HRenewingTokenSS(access, tokenURL);
204             access.log(Level.INFO, "AAF OAUTH2 Information found with ID",hrtss.getID(),".  AAF OAUTH2 tests will be performed.");
205             lss.add(hrtss);
206         } catch (Exception e) {
207             access.log(Level.INFO, "AAF OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". AAF OAUTH2 tests will not be conducted... Continuing");
208         }
209         
210         tokenURL = access.getProperty(Config.AAF_ALT_OAUTH2_TOKEN_URL);
211         if (tokenURL==null) {
212             access.log(Level.INFO, "AAF Alternative OAUTH2 requires",Config.AAF_ALT_OAUTH2_TOKEN_URL, "OAuth2 tests to", tokenURL, "will not be conducted... Continuing");
213         } else {
214             try {
215                 HRenewingTokenSS hrtss = new HRenewingTokenSS(access, tokenURL);
216                 access.log(Level.INFO, "ALT OAUTH2 Information found with ID",hrtss.getID(),".  ALT OAUTH2 tests will be performed.");
217                 lss.add(hrtss);
218             } catch (Exception e) {
219                 access.log(Level.INFO, "ALT OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". ALT OAuth2 tests to", tokenURL, " will not be conducted... Continuing");
220             }
221         }
222         
223         return lss;
224     }
225
226     private static void print(Boolean strong, String ... args) {
227         PrintStream out = System.out;
228         out.println();
229         if (strong) {
230             for (int i=0;i<70;++i) {
231                 out.print('=');
232             }
233             out.println();
234         }
235         for (String s : args) {
236             out.print(strong?"==  ":"------ ");
237             out.print(s);
238             if (!strong) {
239                 out.print("  ------");
240             }
241             out.println();
242         }
243         if (strong) {
244             for (int i=0;i<70;++i) {
245                 out.print('=');
246             }
247         }
248         out.println();
249     }
250
251     private static void connectTest(Locator<URI> dl, URI locatorURI) throws LocatorException {
252         URI uri;
253         Socket socket;
254         print(false,"TCP/IP Connect test to all Located Services for "  + locatorURI.toString() );
255         for (Item li = dl.first();li!=null;li=dl.next(li)) {
256             if ((uri = dl.get(li)) == null) {
257                 System.out.println("Locator Item empty");
258             } else {
259                 socket = new Socket();
260                 try {
261                         FixURIinfo fui = new FixURIinfo(uri);
262                     try {
263                         socket.connect(new InetSocketAddress(fui.getHost(),  fui.getPort()),3000);
264                         System.out.printf("Can Connect a Socket to %s %d\n",fui.getHost(),fui.getPort());
265                     } catch (IOException e) {
266                         System.out.printf("Cannot Connect a Socket to  %s %d: %s\n",fui.getHost(),fui.getPort(),e.getMessage());
267                     }
268                 } finally {
269                     try {
270                         socket.close();
271                     } catch (IOException e1) {
272                         System.out.printf("Could not close Socket Connection: %s\n",e1.getMessage());
273                     }
274                 }
275             }
276         }
277     }
278
279     private static void permTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss)  {
280         try {
281             URI uri = dl.get(dl.best());
282             if (uri==null) {
283                 System.out.print("No URI available using " + ss.getClass().getSimpleName());
284                 System.out.println();
285                 return;
286             } else {
287                 System.out.print("Resolved to: " + uri + " using " + ss.getClass().getSimpleName());
288             }
289             if (ss instanceof HRenewingTokenSS) {
290                 System.out.println(" " + ((HRenewingTokenSS)ss).tokenURL());
291             } else {
292                 System.out.println();
293             }
294             HClient client = new HClient(ss, uri, 3000);
295             client.setMethod("GET");
296             String user = ss.getID();
297
298             String pathInfo = "/authz/perms/user/"+user;
299             client.setPathInfo(pathInfo);
300             System.out.println(pathInfo);
301             
302             client.send();
303             Future<String> future = client.futureReadString();
304             if (future.get(7000)) {
305                 System.out.println(future.body());    
306             } else {
307                 if (future.code()==401 && ss instanceof HX509SS) {
308                     System.out.println("  Authentication denied with 401 for Certificate.\n\t"
309                             + "This means Certificate isn't valid for this environment, and has attempted another method of Authentication");
310                 } else {
311                     System.out.println(future.code() + ":" + future.body());
312                 }
313             }
314         } catch (CadiException | LocatorException | APIException e) {
315             e.printStackTrace();
316         }
317     }
318
319
320     private static void basicAuthTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss) {
321         try {
322             URI uri = dl.get(dl.best());
323             System.out.println("Resolved to: " + uri);
324             HClient client = new HClient(ss, uri, 3000);
325             client.setMethod("GET");
326             client.setPathInfo("/authn/basicAuth");
327             client.addHeader("Accept", "text/plain");
328             client.send();
329     
330         
331             Future<String> future = client.futureReadString();
332             if (future.get(7000)) {
333                 System.out.println("BasicAuth Validated");    
334             } else {
335                 System.out.println("Failure " + future.code() + ":" + future.body());
336             }
337         } catch (CadiException | LocatorException | APIException e) {
338             e.printStackTrace();
339         }
340     }
341 }