Use dynamic certificates
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / client / MrProvConnection.java
index 6e692fa..9c3fa4e 100644 (file)
@@ -30,7 +30,10 @@ import org.onap.dmaap.dbcapi.model.MR_Cluster;
 import org.onap.dmaap.dbcapi.model.Topic;
 import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
+import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLSession;
+
 import java.io.*;
 import java.net.*;
 import java.util.Arrays;
@@ -43,9 +46,11 @@ public class MrProvConnection extends BaseLoggingClass{
 
     
     private String topicMgrCred;
-    private boolean useAAF;
+    private String authMethod;
     private    String    user;
     private    String    encPwd;
+    private    String  unit_test;
+    private boolean hostnameVerify;
     
     public MrProvConnection() {
         String mechIdProperty = "aaf.TopicMgrUser";
@@ -53,8 +58,10 @@ public class MrProvConnection extends BaseLoggingClass{
         DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
         user = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" );
         encPwd = p.getProperty( pwdProperty, "notSet" );
-        useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
+        authMethod = p.getProperty("MR.authentication", "none");
         topicMgrCred =  getCred();
+        hostnameVerify= "true".equalsIgnoreCase(p.getProperty("MR.hostnameVerify", "true"));
+        unit_test = p.getProperty( "UnitTest", "No" );
         
     }
     
@@ -69,25 +76,46 @@ public class MrProvConnection extends BaseLoggingClass{
     
     
     public boolean makeTopicConnection( MR_Cluster cluster ) {
-        logger.info( "connect to cluster: " + cluster.getDcaeLocationName());
-    
+        boolean rc = false;
+       logger.info( "connect to cluster: " + cluster.getDcaeLocationName());
+        
 
         provURL = cluster.getTopicProtocol() + "://" + cluster.getFqdn() + ":" + cluster.getTopicPort() + "/topics/create";
 
         if ( cluster.getTopicProtocol().equals( "https" ) ) {
-            return makeSecureConnection( provURL );
+            rc = makeSecureConnection( provURL );
+        } else {
+               rc = makeConnection( provURL );
         }
-        return makeConnection( provURL );
+       if ( rc  && unit_test.equals( "Yes" ) ) {
+               // set timeouts low so we don't hold up unit tests in build process
+            uc.setReadTimeout(5);
+            uc.setConnectTimeout(5);                   
+       }
+       return rc;
+        
     }
 
     private boolean makeSecureConnection( String pURL ) {
         logger.info( "makeConnection to " + pURL );
     
         try {
+       
+                       HostnameVerifier hostnameVerifier = new HostnameVerifier() {
+                               @Override
+                               public boolean verify( String hostname, SSLSession session ) {
+                                       return true;
+                               }
+                       
+                       };
             URL u = new URL( pURL );
             uc = (HttpsURLConnection) u.openConnection();
             uc.setInstanceFollowRedirects(false);
-            logger.info( "open connect to " + pURL );
+            if ( ! hostnameVerify ) {
+                               HttpsURLConnection ucs = (HttpsURLConnection) uc;
+                               ucs.setHostnameVerifier(hostnameVerifier);
+                       }
+            logger.info( "open secure connect to " + pURL );
             return(true);
         } catch( UnknownHostException uhe ){
             logger.error( "Caught UnknownHostException for " + pURL);
@@ -105,7 +133,8 @@ public class MrProvConnection extends BaseLoggingClass{
         try {
             URL u = new URL( pURL );
             uc = (HttpURLConnection) u.openConnection();
-            uc.setInstanceFollowRedirects(false);
+            uc.setInstanceFollowRedirects(false);                      
+
             logger.info( "open connect to " + pURL );
             return(true);
         } catch( UnknownHostException uhe ){
@@ -146,11 +175,12 @@ public class MrProvConnection extends BaseLoggingClass{
             byte[] postData = postTopic.getBytes();
             logger.info( "post fields=" + Arrays.toString(postData));
             
-            // when not using AAF, do not attempt Basic Authentication
-            if ( useAAF ) {
-                uc.setRequestProperty("Authorization", auth);
-                logger.info( "Authenticating with " + auth );
-            }
+                       if ( authMethod.equalsIgnoreCase("basicAuth") ) {
+                               uc.setRequestProperty("Authorization", auth);
+                               logger.info( "Authenticating with " + auth );
+                       } else if ( authMethod.equalsIgnoreCase("cert")) {
+                               logger.error( "MR.authentication set for client certificate.  Not supported yet.");
+                       }
             uc.setRequestMethod("POST");
             uc.setRequestProperty("Content-Type", "application/json");
             uc.setRequestProperty( "charset", "utf-8");
@@ -180,11 +210,17 @@ public class MrProvConnection extends BaseLoggingClass{
                 err.setFields( uc.getURL().getHost());
                 return new String( "500: " + uhe.getMessage());
             }catch ( ConnectException ce ) {
-                errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, "HTTP Connection Exception"  );
-                err.setCode(500);
-                err.setMessage("HTTP Connection Exception");
-                err.setFields( uc.getURL().getHost());
+                       if ( unit_test.equals( "Yes" ) ) {
+                               err.setCode(200);
+                               err.setMessage( "simulated response");
+                               logger.info( "artificial 200 response from doPostMessage because unit_test =" + unit_test );
+               } else { 
+                       errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, "HTTP Connection Exception"  );
+                       err.setCode(500);
+                       err.setMessage("HTTP Connection Exception");
+                       err.setFields( uc.getURL().getHost());
                 return new String( "500: " + ce.getMessage());
+               }
             }
             rc = uc.getResponseCode();
             logger.info( "http response code:" + rc );
@@ -215,7 +251,7 @@ public class MrProvConnection extends BaseLoggingClass{
             } 
             
         } catch (Exception e) {
-            errorLogger.error("Unable to read response  " );
+            errorLogger.error("Unable to read response:  " + e.getMessage() );
            
         }
         finally {