]> git.ipfire.org Git - pakfire.git/commitdiff
_pakfire: Implement loading keys
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Jun 2023 18:01:07 +0000 (18:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 1 Jun 2023 18:01:58 +0000 (18:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/_pakfire/util.c
src/_pakfire/util.h
tests/python/keys.py

index 510a75bb493b3928d0278cf9abee1d82763ec85b..17cf72c0079a1fb45e53ad55a45cbe139a62a4ad 100644 (file)
@@ -616,6 +616,42 @@ static PyObject* Pakfire_generate_key(PakfireObject* self, PyObject* args, PyObj
        return object;
 }
 
+static PyObject* Pakfire_import_key(PakfireObject* self, PyObject* args) {
+       struct pakfire_key* key = NULL;
+       PyObject* object = NULL;
+       PyObject* file = NULL;
+       int r;
+
+       // Parse arguments
+       if (!PyArg_ParseTuple(args, "O", &file))
+               return NULL;
+
+       // Treat the object as a file
+       FILE* f = PyObject_AsFileHandle(file, "r");
+       if (!f)
+               return NULL;
+
+       // Import the key
+       r = pakfire_key_import(&key, self->pakfire, f);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               goto ERROR;
+       }
+
+       // Convert the key into a Key object
+       object = new_key(&KeyType, key);
+       if (!object)
+               goto ERROR;
+
+ERROR:
+       if (key)
+               pakfire_key_unref(key);
+       if (f)
+               fclose(f);
+
+       return object;
+}
+
 static PyObject* Pakfire_whatprovides(PakfireObject* self, PyObject* args) {
        const char* provides = NULL;
        struct pakfire_packagelist* list = NULL;
@@ -1487,6 +1523,12 @@ static struct PyMethodDef Pakfire_methods[] = {
                METH_VARARGS,
                NULL
        },
+       {
+               "import_key",
+               (PyCFunction)Pakfire_import_key,
+               METH_VARARGS,
+               NULL,
+       },
        {
                "install",
                (PyCFunction)Pakfire_install,
index f0842b10156e343cb288cee674dab07fbb4fb79b..3fda33ed4e0d7f1da17f56a51edd3cad457a4402 100644 (file)
@@ -23,6 +23,7 @@
 #include <datetime.h>
 
 #include <errno.h>
+#include <stdio.h>
 
 #include <pakfire/filelist.h>
 #include <pakfire/package.h>
@@ -110,3 +111,22 @@ PyObject* PyDateTime_FromTime_t(const time_t* t) {
                0
        );
 }
+
+FILE* PyObject_AsFileHandle(PyObject* object, const char* mode) {
+       // Get a file descriptor
+       int fd = PyObject_AsFileDescriptor(object);
+       if (fd < 0)
+               return NULL;
+
+       // Duplicate the file handle
+       fd = dup(fd);
+
+       // Convert to file handle
+       FILE* f = fdopen(fd, mode);
+       if (!f) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       return f;
+}
index c014a08e145c0a3b25c7b5301dcd296d6800c78b..adf9f651fcc0238159f5c35f47b1cb3bebaf0112 100644 (file)
@@ -22,6 +22,7 @@
 #define PYTHON_PAKFIRE_UTIL_H
 
 #include <Python.h>
+#include <stdio.h>
 
 #include <pakfire/filelist.h>
 #include <pakfire/packagelist.h>
@@ -31,4 +32,6 @@ PyObject* PyList_FromFileList(struct pakfire_filelist* filelist);
 
 PyObject* PyDateTime_FromTime_t(const time_t* t);
 
+FILE* PyObject_AsFileHandle(PyObject* object, const char* mode);
+
 #endif /* PYTHON_PAKFIRE_UTIL_H */
index 0f9dd46376db405b8cdb6fdf7471d5cfb30d010d..271bffd04e281fd170e852116a82b9528f2a6d36 100755 (executable)
@@ -49,6 +49,21 @@ class KeysTests(unittest.TestCase):
                # Check that the algorithm matches
                self.assertEqual(key.algorithm, "Ed255919")
 
+       def _import(self, path):
+               path = os.path.join(
+                       os.environ.get("TEST_DATA_DIR"), path,
+               )
+
+               with open(path, "rb") as f:
+                       return self.pakfire.import_key(f)
+
+       def test_import(self):
+               # Import a public key
+               key = self._import("keys/key1.pub")
+
+               # Import a secret key
+               key = self._import("keys/key1.sec")
+
 
 if __name__ == "__main__":
        unittest.main()