From 7de94ec405d633bc6bf9343ac18d86aba5da9ebe Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Wed, 6 Feb 2013 16:39:33 -0500
Subject: [PATCH] Fixed conflicting security domain hosts.

The SecurityDomainProcessor has been modified to generate the host
ID from the subsystem type, hostname, and secure port instead of
relying on the user-configurable SubsystemName attribute.

Ticket #503
---
 .../com/netscape/certsrv/system/DomainInfo.java    |  2 +-
 .../certsrv/system/SecurityDomainHost.java         | 32 ++++++---
 .../certsrv/system/SecurityDomainSubsystem.java    |  2 +-
 .../servlet/csadmin/SecurityDomainProcessor.java   | 82 ++++++++++++----------
 4 files changed, 71 insertions(+), 47 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/system/DomainInfo.java b/base/common/src/com/netscape/certsrv/system/DomainInfo.java
index 3f38c64ab6477f0a7de7eeb372aca18b188388ad..32e64bf6a6f53f9601a951b2bfce72269e497bd4 100644
--- a/base/common/src/com/netscape/certsrv/system/DomainInfo.java
+++ b/base/common/src/com/netscape/certsrv/system/DomainInfo.java
@@ -148,7 +148,7 @@ public class DomainInfo {
         before.setName("EXAMPLE");
 
         SecurityDomainHost host = new SecurityDomainHost();
-        host.setId("CA localhost:8443");
+        host.setId("CA localhost 8443");
         host.setHostname("localhost");
         host.setPort("8080");
         host.setSecurePort("8443");
diff --git a/base/common/src/com/netscape/certsrv/system/SecurityDomainHost.java b/base/common/src/com/netscape/certsrv/system/SecurityDomainHost.java
index e4357ffe827f58922adfab4fc35f893104fab21d..730e36caece5c62a205125c6e5bb211ce33a2b5a 100644
--- a/base/common/src/com/netscape/certsrv/system/SecurityDomainHost.java
+++ b/base/common/src/com/netscape/certsrv/system/SecurityDomainHost.java
@@ -41,10 +41,11 @@ public class SecurityDomainHost {
     protected String hostname;
     protected String port;
     protected String securePort;
+    protected String secureEEClientAuthPort;
     protected String secureAgentPort;
     protected String secureAdminPort;
-    protected String secureEEClientAuthPort;
     protected String clone;
+    protected String subsystemName;
     protected String domainManager;
 
     @XmlAttribute(name="id")
@@ -119,6 +120,15 @@ public class SecurityDomainHost {
         this.clone = clone;
     }
 
+    @XmlElement(name="SubsystemName")
+    public String getSubsystemName() {
+        return subsystemName;
+    }
+
+    public void setSubsystemName(String subsystemName) {
+        this.subsystemName = subsystemName;
+    }
+
     @XmlElement(name="DomainManager")
     public String getDomainManager() {
         return domainManager;
@@ -159,10 +169,11 @@ public class SecurityDomainHost {
         result = prime * result + ((hostname == null) ? 0 : hostname.hashCode());
         result = prime * result + ((id == null) ? 0 : id.hashCode());
         result = prime * result + ((port == null) ? 0 : port.hashCode());
-        result = prime * result + ((secureAgentPort == null) ? 0 : secureAgentPort.hashCode());
         result = prime * result + ((secureAdminPort == null) ? 0 : secureAdminPort.hashCode());
+        result = prime * result + ((secureAgentPort == null) ? 0 : secureAgentPort.hashCode());
         result = prime * result + ((secureEEClientAuthPort == null) ? 0 : secureEEClientAuthPort.hashCode());
         result = prime * result + ((securePort == null) ? 0 : securePort.hashCode());
+        result = prime * result + ((subsystemName == null) ? 0 : subsystemName.hashCode());
         return result;
     }
 
@@ -200,16 +211,16 @@ public class SecurityDomainHost {
                 return false;
         } else if (!port.equals(other.port))
             return false;
-        if (secureAgentPort == null) {
-            if (other.secureAgentPort != null)
-                return false;
-        } else if (!secureAgentPort.equals(other.secureAgentPort))
-            return false;
         if (secureAdminPort == null) {
             if (other.secureAdminPort != null)
                 return false;
         } else if (!secureAdminPort.equals(other.secureAdminPort))
             return false;
+        if (secureAgentPort == null) {
+            if (other.secureAgentPort != null)
+                return false;
+        } else if (!secureAgentPort.equals(other.secureAgentPort))
+            return false;
         if (secureEEClientAuthPort == null) {
             if (other.secureEEClientAuthPort != null)
                 return false;
@@ -220,13 +231,18 @@ public class SecurityDomainHost {
                 return false;
         } else if (!securePort.equals(other.securePort))
             return false;
+        if (subsystemName == null) {
+            if (other.subsystemName != null)
+                return false;
+        } else if (!subsystemName.equals(other.subsystemName))
+            return false;
         return true;
     }
 
     public static void main(String args[]) throws Exception {
 
         SecurityDomainHost before = new SecurityDomainHost();
-        before.setId("CA localhost:8443");
+        before.setId("CA localhost 8443");
         before.setHostname("localhost");
         before.setPort("8080");
         before.setSecurePort("8443");
diff --git a/base/common/src/com/netscape/certsrv/system/SecurityDomainSubsystem.java b/base/common/src/com/netscape/certsrv/system/SecurityDomainSubsystem.java
index 74273c244b1ff9533f155faca82ccd727f388f9f..eec37a055494fcc39fc1cce16791af21b2307c9b 100644
--- a/base/common/src/com/netscape/certsrv/system/SecurityDomainSubsystem.java
+++ b/base/common/src/com/netscape/certsrv/system/SecurityDomainSubsystem.java
@@ -120,7 +120,7 @@ import javax.xml.bind.annotation.XmlRootElement;
         before.setName("CA");
 
         SecurityDomainHost host = new SecurityDomainHost();
-        host.setId("CA localhost:8443");
+        host.setId("CA localhost 8443");
         host.setHostname("localhost");
         host.setPort("8080");
         host.setSecurePort("8443");
diff --git a/base/common/src/com/netscape/cms/servlet/csadmin/SecurityDomainProcessor.java b/base/common/src/com/netscape/cms/servlet/csadmin/SecurityDomainProcessor.java
index 97e9d41b7a07f88c3c159c7ad1491b9f643f625e..ddfa8fae11b31761a11956167372cb0cc9ada909 100644
--- a/base/common/src/com/netscape/cms/servlet/csadmin/SecurityDomainProcessor.java
+++ b/base/common/src/com/netscape/cms/servlet/csadmin/SecurityDomainProcessor.java
@@ -195,35 +195,39 @@ public class SecurityDomainProcessor extends Processor {
                         String attrValue = (String) nextAttr.getStringValues().nextElement();
                         CMS.debug("SecurityDomainProcessor:    - "+attrName+": "+attrValue);
 
-                        if ("SubsystemName".equalsIgnoreCase(attrName)) {
-                            host.setId(attrValue);
-
-                        } else if ("Host".equalsIgnoreCase(attrName)) {
+                        if ("Host".equalsIgnoreCase(attrName)) {
                             host.setHostname(attrValue);
 
-                        } else if ("SecurePort".equalsIgnoreCase(attrName)) {
-                            host.setSecurePort(attrValue);
-
-                        } else if ("Clone".equalsIgnoreCase(attrName)) {
-                            host.setClone(attrValue);
-
-                        } else if ("DomainManager".equalsIgnoreCase(attrName)) {
-                            host.setDomainManager(attrValue);
-
                         } else if ("UnSecurePort".equalsIgnoreCase(attrName)) {
                             host.setPort(attrValue);
 
+                        } else if ("SecurePort".equalsIgnoreCase(attrName)) {
+                            host.setSecurePort(attrValue);
+
+                        } else if ("SecureEEClientAuthPort".equalsIgnoreCase(attrName)) {
+                            host.setSecureEEClientAuthPort(attrValue);
+
                         } else if ("SecureAgentPort".equalsIgnoreCase(attrName)) {
                             host.setSecureAgentPort(attrValue);
 
                         } else if ("SecureAdminPort".equalsIgnoreCase(attrName)) {
                             host.setSecureAdminPort(attrValue);
 
-                        } else if ("SecureEEClientAuthPort".equalsIgnoreCase(attrName)) {
-                            host.setSecureEEClientAuthPort(attrValue);
+                        } else if ("Clone".equalsIgnoreCase(attrName)) {
+                            host.setClone(attrValue);
+
+                        } else if ("SubsystemName".equalsIgnoreCase(attrName)) {
+                            host.setSubsystemName(attrValue);
+
+                        } else if ("DomainManager".equalsIgnoreCase(attrName)) {
+                            host.setDomainManager(attrValue);
                         }
                     }
 
+                    String port = host.getSecurePort();
+                    if (port == null) port = host.getSecureEEClientAuthPort();
+                    host.setId(subType+" "+host.getHostname()+" "+port);
+
                     domain.addHost(subType, host);
                 }
             }
@@ -270,25 +274,25 @@ public class SecurityDomainProcessor extends Processor {
                     String value = host.getHostname();
                     if (value != null) xmlObject.addItemToContainer(node, "Host", value);
 
+                    value = host.getPort();
+                    if (value != null) xmlObject.addItemToContainer(node, "UnSecurePort", value);
+
                     value = host.getSecurePort();
                     if (value != null) xmlObject.addItemToContainer(node, "SecurePort", value);
 
+                    value = host.getSecureEEClientAuthPort();
+                    if (value != null) xmlObject.addItemToContainer(node, "SecureEEClientAuthPort", value);
+
                     value = host.getSecureAgentPort();
                     if (value != null) xmlObject.addItemToContainer(node, "SecureAgentPort", value);
 
                     value = host.getSecureAdminPort();
                     if (value != null) xmlObject.addItemToContainer(node, "SecureAdminPort", value);
 
-                    value = host.getSecureEEClientAuthPort();
-                    if (value != null) xmlObject.addItemToContainer(node, "SecureEEClientAuthPort", value);
-
-                    value = host.getPort();
-                    if (value != null) xmlObject.addItemToContainer(node, "UnSecurePort", value);
-
                     value = host.getClone();
                     if (value != null) xmlObject.addItemToContainer(node, "Clone", value);
 
-                    value = host.getId();
+                    value = host.getSubsystemName();
                     if (value != null) xmlObject.addItemToContainer(node, "SubsystemName", value);
 
                     value = host.getDomainManager();
@@ -318,32 +322,36 @@ public class SecurityDomainProcessor extends Processor {
                 Node hostNode = hosts.item(j);
                 SecurityDomainHost host = new SecurityDomainHost();
 
-                values = xmlObject.getValuesFromContainer(hostNode, "SubsystemName");
-                if (!values.isEmpty()) host.setId(values.firstElement());
-
                 values = xmlObject.getValuesFromContainer(hostNode, "Host");
                 if (!values.isEmpty()) host.setHostname(values.firstElement());
 
-                values = xmlObject.getValuesFromContainer(hostNode, "SecurePort");
-                if (!values.isEmpty()) host.setSecurePort(values.firstElement());
-
-                values = xmlObject.getValuesFromContainer(hostNode, "Clone");
-                if (!values.isEmpty()) host.setClone(values.firstElement());
-
-                values = xmlObject.getValuesFromContainer(hostNode, "DomainManager");
-                if (!values.isEmpty()) host.setDomainManager(values.firstElement());
-
                 values = xmlObject.getValuesFromContainer(hostNode, "UnSecurePort");
                 if (!values.isEmpty()) host.setPort(values.firstElement());
 
+                values = xmlObject.getValuesFromContainer(hostNode, "SecurePort");
+                if (!values.isEmpty()) host.setSecurePort(values.firstElement());
+
+                values = xmlObject.getValuesFromContainer(hostNode, "SecureEEClientAuthPort");
+                if (!values.isEmpty()) host.setSecureEEClientAuthPort(values.firstElement());
+
                 values = xmlObject.getValuesFromContainer(hostNode, "SecureAgentPort");
                 if (!values.isEmpty()) host.setSecureAgentPort(values.firstElement());
 
                 values = xmlObject.getValuesFromContainer(hostNode, "SecureAdminPort");
                 if (!values.isEmpty()) host.setSecureAdminPort(values.firstElement());
 
-                values = xmlObject.getValuesFromContainer(hostNode, "SecureEEClientAuthPort");
-                if (!values.isEmpty()) host.setSecureEEClientAuthPort(values.firstElement());
+                values = xmlObject.getValuesFromContainer(hostNode, "Clone");
+                if (!values.isEmpty()) host.setClone(values.firstElement());
+
+                values = xmlObject.getValuesFromContainer(hostNode, "SubsystemName");
+                if (!values.isEmpty()) host.setSubsystemName(values.firstElement());
+
+                values = xmlObject.getValuesFromContainer(hostNode, "DomainManager");
+                if (!values.isEmpty()) host.setDomainManager(values.firstElement());
+
+                String port = host.getSecurePort();
+                if (port == null) port = host.getSecureEEClientAuthPort();
+                host.setId(type+" "+host.getHostname()+" "+port);
 
                 domain.addHost(type, host);
             }
@@ -358,7 +366,7 @@ public class SecurityDomainProcessor extends Processor {
         before.setName("EXAMPLE");
 
         SecurityDomainHost host = new SecurityDomainHost();
-        host.setId("CA localhost:8443");
+        host.setId("CA localhost 8443");
         host.setHostname("localhost");
         host.setPort("8080");
         host.setSecurePort("8443");
-- 
1.7.12.1

