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