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