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