[diffoscope] 01/01: Use ssh-keygen for comparing OpenSSH public keys

Emanuel Bronshtein e3amn2l-guest at moszumanska.debian.org
Thu Dec 22 16:26:52 CET 2016


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

e3amn2l-guest pushed a commit to branch master
in repository diffoscope.

commit 0309e93b05c914474ca9f3bcdf6487ec122fa3d4
Author: Emanuel Bronshtein <e3amn2l at gmx.com>
Date:   Thu Dec 22 17:26:41 2016 +0200

    Use ssh-keygen for comparing OpenSSH public keys
---
 debian/control                            |  1 +
 diffoscope/comparators/__init__.py        |  1 +
 diffoscope/comparators/openssh.py         | 42 ++++++++++++++++++++++++
 diffoscope/exc.py                         |  3 ++
 tests/comparators/test_openssh_pub_key.py | 54 +++++++++++++++++++++++++++++++
 tests/data/openssh_pub_key_expected_diff  |  3 ++
 tests/data/test_openssh_pub_key1.pub      |  1 +
 tests/data/test_openssh_pub_key2.pub      |  1 +
 8 files changed, 106 insertions(+)

diff --git a/debian/control b/debian/control
index 724aef3..242e3b4 100644
--- a/debian/control
+++ b/debian/control
@@ -30,6 +30,7 @@ Build-Depends:
  libjs-jquery-throttle-debounce <!nocheck>,
  llvm <!nocheck>,
  mono-utils <!nocheck>,
+ openssh-client <!nocheck>,
  pdftk <!nocheck>,
  poppler-utils <!nocheck>,
  python-argcomplete,
diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index fe47ad1..7af68b3 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -86,6 +86,7 @@ COMPARATORS = (
     ('image.ImageFile',),
     ('cbfs.CbfsFile',),
     ('git.GitIndexFile',),
+    ('openssh.PublicKeyFile',),
 )
 
 
diff --git a/diffoscope/comparators/openssh.py b/diffoscope/comparators/openssh.py
new file mode 100644
index 0000000..f912fb0
--- /dev/null
+++ b/diffoscope/comparators/openssh.py
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2016 Emanuel Bronshtein <e3amn2l at gmx.com>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
+
+import re
+
+from diffoscope import tool_required
+from diffoscope.difference import Difference
+from diffoscope.comparators.utils import Command
+from diffoscope.comparators.binary import File
+
+
+class SSHKeyList(Command):
+    @tool_required('ssh-keygen')
+    def cmdline(self):
+        return ['ssh-keygen', '-l', '-f', self.path]
+
+class PublicKeyFile(File):
+    RE_FILE_TYPE = re.compile(r'^OpenSSH \S+ public key')
+
+    @staticmethod
+    def recognizes(file):
+        return PublicKeyFile.RE_FILE_TYPE.match(file.magic_file_type)
+
+    def compare_details(self, other, source=None):
+        return [Difference.from_command(SSHKeyList, self.path, other.path)]
+
diff --git a/diffoscope/exc.py b/diffoscope/exc.py
index d87ef76..1cad308 100644
--- a/diffoscope/exc.py
+++ b/diffoscope/exc.py
@@ -171,6 +171,9 @@ class RequiredToolNotFound(Exception):
         'sng': {
             'debian': 'sng',
         },
+        'ssh-keygen': {
+            'debian': 'openssh-client',
+        },
         'stat': {
             'debian': 'coreutils',
             'arch': 'coreutils',
diff --git a/tests/comparators/test_openssh_pub_key.py b/tests/comparators/test_openssh_pub_key.py
new file mode 100644
index 0000000..ac42f48
--- /dev/null
+++ b/tests/comparators/test_openssh_pub_key.py
@@ -0,0 +1,54 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2016 Emanuel Bronshtein <e3amn2l at gmx.com>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
+
+import pytest
+
+from diffoscope.config import Config
+from diffoscope.comparators.openssh import PublicKeyFile
+from diffoscope.comparators.binary import NonExistingFile
+
+from utils import skip_unless_tools_exist, data, load_fixture
+
+# Generated by: ssh-keygen -t dsa -C "Test1"
+opensshpubkey1 = load_fixture(data('test_openssh_pub_key1.pub'))
+# Generated by: ssh-keygen -t rsa -b 4096 -C "Test2"
+opensshpubkey2 = load_fixture(data('test_openssh_pub_key2.pub'))
+
+def test_identification(opensshpubkey1):
+    assert isinstance(opensshpubkey1, PublicKeyFile)
+
+def test_no_differences(opensshpubkey1):
+    difference = opensshpubkey1.compare(opensshpubkey1)
+    assert difference is None
+
+ at pytest.fixture
+def differences(opensshpubkey1, opensshpubkey2):
+    return opensshpubkey1.compare(opensshpubkey2).details
+
+ at skip_unless_tools_exist('ssh-keygen')
+def test_diff(differences):
+    expected_diff = open(data('openssh_pub_key_expected_diff')).read()
+    assert differences[0].unified_diff == expected_diff
+
+ at skip_unless_tools_exist('ssh-keygen')
+def test_compare_non_existing(monkeypatch, opensshpubkey1):
+    monkeypatch.setattr(Config(), 'new_file', True)
+    difference = opensshpubkey1.compare(NonExistingFile('/nonexisting', opensshpubkey1))
+    assert difference.source2 == '/nonexisting'
+    assert len(difference.details) > 0
diff --git a/tests/data/openssh_pub_key_expected_diff b/tests/data/openssh_pub_key_expected_diff
new file mode 100644
index 0000000..0b6ec62
--- /dev/null
+++ b/tests/data/openssh_pub_key_expected_diff
@@ -0,0 +1,3 @@
+@@ -1 +1 @@
+-1024 SHA256:v/O+0ETvi2H5TGRXky1RhQ1/WFwLlPpxch5E2Mrj6FM Test1 (DSA)
++4096 SHA256:9dH1CMkA6DSfPWU7vNwdPKS5/ppN4LMdvHTP60l7aSA Test2 (RSA)
diff --git a/tests/data/test_openssh_pub_key1.pub b/tests/data/test_openssh_pub_key1.pub
new file mode 100644
index 0000000..a2674ad
--- /dev/null
+++ b/tests/data/test_openssh_pub_key1.pub
@@ -0,0 +1 @@
+ssh-dss AAAAB3NzaC1kc3MAAACBAIa8YajID9g38jwQm8sNBGoGkkwIOv6sQ8k+Bcq8oPXPy1FRXWcra6Kd3iKqApIzLuZUvoYO/f3G2K4lue5yrv72rgwANWmyL4dHVXgcsjwvWwjOl6o4xWTPFspkdNcAAMcZfzG0+w1AOkQxhwMsnK380m3J9a3VOWugUiU4fV1jAAAAFQDzkrIZuJoxlxes564ltb2Vn3hnpQAAAIBHr6uzpiSeSkWLuItB00hHx1RHtBns0zaheNFTTUMGftxtfBU2eBLqObcTlqHJZ3UUY3/YAvD6Ux/uLSgUzEe7JaqvHcgML3K5V4HWIwE0ARRIwzrfU4cAErJObmZZ/OXbXNNRmW2IJgQJI52x4gVuSt0EEuctzASOOvyPA8IekAAAAIA7xe4o0o/ZwUqfWKR9K4QrbPPa6/D4ruFVhMcRJEE/A1LMY1Xo4nVSRU5bxzvMmJPBZvsbR5NEE3Cg [...]
diff --git a/tests/data/test_openssh_pub_key2.pub b/tests/data/test_openssh_pub_key2.pub
new file mode 100644
index 0000000..3e44cdb
--- /dev/null
+++ b/tests/data/test_openssh_pub_key2.pub
@@ -0,0 +1 @@
+ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDDfey/WO9EfGVjcYaBkuSfFYRSu+qSHCfZ55W8XMX/oc583USAofVD+vdq3ekG8J3Z9sKeIJbXzbxOsi1TrDzj4acCj8Sfp3PyMYr7bHkc0Jkp9p1DxY33+SS2zuJOQUwNU763HAJ3jdA3D2Y9EzXTwhuB2zuVEPNU1HGeAEldbdoa7kycPt/3UklBe6RzoFVYaODY1un9pBUVOZQuQomBrsbGKTeLk5w4b5pt/Sgd7aSpEPEyY/mB62Ac9ZXpPpwK/wIeo36kbfWyjYeM7YkyKoej6JHgG8S1YS+zZQGi9XDSWdIs7l31McIJO1XA1udHagKD114a3v4OMeH+Gn8zTkIyQ4gZovP9vmtoWGrZdq3yWjMZS68/ST+oEk6Wvh/LC7+D5+XR8ENViTE03KaoG+40ZwL2XSQ7WmVUfIS75Q4dOnhgnwLpl3mGlZQ97czlJ/pf [...]

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list