Repository: privacyidea/privacyidea

Issue:
Add encoding to Popen calls.
Add encoding to Popen

https://github.com/privacyidea/privacyidea/blob/0989bea33833728f3cdae33d42c71ab9284de276/privacyidea/lib/caconnectors/localca.py#L426


Discussion:
We probably only need to do this in the occurrances with ``communicate``.

Resolving patch:
diff --git a/privacyidea/lib/caconnectors/localca.py b/privacyidea/lib/caconnectors/localca.py
--- a/privacyidea/lib/caconnectors/localca.py
+++ b/privacyidea/lib/caconnectors/localca.py
@@ -32,6 +32,7 @@
 from privacyidea.lib.caconnectors.baseca import BaseCAConnector
 from OpenSSL import crypto
 from subprocess import Popen, PIPE
+from privacyidea.lib.utils import my_Popen
 import yaml
 import datetime
 import shlex
@@ -423,7 +424,7 @@ def sign_request(self, csr, options=None):
                                                           certificate_filename))
         # run the command
         args = shlex.split(cmd)
-        p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
+        p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
         result, error = p.communicate()
         if p.returncode != 0:  # pragma: no cover
             # Some error occurred
@@ -508,7 +509,7 @@ def revoke_cert(self, certificate, reason=CRL_REASONS[0]):
                                reason=reason)
         workingdir = self.config.get(ATTR.WORKING_DIR)
         args = shlex.split(cmd)
-        p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
+        p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
         result, error = p.communicate()
         if p.returncode != 0:  # pragma: no cover
             # Some error occurred
@@ -553,7 +554,7 @@ def create_crl(self, publish=True, check_validity=False):
                                          config=self.config.get(ATTR.OPENSSL_CNF),
                                          CRL=crl)
             args = shlex.split(cmd)
-            p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
+            p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
             result, error = p.communicate()
             if p.returncode != 0:  # pragma: no cover
                 # Some error occurred
@@ -702,7 +703,7 @@ def _init_ca(config):
     print("Running command...")
     print(command)
     args = shlex.split(command)
-    p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory)
+    p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory)
     result, error = p.communicate()
     if p.returncode != 0:  # pragma: no cover
         # Some error occurred
@@ -716,7 +717,7 @@ def _init_ca(config):
     print("Running command...")
     print(command)
     args = shlex.split(command)
-    p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory)
+    p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory)
     result, error = p.communicate()
     if p.returncode != 0:  # pragma: no cover
         # Some error occurred
diff --git a/privacyidea/lib/utils/__init__.py b/privacyidea/lib/utils/__init__.py
--- a/privacyidea/lib/utils/__init__.py
+++ b/privacyidea/lib/utils/__init__.py
@@ -49,6 +49,7 @@
 import pkg_resources
 import time
 import cgi
+from subprocess import Popen
 
 from privacyidea.lib.error import ParameterError, ResourceNotFoundError
 
@@ -1336,4 +1337,15 @@ def check_serial_valid(serial):
     """
     if not re.match(ALLOWED_SERIAL, serial):
         raise ParameterError("Invalid serial number. Must comply to {0!s}.".format(ALLOWED_SERIAL))
-    return True
\ No newline at end of file
+    return True
+
+
+def my_Popen(command, stdin=None, stdout=None, stderr=None, cwd=None, shell=False,
+             encoding='utf8'):
+    try:
+        p = Popen(command, stdin=stdin, stdout=stdout, stderr=stderr,
+                  cwd=cwd, shell=shell, encoding=encoding)
+    except TypeError:
+        p = Popen(command, stdin=stdin, stdout=stdout, stderr=stderr,
+                  cwd=cwd, shell=shell)
+    return p


Test patch: