Repository: privacyidea/privacyidea

Issue:
pi-manage backup restore fails with charset
pi-manage backup restore does not work, when the database has some charset:

ERROR 1044 (42000): Access denied for user 'pi'@'localhost' to database 'pi?charset=utf8mb4'



Discussion:

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
@@ -31,8 +31,7 @@
 from privacyidea.lib.utils import int_to_hex, to_unicode
 from privacyidea.lib.caconnectors.baseca import BaseCAConnector
 from OpenSSL import crypto
-from subprocess import Popen, PIPE
-from privacyidea.lib.utils import my_Popen
+from subprocess import Popen, PIPE  
 import yaml
 import datetime
 import shlex
@@ -424,7 +423,7 @@ def sign_request(self, csr, options=None):
                                                           certificate_filename))
         # run the command
         args = shlex.split(cmd)
-        p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
+        p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir, universal_newlines=True)
         result, error = p.communicate()
         if p.returncode != 0:  # pragma: no cover
             # Some error occurred
@@ -509,7 +508,7 @@ def revoke_cert(self, certificate, reason=CRL_REASONS[0]):
                                reason=reason)
         workingdir = self.config.get(ATTR.WORKING_DIR)
         args = shlex.split(cmd)
-        p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
+        p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir, universal_newlines=True)
         result, error = p.communicate()
         if p.returncode != 0:  # pragma: no cover
             # Some error occurred
@@ -554,7 +553,7 @@ def create_crl(self, publish=True, check_validity=False):
                                          config=self.config.get(ATTR.OPENSSL_CNF),
                                          CRL=crl)
             args = shlex.split(cmd)
-            p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir)
+            p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=workingdir, universal_newlines=True)
             result, error = p.communicate()
             if p.returncode != 0:  # pragma: no cover
                 # Some error occurred
@@ -703,7 +702,7 @@ def _init_ca(config):
     print("Running command...")
     print(command)
     args = shlex.split(command)
-    p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory)
+    p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory, universal_newlines=True)
     result, error = p.communicate()
     if p.returncode != 0:  # pragma: no cover
         # Some error occurred
@@ -717,7 +716,7 @@ def _init_ca(config):
     print("Running command...")
     print(command)
     args = shlex.split(command)
-    p = my_Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory)
+    p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=config.directory, universal_newlines=True)
     result, error = p.communicate()
     if p.returncode != 0:  # pragma: no cover
         # Some error occurred
diff --git a/privacyidea/lib/eventhandler/scripthandler.py b/privacyidea/lib/eventhandler/scripthandler.py
--- a/privacyidea/lib/eventhandler/scripthandler.py
+++ b/privacyidea/lib/eventhandler/scripthandler.py
@@ -189,7 +189,7 @@ def do(self, action, options=None):
         rcode = 0
         try:
             log.info("Starting script {script!r}.".format(script=script_name))
-            p = subprocess.Popen(proc_args, cwd=self.script_directory)
+            p = subprocess.Popen(proc_args, cwd=self.script_directory, universal_newlines=True)
             if handler_options.get("background") == SCRIPT_WAIT:
                 rcode = p.wait()
 
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
@@ -1338,14 +1338,3 @@ 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
-
-
-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: