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