>From 877897468366da577969eeac44268abe0e12b629 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 19 Jan 2017 20:50:42 +0100
Subject: [PATCH] Merged /pki webapps.

Previously the /pki webapp was only added if the theme was present
during installation, and there were separate webapps for /pki/admin
and /pki/js. If the theme was installed later, the /pki webapp had
to be configured manually.

To simplify the installation and to support other developments
(e.g. login banner), the /pki webapp will always be added during
installation regardless of theme, and the /pki/admin and /pki/js
webapps are merged into /pki webapp. When the theme package is
installed, it will create links in /pki webapp so the theme files
will become available without additional configuration.

An upgrade script has been added to merge the /pki webapp in
existing instances.

https://fedorahosted.org/pki/ticket/2582
---
 .../deployment/scriptlets/instance_layout.py       | 36 ++--------
 .../tomcat7/conf/Catalina/localhost/pki#admin.xml  | 30 --------
 .../tomcat7/conf/Catalina/localhost/pki#js.xml     | 30 --------
 .../server/tomcat7/conf/Catalina/localhost/pki.xml |  2 +-
 .../tomcat8/conf/Catalina/localhost/pki#admin.xml  | 32 ---------
 .../tomcat8/conf/Catalina/localhost/pki#js.xml     | 32 ---------
 .../server/tomcat8/conf/Catalina/localhost/pki.xml |  2 +-
 base/server/upgrade/10.4.0/02-MergePKIWebapps      | 79 ++++++++++++++++++++++
 dogtag/common-ui/CMakeLists.txt                    | 23 +++++++
 specs/dogtag-pki-theme.spec                        |  9 +++
 10 files changed, 118 insertions(+), 157 deletions(-)
 delete mode 100644 base/server/tomcat7/conf/Catalina/localhost/pki#admin.xml
 delete mode 100644 base/server/tomcat7/conf/Catalina/localhost/pki#js.xml
 delete mode 100644 base/server/tomcat8/conf/Catalina/localhost/pki#admin.xml
 delete mode 100644 base/server/tomcat8/conf/Catalina/localhost/pki#js.xml
 create mode 100755 base/server/upgrade/10.4.0/02-MergePKIWebapps

diff --git a/base/server/python/pki/server/deployment/scriptlets/instance_layout.py b/base/server/python/pki/server/deployment/scriptlets/instance_layout.py
index 330aa46f718b1783dcd04cab1e72a952b74df381..a9a2840c77db032219930364d67a764e67b04b7a 100644
--- a/base/server/python/pki/server/deployment/scriptlets/instance_layout.py
+++ b/base/server/python/pki/server/deployment/scriptlets/instance_layout.py
@@ -88,46 +88,20 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
                     "localhost",
                     "ROOT.xml"))
 
-            if os.path.exists(deployer.mdict['pki_theme_server_dir']):
-                # Deploy theme web application if available
-                deployer.deploy_webapp(
-                    "pki",
-                    deployer.mdict['pki_theme_server_dir'],
-                    os.path.join(
-                        deployer.mdict['pki_source_server_path'],
-                        "Catalina",
-                        "localhost",
-                        "pki.xml"))
-
-            # Deploy admin templates
+            # Deploy pki web application which includes themes,
+            # admin templates, and JS libraries
             deployer.deploy_webapp(
-                "pki#admin",
+                "pki",
                 os.path.join(
                     config.PKI_DEPLOYMENT_SOURCE_ROOT,
                     "server",
                     "webapps",
-                    "pki",
-                    "admin"),
+                    "pki"),
                 os.path.join(
                     deployer.mdict['pki_source_server_path'],
                     "Catalina",
                     "localhost",
-                    "pki#admin.xml"))
-
-            # Deploy JS library
-            deployer.deploy_webapp(
-                "pki#js",
-                os.path.join(
-                    config.PKI_DEPLOYMENT_SOURCE_ROOT,
-                    "server",
-                    "webapps",
-                    "pki",
-                    "js"),
-                os.path.join(
-                    deployer.mdict['pki_source_server_path'],
-                    "Catalina",
-                    "localhost",
-                    "pki#js.xml"))
+                    "pki.xml"))
 
             # Create Tomcat instance library
             deployer.directory.create(deployer.mdict['pki_instance_lib'])
diff --git a/base/server/tomcat7/conf/Catalina/localhost/pki#admin.xml b/base/server/tomcat7/conf/Catalina/localhost/pki#admin.xml
deleted file mode 100644
index e7f2e54a3ae3e9ead1b86cc8423ee6e68ee26286..0000000000000000000000000000000000000000
--- a/base/server/tomcat7/conf/Catalina/localhost/pki#admin.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<?xml version='1.0' encoding='utf-8'?>
-<!-- BEGIN COPYRIGHT BLOCK
-     Copyright (C) 2012 Red Hat, Inc.
-     All rights reserved.
-     Modifications: configuration parameters
-     END COPYRIGHT BLOCK
--->
-
-<!--
-  Licensed to the Apache Software Foundation (ASF) under one or more
-  contributor license agreements.  See the NOTICE file distributed with
-  this work for additional information regarding copyright ownership.
-  The ASF licenses this file to You 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.
-  See the License for the specific language governing permissions and
-  limitations under the License.
--->
-<Context docBase="/usr/share/pki/server/webapps/pki/admin" crossContext="true" allowLinking="true">
-
-    <Manager
-        secureRandomProvider="Mozilla-JSS" secureRandomAlgorithm="pkcs11prng"/>
-
-</Context>
diff --git a/base/server/tomcat7/conf/Catalina/localhost/pki#js.xml b/base/server/tomcat7/conf/Catalina/localhost/pki#js.xml
deleted file mode 100644
index cef2f1c6ba949e811c7d627a937a7d8b7f2b4b93..0000000000000000000000000000000000000000
--- a/base/server/tomcat7/conf/Catalina/localhost/pki#js.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<?xml version='1.0' encoding='utf-8'?>
-<!-- BEGIN COPYRIGHT BLOCK
-     Copyright (C) 2012 Red Hat, Inc.
-     All rights reserved.
-     Modifications: configuration parameters
-     END COPYRIGHT BLOCK
--->
-
-<!--
-  Licensed to the Apache Software Foundation (ASF) under one or more
-  contributor license agreements.  See the NOTICE file distributed with
-  this work for additional information regarding copyright ownership.
-  The ASF licenses this file to You 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.
-  See the License for the specific language governing permissions and
-  limitations under the License.
--->
-<Context docBase="/usr/share/pki/server/webapps/pki/js" crossContext="true" allowLinking="true">
-
-    <Manager
-        secureRandomProvider="Mozilla-JSS" secureRandomAlgorithm="pkcs11prng"/>
-
-</Context>
diff --git a/base/server/tomcat7/conf/Catalina/localhost/pki.xml b/base/server/tomcat7/conf/Catalina/localhost/pki.xml
index 77d1f27b1631b2d1e9d9922cbcc82ef8fddcd31e..f2d779ab57adea17d033f4c77ac97ae9412e7eee 100644
--- a/base/server/tomcat7/conf/Catalina/localhost/pki.xml
+++ b/base/server/tomcat7/conf/Catalina/localhost/pki.xml
@@ -22,7 +22,7 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
-<Context docBase="/usr/share/pki/common-ui" crossContext="true" allowLinking="true">
+<Context docBase="/usr/share/pki/server/webapps/pki" crossContext="true" allowLinking="true">
 
     <Manager
         secureRandomProvider="Mozilla-JSS" secureRandomAlgorithm="pkcs11prng"/>
diff --git a/base/server/tomcat8/conf/Catalina/localhost/pki#admin.xml b/base/server/tomcat8/conf/Catalina/localhost/pki#admin.xml
deleted file mode 100644
index 16db436053f1b33617d55c0f64881a3d0afb6336..0000000000000000000000000000000000000000
--- a/base/server/tomcat8/conf/Catalina/localhost/pki#admin.xml
+++ /dev/null
@@ -1,32 +0,0 @@
-<?xml version='1.0' encoding='utf-8'?>
-<!-- BEGIN COPYRIGHT BLOCK
-     Copyright (C) 2012 Red Hat, Inc.
-     All rights reserved.
-     Modifications: configuration parameters
-     END COPYRIGHT BLOCK
--->
-
-<!--
-  Licensed to the Apache Software Foundation (ASF) under one or more
-  contributor license agreements.  See the NOTICE file distributed with
-  this work for additional information regarding copyright ownership.
-  The ASF licenses this file to You 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.
-  See the License for the specific language governing permissions and
-  limitations under the License.
--->
-<Context docBase="/usr/share/pki/server/webapps/pki/admin" crossContext="true">
-
-    <Manager
-        secureRandomProvider="Mozilla-JSS" secureRandomAlgorithm="pkcs11prng"/>
-
-    <Resources allowLinking="true" />
-
-</Context>
diff --git a/base/server/tomcat8/conf/Catalina/localhost/pki#js.xml b/base/server/tomcat8/conf/Catalina/localhost/pki#js.xml
deleted file mode 100644
index 9b0903ddf3ff8d113ed9c10877e67d423d6967aa..0000000000000000000000000000000000000000
--- a/base/server/tomcat8/conf/Catalina/localhost/pki#js.xml
+++ /dev/null
@@ -1,32 +0,0 @@
-<?xml version='1.0' encoding='utf-8'?>
-<!-- BEGIN COPYRIGHT BLOCK
-     Copyright (C) 2012 Red Hat, Inc.
-     All rights reserved.
-     Modifications: configuration parameters
-     END COPYRIGHT BLOCK
--->
-
-<!--
-  Licensed to the Apache Software Foundation (ASF) under one or more
-  contributor license agreements.  See the NOTICE file distributed with
-  this work for additional information regarding copyright ownership.
-  The ASF licenses this file to You 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.
-  See the License for the specific language governing permissions and
-  limitations under the License.
--->
-<Context docBase="/usr/share/pki/server/webapps/pki/js" crossContext="true">
-
-    <Manager
-        secureRandomProvider="Mozilla-JSS" secureRandomAlgorithm="pkcs11prng"/>
-
-    <Resources allowLinking="true" />
-
-</Context>
diff --git a/base/server/tomcat8/conf/Catalina/localhost/pki.xml b/base/server/tomcat8/conf/Catalina/localhost/pki.xml
index 1e87c1cd568c271c498d5aeff6ce7c2af6f8065a..59249c725b4dc36819100585183478624b2a0db5 100644
--- a/base/server/tomcat8/conf/Catalina/localhost/pki.xml
+++ b/base/server/tomcat8/conf/Catalina/localhost/pki.xml
@@ -22,7 +22,7 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
-<Context docBase="/usr/share/pki/common-ui" crossContext="true">
+<Context docBase="/usr/share/pki/server/webapps/pki" crossContext="true">
 
     <Manager
         secureRandomProvider="Mozilla-JSS" secureRandomAlgorithm="pkcs11prng"/>
diff --git a/base/server/upgrade/10.4.0/02-MergePKIWebapps b/base/server/upgrade/10.4.0/02-MergePKIWebapps
new file mode 100755
index 0000000000000000000000000000000000000000..4bd3deeeed64d0718a3867c19a3f12a48f258ffc
--- /dev/null
+++ b/base/server/upgrade/10.4.0/02-MergePKIWebapps
@@ -0,0 +1,79 @@
+#!/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) 2017 Red Hat, Inc.
+# All rights reserved.
+#
+
+from __future__ import absolute_import
+import os
+import shutil
+
+import pki
+
+
+class MergePKIWebapps(
+        pki.server.upgrade.PKIServerUpgradeScriptlet):
+
+    def __init__(self):
+        super(MergePKIWebapps, self).__init__()
+        self.message = 'Merge PKI webapps'
+
+    def upgrade_instance(self, instance):
+
+        # undeploy /pki/admin webapp
+
+        pki_admin_xml = os.path.join(
+            instance.conf_dir,
+            'Catalina',
+            'localhost',
+            'pki#admin.xml')
+
+        self.backup(pki_admin_xml)
+        os.remove(pki_admin_xml)
+
+        # undeploy /pki/admin webapp
+
+        pki_js_xml = os.path.join(
+            instance.conf_dir,
+            'Catalina',
+            'localhost',
+            'pki#js.xml')
+
+        self.backup(pki_js_xml)
+        os.remove(pki_js_xml)
+
+        # deploy /pki webapp (which includes /pki/admin and /pki/js)
+
+        source_pki_xml = os.path.join(
+            pki.SHARE_DIR,
+            'server',
+            'conf',
+            'Catalina',
+            'localhost',
+            'pki.xml')
+
+        dest_pki_xml = os.path.join(
+            instance.conf_dir,
+            'Catalina',
+            'localhost',
+            'pki.xml')
+
+        self.backup(dest_pki_xml)
+        shutil.copyfile(source_pki_xml, dest_pki_xml)
+        os.chown(dest_pki_xml, instance.uid, instance.gid)
+        os.chmod(dest_pki_xml, 0o0660)
diff --git a/dogtag/common-ui/CMakeLists.txt b/dogtag/common-ui/CMakeLists.txt
index c4288e51a158a38547bd44c52b4600a0bd5b3a43..9da1a768b249fc8c42c5784191eac5431eb8aeff 100644
--- a/dogtag/common-ui/CMakeLists.txt
+++ b/dogtag/common-ui/CMakeLists.txt
@@ -1,5 +1,28 @@
 project(common-ui)
 
+add_custom_target(pki-server-theme-links ALL)
+
+add_custom_command(
+    TARGET pki-server-theme-links
+    COMMAND ${CMAKE_COMMAND} -E make_directory links
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/ca links/ca
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/css links/css
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/esc links/esc
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/fonts links/fonts
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/images links/images
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/kra links/kra
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/ocsp links/ocsp
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/pki.properties links/pki.properties
+    COMMAND ${CMAKE_COMMAND} -E create_symlink ${DATA_INSTALL_DIR}/common-ui/tks links/tks
+)
+
+install(
+    DIRECTORY
+        ${CMAKE_CURRENT_BINARY_DIR}/links/
+    DESTINATION
+        ${DATA_INSTALL_DIR}/server/webapps/pki
+)
+
 install(
     DIRECTORY
         shared/
diff --git a/specs/dogtag-pki-theme.spec b/specs/dogtag-pki-theme.spec
index 11a17c9b10fc9bb7e31ceaa9bfd69a430786fc5b..f87c1b578d9835937c117e94851f43b337a6edbf 100644
--- a/specs/dogtag-pki-theme.spec
+++ b/specs/dogtag-pki-theme.spec
@@ -150,6 +150,15 @@ cd build
 %doc dogtag/common-ui/LICENSE
 %dir %{_datadir}/pki
 %{_datadir}/pki/common-ui/
+%{_datadir}/pki/server/webapps/pki/ca
+%{_datadir}/pki/server/webapps/pki/css
+%{_datadir}/pki/server/webapps/pki/esc
+%{_datadir}/pki/server/webapps/pki/fonts
+%{_datadir}/pki/server/webapps/pki/images
+%{_datadir}/pki/server/webapps/pki/kra
+%{_datadir}/pki/server/webapps/pki/ocsp
+%{_datadir}/pki/server/webapps/pki/pki.properties
+%{_datadir}/pki/server/webapps/pki/tks
 
 
 %files -n dogtag-pki-console-theme
-- 
2.5.5

