Fix CADI Connection to Remote DNS AAF servers on proxied network
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / TestConnectivity.java
index 08ee900..6301ac3 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,10 +27,10 @@ import java.net.HttpURLConnection;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 import org.onap.aaf.cadi.Access.Level;
 import org.onap.aaf.cadi.CadiException;
@@ -43,14 +43,20 @@ import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
 import org.onap.aaf.cadi.client.Future;
 import org.onap.aaf.cadi.config.Config;
 import org.onap.aaf.cadi.config.SecurityInfoC;
+import org.onap.aaf.cadi.configure.Agent;
 import org.onap.aaf.cadi.http.HBasicAuthSS;
 import org.onap.aaf.cadi.http.HClient;
 import org.onap.aaf.cadi.http.HX509SS;
+import org.onap.aaf.cadi.locator.SingleEndpointLocator;
 import org.onap.aaf.cadi.oauth.HRenewingTokenSS;
+import org.onap.aaf.cadi.util.FixURIinfo;
 import org.onap.aaf.misc.env.APIException;
 
 public class TestConnectivity {
-    
+
+    private static Map<String, String> aaf_urls;
+
+
     public static void main(String[] args) {
         if (args.length<1) {
             System.out.println("Usage: ConnectivityTester <cadi_prop_files> [<AAF FQDN (i.e. aaf.dev.att.com)>]");
@@ -63,63 +69,66 @@ public class TestConnectivity {
             }
 
             PropAccess access = new PropAccess(args);
-            String aaflocate;
-            if (args.length>1) {
-                aaflocate = "https://" + args[1];
-                access.setProperty(Config.AAF_LOCATE_URL, "https://" + args[1]);
-            } else {
-                aaflocate = access.getProperty(Config.AAF_LOCATE_URL);
-                if (aaflocate==null) {
-                    print(true,"Properties must contain ",Config.AAF_LOCATE_URL);
-                }
-            }
-            
             try {
                 SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
-                
+                aaf_urls = Agent.loadURLs(access);
+
                 List<SecuritySetter<HttpURLConnection>> lss = loadSetters(access,si);
                 /////////
-                print(true,"Test Connections driven by AAFLocator");
-                URI serviceURI = uri(access,"service");
-
-                for (URI uri : new URI[] {
-                        serviceURI,
-                        uri(access,"token"),
-                        uri(access,"introspect"),
-                        uri(access,"cm"),
-                        uri(access,"gui"),
-                        uri(access,"fs"),
-                        uri(access,"hello")
-                }) {
-                    Locator<URI> locator = new AAFLocator(si, uri);
-                    try {
-                        connectTest(locator, uri);
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                        System.err.flush();
-                    }
-                }
+                String directAAFURL = aaf_urls.get(Config.AAF_URL);
+                if(directAAFURL!=null && !(directAAFURL.contains("/locate/") || directAAFURL.contains("AAF_LOCATE_URL"))) {
+                    print(true,"Test Connections by non-located aaf_url");
+                    Locator<URI> locator = new SingleEndpointLocator(directAAFURL);
+                    connectTest(locator,new URI(directAAFURL));
 
-                /////////
-                print(true,"Test Service for Perms driven by AAFLocator");
-                Locator<URI> locator = new AAFLocator(si,serviceURI);
-                for (SecuritySetter<HttpURLConnection> ss : lss) {
+                    SecuritySetter<HttpURLConnection> ss = si.defSS;
                     permTest(locator,ss);
-                }
+                    basicAuthTest(locator,ss);
+                } else {
+                    /////////
+                    print(true,"Test Connections driven by AAFLocator");
+                    String serviceURI = aaf_urls.get(Config.AAF_URL);
 
-                //////////
-                print(true,"Test essential BasicAuth Service call, driven by AAFLocator");
-                boolean hasBath=false;
-                for (SecuritySetter<HttpURLConnection> ss : lss) {
-                    if (ss instanceof HBasicAuthSS) {
-                       hasBath=true;
-                        basicAuthTest(new AAFLocator(si, serviceURI),ss);
+                    for (String url : new String[] {
+                            serviceURI,
+                            aaf_urls.get(Config.AAF_OAUTH2_TOKEN_URL),
+                            aaf_urls.get(Config.AAF_OAUTH2_INTROSPECT_URL),
+                            aaf_urls.get(Config.AAF_URL_CM),
+                            aaf_urls.get(Config.AAF_URL_GUI),
+                            aaf_urls.get(Config.AAF_URL_FS),
+                            aaf_urls.get(Config.AAF_URL_HELLO)
+                    }) {
+                        URI uri = new URI(url);
+                        Locator<URI> locator = new AAFLocator(si, uri);
+                        try {
+                            connectTest(locator, uri);
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                            System.err.flush();
+                        }
+                    }
+
+                    /////////
+                    print(true,"Test Service for Perms driven by AAFLocator");
+                    Locator<URI> locator = new AAFLocator(si,new URI(serviceURI));
+                    for (SecuritySetter<HttpURLConnection> ss : lss) {
+                        permTest(locator,ss);
+                    }
+
+                    //////////
+                    print(true,"Test essential BasicAuth Service call, driven by AAFLocator");
+                    boolean hasBath=false;
+                    for (SecuritySetter<HttpURLConnection> ss : lss) {
+                        if (ss instanceof HBasicAuthSS) {
+                            hasBath=true;
+                            basicAuthTest(new AAFLocator(si, new URI(serviceURI)),ss);
+                        }
+                    }
+                    if(!hasBath) {
+                        System.out.println("No User/Password to test");
                     }
                 }
-                if(!hasBath) {
-                       System.out.println("No User/Password to test");
-                }
-                
+
             } catch (Exception e) {
                 e.printStackTrace(System.err);
             } finally {
@@ -127,23 +136,14 @@ public class TestConnectivity {
             }
         }
     }
-    
-    private static URI uri(PropAccess access, String ms) throws URISyntaxException {
-               String aaf_root_ns = access.getProperty(Config.AAF_ROOT_NS,"AAF_NS");
-               String aaf_api_version = access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
-               String aaf_locate_url = access.getProperty(Config.AAF_LOCATE_URL,Defaults.AAF_LOCATE_CONST);
-               if("cm".equals(ms) && "2.0".equals(aaf_api_version)) {
-                       ms = "certman";
-               }
-               return new URI(aaf_locate_url + "/locate/" + aaf_root_ns + '.' + ms + ':' + aaf_api_version);
-       }
-
-       private static List<SecuritySetter<HttpURLConnection>> loadSetters(PropAccess access, SecurityInfoC<HttpURLConnection> si)  {
+
+
+    private static List<SecuritySetter<HttpURLConnection>> loadSetters(PropAccess access, SecurityInfoC<HttpURLConnection> si)  {
         print(true,"Load Security Setters from Configuration Information");
         String user = access.getProperty(Config.AAF_APPID);
 
         ArrayList<SecuritySetter<HttpURLConnection>> lss = new ArrayList<>();
-        
+
 
         try {
             HBasicAuthSS hbass = new HBasicAuthSS(si,true);
@@ -169,11 +169,7 @@ public class TestConnectivity {
             access.log(Level.INFO, "X509 (Client certificate) Security Setter constructor threw exception: \"",e.getMessage(),"\". X509 tests will not be performed");
         }
 
-        String tokenURL = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL);
-        String locateURL=access.getProperty(Config.AAF_LOCATE_URL);
-        if (tokenURL==null || (tokenURL.contains("/locate/") && locateURL!=null)) {
-            tokenURL=Defaults.OAUTH2_TOKEN_URL+"/token";
-        }
+        String tokenURL = aaf_urls.get(Config.AAF_OAUTH2_TOKEN_URL);
 
         try {
             HRenewingTokenSS hrtss = new HRenewingTokenSS(access, tokenURL);
@@ -182,7 +178,7 @@ public class TestConnectivity {
         } catch (Exception e) {
             access.log(Level.INFO, "AAF OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". AAF OAUTH2 tests will not be conducted... Continuing");
         }
-        
+
         tokenURL = access.getProperty(Config.AAF_ALT_OAUTH2_TOKEN_URL);
         if (tokenURL==null) {
             access.log(Level.INFO, "AAF Alternative OAUTH2 requires",Config.AAF_ALT_OAUTH2_TOKEN_URL, "OAuth2 tests to", tokenURL, "will not be conducted... Continuing");
@@ -195,7 +191,7 @@ public class TestConnectivity {
                 access.log(Level.INFO, "ALT OAUTH2 Security Setter constructor threw exception: \"",e.getMessage(),"\". ALT OAuth2 tests to", tokenURL, " will not be conducted... Continuing");
             }
         }
-        
+
         return lss;
     }
 
@@ -232,13 +228,15 @@ public class TestConnectivity {
             if ((uri = dl.get(li)) == null) {
                 System.out.println("Locator Item empty");
             } else {
+                System.out.printf("Located %s using %s\n",uri.toString(), locatorURI.toString());
                 socket = new Socket();
                 try {
+                    FixURIinfo fui = new FixURIinfo(uri);
                     try {
-                        socket.connect(new InetSocketAddress(uri.getHost(),  uri.getPort()),3000);
-                        System.out.printf("Can Connect a Socket to %s %d\n",uri.getHost(),uri.getPort());
+                        socket.connect(new InetSocketAddress(fui.getHost(),  fui.getPort()),3000);
+                        System.out.printf("Can Connect a Socket to %s %d\n",fui.getHost(),fui.getPort());
                     } catch (IOException e) {
-                        System.out.printf("Cannot Connect a Socket to  %s %d: %s\n",uri.getHost(),uri.getPort(),e.getMessage());
+                        System.out.printf("Cannot Connect a Socket to  %s %d: %s\n",fui.getHost(),fui.getPort(),e.getMessage());
                     }
                 } finally {
                     try {
@@ -270,11 +268,14 @@ public class TestConnectivity {
             client.setMethod("GET");
             String user = ss.getID();
 
-            client.setPathInfo("/authz/perms/user/"+user);
+            String pathInfo = "/authz/perms/user/"+user;
+            client.setPathInfo(pathInfo);
+            System.out.println(pathInfo);
+
             client.send();
             Future<String> future = client.futureReadString();
             if (future.get(7000)) {
-                System.out.println(future.body());    
+                System.out.println(future.body());
             } else {
                 if (future.code()==401 && ss instanceof HX509SS) {
                     System.out.println("  Authentication denied with 401 for Certificate.\n\t"
@@ -298,11 +299,11 @@ public class TestConnectivity {
             client.setPathInfo("/authn/basicAuth");
             client.addHeader("Accept", "text/plain");
             client.send();
-    
-        
+
+
             Future<String> future = client.futureReadString();
             if (future.get(7000)) {
-                System.out.println("BasicAuth Validated");    
+                System.out.println("BasicAuth Validated");
             } else {
                 System.out.println("Failure " + future.code() + ":" + future.body());
             }