File: gpg.py

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (69 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import inspect
import os
import subprocess
import tempfile
import json

from api_lib import APITest


def check_gpgkey_exists(gpg_key, keyring):
    p = subprocess.Popen([
        "gpg", "--no-default-keyring",
        "--keyring", keyring,
        "--fingerprint", gpg_key],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    p.communicate()
    if p.returncode != 0:
        raise Exception("gpg key does not exists")


class GPGAPITestAddKey(APITest):
    """
    POST /gpg/key
    """
    requiresGPG2 = True

    fixtureCmds = [
        "gpgconf --kill dirmngr",
        "gpgconf --launch dirmngr",
        "sleep 2"
    ]

    def check(self):
        with tempfile.NamedTemporaryFile(suffix=".pub") as keyring:
            gpgkeyid = "9E3E53F19C7DE460"
            resp = self.post("/api/gpg/key", json={
                "Keyserver": "hkp://keyserver.ubuntu.com:80",
                "Keyring": keyring.name,
                "GpgKeyID": gpgkeyid
            })
            if resp.status_code != 200:
                output = json.loads(resp.text)
                print(f"{output}\n")
            self.check_equal(resp.status_code, 200)
            check_gpgkey_exists(gpgkeyid, keyring.name)


class GPGAPITestAddKeyArmor(APITest):
    """
    POST /gpg/key
    """
    def check(self):
        keyfile = os.path.join(os.path.dirname(inspect.getsourcefile(APITest)),
                               "files") + "/launchpad.key"
        gpgkeyid = "3B1F56C0"

        with open(keyfile, 'r') as keyf:
            gpgkeyarmor = keyf.read()

        with tempfile.NamedTemporaryFile(suffix=".pub") as keyring:
            resp = self.post("/api/gpg/key", json={
                "Keyring": keyring.name,
                "GpgKeyArmor": gpgkeyarmor
            })

            self.check_equal(resp.status_code, 200)
            check_gpgkey_exists(gpgkeyid, keyring.name)