2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.aaf;
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;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
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;
51 public class TestConnectivity {
53 public static void main(String[] args) {
55 System.out.println("Usage: ConnectivityTester <cadi_prop_files> [<AAF FQDN (i.e. aaf.dev.att.com)>]");
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>");
60 if(!args[0].contains(Config.CADI_PROP_FILES+'=')) {
61 args[0]=Config.CADI_PROP_FILES+'='+args[0];
64 PropAccess access = new PropAccess(args);
67 aaflocate = "https://" + args[1];
68 access.setProperty(Config.AAF_LOCATE_URL, "https://" + args[1]);
70 aaflocate = access.getProperty(Config.AAF_LOCATE_URL);
72 print(true,"Properties must contain ",Config.AAF_LOCATE_URL);
77 SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
79 List<SecuritySetter<HttpURLConnection>> lss = loadSetters(access,si);
81 print(true,"Test Connections driven by AAFLocator");
82 URI serviceURI = new URI(aaflocate+"/locate/AAF_NS.service:2.0");
84 for(URI uri : new URI[] {
86 new URI(aaflocate+"/locate/AAF_NS.service:2.0"),
87 new URI(aaflocate+"/locate/AAF_NS.locate:2.0"),
88 new URI(aaflocate+"/locate/AAF_NS.token:2.0"),
89 new URI(aaflocate+"/locate/AAF_NS.certman:2.0"),
90 new URI(aaflocate+"/locate/AAF_NS.hello")
92 Locator<URI> locator = new AAFLocator(si, uri);
94 connectTest(locator, uri);
95 } catch (Exception e) {
102 print(true,"Test Service for Perms driven by AAFLocator");
103 Locator<URI> locator = new AAFLocator(si,serviceURI);
104 for(SecuritySetter<HttpURLConnection> ss : lss) {
105 permTest(locator,ss);
110 // print(true,"Test Proxy Access driven by AAFLocator");
111 // locator = new AAFLocator(si, new URI(aaflocate+"/AAF_NS.gw:2.0/proxy"));
112 // for(SecuritySetter<HttpURLConnection> ss : lss) {
113 // permTest(locator,ss);
117 print(true,"Test essential BasicAuth Service call, driven by AAFLocator");
118 for(SecuritySetter<HttpURLConnection> ss : lss) {
119 if(ss instanceof HBasicAuthSS) {
120 basicAuthTest(new AAFLocator(si, serviceURI),ss);
124 } catch(Exception e) {
125 e.printStackTrace(System.err);
127 print(true,"END OF TESTS");
132 private static List<SecuritySetter<HttpURLConnection>> loadSetters(PropAccess access, SecurityInfoC<HttpURLConnection> si) {
133 print(true,"Load Security Setters from Configuration Information");
134 String user = access.getProperty(Config.AAF_APPID);
136 ArrayList<SecuritySetter<HttpURLConnection>> lss = new ArrayList<>();
140 HBasicAuthSS hbass = new HBasicAuthSS(si,true);
141 if(hbass==null || hbass.getID()==null) {
142 access.log(Level.INFO, "BasicAuth Information is not available in configuration, BasicAuth tests will not be conducted... Continuing");
144 access.log(Level.INFO, "BasicAuth Information found with ID",hbass.getID(),". BasicAuth tests will be performed.");
147 } catch (Exception e) {
148 access.log(Level.INFO, "BasicAuth Security Setter constructor threw exception: \"",e.getMessage(),"\". BasicAuth tests will not be performed");
152 HX509SS hxss = new HX509SS(user,si);
153 if(hxss==null || hxss.getID()==null) {
154 access.log(Level.INFO, "X509 (Client certificate) Information is not available in configuration, X509 tests will not be conducted... Continuing");
156 access.log(Level.INFO, "X509 (Client certificate) Information found with ID",hxss.getID(),". X509 tests will be performed.");
159 } catch (Exception e) {
160 access.log(Level.INFO, "X509 (Client certificate) Security Setter constructor threw exception: \"",e.getMessage(),"\". X509 tests will not be performed");
163 String tokenURL = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL);
164 String locateURL=access.getProperty(Config.AAF_LOCATE_URL);
165 if(tokenURL==null || (tokenURL.contains("/locate/") && locateURL!=null)) {
166 tokenURL=locateURL+"/locate/AAF_NS.token:2.0/token";
170 HRenewingTokenSS hrtss = new HRenewingTokenSS(access, tokenURL);
171 access.log(Level.INFO, "AAF OAUTH2 Information found with ID",hrtss.getID(),". AAF OAUTH2 tests will be performed.");
173 } catch (Exception e) {
174 access.log(Level.INFO, "AAF OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". AAF OAUTH2 tests will not be conducted... Continuing");
177 tokenURL = access.getProperty(Config.AAF_ALT_OAUTH2_TOKEN_URL);
179 access.log(Level.INFO, "AAF Alternative OAUTH2 requires",Config.AAF_ALT_OAUTH2_TOKEN_URL, "OAuth2 tests to", tokenURL, "will not be conducted... Continuing");
182 HRenewingTokenSS hrtss = new HRenewingTokenSS(access, tokenURL);
183 access.log(Level.INFO, "ALT OAUTH2 Information found with ID",hrtss.getID(),". ALT OAUTH2 tests will be performed.");
185 } catch (Exception e) {
186 access.log(Level.INFO, "ALT OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". ALT OAuth2 tests to", tokenURL, " will not be conducted... Continuing");
193 private static void print(Boolean strong, String ... args) {
194 PrintStream out = System.out;
197 for(int i=0;i<70;++i) {
202 for(String s : args) {
203 out.print(strong?"== ":"------ ");
206 out.print(" ------");
211 for(int i=0;i<70;++i) {
218 private static void connectTest(Locator<URI> dl, URI locatorURI) throws LocatorException {
221 print(false,"TCP/IP Connect test to all Located Services for " + locatorURI.toString() );
222 for(Item li = dl.first();li!=null;li=dl.next(li)) {
223 if((uri = dl.get(li)) == null) {
224 System.out.println("Locator Item empty");
226 socket = new Socket();
229 socket.connect(new InetSocketAddress(uri.getHost(), uri.getPort()),3000);
230 System.out.printf("Can Connect a Socket to %s %d\n",uri.getHost(),uri.getPort());
231 } catch (IOException e) {
232 System.out.printf("Cannot Connect a Socket to %s %d: %s\n",uri.getHost(),uri.getPort(),e.getMessage());
237 } catch (IOException e1) {
238 System.out.printf("Could not close Socket Connection: %s\n",e1.getMessage());
245 private static void permTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss) {
247 URI uri = dl.get(dl.best());
249 System.out.print("No URI available using " + ss.getClass().getSimpleName());
250 System.out.println();
253 System.out.print("Resolved to: " + uri + " using " + ss.getClass().getSimpleName());
255 if(ss instanceof HRenewingTokenSS) {
256 System.out.println(" " + ((HRenewingTokenSS)ss).tokenURL());
258 System.out.println();
260 HClient client = new HClient(ss, uri, 3000);
261 client.setMethod("GET");
262 String user = ss.getID();
263 if(user.indexOf('@')<0) {
264 user+="@isam.att.com";
266 client.setPathInfo("/authz/perms/user/"+user);
268 Future<String> future = client.futureReadString();
269 if(future.get(7000)) {
270 System.out.println(future.body());
272 if(future.code()==401 && ss instanceof HX509SS) {
273 System.out.println(" Authentication denied with 401 for Certificate.\n\t"
274 + "This means Certificate isn't valid for this environment, and has attempted another method of Authentication");
276 System.out.println(future.code() + ":" + future.body());
279 } catch (CadiException | LocatorException | APIException e) {
285 private static void basicAuthTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss) {
287 URI uri = dl.get(dl.best());
288 System.out.println("Resolved to: " + uri);
289 HClient client = new HClient(ss, uri, 3000);
290 client.setMethod("GET");
291 client.setPathInfo("/authn/basicAuth");
292 client.addHeader("Accept", "text/plain");
296 Future<String> future = client.futureReadString();
297 if(future.get(7000)) {
298 System.out.println("BasicAuth Validated");
300 System.out.println("Failure " + future.code() + ":" + future.body());
302 } catch (CadiException | LocatorException | APIException e) {