diff --git a/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/AtsConfigurator.java b/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/AtsConfigurator.java index 8650a2b0..fbc60576 100644 --- a/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/AtsConfigurator.java +++ b/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/AtsConfigurator.java @@ -49,7 +49,10 @@ public final class AtsConfigurator " HOSTNAME - Name of the host.\n" + " IAREALMS_FILE_PATH - Path to the iaRealms file to be used by the service.\n" + " The following properties are optional:\n" + - " TOMCAT5 - Location of the tomcat5 executable.\n"; + " TOMCAT5 - Location of the tomcat5 executable.\n" + + " KEYSTORE_PATH - Location of keystore that should be used by connector.\n" + + " KEYSTORE_PASS - Password for keystore specified via KEYSTORE_PATH.\n" + + " KEYSTORE_ALG - Algorithm (SunX509 or IbmX509) for keystore specified via KEYSTORE_PATH.\n"; // Error codes final static int ERROR_NO_ERROR = 0; @@ -76,6 +79,8 @@ public final class AtsConfigurator final static int ERROR_JOS_CLOSE_FAILED = -25; final static int ERROR_FOS_CLOSE_FAILED = -26; final static int ERROR_JFTEMPLATE_CLOSE_FAILED = -27; + final static int ERROR_MISSING_KEYSTORE_PROPERTY = -28; + final static int ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT = -29; // Required properties final static String INSTALL_DIR_PROPERTY = "ATS_INSTALL_DIR"; @@ -84,7 +89,12 @@ public final class AtsConfigurator final static String JAVA_HOME_PROPERTY = "ATS_JAVA_HOME"; final static String HOSTNAME_PROPERTY = "HOSTNAME"; final static String IAREALMS_FILE_PROPERTY = "IAREALMS_FILE_PATH"; + + // Optional properties final static String TOMCAT5_PROPERTY = "TOMCAT5"; + final static String KEYSTORE_PATH_PROPERTY = "KEYSTORE_PATH"; + final static String KEYSTORE_PASS_PROPERTY = "KEYSTORE_PASS"; + final static String KEYSTORE_ALG_PROPERTY = "KEYSTORE_ALG"; final static String[] m_requiredProperties = { INSTALL_DIR_PROPERTY, @@ -383,6 +393,12 @@ public final class AtsConfigurator case ERROR_EXCEPTION: rcMessage = "Exception"; break; + case ERROR_MISSING_KEYSTORE_PROPERTY: + rcMessage = "Missing Keystore related property"; + break; + case ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT: + rcMessage = "Invalid tomcat server.xml format"; + break; default: rcMessage = "Unknown error"; break; diff --git a/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/Configure.java b/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/Configure.java index 303ca611..cd7de0b1 100644 --- a/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/Configure.java +++ b/CASA-auth-token/server-java/package/windows/AtsConfigurator/src/Configure.java @@ -23,12 +23,21 @@ * ***********************************************************************/ +import com.sun.org.apache.xerces.internal.parsers.DOMParser; +import com.sun.org.apache.xml.internal.serialize.OutputFormat; +import com.sun.org.apache.xml.internal.serialize.XMLSerializer; + import java.io.*; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import java.util.zip.ZipFile; +import org.xml.sax.InputSource; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NamedNodeMap; + /** * Configure Class. *
@@ -80,11 +89,15 @@ public class Configure // Setup the relocatable files if (AtsConfigurator.ERROR_NO_ERROR == (m_rc = setupRelocatableFiles())) { - // Setup the War file - if (AtsConfigurator.ERROR_NO_ERROR == (m_rc = createWarFileFromTemplate())) + // Update the tomcat server.xml + if (AtsConfigurator.ERROR_NO_ERROR == (m_rc = updateTomcatServerXML())) { - // Create the ATS Service - m_rc = createATSService(); + // Setup the War file + if (AtsConfigurator.ERROR_NO_ERROR == (m_rc = createWarFileFromTemplate())) + { + // Create the ATS Service + m_rc = createATSService(); + } } } } @@ -144,7 +157,7 @@ public class Configure AtsConfigurator.log("Configure.keyStoreAlreadyExists()- Start, relPath = " + keyStoreRelPath); - File fileKeystore = new File(((String) m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY)) + keyStoreRelPath); + File fileKeystore = new File(m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY) + keyStoreRelPath); File fileParent = fileKeystore.getParentFile(); String[] rgChildren = fileParent.list(); if (null != rgChildren) @@ -189,7 +202,7 @@ public class Configure commandArray[4] = "-keypass"; commandArray[5] = "secret"; commandArray[6] = "-keystore"; commandArray[7] = installDir + SERVER_KEY_STORE_RELATIVE_PATH; commandArray[8] = "-storepass"; commandArray[9] = "secret"; - commandArray[10] = "-dname"; commandArray[11] = "cn=casaatsd@" + (String) m_properties.get("COMPUTERNAME"); + commandArray[10] = "-dname"; commandArray[11] = "cn=casaatsd@" + m_properties.get("COMPUTERNAME"); commandArray[12] = "-validity"; commandArray[13] = "3600"; // Create the signing key @@ -216,7 +229,7 @@ public class Configure commandArray[4] = "-keypass"; commandArray[5] = "secret"; commandArray[6] = "-keystore"; commandArray[7] = installDir + SERVER_KEY_STORE_RELATIVE_PATH; commandArray[8] = "-storepass"; commandArray[9] = "secret"; - commandArray[10] = "-dname"; commandArray[11] = "cn=" + (String) m_properties.get("COMPUTERNAME"); + commandArray[10] = "-dname"; commandArray[11] = "cn=" + m_properties.get("COMPUTERNAME"); commandArray[12] = "-keyalg"; commandArray[13] = "RSA"; // Create a key for Tomcat to do SSL communications @@ -424,6 +437,197 @@ public class Configure return rc; } + /** + * Find first child node with matching node name. + * + * @param parentNode Parent node. + * @param nodeName Name of node. + * @return Child node found or null. + */ + public static Node findChildNode(Node parentNode, String nodeName) + { + Node retNode = null; + Node childNode; + Node nextNode = parentNode.getFirstChild(); + while ((childNode = nextNode) != null) + { + // Set next before we change anything + nextNode = childNode.getNextSibling(); + + // Handle child by node type + if (childNode.getNodeType() == Node.ELEMENT_NODE) + { + // Check if this is the element node wanted + if (childNode.getNodeName().equalsIgnoreCase(nodeName)) + { + // Found the node + retNode = childNode; + break; + } + } + } + + return retNode; + } + + /** + * Update the Setup all of the needed relocatable files. + * + * @return Return code. + */ + int updateTomcatServerXML() + { + int rc = AtsConfigurator.ERROR_NO_ERROR; + + AtsConfigurator.log("Configure.updateTomcatServerXML()- Start"); + + // Check if a keystore path property was specified + String keystorePath = (String) m_properties.get(AtsConfigurator.KEYSTORE_PATH_PROPERTY); + if (keystorePath != null) + { + AtsConfigurator.log("Need to update server.xml"); + + // The keystore path property was specified. The keystore pass and keystore algorithms + // should also be specified. + String keystorePass = (String) m_properties.get(AtsConfigurator.KEYSTORE_PASS_PROPERTY); + String keystoreAlg = (String) m_properties.get(AtsConfigurator.KEYSTORE_ALG_PROPERTY); + if (keystorePass != null && keystoreAlg != null) + { + // We have all of the information necessary to update the connector in the server.xml + // file, proceed. + FileInputStream inStream = null; + FileOutputStream outStream = null; + try + { + String installDir = (String) m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY); + File serverXmlFile = new File(installDir + "\\catalinabase\\conf\\server.xml"); + inStream = new FileInputStream(serverXmlFile); + + // Pull the server.xml file into a DOM + DOMParser parser = new DOMParser(); + parser.parse(new InputSource(inStream)); + Document doc = parser.getDocument(); + + Node docElement = doc.getDocumentElement(); + if (docElement != null) + { + Node serviceElement = findChildNode(docElement, "service"); + if (serviceElement != null) + { + Node connectorElement = findChildNode(serviceElement, "connector"); + if (connectorElement != null) + { + NamedNodeMap connectorAttributesMap = connectorElement.getAttributes(); + if (connectorAttributesMap != null) + { + int attributeSetFailures = 0; + + Node keystoreFileAttributeNode = connectorAttributesMap.getNamedItem("keystoreFile"); + if (keystoreFileAttributeNode != null) + { + keystoreFileAttributeNode.setNodeValue(keystorePath); + } + else + { + AtsConfigurator.log("keystoreFile attribute not configured for connector"); + attributeSetFailures ++; + rc = AtsConfigurator.ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT; + } + + Node keystorePassAttributeNode = connectorAttributesMap.getNamedItem("keystorePass"); + if (keystorePassAttributeNode != null) + { + keystorePassAttributeNode.setNodeValue(keystorePass); + } + else + { + AtsConfigurator.log("keystorePass attribute not configured for connector"); + attributeSetFailures ++; + rc = AtsConfigurator.ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT; + } + + Node algorithmAttributeNode = connectorAttributesMap.getNamedItem("algorithm"); + if (algorithmAttributeNode != null) + { + algorithmAttributeNode.setNodeValue(keystoreAlg); + } + else + { + AtsConfigurator.log("algorithm attribute not configured for connector"); + attributeSetFailures ++; + rc = AtsConfigurator.ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT; + } + + if (attributeSetFailures == 0) + { + outStream = new FileOutputStream(serverXmlFile); + OutputFormat format = new OutputFormat(doc); + XMLSerializer serializer = new XMLSerializer(outStream, format); + serializer.serialize(doc.getDocumentElement()); + } + } + else + { + AtsConfigurator.log("Failed to obtain connector attributes map"); + rc = AtsConfigurator.ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT; + } + } + else + { + AtsConfigurator.log("Failed to obtain connector node"); + rc = AtsConfigurator.ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT; + } + } + else + { + AtsConfigurator.log("Failed to obtain service node"); + rc = AtsConfigurator.ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT; + } + } + else + { + AtsConfigurator.log("Failed to obtain document element"); + rc = AtsConfigurator.ERROR_INVALID_TOMCAT_SERVER_XML_FORMAT; + } + } + catch (Exception e) + { + AtsConfigurator.log("Exception: " + e.getMessage()); + rc = AtsConfigurator.ERROR_EXCEPTION; + } + finally + { + try + { + // Close input stream if necessary + if (inStream != null) + inStream.close(); + + // Flush and close output stream if necessary + if (outStream != null) + { + outStream.flush(); + outStream.close(); + } + } + catch (Exception e) + { + // Do nothing + } + } + } + else + { + AtsConfigurator.log("Not all keystore related properties specified"); + rc = AtsConfigurator.ERROR_MISSING_KEYSTORE_PROPERTY; + } + } + + AtsConfigurator.log("Configure.updateTomcatServerXML()- End, rc= ", rc); + + return rc; + } + /** * Move files from Jar to Jar. * @@ -448,7 +652,6 @@ public class Configure if (jeLoop.getName().equalsIgnoreCase("WEB-INF/classes/casa_crypto.properties")) { AtsConfigurator.log("skipping: " + "WEB-INF/classes/casa_crypto.properties"); - continue; } // Move the rest of the files over @@ -540,8 +743,8 @@ public class Configure */ int createWarFileFromTemplate() { - String sWarTemplate = ((String) m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY)) + "\\etc\\svc\\templates\\CasaAuthTokenSvc.war"; - String sWarNew = ((String) m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY)) + "\\catalinabase\\webapps\\CasaAuthTokenSvc.war"; + String sWarTemplate = m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY) + "\\etc\\svc\\templates\\CasaAuthTokenSvc.war"; + String sWarNew = m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY) + "\\catalinabase\\webapps\\CasaAuthTokenSvc.war"; File fileWarTemplate = new File(sWarTemplate); File fileWarNew = new File(sWarNew); FileOutputStream fosWarNew = null; @@ -612,7 +815,7 @@ public class Configure { int i; String [] rgsFilesToAdd = new String[1]; - rgsFilesToAdd[0] = ((String) m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY)) + "\\etc\\svc\\casa_crypto.properties"; + rgsFilesToAdd[0] = m_properties.get(AtsConfigurator.INSTALL_DIR_PROPERTY) + "\\etc\\svc\\casa_crypto.properties"; String [] rgsNames = new String[1]; rgsNames[0] = "WEB-INF/classes/casa_crypto.properties"; for (i = 0; i < rgsFilesToAdd.length; i++) @@ -690,7 +893,7 @@ public class Configure // Determine the path to the Tomcat5 executable String sExe = (String) m_properties.get(AtsConfigurator.TOMCAT5_PROPERTY); if (sExe == null) - sExe = ((String) m_properties.get(AtsConfigurator.TOMCAT_HOME_PROPERTY)) + "\\bin\\tomcat5.exe"; + sExe = m_properties.get(AtsConfigurator.TOMCAT_HOME_PROPERTY) + "\\bin\\tomcat5.exe"; /* * Note that in the following code we do not bother to check the return of the invokeExternalCommand