Пример #1
0
class TestKeyServer(unittest.TestCase):
    """
    Unit test for KeyServer class.
    """

    def setUp(self):
        """
        Set up random KeyServer.
        """
        self.server_host = choice(KEY_SERVERS)
        self.serv = KeyServer(self.server_host)
        self.begin_header = '-----BEGIN PGP PUBLIC KEY BLOCK-----'
        self.end_header = '-----END PGP PUBLIC KEY BLOCK-----'

    def test_init(self):
        """
        Test KeyServer constructor.
        """
        self.assertEqual(self.serv.host, self.server_host)
        self.assertEqual(self.serv.port, PORT)

    def test_search(self):
        """
        Test search with keyid, fingerprint and uid.
        """
        result = self.serv.search(KEYID)
        self.assertEqual(len(result), 1)
        result[0].keyid = KEYID
        result[0].identities[0].uid = UID

        result = self.serv.search(FINGERPRINT)
        self.assertEqual(len(result), 1)
        result[0].keyid = KEYID
        result[0].identities[0].uid = UID

        result = self.serv.search(UID)
        self.assertEqual(len(result), 1)
        result[0].keyid = KEYID
        result[0].identities[0].uid = UID

    def test_add(self):
        """
        Test ASCII armored key upload
        """
        stored_key = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            'ubuntu.key',
        )
        key = open(stored_key, 'r').read()
        self.serv.add('%s\n\n%s\n%s' % (
            self.begin_header,
            key,
            self.end_header,
        ))
Пример #2
0
def pgp(ctx, search_string, key_id, all, server):
    '''Search for a PGP key on keyservers.

       The following keyservers are searched in order until a match is found:

       - pool.sks-keyservers.net

       - keys.gnupg.net

       - pgp.mit.edu

       - keyserver.ubuntu.com

       - zimmermann.mayfirst.org
    '''

    servers = []
    if server:
        servers.append(server)
    else:
        doc = pgp.__doc__
        for line in doc.split("\n"):
            delimiter = '       - '
            if not line.startswith(delimiter):
                continue
            server = line.replace(delimiter, '')
            servers.append(server)

    for server in servers:
        addr = 'http://{}'.format(server)
        click.echo('Searching {}'.format(addr), err=True)
        serv = KeyServer(addr)
        try:
            responses = serv.search(search_string)
        except Exception as e:
            msg = "Error from server: {}".format(e.msg)
            click.echo(msg, err=True)
            continue
        # FIXME: DRY up this bit of code with jwk code too
        keys = []
        for key in responses:
            if 'RSA' in key.algo:
                keys.append(key)
        if not key_id and len(keys) > 1:
            click.echo("Multiple keys found: ", err=True)
            for key in keys:
                click.echo("  - {}".format(key.keyid), err=True)
            msg = "Printing the first key ('{}')".format(keys[0].keyid)
            click.echo(msg, err=True)
        key_id_to_print = None
        if key_id:
            key_id_to_print = key_id
        else:
            key_id_to_print = keys[0].keyid
        for key in keys:
            if key.keyid != key_id_to_print:
                continue
            print key.key
        if not all:
            return
Пример #3
0
    def query(self, term):
        results = {}
        for keyserver in self.keyservers:
            url = u'http://%s' % keyserver
            server = KeyServer(url)
            try:
                key_list = server.search(term)
                for key in key_list:
                    results[key.keyid] = key
            except:
                pass

        return results.values()
Пример #4
0
    def query(self, term):
        results = {}
        for keyserver in self.keyservers:
            url = u'http://%s' % keyserver
            server = KeyServer(url)
            try:
                key_list = server.search(term)
                for key in key_list:
                    results[key.keyid] = key
            except:
                pass

        return results.values()
Пример #5
0
class TestKeyServer(unittest.TestCase):
    """
    Unit test for KeyServer class.
    """

    def setUp(self):
        """
        Set up random KeyServer.
        """
        self.server_host = KEY_SERVER
        self.serv = KeyServer(self.server_host)
        self.begin_header = '-----BEGIN PGP PUBLIC KEY BLOCK-----'
        self.end_header = '-----END PGP PUBLIC KEY BLOCK-----'
        DINGUS.reset()
        DINGUS.return_value = load_fixture('search_answer')

    def test_init(self):
        """
        Test KeyServer constructor.
        """
        self.assertEqual(self.serv.host, self.server_host)
        self.assertEqual(self.serv.port, PORT)

    @patch('urllib2.urlopen', DINGUS)
    def test_search_by_id(self):
        """
        Test search with keyid.
        """
        result = self.serv.search(KEYID)
        search_url = (self.server_host + ':11371/pks/lookup'
                '?search=' + KEYID + '&exact=off&options=mr&op=index')
        self.assertTrue(DINGUS.calls('()', search_url).once())
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].keyid, KEYID[2:])
        self.assertEqual(result[0].identities[0].uid, FULL_UID)

    @patch('urllib2.urlopen', DINGUS)
    def test_search_by_fingerprint(self):
        """
        Test search with fingerprint.
        """
        result = self.serv.search(FINGERPRINT)
        search_url = (self.server_host + ':11371/pks/lookup'
                '?search=' + FINGERPRINT + '&exact=off&options=mr&op=index')
        self.assertTrue(DINGUS.calls('()', search_url).once())
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].keyid, KEYID[2:])
        self.assertEqual(result[0].identities[0].uid, FULL_UID)

    @patch('urllib2.urlopen', DINGUS)
    def test_search_by_uid(self):
        """
        Test search with uid.
        """
        result = self.serv.search(UID)
        search_url = (self.server_host + ':11371/pks/lookup'
                '?search=' + UID.replace(' ', '+') +
                '&exact=off&options=mr&op=index')
        self.assertTrue(DINGUS.calls('()', search_url).once())
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].keyid, KEYID[2:])
        self.assertEqual(result[0].identities[0].uid, FULL_UID)

    @patch('urllib2.urlopen', DINGUS)
    def test_add(self):
        """
        Test ASCII armored key upload
        """
        stored_key = 'ubuntu.key'
        key = load_fixture(stored_key).read()
        keytext = '%s\n\n%s\n%s' % (
            self.begin_header,
            key,
            self.end_header,
        )

        self.serv.add(keytext)

        add_url = self.server_host + ':11371/pks/add'
        self.assertTrue(DINGUS.calls('()', add_url,
            urlencode({'keytext': keytext})
            ).once())
Пример #6
0
class TestKeyServer(unittest.TestCase):
    """
    Unit test for KeyServer class.
    """
    def setUp(self):
        """
        Set up random KeyServer.
        """
        self.server_host = KEY_SERVER
        self.serv = KeyServer(self.server_host)
        self.begin_header = '-----BEGIN PGP PUBLIC KEY BLOCK-----'
        self.end_header = '-----END PGP PUBLIC KEY BLOCK-----'
        DINGUS.reset()
        DINGUS.return_value = load_fixture('search_answer')

    def test_init(self):
        """
        Test KeyServer constructor.
        """
        self.assertEqual(self.serv.host, self.server_host)
        self.assertEqual(self.serv.port, PORT)

    @patch('urllib2.urlopen', DINGUS)
    def test_search_by_id(self):
        """
        Test search with keyid.
        """
        result = self.serv.search(KEYID)
        search_url = (self.server_host + ':11371/pks/lookup'
                      '?search=' + KEYID + '&exact=off&options=mr&op=index')
        self.assertTrue(DINGUS.calls('()', search_url).once())
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].keyid, KEYID[2:])
        self.assertEqual(result[0].identities[0].uid, FULL_UID)

    @patch('urllib2.urlopen', DINGUS)
    def test_search_by_fingerprint(self):
        """
        Test search with fingerprint.
        """
        result = self.serv.search(FINGERPRINT)
        search_url = (self.server_host + ':11371/pks/lookup'
                      '?search=' + FINGERPRINT +
                      '&exact=off&options=mr&op=index')
        self.assertTrue(DINGUS.calls('()', search_url).once())
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].keyid, KEYID[2:])
        self.assertEqual(result[0].identities[0].uid, FULL_UID)

    @patch('urllib2.urlopen', DINGUS)
    def test_search_by_uid(self):
        """
        Test search with uid.
        """
        result = self.serv.search(UID)
        search_url = (self.server_host + ':11371/pks/lookup'
                      '?search=' + UID.replace(' ', '+') +
                      '&exact=off&options=mr&op=index')
        self.assertTrue(DINGUS.calls('()', search_url).once())
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].keyid, KEYID[2:])
        self.assertEqual(result[0].identities[0].uid, FULL_UID)

    @patch('urllib2.urlopen', DINGUS)
    def test_add(self):
        """
        Test ASCII armored key upload
        """
        stored_key = 'ubuntu.key'
        key = load_fixture(stored_key).read()
        keytext = '%s\n\n%s\n%s' % (
            self.begin_header,
            key,
            self.end_header,
        )

        self.serv.add(keytext)

        add_url = self.server_host + ':11371/pks/add'
        self.assertTrue(
            DINGUS.calls('()', add_url, urlencode({'keytext':
                                                   keytext})).once())