From c520fad552a12d50a9efe22dadbf978f6f181675 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Sun, 16 Aug 2015 19:00:00 +0200
Subject: [PATCH 36/38] Py3 compatibility: encode output of subprocess call

In Python 3 subprocess.Popen() and check_out() return bytes. The rest of
PKI expects text, so the output has to be decoded.

- ascii for dnsdomainname
- sys.getfilesystemencoding() for paths
- utf-8 for the rest
---
 base/java-tools/bin/pki                                   | 5 +++--
 base/server/python/pki/server/deployment/pkihelper.py     | 8 +++++---
 base/server/python/pki/server/deployment/pkiparser.py     | 7 +++++--
 base/server/sbin/pkidestroy                               | 8 +++-----
 base/server/sbin/pkispawn                                 | 7 +++----
 base/server/upgrade/10.1.99/02-ReplaceJettisonWithJackson | 4 +++-
 base/server/upgrade/10.1.99/03-AddedRESTEasyClient        | 4 +++-
 7 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/base/java-tools/bin/pki b/base/java-tools/bin/pki
index 568093892584ef25d1c4d35addf86b8e0376d600..4b8e69a55f38fa9a6e5272520b585e9ed7d23be1 100644
--- a/base/java-tools/bin/pki
+++ b/base/java-tools/bin/pki
@@ -31,7 +31,7 @@ def run_java_cli(args):
     value = subprocess.check_output(
         '. /etc/pki/pki.conf && echo $RESTEASY_LIB',
         shell=True)
-    resteasy_lib = str(value).strip()
+    resteasy_lib = value.decode(sys.getfilesystemencoding()).strip()
 
     # construct classpath
     classpath = [
@@ -92,7 +92,8 @@ def main(argv):
     value = subprocess.check_output(
         '. /etc/pki/pki.conf && echo $PKI_CLI_OPTIONS',
         shell=True)
-    args = shlex.split(value.strip())
+    value = value.decode(sys.getfilesystemencoding()).strip()
+    args = shlex.split(value)
     args.extend(argv[1:])
 
     client_type = 'java'
diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py
index 35e218a7b8c2b30946f4d84c3d1086b71bacab1b..61f04d215ba3eba48b7e18733fd58b29555ced83 100644
--- a/base/server/python/pki/server/deployment/pkihelper.py
+++ b/base/server/python/pki/server/deployment/pkihelper.py
@@ -2717,10 +2717,10 @@ class Modutil:
         # execute command
         p = subprocess.Popen(command, stdout=subprocess.PIPE)
         output = p.communicate()[0]
-
         p.wait()
         # ignore return code due to issues with HSM
         # https://fedorahosted.org/pki/ticket/1444
+        output = output.decode('utf-8')
 
         # find modules from lines such as '1. NSS Internal PKCS #11 Module'
         modules = re.findall(r'^ +\d+\. +(.*)$', output, re.MULTILINE)
@@ -3052,7 +3052,7 @@ class KRAConnector:
 
         output = subprocess.check_output(command,
                                          stderr=subprocess.STDOUT)
-
+        output = output.decode('utf-8')
         error = re.findall("ClientResponseFailure:(.*?)", output)
         if error:
             config.pki_log.warning(
@@ -3206,7 +3206,7 @@ class TPSConnector:
         output = subprocess.check_output(command,
                                          stderr=subprocess.STDOUT,
                                          shell=False)
-
+        output = output.decode('utf-8')
         error = re.findall("ClientResponseFailure:(.*?)", output)
         if error:
             config.pki_log.warning(
@@ -3304,6 +3304,7 @@ class SecurityDomain:
                 output = subprocess.check_output(
                     command,
                     stderr=subprocess.STDOUT)
+                output = output.decode('utf-8')
             except subprocess.CalledProcessError:
                 config.pki_log.warning(
                     log.PKIHELPER_SECURITY_DOMAIN_UNREACHABLE_1,
@@ -3418,6 +3419,7 @@ class SecurityDomain:
         try:
             output = subprocess.check_output(command,
                                              stderr=subprocess.STDOUT)
+            output = output.decode('utf-8')
             return output
         except subprocess.CalledProcessError as exc:
             config.pki_log.warning(
diff --git a/base/server/python/pki/server/deployment/pkiparser.py b/base/server/python/pki/server/deployment/pkiparser.py
index a36e51dadd393ad9e27999312d2eef052113f410..b7267a3a24ab9236f12d2d2c895889330ebf6585 100644
--- a/base/server/python/pki/server/deployment/pkiparser.py
+++ b/base/server/python/pki/server/deployment/pkiparser.py
@@ -31,6 +31,7 @@ import random
 import requests.exceptions
 import string
 import subprocess
+import sys
 import xml.etree.ElementTree as ET
 
 from six.moves import input, range  # pylint: disable=W0622,F0401
@@ -174,16 +175,18 @@ class PKIConfigParser:
         resteasy_lib = subprocess.check_output(
             '. /etc/pki/pki.conf && echo $RESTEASY_LIB',
             shell=True)
+        resteasy_lib = resteasy_lib.decode(sys.getfilesystemencoding())
         # workaround for pylint error E1103
-        resteasy_lib = str(resteasy_lib).strip()
+        resteasy_lib = resteasy_lib.strip()
 
         # JNI jar location
         jni_jar_dir = subprocess.check_output(
             '. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf '
             '&& echo $JNI_JAR_DIR',
             shell=True)
+        jni_jar_dir = jni_jar_dir.decode(sys.getfilesystemencoding())
         # workaround for pylint error E1103
-        jni_jar_dir = str(jni_jar_dir).strip()
+        jni_jar_dir = jni_jar_dir.strip()
 
         default_instance_name = 'pki-tomcat'
         default_http_port = '8080'
diff --git a/base/server/sbin/pkidestroy b/base/server/sbin/pkidestroy
index ff7296f7b1ac626cb4288233212d0c29df40212e..032c97984280a8c6ef701acfdeced6a066f56801 100755
--- a/base/server/sbin/pkidestroy
+++ b/base/server/sbin/pkidestroy
@@ -86,11 +86,9 @@ def main(argv):
     # Retrieve DNS domainname
     config.pki_dns_domainname = None
     try:
-        dnsdomainname = subprocess.check_output(
-            "dnsdomainname",
-            shell=True)
-        # workaround for pylint error E1103
-        config.pki_dns_domainname = str(dnsdomainname).rstrip('\n')
+        dnsdomainname = subprocess.check_output(["dnsdomainname"])
+        dnsdomainname = dnsdomainname.decode('ascii').rstrip('\n')
+        config.pki_dns_domainname = dnsdomainname
         if not len(config.pki_dns_domainname):
             print(log.PKI_DNS_DOMAIN_NOT_SET)
             sys.exit(1)
diff --git a/base/server/sbin/pkispawn b/base/server/sbin/pkispawn
index 7c87c52c428dd7493b310191bfc0058db8f3d051..f29dec33398e3fb5b7414d32043c547a83a3d1d4 100755
--- a/base/server/sbin/pkispawn
+++ b/base/server/sbin/pkispawn
@@ -88,10 +88,9 @@ def main(argv):
 
     # Retrieve DNS domainname
     try:
-        dnsdomainname = subprocess.check_output(
-            "dnsdomainname", shell=True)
-        # workaround for pylint error E1103
-        config.pki_dns_domainname = str(dnsdomainname).rstrip('\n')
+        dnsdomainname = subprocess.check_output(["dnsdomainname"])
+        dnsdomainname = dnsdomainname.decode('ascii').rstrip('\n')
+        config.pki_dns_domainname = dnsdomainname
         if not len(config.pki_dns_domainname):
             print(log.PKI_DNS_DOMAIN_NOT_SET)
             sys.exit(1)
diff --git a/base/server/upgrade/10.1.99/02-ReplaceJettisonWithJackson b/base/server/upgrade/10.1.99/02-ReplaceJettisonWithJackson
index 9e4fb4c124140d29366d93ac91587ee6db6411cd..9926b60bfc95b823ea43308ab923d60d718dac8c 100755
--- a/base/server/upgrade/10.1.99/02-ReplaceJettisonWithJackson
+++ b/base/server/upgrade/10.1.99/02-ReplaceJettisonWithJackson
@@ -21,6 +21,7 @@
 
 from __future__ import absolute_import
 import os
+import sys
 import subprocess
 
 import pki.server.upgrade
@@ -41,8 +42,9 @@ class ReplaceJettisonWithJackson(pki.server.upgrade.PKIServerUpgradeScriptlet):
         resteasy_lib = subprocess.check_output(
             '. /etc/pki/pki.conf && echo $RESTEASY_LIB',
             shell=True)
+        resteasy_lib = resteasy_lib.decode(sys.getfilesystemencoding())
         # workaround for pylint error E1103
-        resteasy_lib = str(resteasy_lib).strip()
+        resteasy_lib = resteasy_lib.strip()
 
         # remove old links
         link = os.path.join(common_lib, 'jettison.jar')
diff --git a/base/server/upgrade/10.1.99/03-AddedRESTEasyClient b/base/server/upgrade/10.1.99/03-AddedRESTEasyClient
index 40169bc66f597e755367daa90ca9fd9576d2d2a7..c38e0a62fa16229320f94d5a40266e79de60ed8f 100755
--- a/base/server/upgrade/10.1.99/03-AddedRESTEasyClient
+++ b/base/server/upgrade/10.1.99/03-AddedRESTEasyClient
@@ -21,6 +21,7 @@
 
 from __future__ import absolute_import
 import os
+import sys
 import subprocess
 
 import pki.server.upgrade
@@ -41,8 +42,9 @@ class AddedRESTEasyClient(pki.server.upgrade.PKIServerUpgradeScriptlet):
         resteasy_lib = subprocess.check_output(
             '. /etc/pki/pki.conf && echo $RESTEASY_LIB',
             shell=True)
+        resteasy_lib = resteasy_lib.decode(sys.getfilesystemencoding())
         # workaround for pylint error E1103
-        resteasy_lib = str(resteasy_lib).strip()
+        resteasy_lib = resteasy_lib.strip()
 
         # create new links
         source = os.path.join(resteasy_lib, 'resteasy-client.jar')
-- 
2.4.3

