08ee900fc514175fbb778f7b1436719274c5f9dc
[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.misc.env.APIException;
51
52 public class TestConnectivity {
53     
54     public static void main(String[] args) {
55         if (args.length<1) {
56             System.out.println("Usage: ConnectivityTester <cadi_prop_files> [<AAF FQDN (i.e. aaf.dev.att.com)>]");
57         } else {
58             print(true,"START OF CONNECTIVITY TESTS",new Date().toString(),System.getProperty("user.name"),
59                     "Note: All API Calls are /authz/perms/user/<AppID/Alias of the caller>");
60
61             if (!args[0].contains(Config.CADI_PROP_FILES+'=')) {
62                 args[0]=Config.CADI_PROP_FILES+'='+args[0];
63             }
64
65             PropAccess access = new PropAccess(args);
66             String aaflocate;
67             if (args.length>1) {
68                 aaflocate = "https://" + args[1];
69                 access.setProperty(Config.AAF_LOCATE_URL, "https://" + args[1]);
70             } else {
71                 aaflocate = access.getProperty(Config.AAF_LOCATE_URL);
72                 if (aaflocate==null) {
73                     print(true,"Properties must contain ",Config.AAF_LOCATE_URL);
74                 }
75             }
76             
77             try {
78                 SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
79                 
80                 List<SecuritySetter<HttpURLConnection>> lss = loadSetters(access,si);
81                 /////////
82                 print(true,"Test Connections driven by AAFLocator");
83                 URI serviceURI = uri(access,"service");
84
85                 for (URI uri : new URI[] {
86                         serviceURI,
87                         uri(access,"token"),
88                         uri(access,"introspect"),
89                         uri(access,"cm"),
90                         uri(access,"gui"),
91                         uri(access,"fs"),
92                         uri(access,"hello")
93                 }) {
94                     Locator<URI> locator = new AAFLocator(si, uri);
95                     try {
96                         connectTest(locator, uri);
97                     } catch (Exception e) {
98                         e.printStackTrace();
99                         System.err.flush();
100                     }
101                 }
102
103                 /////////
104                 print(true,"Test Service for Perms driven by AAFLocator");
105                 Locator<URI> locator = new AAFLocator(si,serviceURI);
106                 for (SecuritySetter<HttpURLConnection> ss : lss) {
107                     permTest(locator,ss);
108                 }
109
110                 //////////
111                 print(true,"Test essential BasicAuth Service call, driven by AAFLocator");
112                 boolean hasBath=false;
113                 for (SecuritySetter<HttpURLConnection> ss : lss) {
114                     if (ss instanceof HBasicAuthSS) {
115                         hasBath=true;
116                         basicAuthTest(new AAFLocator(si, serviceURI),ss);
117                     }
118                 }
119                 if(!hasBath) {
120                         System.out.println("No User/Password to test");
121                 }
122                 
123             } catch (Exception e) {
124                 e.printStackTrace(System.err);
125             } finally {
126                 print(true,"END OF TESTS");
127             }
128         }
129     }
130     
131     private static URI uri(PropAccess access, String ms) throws URISyntaxException {
132                 String aaf_root_ns = access.getProperty(Config.AAF_ROOT_NS,"AAF_NS");
133                 String aaf_api_version = access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
134                 String aaf_locate_url = access.getProperty(Config.AAF_LOCATE_URL,Defaults.AAF_LOCATE_CONST);
135                 if("cm".equals(ms) && "2.0".equals(aaf_api_version)) {
136                         ms = "certman";
137                 }
138                 return new URI(aaf_locate_url + "/locate/" + aaf_root_ns + '.' + ms + ':' + aaf_api_version);
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 = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL);
173         String locateURL=access.getProperty(Config.AAF_LOCATE_URL);
174         if (tokenURL==null || (tokenURL.contains("/locate/") && locateURL!=null)) {
175             tokenURL=Defaults.OAUTH2_TOKEN_URL+"/token";
176         }
177
178         try {
179             HRenewingTokenSS hrtss = new HRenewingTokenSS(access, tokenURL);
180             access.log(Level.INFO, "AAF OAUTH2 Information found with ID",hrtss.getID(),".  AAF OAUTH2 tests will be performed.");
181             lss.add(hrtss);
182         } catch (Exception e) {
183             access.log(Level.INFO, "AAF OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". AAF OAUTH2 tests will not be conducted... Continuing");
184         }
185         
186         tokenURL = access.getProperty(Config.AAF_ALT_OAUTH2_TOKEN_URL);
187         if (tokenURL==null) {
188             access.log(Level.INFO, "AAF Alternative OAUTH2 requires",Config.AAF_ALT_OAUTH2_TOKEN_URL, "OAuth2 tests to", tokenURL, "will not be conducted... Continuing");
189         } else {
190             try {
191                 HRenewingTokenSS hrtss = new HRenewingTokenSS(access, tokenURL);
192                 access.log(Level.INFO, "ALT OAUTH2 Information found with ID",hrtss.getID(),".  ALT OAUTH2 tests will be performed.");
193                 lss.add(hrtss);
194             } catch (Exception e) {
195                 access.log(Level.INFO, "ALT OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". ALT OAuth2 tests to", tokenURL, " will not be conducted... Continuing");
196             }
197         }
198         
199         return lss;
200     }
201
202     private static void print(Boolean strong, String ... args) {
203         PrintStream out = System.out;
204         out.println();
205         if (strong) {
206             for (int i=0;i<70;++i) {
207                 out.print('=');
208             }
209             out.println();
210         }
211         for (String s : args) {
212             out.print(strong?"==  ":"------ ");
213             out.print(s);
214             if (!strong) {
215                 out.print("  ------");
216             }
217             out.println();
218         }
219         if (strong) {
220             for (int i=0;i<70;++i) {
221                 out.print('=');
222             }
223         }
224         out.println();
225     }
226
227     private static void connectTest(Locator<URI> dl, URI locatorURI) throws LocatorException {
228         URI uri;
229         Socket socket;
230         print(false,"TCP/IP Connect test to all Located Services for "  + locatorURI.toString() );
231         for (Item li = dl.first();li!=null;li=dl.next(li)) {
232             if ((uri = dl.get(li)) == null) {
233                 System.out.println("Locator Item empty");
234             } else {
235                 socket = new Socket();
236                 try {
237                     try {
238                         socket.connect(new InetSocketAddress(uri.getHost(),  uri.getPort()),3000);
239                         System.out.printf("Can Connect a Socket to %s %d\n",uri.getHost(),uri.getPort());
240                     } catch (IOException e) {
241                         System.out.printf("Cannot Connect a Socket to  %s %d: %s\n",uri.getHost(),uri.getPort(),e.getMessage());
242                     }
243                 } finally {
244                     try {
245                         socket.close();
246                     } catch (IOException e1) {
247                         System.out.printf("Could not close Socket Connection: %s\n",e1.getMessage());
248                     }
249                 }
250             }
251         }
252     }
253
254     private static void permTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss)  {
255         try {
256             URI uri = dl.get(dl.best());
257             if (uri==null) {
258                 System.out.print("No URI available using " + ss.getClass().getSimpleName());
259                 System.out.println();
260                 return;
261             } else {
262                 System.out.print("Resolved to: " + uri + " using " + ss.getClass().getSimpleName());
263             }
264             if (ss instanceof HRenewingTokenSS) {
265                 System.out.println(" " + ((HRenewingTokenSS)ss).tokenURL());
266             } else {
267                 System.out.println();
268             }
269             HClient client = new HClient(ss, uri, 3000);
270             client.setMethod("GET");
271             String user = ss.getID();
272
273             client.setPathInfo("/authz/perms/user/"+user);
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 }