From 5771d435f922854d9664e92ee5d5e79919fec6f5 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 28 Nov 2013 03:25:24 -0500
Subject: [PATCH] Removed config path from web.xml.

Previously the CMSStartServlet always requires a cfgPath parameter
pointing to the CS.cfg location. By default the parameter points to
<instance>/conf/<subsystem>/CS.cfg unless it's manually changed by
the admin after installation.

Recently the servlet has been modified such that if the parameter
is not specified it will generate the default path automatically.
So it is no longer necessary to keep the cfgPath parameter in the
web.xml templates because it will point to the same location.

This patch removes the cfgPath parameters from all web.xml templates.
This way newly created subsystems will not have this parameter, which
will help direct deployment in the future. An upgrade script has been
added to remove the parameter from existing instances if it points to
the default location. If the parameter points to a different location
that means the subsystem has been customized so it will not be changed.

Ticket #748, #499
---
 base/ca/shared/webapps/ca/WEB-INF/web.xml          |  2 -
 base/kra/shared/webapps/kra/WEB-INF/web.xml        |  2 -
 base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml      |  2 -
 .../upgrade/10.1.99/05-RemoveConfigPathFromWebXML  | 80 ++++++++++++++++++++++
 base/tks/shared/webapps/tks/WEB-INF/web.xml        |  2 -
 5 files changed, 80 insertions(+), 8 deletions(-)
 create mode 100755 base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML

diff --git a/base/ca/shared/webapps/ca/WEB-INF/web.xml b/base/ca/shared/webapps/ca/WEB-INF/web.xml
index 5f20ed1943754217fb81f26e1673bfd460e1fe1a..508c31694ea79c9f43e189bb305e107412dc3ffc 100644
--- a/base/ca/shared/webapps/ca/WEB-INF/web.xml
+++ b/base/ca/shared/webapps/ca/WEB-INF/web.xml
@@ -351,8 +351,6 @@
       <servlet-class> com.netscape.cms.servlet.base.CMSStartServlet  </servlet-class>
              <init-param><param-name>  AuthzMgr    </param-name>
                          <param-value> BasicAclAuthz </param-value> </init-param>
-             <init-param><param-name>  cfgPath     </param-name>
-                         <param-value> [PKI_INSTANCE_PATH]/conf/[PKI_SUBSYSTEM_TYPE]/CS.cfg </param-value> </init-param>
              <init-param><param-name>  ID          </param-name>
                          <param-value> castart     </param-value> </init-param>
       <load-on-startup>  1  </load-on-startup>
diff --git a/base/kra/shared/webapps/kra/WEB-INF/web.xml b/base/kra/shared/webapps/kra/WEB-INF/web.xml
index 0a5f92ae2804ea82c7c99c35b4599281ee8a7282..d0573d5d4104045b163f18c26c85c89df72a8815 100644
--- a/base/kra/shared/webapps/kra/WEB-INF/web.xml
+++ b/base/kra/shared/webapps/kra/WEB-INF/web.xml
@@ -576,8 +576,6 @@
       <servlet-class> com.netscape.cms.servlet.base.CMSStartServlet  </servlet-class>
              <init-param><param-name>  AuthzMgr    </param-name>
                          <param-value> BasicAclAuthz </param-value> </init-param>
-             <init-param><param-name>  cfgPath     </param-name>
-                         <param-value> [PKI_INSTANCE_PATH]/conf/[PKI_SUBSYSTEM_TYPE]/CS.cfg  </param-value> </init-param>
              <init-param><param-name>  ID          </param-name>
                          <param-value> krastart    </param-value> </init-param>
       <load-on-startup>  1  </load-on-startup>
diff --git a/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml b/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml
index 249f1eb1b1b02d23d605a65c0d6ef50ba088c2bc..21067d7d8eccb894a6c51c0f82b828be3684bb0e 100644
--- a/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml
+++ b/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml
@@ -96,8 +96,6 @@
       <servlet-class> com.netscape.cms.servlet.base.CMSStartServlet  </servlet-class>
              <init-param><param-name>  AuthzMgr    </param-name>
                          <param-value> BasicAclAuthz </param-value> </init-param>
-             <init-param><param-name>  cfgPath     </param-name>
-                         <param-value> [PKI_INSTANCE_PATH]/conf/[PKI_SUBSYSTEM_TYPE]/CS.cfg </param-value> </init-param>
              <init-param><param-name>  ID          </param-name>
                          <param-value> ocspstart   </param-value> </init-param>
       <load-on-startup>  1  </load-on-startup>
diff --git a/base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML b/base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML
new file mode 100755
index 0000000000000000000000000000000000000000..271ca41e019a0e3b02af030058b53f5e0645d97c
--- /dev/null
+++ b/base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+# Authors:
+#     Endi S. Dewata <edewata@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2014 Red Hat, Inc.
+# All rights reserved.
+#
+
+import os
+from lxml import etree
+
+import pki.server.upgrade
+
+
+class RemoveConfigPathFromWebXML(pki.server.upgrade.PKIServerUpgradeScriptlet):
+
+    def __init__(self):
+
+        self.message = 'Remove config path from web.xml'
+
+    def upgrade_subsystem(self, instance, subsystem):
+
+        default_cs_cfg = os.path.join(instance.base_dir, 'conf', subsystem.name, 'CS.cfg')
+        web_xml = os.path.join(instance.base_dir, 'webapps', subsystem.name, 'WEB-INF', 'web.xml')
+
+        if not os.path.exists(web_xml):
+            return
+
+        self.backup(web_xml)
+
+        document = etree.parse(web_xml)
+        self.remove_config_path(document, default_cs_cfg)
+
+        with open(web_xml, 'w') as f:
+            f.write(etree.tostring(document, pretty_print=True))
+
+    def remove_config_path(self, document, default_cs_cfg):
+
+        context = document.getroot()
+
+        # find CMSStartServlet servlet
+        for servlet in context.findall('servlet'):
+
+            servlet_class = servlet.find('servlet-class')
+            value = servlet_class.text.strip()
+
+            if value != 'com.netscape.cms.servlet.base.CMSStartServlet':
+                continue
+
+            # servlet found, find cfgPath parameter
+            for init_param in servlet.findall('init-param'):
+
+                param_name = init_param.find('param-name')
+                value = param_name.text.strip()
+
+                if value != 'cfgPath':
+                    continue
+
+                # parameter found, check value
+                param_value = init_param.find('param-value')
+                value = param_value.text.strip()
+
+                # if cfgPath points to the default CS.cfg, remove the parameter
+                if value == default_cs_cfg:
+                    servlet.remove(init_param)
+
+                return
diff --git a/base/tks/shared/webapps/tks/WEB-INF/web.xml b/base/tks/shared/webapps/tks/WEB-INF/web.xml
index e4650475d45fa77b7b74ebf0473ded52408e0457..0642faea1305cc32cb56cc3f9b934cf11985407b 100644
--- a/base/tks/shared/webapps/tks/WEB-INF/web.xml
+++ b/base/tks/shared/webapps/tks/WEB-INF/web.xml
@@ -40,8 +40,6 @@
       <servlet-class> com.netscape.cms.servlet.base.CMSStartServlet  </servlet-class>
              <init-param><param-name>  AuthzMgr    </param-name>
                          <param-value> BasicAclAuthz </param-value> </init-param>
-             <init-param><param-name>  cfgPath     </param-name>
-                         <param-value> [PKI_INSTANCE_PATH]/conf/[PKI_SUBSYSTEM_TYPE]/CS.cfg </param-value> </init-param>
              <init-param><param-name>  ID          </param-name>
                          <param-value> tksstart    </param-value> </init-param>
       <load-on-startup>  1  </load-on-startup>
-- 
1.8.4.2

