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