From: Michael Tremer Date: Thu, 1 Jun 2023 18:01:07 +0000 (+0000) Subject: _pakfire: Implement loading keys X-Git-Tag: 0.9.29~130 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=44cbdc0a520016feb730cde288f87247bde62adf;p=people%2Fms%2Fpakfire.git _pakfire: Implement loading keys Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 510a75bb4..17cf72c00 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -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, diff --git a/src/_pakfire/util.c b/src/_pakfire/util.c index f0842b101..3fda33ed4 100644 --- a/src/_pakfire/util.c +++ b/src/_pakfire/util.c @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -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; +} diff --git a/src/_pakfire/util.h b/src/_pakfire/util.h index c014a08e1..adf9f651f 100644 --- a/src/_pakfire/util.h +++ b/src/_pakfire/util.h @@ -22,6 +22,7 @@ #define PYTHON_PAKFIRE_UTIL_H #include +#include #include #include @@ -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 */ diff --git a/tests/python/keys.py b/tests/python/keys.py index 0f9dd4637..271bffd04 100755 --- a/tests/python/keys.py +++ b/tests/python/keys.py @@ -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()