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 org.onap.aaf.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 org.onap.aaf.cadi.CadiException;
\r
37 import org.onap.aaf.cadi.Locator;
\r
38 import org.onap.aaf.cadi.LocatorException;
\r
39 import org.onap.aaf.cadi.Lur;
\r
40 import org.onap.aaf.cadi.PropAccess;
\r
41 import org.onap.aaf.cadi.SecuritySetter;
\r
42 import org.onap.aaf.cadi.TrustChecker;
\r
43 import org.onap.aaf.cadi.Locator.Item;
\r
44 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
\r
45 import org.onap.aaf.cadi.aaf.v2_0.AAFConDME2;
\r
46 import org.onap.aaf.cadi.client.Future;
\r
47 import org.onap.aaf.cadi.config.Config;
\r
48 import org.onap.aaf.cadi.config.SecurityInfoC;
\r
49 import org.onap.aaf.cadi.http.HBasicAuthSS;
\r
50 import org.onap.aaf.cadi.http.HClient;
\r
51 import org.onap.aaf.cadi.http.HX509SS;
\r
52 import org.onap.aaf.cadi.locator.DME2Locator;
\r
53 import org.onap.aaf.cadi.locator.PropertyLocator;
\r
55 import com.att.aft.dme2.api.DME2Client;
\r
56 import com.att.aft.dme2.api.DME2Manager;
\r
57 import org.onap.aaf.inno.env.APIException;
\r
58 import org.onap.aaf.rosetta.env.RosettaDF;
\r
59 import org.onap.aaf.rosetta.env.RosettaEnv;
\r
61 import aaf.v2_0.Perms;
\r
63 public class ConnectivityTest {
\r
64 private static final String PROD = "PROD";
\r
65 private static final String SRV_RESOLVE = "https://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=2.0/envContext=%s/routeOffer=%s";
\r
66 private static final String GW_RESOLVE = "https://DME2RESOLVE/service=com.att.authz.authz-gw/version=2.0/envContext=%s/routeOffer=%s";
\r
68 public static void main(String[] args) {
\r
70 System.out.println("Usage: ConnectivityTester <TEST|IST|PROD> <cadi_prop_files>");
\r
72 print(true,"START OF CONNECTIVITY TESTS",new Date().toString(),System.getProperty("user.name"),
\r
73 "Note: All API Calls are /authz/perms/user/<MechID/Alias of the caller>");
\r
75 final String aaf_env = args[0];
\r
76 args[1]=Config.CADI_PROP_FILES+'='+args[1];
\r
78 PropAccess pa = new PropAccess(args);
\r
79 String user = pa.getProperty(Config.AAF_MECHID);
\r
80 String pass = pa.getProperty(Config.AAF_MECHPASS);
\r
81 String alias = pa.getProperty(Config.CADI_ALIAS);
\r
85 RosettaEnv env = new RosettaEnv(pa.getProperties());
\r
88 RosettaDF<Perms> permsDF = env.newDataFactory(Perms.class);
\r
89 SecurityInfoC<HttpURLConnection> si = new SecurityInfoC<HttpURLConnection>(pa);
\r
90 HBasicAuthSS hbass = new HBasicAuthSS(pa,si);
\r
91 if(hbass.getID()==null) {
\r
92 hbass=null; // not configured with ID.
\r
98 hxss = new HX509SS(user,si);
\r
99 } catch(Exception e) {
\r
100 e.printStackTrace();
\r
101 print(false,"Continuing");
\r
104 if(user==null || (pass==null && alias==null)) {
\r
105 System.out.printf("ERROR: DME2 Client cannot be tested with out %s and %s properties"
\r
106 , Config.AAF_MECHID, Config.AAF_MECHPASS );
\r
108 if("TEST".equals(aaf_env) || "IST".equals(aaf_env) || "PROD".equals(aaf_env)) {
\r
109 DME2Manager dm = null;
\r
110 print(false,"Attempt DME2Manager Load");
\r
111 if(Class.forName("com.att.aft.dme2.api.DME2Manager")==null) {
\r
112 print(true,"DME2 jar is not available: Skipping DME2 Tests");
\r
113 } else { // DME2 Client Tests
\r
114 pass=pa.decrypt(pass,false);
\r
115 // Out of the box DME2
\r
116 aafurl = String.format(SRV_RESOLVE, aaf_env, PROD.equals(aaf_env)?"DEFAULT":"BAU_SE");
\r
117 print(true,"TEST CADI Config",aafurl);
\r
118 aafcon = testConfig(pa,aafurl);
\r
119 test(aafcon,permsDF,user);
\r
121 print(true,"Find and TEST Connections with DME2Locator",aafurl);
\r
122 DME2Locator dl = new DME2Locator(pa,dm,aafurl);
\r
125 dm = new DME2Manager("DME2Manager",pa.getProperties());
\r
127 dme2RawTest(dm, aafurl,user,pass);
\r
129 // URL specific Variant
\r
130 if((aafurl = specificDME2URL(dl, aafurl))!=null) {
\r
131 print(true,"TEST Specific DME2 CADI Config",aafurl);
\r
132 aafcon = testConfig(pa,aafurl);
\r
133 test(aafcon,permsDF,user);
\r
135 dme2RawTest(dm,aafurl,user,pass);
\r
138 print(true,"CADI Direct AAFConDME2 Object Usage",aafurl);
\r
140 pa.setProperty(Config.AAF_URL,aafurl);
\r
141 aafcon = new AAFConDME2(pa);
\r
142 test(aafcon,permsDF,user);
\r
143 } catch(Throwable t) {
\r
144 t.printStackTrace();
\r
147 // find a direct client to code a Direct HTTP with
\r
150 print(true,"CADI Http DME2Locator Client Coding Methodology BasicAuth",aafurl);
\r
151 hClientTest(dl,hbass,user);
\r
154 print(true,"CADI Http DME2Locator Client Coding Methodology X509",aafurl);
\r
155 hClientTest(dl,hxss,user);
\r
158 // ##### PROXY CHECKS
\r
159 aafurl = String.format(GW_RESOLVE, aaf_env, PROD.equals(aaf_env)?"DEFAULT":"BAU_SE");
\r
160 print(true,"TEST PROXY DME2 CADI Config",aafurl);
\r
161 aafcon = testConfig(pa,aafurl);
\r
162 test(aafcon,permsDF,user);
\r
165 dme2RawTest(dm, aafurl,user,pass);
\r
167 // URL specific Variant
\r
168 dl = new DME2Locator(pa,dm,aafurl);
\r
169 if((aafurl = specificDME2URL(dl, aafurl))!=null) {
\r
170 print(true,"TEST PROXY Specific DME2 CADI Config",aafurl);
\r
171 aafcon = testConfig(pa,aafurl);
\r
172 test(aafcon,permsDF,user);
\r
174 dme2RawTest(dm,aafurl,user,pass);
\r
180 PropertyLocator pl = servicePropLocator(aaf_env);
\r
182 URI uri = pl.get(pl.best());
\r
184 aafurl = uri.toString();
\r
185 print(true,"TEST Service PropertyLocator based Config",aafurl);
\r
186 aafcon = testConfig(pa,aafurl);
\r
187 test(aafcon,permsDF,user);
\r
190 print(true,"CADI Service Http PropLocator Client Coding Methodology Basic Auth",aafurl);
\r
191 hClientTest(pl,hbass, user);
\r
192 print(true,"CADI Service Http PropLocator Client Coding Methodology /authn/basicAuth",aafurl);
\r
193 basicAuthTest(pl,hbass);
\r
196 print(true,"CADI Service Http PropLocator Client Coding Methodology X509",aafurl);
\r
197 hClientTest(pl,hxss, user);
\r
200 pl = proxyPropLocator(aaf_env);
\r
202 uri = pl.get(pl.best());
\r
204 aafurl = uri.toString();
\r
205 print(true,"TEST PROXY PropertyLocator based Config",aafurl);
\r
206 aafcon = testConfig(pa,aafurl);
\r
207 test(aafcon,permsDF,user);
\r
210 print(true,"CADI PROXY Http PropLocator Client Coding Methodology Basic Auth",aafurl);
\r
211 hClientTest(pl,hbass, user);
\r
212 print(true,"CADI PROXY Http PropLocator Client Coding Methodology /proxy/authn/basicAuth",aafurl);
\r
213 basicAuthTest(pl,hbass);
\r
216 print(true,"CADI PROXY Http PropLocator Client Coding Methodology X509",aafurl);
\r
217 hClientTest(pl,hxss, user);
\r
222 } catch(Exception e) {
\r
223 e.printStackTrace(System.err);
\r
225 print(true,"END OF TESTS");
\r
230 private static void print(Boolean strong, String ... args) {
\r
231 PrintStream out = System.out;
\r
234 for(int i=0;i<70;++i) {
\r
239 for(String s : args) {
\r
240 out.print(strong?"== ":"------ ");
\r
243 out.print(" ------");
\r
248 for(int i=0;i<70;++i) {
\r
255 private static void test(AAFCon<?> aafcon,RosettaDF<Perms> permsDF,String user) {
\r
257 print(false,"AAFCon is null");
\r
260 print(false,"Calling with AAFCon");
\r
261 Future<Perms> fp = aafcon.client("2.0").read("/authz/perms/user/"+user, Perms.class, permsDF);
\r
263 System.out.printf("Found %d Permission(s)\n",fp.value.getPerm().size());
\r
265 System.out.printf("Error: %d %s\n",fp.code(),fp.body());
\r
267 } catch (Throwable t) {
\r
268 t.printStackTrace();
\r
273 private static AAFCon<?> testConfig(PropAccess pa, String aafurl) {
\r
275 pa.setProperty(Config.AAF_URL, aafurl);
\r
276 Lur lur = Config.configLur(pa);
\r
277 Config.configHttpTaf(pa, TrustChecker.NOTRUST, null, lur);
\r
281 f = lur.getClass().getField("aaf");
\r
282 return (AAFCon<?>)f.get(lur);
\r
283 } catch (Exception nsfe) {
\r
287 } catch(Throwable t) {
\r
288 t.printStackTrace();
\r
293 private static String specificDME2URL(Locator<URI> loc, String aafurl) throws LocatorException {
\r
294 Item item = loc.best();
\r
296 URI uri = loc.get(item);
\r
297 return aafurl.replace("DME2RESOLVE", String.format("%s:%d",uri.getHost(),uri.getPort()));
\r
302 private static void connectTest(Locator<URI> dl) throws LocatorException {
\r
305 print(false,"TCP/IP Connect test to all Located Services");
\r
306 for(Item li = dl.first();li!=null;li=dl.next(li)) {
\r
307 if((uri = dl.get(li)) == null) {
\r
308 System.out.println("Locator Item empty");
\r
311 socket = new Socket();
\r
312 socket.connect(new InetSocketAddress(uri.getHost(), uri.getPort()),3000);
\r
313 System.out.printf("Can Connect a Socket to %s %d\n",uri.getHost(),uri.getPort());
\r
316 } catch (IOException e1) {
\r
317 System.out.printf("Could not close Socket Connection: %s\n",e1.getMessage());
\r
319 } catch (IOException e) {
\r
320 System.out.printf("Cannot Connect a Socket to %s %d: %s\n",uri.getHost(),uri.getPort(),e.getMessage());
\r
326 private static PropertyLocator servicePropLocator(String env) throws LocatorException {
\r
331 purls="https://"+InetAddress.getLocalHost().getHostName()+":8100";
\r
332 } catch (UnknownHostException e) {
\r
333 throw new LocatorException(e);
\r
337 purls="https://aaf.dev.att.com:8100,https://aaf.dev.att.com:8101";
\r
340 purls="https://aaftest.test.att.com:8100,https://aaftest.test.att.com:8101";
\r
343 purls="https://aafist.test.att.com:8100,https://aafist.test.att.com:8101";
\r
346 purls="https://aaf.it.att.com:8100,https://aaf.it.att.com:8101";
\r
349 if(env.contains(".")) {
\r
350 purls="https://"+env+":8100";
\r
352 throw new LocatorException(ConnectivityTest.class.getSimpleName() + ": unknown Env");
\r
355 System.out.printf("Creating a PropertyLocator for %s\n",purls);
\r
356 return new PropertyLocator(purls);
\r
359 private static PropertyLocator proxyPropLocator(String env) throws LocatorException {
\r
364 purls="https://"+InetAddress.getLocalHost().getHostAddress()+":8100";
\r
365 } catch (UnknownHostException e) {
\r
366 throw new LocatorException(e);
\r
370 purls="https://aaf.dev.att.com:8095/proxy";
\r
373 purls="https://aaftest.test.att.com:8095/proxy";
\r
376 purls="https://aafist.test.att.com:8095/proxy";
\r
379 purls="https://aaf.it.att.com:8095/proxy";
\r
382 if(env.contains(".")) {
\r
383 purls="https://"+env+":8095/proxy";
\r
385 throw new LocatorException(ConnectivityTest.class.getSimpleName() + ": unknown Env");
\r
389 System.out.printf("Creating a PropertyLocator for %s\n",purls);
\r
390 return new PropertyLocator(purls);
\r
396 private static void hClientTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss, String user) {
\r
398 URI uri = dl.get(dl.best());
\r
399 System.out.println("Resolved to: " + uri);
\r
400 HClient client = new HClient(ss, uri, 3000);
\r
401 client.setMethod("GET");
\r
402 client.setPathInfo("/authz/perms/user/"+user);
\r
404 Future<String> future = client.futureReadString();
\r
405 if(future.get(7000)) {
\r
406 System.out.println(future.body());
\r
408 System.out.println(future.code() + ":" + future.body());
\r
410 } catch (CadiException | LocatorException | APIException e) {
\r
411 e.printStackTrace();
\r
416 private static void basicAuthTest(PropertyLocator dl, SecuritySetter<HttpURLConnection> ss) {
\r
418 URI uri = dl.get(dl.best());
\r
419 System.out.println("Resolved to: " + uri);
\r
420 HClient client = new HClient(ss, uri, 3000);
\r
421 client.setMethod("GET");
\r
422 client.setPathInfo("/authn/basicAuth");
\r
423 client.addHeader("Accept", "text/plain");
\r
427 Future<String> future = client.futureReadString();
\r
428 if(future.get(7000)) {
\r
429 System.out.println("BasicAuth Validated");
\r
431 System.out.println("Failure " + future.code() + ":" + future.body());
\r
433 } catch (CadiException | LocatorException | APIException e) {
\r
434 e.printStackTrace();
\r
438 // Regular DME2Client Coding Style
\r
439 private static void dme2RawTest(DME2Manager dm, String aafurl, String user, String pass) {
\r
444 URI uri = new URI(aafurl);
\r
445 print(true,"DME2 Direct Client Coding Methodology",uri.toString());
\r
446 DME2Client client = dm.newClient( uri, 3000);
\r
447 client.setMethod("GET"); // FYI, DME2 defaults to "POST"
\r
448 client.setContext("/authz/perms/user/"+user); // DME2 direct requires separate setting of Context from URI
\r
449 if(pass!=null) { // rely on Cert if no pass
\r
450 client.setCredentials(user, pass);
\r
452 client.setPayload(""); // DME2 will not send without something
\r
453 String resp = client.sendAndWait(7000);
\r
454 System.out.println(resp);
\r
455 } catch(Throwable e) {
\r
456 e.printStackTrace();
\r