1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cadi.aaf;
\r
25 import java.io.IOException;
\r
26 import java.io.PrintStream;
\r
27 import java.lang.reflect.Field;
\r
28 import java.net.HttpURLConnection;
\r
29 import java.net.InetAddress;
\r
30 import java.net.InetSocketAddress;
\r
31 import java.net.Socket;
\r
32 import java.net.URI;
\r
33 import java.net.UnknownHostException;
\r
34 import java.util.Date;
\r
36 import com.att.aft.dme2.api.DME2Client;
\r
37 import com.att.aft.dme2.api.DME2Manager;
\r
38 import com.att.cadi.CadiException;
\r
39 import com.att.cadi.Locator;
\r
40 import com.att.cadi.Locator.Item;
\r
41 import com.att.cadi.LocatorException;
\r
42 import com.att.cadi.Lur;
\r
43 import com.att.cadi.PropAccess;
\r
44 import com.att.cadi.SecuritySetter;
\r
45 import com.att.cadi.TrustChecker;
\r
46 import com.att.cadi.aaf.v2_0.AAFCon;
\r
47 import com.att.cadi.aaf.v2_0.AAFConDME2;
\r
48 import com.att.cadi.client.Future;
\r
49 import com.att.cadi.config.Config;
\r
50 import com.att.cadi.config.SecurityInfoC;
\r
51 import com.att.cadi.http.HBasicAuthSS;
\r
52 import com.att.cadi.http.HClient;
\r
53 import com.att.cadi.http.HX509SS;
\r
54 import com.att.cadi.locator.DME2Locator;
\r
55 import com.att.cadi.locator.PropertyLocator;
\r
56 import com.att.inno.env.APIException;
\r
57 import com.att.rosetta.env.RosettaDF;
\r
58 import com.att.rosetta.env.RosettaEnv;
\r
60 import aaf.v2_0.Perms;
\r
62 public class ConnectivityTest {
\r
63 private static final String PROD = "PROD";
\r
64 private static final String SRV_RESOLVE = "https://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=2.0/envContext=%s/routeOffer=%s";
\r
65 private static final String GW_RESOLVE = "https://DME2RESOLVE/service=com.att.authz.authz-gw/version=2.0/envContext=%s/routeOffer=%s";
\r
67 public static void main(String[] args) {
\r
69 System.out.println("Usage: ConnectivityTester <TEST|IST|PROD> <cadi_prop_files>");
\r
71 print(true,"START OF CONNECTIVITY TESTS",new Date().toString(),System.getProperty("user.name"),
\r
72 "Note: All API Calls are /authz/perms/user/<MechID/Alias of the caller>");
\r
74 final String aaf_env = args[0];
\r
75 args[1]=Config.CADI_PROP_FILES+'='+args[1];
\r
77 PropAccess pa = new PropAccess(args);
\r
78 String user = pa.getProperty(Config.AAF_MECHID);
\r
79 String pass = pa.getProperty(Config.AAF_MECHPASS);
\r
80 String alias = pa.getProperty(Config.CADI_ALIAS);
\r
84 RosettaEnv env = new RosettaEnv(pa.getProperties());
\r
87 RosettaDF<Perms> permsDF = env.newDataFactory(Perms.class);
\r
88 SecurityInfoC<HttpURLConnection> si = new SecurityInfoC<HttpURLConnection>(pa);
\r
89 HBasicAuthSS hbass = new HBasicAuthSS(pa,si);
\r
90 if(hbass.getID()==null) {
\r
91 hbass=null; // not configured with ID.
\r
97 hxss = new HX509SS(user,si);
\r
98 } catch(Exception e) {
\r
99 e.printStackTrace();
\r
100 print(false,"Continuing");
\r
103 if(user==null || (pass==null && alias==null)) {
\r
104 System.out.printf("ERROR: DME2 Client cannot be tested with out %s and %s properties"
\r
105 , Config.AAF_MECHID, Config.AAF_MECHPASS );
\r
107 if("TEST".equals(aaf_env) || "IST".equals(aaf_env) || "PROD".equals(aaf_env)) {
\r
108 DME2Manager dm = null;
\r
109 print(false,"Attempt DME2Manager Load");
\r
110 if(Class.forName("com.att.aft.dme2.api.DME2Manager")==null) {
\r
111 print(true,"DME2 jar is not available: Skipping DME2 Tests");
\r
112 } else { // DME2 Client Tests
\r
113 pass=pa.decrypt(pass,false);
\r
114 // Out of the box DME2
\r
115 aafurl = String.format(SRV_RESOLVE, aaf_env, PROD.equals(aaf_env)?"DEFAULT":"BAU_SE");
\r
116 print(true,"TEST CADI Config",aafurl);
\r
117 aafcon = testConfig(pa,aafurl);
\r
118 test(aafcon,permsDF,user);
\r
120 print(true,"Find and TEST Connections with DME2Locator",aafurl);
\r
121 DME2Locator dl = new DME2Locator(pa,dm,aafurl);
\r
124 dm = new DME2Manager("DME2Manager",pa.getProperties());
\r
126 dme2RawTest(dm, aafurl,user,pass);
\r
128 // URL specific Variant
\r
129 if((aafurl = specificDME2URL(dl, aafurl))!=null) {
\r
130 print(true,"TEST Specific DME2 CADI Config",aafurl);
\r
131 aafcon = testConfig(pa,aafurl);
\r
132 test(aafcon,permsDF,user);
\r
134 dme2RawTest(dm,aafurl,user,pass);
\r
137 print(true,"CADI Direct AAFConDME2 Object Usage",aafurl);
\r
139 pa.setProperty(Config.AAF_URL,aafurl);
\r
140 aafcon = new AAFConDME2(pa);
\r
141 test(aafcon,permsDF,user);
\r
142 } catch(Throwable t) {
\r
143 t.printStackTrace();
\r
146 // find a direct client to code a Direct HTTP with
\r
149 print(true,"CADI Http DME2Locator Client Coding Methodology BasicAuth",aafurl);
\r
150 hClientTest(dl,hbass,user);
\r
153 print(true,"CADI Http DME2Locator Client Coding Methodology X509",aafurl);
\r
154 hClientTest(dl,hxss,user);
\r
157 // ##### PROXY CHECKS
\r
158 aafurl = String.format(GW_RESOLVE, aaf_env, PROD.equals(aaf_env)?"DEFAULT":"BAU_SE");
\r
159 print(true,"TEST PROXY DME2 CADI Config",aafurl);
\r
160 aafcon = testConfig(pa,aafurl);
\r
161 test(aafcon,permsDF,user);
\r
164 dme2RawTest(dm, aafurl,user,pass);
\r
166 // URL specific Variant
\r
167 dl = new DME2Locator(pa,dm,aafurl);
\r
168 if((aafurl = specificDME2URL(dl, aafurl))!=null) {
\r
169 print(true,"TEST PROXY Specific DME2 CADI Config",aafurl);
\r
170 aafcon = testConfig(pa,aafurl);
\r
171 test(aafcon,permsDF,user);
\r
173 dme2RawTest(dm,aafurl,user,pass);
\r
179 PropertyLocator pl = servicePropLocator(aaf_env);
\r
181 URI uri = pl.get(pl.best());
\r
183 aafurl = uri.toString();
\r
184 print(true,"TEST Service PropertyLocator based Config",aafurl);
\r
185 aafcon = testConfig(pa,aafurl);
\r
186 test(aafcon,permsDF,user);
\r
189 print(true,"CADI Service Http PropLocator Client Coding Methodology Basic Auth",aafurl);
\r
190 hClientTest(pl,hbass, user);
\r
191 print(true,"CADI Service Http PropLocator Client Coding Methodology /authn/basicAuth",aafurl);
\r
192 basicAuthTest(pl,hbass);
\r
195 print(true,"CADI Service Http PropLocator Client Coding Methodology X509",aafurl);
\r
196 hClientTest(pl,hxss, user);
\r
199 pl = proxyPropLocator(aaf_env);
\r
201 uri = pl.get(pl.best());
\r
203 aafurl = uri.toString();
\r
204 print(true,"TEST PROXY PropertyLocator based Config",aafurl);
\r
205 aafcon = testConfig(pa,aafurl);
\r
206 test(aafcon,permsDF,user);
\r
209 print(true,"CADI PROXY Http PropLocator Client Coding Methodology Basic Auth",aafurl);
\r
210 hClientTest(pl,hbass, user);
\r
211 print(true,"CADI PROXY Http PropLocator Client Coding Methodology /proxy/authn/basicAuth",aafurl);
\r
212 basicAuthTest(pl,hbass);
\r
215 print(true,"CADI PROXY Http PropLocator Client Coding Methodology X509",aafurl);
\r
216 hClientTest(pl,hxss, user);
\r
221 } catch(Exception e) {
\r
222 e.printStackTrace(System.err);
\r
224 print(true,"END OF TESTS");
\r
229 private static void print(Boolean strong, String ... args) {
\r
230 PrintStream out = System.out;
\r
233 for(int i=0;i<70;++i) {
\r
238 for(String s : args) {
\r
239 out.print(strong?"== ":"------ ");
\r
242 out.print(" ------");
\r
247 for(int i=0;i<70;++i) {
\r
254 private static void test(AAFCon<?> aafcon,RosettaDF<Perms> permsDF,String user) {
\r
256 print(false,"AAFCon is null");
\r
259 print(false,"Calling with AAFCon");
\r
260 Future<Perms> fp = aafcon.client("2.0").read("/authz/perms/user/"+user, Perms.class, permsDF);
\r
262 System.out.printf("Found %d Permission(s)\n",fp.value.getPerm().size());
\r
264 System.out.printf("Error: %d %s\n",fp.code(),fp.body());
\r
266 } catch (Throwable t) {
\r
267 t.printStackTrace();
\r
272 private static AAFCon<?> testConfig(PropAccess pa, String aafurl) {
\r
274 pa.setProperty(Config.AAF_URL, aafurl);
\r
275 Lur lur = Config.configLur(pa);
\r
276 Config.configHttpTaf(pa, TrustChecker.NOTRUST, null, lur);
\r
280 f = lur.getClass().getField("aaf");
\r
281 return (AAFCon<?>)f.get(lur);
\r
282 } catch (Exception nsfe) {
\r
286 } catch(Throwable t) {
\r
287 t.printStackTrace();
\r
292 private static String specificDME2URL(Locator<URI> loc, String aafurl) throws LocatorException {
\r
293 Item item = loc.best();
\r
295 URI uri = loc.get(item);
\r
296 return aafurl.replace("DME2RESOLVE", String.format("%s:%d",uri.getHost(),uri.getPort()));
\r
301 private static void connectTest(Locator<URI> dl) throws LocatorException {
\r
304 print(false,"TCP/IP Connect test to all Located Services");
\r
305 for(Item li = dl.first();li!=null;li=dl.next(li)) {
\r
306 if((uri = dl.get(li)) == null) {
\r
307 System.out.println("Locator Item empty");
\r
310 socket = new Socket();
\r
311 socket.connect(new InetSocketAddress(uri.getHost(), uri.getPort()),3000);
\r
312 System.out.printf("Can Connect a Socket to %s %d\n",uri.getHost(),uri.getPort());
\r
315 } catch (IOException e1) {
\r
316 System.out.printf("Could not close Socket Connection: %s\n",e1.getMessage());
\r
318 } catch (IOException e) {
\r
319 System.out.printf("Cannot Connect a Socket to %s %d: %s\n",uri.getHost(),uri.getPort(),e.getMessage());
\r
325 private static PropertyLocator servicePropLocator(String env) throws LocatorException {
\r
330 purls="https://"+InetAddress.getLocalHost().getHostName()+":8100";
\r
331 } catch (UnknownHostException e) {
\r
332 throw new LocatorException(e);
\r
336 purls="https://aaf.dev.att.com:8100,https://aaf.dev.att.com:8101";
\r
339 purls="https://aaftest.test.att.com:8100,https://aaftest.test.att.com:8101";
\r
342 purls="https://aafist.test.att.com:8100,https://aafist.test.att.com:8101";
\r
345 purls="https://aaf.it.att.com:8100,https://aaf.it.att.com:8101";
\r
348 if(env.contains(".")) {
\r
349 purls="https://"+env+":8100";
\r
351 throw new LocatorException(ConnectivityTest.class.getSimpleName() + ": unknown Env");
\r
354 System.out.printf("Creating a PropertyLocator for %s\n",purls);
\r
355 return new PropertyLocator(purls);
\r
358 private static PropertyLocator proxyPropLocator(String env) throws LocatorException {
\r
363 purls="https://"+InetAddress.getLocalHost().getHostAddress()+":8100";
\r
364 } catch (UnknownHostException e) {
\r
365 throw new LocatorException(e);
\r
369 purls="https://aaf.dev.att.com:8095/proxy";
\r
372 purls="https://aaftest.test.att.com:8095/proxy";
\r
375 purls="https://aafist.test.att.com:8095/proxy";
\r
378 purls="https://aaf.it.att.com:8095/proxy";
\r
381 if(env.contains(".")) {
\r
382 purls="https://"+env+":8095/proxy";
\r
384 throw new LocatorException(ConnectivityTest.class.getSimpleName() + ": unknown Env");
\r
388 System.out.printf("Creating a PropertyLocator for %s\n",purls);
\r
389 return new PropertyLocator(purls);
\r
395 private static void hClientTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss, String user) {
\r
397 URI uri = dl.get(dl.best());
\r
398 System.out.println("Resolved to: " + uri);
\r
399 HClient client = new HClient(ss, uri, 3000);
\r
400 client.setMethod("GET");
\r
401 client.setPathInfo("/authz/perms/user/"+user);
\r
403 Future<String> future = client.futureReadString();
\r
404 if(future.get(7000)) {
\r
405 System.out.println(future.body());
\r
407 System.out.println(future.code() + ":" + future.body());
\r
409 } catch (CadiException | LocatorException | APIException e) {
\r
410 e.printStackTrace();
\r
415 private static void basicAuthTest(PropertyLocator dl, SecuritySetter<HttpURLConnection> ss) {
\r
417 URI uri = dl.get(dl.best());
\r
418 System.out.println("Resolved to: " + uri);
\r
419 HClient client = new HClient(ss, uri, 3000);
\r
420 client.setMethod("GET");
\r
421 client.setPathInfo("/authn/basicAuth");
\r
422 client.addHeader("Accept", "text/plain");
\r
426 Future<String> future = client.futureReadString();
\r
427 if(future.get(7000)) {
\r
428 System.out.println("BasicAuth Validated");
\r
430 System.out.println("Failure " + future.code() + ":" + future.body());
\r
432 } catch (CadiException | LocatorException | APIException e) {
\r
433 e.printStackTrace();
\r
437 // Regular DME2Client Coding Style
\r
438 private static void dme2RawTest(DME2Manager dm, String aafurl, String user, String pass) {
\r
443 URI uri = new URI(aafurl);
\r
444 print(true,"DME2 Direct Client Coding Methodology",uri.toString());
\r
445 DME2Client client = dm.newClient( uri, 3000);
\r
446 client.setMethod("GET"); // FYI, DME2 defaults to "POST"
\r
447 client.setContext("/authz/perms/user/"+user); // DME2 direct requires separate setting of Context from URI
\r
448 if(pass!=null) { // rely on Cert if no pass
\r
449 client.setCredentials(user, pass);
\r
451 client.setPayload(""); // DME2 will not send without something
\r
452 String resp = client.sendAndWait(7000);
\r
453 System.out.println(resp);
\r
454 } catch(Throwable e) {
\r
455 e.printStackTrace();
\r