[Libvir] PATCH: 7/10: python auth callback API

Jim Meyering jim at meyering.net
Tue Dec 4 21:10:28 UTC 2007


"Daniel P. Berrange" <berrange at redhat.com> wrote:
> This adds a binding for the virConnectOpenAuth() api in the python
> API. This allows a python function to be used as the callback.
>
> This short example code illustrates the use of the API from a
> python app which wants to support username/password credentials
> only.
>
>   from getpass import getpass
>   mydata = "Hello"
>
>   def getCred(creds, data):
>       print "yes"
>       print str(creds)
>       for cred in creds:
>           print cred[1] + ": ",
>           if cred[0] == libvirt.VIR_CRED_AUTHNAME:
>               data = sys.stdin.readline()
>               data = data[0:len(data)-1]
>               cred[4] = data
>           elif cred[0] == libvirt.VIR_CRED_PASSPHRASE:
>              cred[4] = getpass("")
>           else:
>              return -1
>       return 0
>
>   uri = "qemu+tcp://localhost/system"
>   conn = libvirt.openAuth(uri,
>                           [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE],
>                            getCred,
>                            mydata], 0)
>
>
>   print str(conn.listDefinedDomains())

Nice.
One nit:

> diff -r 8a79678f789f python/libvir.c
> --- a/python/libvir.c	Wed Nov 28 23:01:30 2007 -0500
> +++ b/python/libvir.c	Wed Nov 28 23:29:40 2007 -0500
...
> +static PyObject *
> +libvirt_virConnectOpenAuth(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
...
> +    auth.ncredtype = PyList_Size(pycredtype);
> +    if (auth.ncredtype) {
> +        int i;
> +        auth.credtype = malloc(sizeof(int) * auth.ncredtype);

Check for malloc failure:

        if (!auth.credtype)
            return NULL;

> +        for (i = 0 ; i < auth.ncredtype ; i++) {
> +            PyObject *val;
> +            val = PyList_GetItem(pycredtype, i);
> +            auth.credtype[i] = (int)PyLong_AsLong(val);
> +        }
> +    }
> +    auth.cb = pycredcb ? virConnectCredCallbackWrapper : NULL;
> +    auth.cbdata = pyauth;




More information about the libvir-list mailing list