Libvirt include sasl authentication setup

inlude sasl auth for uri='qemu+tcp://<host>/system' to provide
access to remote libvirtd servers providing user and pass
authentication.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/901ff369
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/901ff369
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/901ff369

Branch: refs/heads/trunk
Commit: 901ff369046d6ade2940f648b5ba295feed99339
Parents: a60e070
Author: Rene Kjellerup <rkjelle...@touchcommerce.com>
Authored: Wed Jun 8 23:53:10 2016 -0700
Committer: Anthony Shaw <anthonys...@apache.org>
Committed: Fri Jun 24 13:56:08 2016 +1000

----------------------------------------------------------------------
 libcloud/compute/drivers/libvirt_driver.py | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/901ff369/libcloud/compute/drivers/libvirt_driver.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/libvirt_driver.py 
b/libcloud/compute/drivers/libvirt_driver.py
index 006a13b..7216538 100644
--- a/libcloud/compute/drivers/libvirt_driver.py
+++ b/libcloud/compute/drivers/libvirt_driver.py
@@ -63,7 +63,7 @@ class LibvirtNodeDriver(NodeDriver):
         7: NodeState.UNKNOWN,  # domain is suspended by guest power management
     }
 
-    def __init__(self, uri):
+    def __init__(self, uri, key=None, secret=None):
         """
         :param  uri: Hypervisor URI (e.g. vbox:///session, qemu:///system,
                      etc.).
@@ -74,7 +74,26 @@ class LibvirtNodeDriver(NodeDriver):
                                'package')
 
         self._uri = uri
-        self.connection = libvirt.open(uri)
+        self._key = key
+        self._secret = secret
+        try:
+            self.connection = libvirt.open(uri)
+        except libvirt.libvirtError:
+            if key is None or secret is None:
+                raise RuntimeError('The remote Libvirt instance requires ' +
+                                   'authenication, please set \'key\' and ' +
+                                   '\'secret\' parameters')
+            auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE],
+                    self._cred_callback, None]
+            self.connection = libvirt.openAuth(uri, auth, 0)
+
+    def _cred_callback(self, cred, user_data):
+        for credential in cred:
+            if credential[0] == libvirt.VIR_CRED_AUTHNAME:
+                credential[4] = self._key
+            elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
+                credential[4] = self._secret
+        return 0
 
     def list_nodes(self):
         domains = self.connection.listAllDomains()

Reply via email to