summaryrefslogtreecommitdiffstats
path: root/base/server/python
diff options
context:
space:
mode:
authorEndi S. Dewata <edewata@redhat.com>2016-05-06 23:56:31 +0200
committerEndi S. Dewata <edewata@redhat.com>2016-05-09 21:42:34 +0200
commit90143ffad6a20493219c67b55c57724afb4edfde (patch)
treea39a2cba95a55dd81f8a2c08c1fc9574277332d4 /base/server/python
parent18dec04095207ab9a9a4660191af0fec1bcc46da (diff)
downloadpki-90143ffad6a20493219c67b55c57724afb4edfde.tar.gz
pki-90143ffad6a20493219c67b55c57724afb4edfde.tar.xz
pki-90143ffad6a20493219c67b55c57724afb4edfde.zip
Simplified slot substitution.
Previously a deployment parameter has to be added to pkislots.cfg before it can be used in copy_with_slot_substitution(). The method has been modified to support substitutions using the deployment parameters directly, which simplifies the development. https://fedorahosted.org/pki/ticket/2278
Diffstat (limited to 'base/server/python')
-rw-r--r--base/server/python/pki/server/deployment/pkihelper.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py
index 2898d7fe0..4fc8afdd5 100644
--- a/base/server/python/pki/server/deployment/pkihelper.py
+++ b/base/server/python/pki/server/deployment/pkihelper.py
@@ -1780,6 +1780,52 @@ class File:
raise
return
+ def substitute_deployment_params(self, line):
+ """
+ Replace all occurrences of [param] in the line with the value of the deployment parameter.
+ """
+
+ # find the first parameter in the line
+ begin = line.find('[')
+
+ # repeat while there are parameters in the line
+ while begin >= 0:
+
+ # find the end of the parameter
+ end = line.find(']', begin + 1)
+
+ # if the end not is found not found, don't do anything
+ if end < 0:
+ return line
+
+ # get parameter name
+ name = line[begin + 1:end]
+
+ try:
+ # get parameter value as string
+ value = str(self.mdict[name])
+
+ config.pki_log.debug(
+ log.PKIHELPER_SLOT_SUBSTITUTION_2,
+ line[begin:end + 1], value,
+ extra=config.PKI_INDENTATION_LEVEL_3)
+
+ # replace parameter with value
+ line = line[0:begin] + value + line[end + 1]
+
+ # calculate the new end position
+ end = begin + len(value) + 1
+
+ except KeyError:
+ # undefined parameter, skip
+ pass
+
+ # find the next parameter in the remainder of the line
+ begin = line.find('[', end + 1)
+
+ # return modified line
+ return line
+
def copy_with_slot_substitution(
self, old_name, new_name, uid=None, gid=None,
perms=config.PKI_DEPLOYMENT_DEFAULT_FILE_PERMISSIONS,
@@ -1805,8 +1851,11 @@ class File:
config.pki_log.info(log.PKIHELPER_COPY_WITH_SLOT_SUBSTITUTION_2,
old_name, new_name,
extra=config.PKI_INDENTATION_LEVEL_2)
+
with open(new_name, "w") as FILE:
for line in fileinput.FileInput(old_name):
+
+ # substitute registered slots
for slot in self.slots:
if slot != '__name__' and self.slots[slot] in line:
config.pki_log.debug(
@@ -1816,7 +1865,12 @@ class File:
line = line.replace(
self.slots[slot],
self.mdict[slot])
+
+ # substitute deployment parameters
+ line = self.substitute_deployment_params(line)
+
FILE.write(line)
+
if uid is None:
uid = self.identity.get_uid()
if gid is None: