[X2Go-Commits] [python-x2go] 01/01: x2go/_paramiko.py: Monkey-patch PKey._write_private_key() method as it is broken under Python3.

git-admin at x2go.org git-admin at x2go.org
Fri Dec 27 15:29:54 CET 2019


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository python-x2go.

commit 4cf1023d3c0cb1a6bb7d54bc4d990b6cfaf8f2ba
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Fri Dec 27 14:27:07 2019 +0000

    x2go/_paramiko.py: Monkey-patch PKey._write_private_key() method as it is broken under Python3.
---
 debian/changelog  |  3 +++
 x2go/_paramiko.py | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/debian/changelog b/debian/changelog
index 828f991..b2fb6ac 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -7,6 +7,9 @@ python-x2go (0.6.1.3-0x2go1) UNRELEASED; urgency=medium
       needs an assignment.
     - Check availability of X2Go KDrive base support server-side before
       firing up x2gokdriveclient.
+    - x2go/_paramiko.py: Monkey-patch PKey._write_private_key() method as
+      it is broken under Python3.
+      See: https://github.com/paramiko/paramiko/pull/1583/
 
  -- X2Go Release Manager <git-admin at x2go.org>  Thu, 26 Dec 2019 10:37:02 +0100
 
diff --git a/x2go/_paramiko.py b/x2go/_paramiko.py
index c3f1d3b..c2e31e8 100644
--- a/x2go/_paramiko.py
+++ b/x2go/_paramiko.py
@@ -27,6 +27,7 @@ __name__    = 'x2go._paramiko'
 
 import paramiko
 import re
+import sys
 try:
     from paramiko.config import SSH_PORT
 except ImportError:
@@ -34,6 +35,8 @@ except ImportError:
 import platform
 from x2go.utils import compare_versions
 
+from cryptography.hazmat.primitives import serialization
+
 PARAMIKO_VERSION = paramiko.__version__.split()[0]
 PARAMIKO_FEATURE = {
     'forward-ssh-agent': compare_versions(PARAMIKO_VERSION, ">=", '1.8.0') and (platform.system() != "Windows"),
@@ -42,8 +45,36 @@ PARAMIKO_FEATURE = {
     'host-entries-reloadable': compare_versions(PARAMIKO_VERSION, ">=", '1.11.0'),
     'preserve-known-hosts': compare_versions(PARAMIKO_VERSION, ">=", '1.11.0'),
     'ecdsa-hostkeys': compare_versions(PARAMIKO_VERSION, ">=", '1.11.6'),
+    'write-private-key-py3': False,
 }
 
+def _Pkey_write_private_key(self, f, key, format, password=None):
+    """\
+    See https://github.com/paramiko/paramiko/pull/1583/
+
+    The upstream version has a bug when run under Python3.
+
+    Monkey patching it until it has been fixed upstream.
+
+    """
+    if password is None:
+        encryption = serialization.NoEncryption()
+    else:
+        encryption = serialization.BestAvailableEncryption(b(password))
+
+    if sys.version_info[0] == 2:
+        f.write(
+            key.private_bytes(
+                serialization.Encoding.PEM, format, encryption
+            )
+        ).decode()
+    else:
+        f.write(
+            key.private_bytes(
+                serialization.Encoding.PEM, format, encryption
+            )
+        )
+
 def _SSHClient_save_host_keys(self, filename):
     """\
     Available since paramiko 1.11.0...
@@ -147,3 +178,5 @@ def monkey_patch_paramiko():
         paramiko.hostkeys.HostKeys.load = _HostKeys_load
     if not PARAMIKO_FEATURE['hash-host-entries']:
         paramiko.hostkeys.HostKeys.add = _HostKeys_add
+    if not PARAMIKO_FEATURE['write-private-key-py3']:
+        paramiko.pkey.PKey._write_private_key = _Pkey_write_private_key

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/python-x2go.git


More information about the x2go-commits mailing list