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.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
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;
52 public class TestConnectivity {
54 public static void main(String[] args) {
56 System.out.println("Usage: ConnectivityTester <cadi_prop_files> [<AAF FQDN (i.e. aaf.dev.att.com)>]");
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>");
61 if (!args[0].contains(Config.CADI_PROP_FILES+'=')) {
62 args[0]=Config.CADI_PROP_FILES+'='+args[0];
65 PropAccess access = new PropAccess(args);
68 aaflocate = "https://" + args[1];
69 access.setProperty(Config.AAF_LOCATE_URL, "https://" + args[1]);
71 aaflocate = access.getProperty(Config.AAF_LOCATE_URL);
72 if (aaflocate==null) {
73 print(true,"Properties must contain ",Config.AAF_LOCATE_URL);
78 SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
80 List<SecuritySetter<HttpURLConnection>> lss = loadSetters(access,si);
82 print(true,"Test Connections driven by AAFLocator");
83 URI serviceURI = uri(access,"service");
85 for (URI uri : new URI[] {
88 uri(access,"introspect"),
94 Locator<URI> locator = new AAFLocator(si, uri);
96 connectTest(locator, uri);
97 } catch (Exception e) {
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);
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) {
116 basicAuthTest(new AAFLocator(si, serviceURI),ss);
120 System.out.println("No User/Password to test");
123 } catch (Exception e) {
124 e.printStackTrace(System.err);
126 print(true,"END OF TESTS");
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)) {
138 return new URI(aaf_locate_url + "/locate/" + aaf_root_ns + '.' + ms + ':' + aaf_api_version);
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);
145 ArrayList<SecuritySetter<HttpURLConnection>> lss = new ArrayList<>();
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");
153 access.log(Level.INFO, "BasicAuth Information found with ID",hbass.getID(),". BasicAuth tests will be performed.");
156 } catch (Exception e) {
157 access.log(Level.INFO, "BasicAuth Security Setter constructor threw exception: \"",e.getMessage(),"\". BasicAuth tests will not be performed");
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");
165 access.log(Level.INFO, "X509 (Client certificate) Information found with ID",hxss.getID(),". X509 tests will be performed.");
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");
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";
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.");
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");
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");
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.");
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");
202 private static void print(Boolean strong, String ... args) {
203 PrintStream out = System.out;
206 for (int i=0;i<70;++i) {
211 for (String s : args) {
212 out.print(strong?"== ":"------ ");
215 out.print(" ------");
220 for (int i=0;i<70;++i) {
227 private static void connectTest(Locator<URI> dl, URI locatorURI) throws LocatorException {
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");
235 socket = new Socket();
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());
246 } catch (IOException e1) {
247 System.out.printf("Could not close Socket Connection: %s\n",e1.getMessage());
254 private static void permTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss) {
256 URI uri = dl.get(dl.best());
258 System.out.print("No URI available using " + ss.getClass().getSimpleName());
259 System.out.println();
262 System.out.print("Resolved to: " + uri + " using " + ss.getClass().getSimpleName());
264 if (ss instanceof HRenewingTokenSS) {
265 System.out.println(" " + ((HRenewingTokenSS)ss).tokenURL());
267 System.out.println();
269 HClient client = new HClient(ss, uri, 3000);
270 client.setMethod("GET");
271 String user = ss.getID();
273 client.setPathInfo("/authz/perms/user/"+user);
275 Future<String> future = client.futureReadString();
276 if (future.get(7000)) {
277 System.out.println(future.body());
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");
283 System.out.println(future.code() + ":" + future.body());
286 } catch (CadiException | LocatorException | APIException e) {
292 private static void basicAuthTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss) {
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");
303 Future<String> future = client.futureReadString();
304 if (future.get(7000)) {
305 System.out.println("BasicAuth Validated");
307 System.out.println("Failure " + future.code() + ":" + future.body());
309 } catch (CadiException | LocatorException | APIException e) {