summaryrefslogtreecommitdiffstats
path: root/base/server/upgrade/10.2.4/01-AddMissingOCSPGETServletMappingToWebXML
blob: e82de538567f33b245a015fcf819ac1f93c4d0af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
# Authors:
#     Christina Fu <cfu@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) 2015 Red Hat, Inc.
# All rights reserved.
#

from __future__ import absolute_import
import os
from lxml import etree as ET

import pki
import pki.server.upgrade

class AddMissingOCSPGETServletMappingToWebXML(pki.server.upgrade.PKIServerUpgradeScriptlet):

    OCSPGETServletMappingData = """
        <servlet-mapping>
<servlet-name> caOCSP </servlet-name>
<url-pattern> /ocsp/* </url-pattern>
   </servlet-mapping>

       """

    def __init__(self):
        super(AddMissingOCSPGETServletMappingToWebXML, self).__init__()
        self.message = 'Add missing OCSP Get Servlet Mapping to upgraded Dogtag 9 instances'
        self.doc = None
        self.root = None


    def upgrade_subsystem(self, instance, subsystem):
        # only affects CA
        if subsystem.name != "ca":
            return

        web_xml = os.path.join(
            instance.base_dir,
            'ca', 'webapps', subsystem.name,
            'WEB-INF', 'web.xml')

        if not os.path.exists(web_xml):
            return

        self.backup(web_xml)

        self.doc = ET.parse(web_xml)
        self.root = self.doc.getroot()
        self.add_ocsp_get_servlet_mapping()

        self.doc.write(web_xml)

    def add_ocsp_get_servlet_mapping(self):
        #add missing OCSP Get servlet mapping
        mappingFound = False
        urlPattern = ""
        index = 0
        for mapping in self.doc.findall('.//servlet-mapping'):
            name = mapping.find('servlet-name').text.strip()
            if name == 'caOCSP':
                urlPattern = mapping.find('url-pattern').text.strip()
                index = self.root.index(mapping) + 1
            if urlPattern == '/ocsp/*':
                mappingFound = True
        if not mappingFound:
            mapping = ET.fromstring(self.OCSPGETServletMappingData)
            mapping.tail = '\n\n    '
            self.root.insert(index, mapping)