virt-manager/virtManager/keyring.py

112 lines
3.8 KiB
Python
Raw Normal View History

# Copyright (C) 2006, 2013 Red Hat, Inc.
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
Convert to use GTK3 and GObject Introspection bindings Switch over to use GObject introspection bindings for all python modules related to GObject/GTK3/etc. It is not possible to mix and match old pyggtk/pygobject manual bindings with new introspection based bindings so it must be all changed in one go. Imports like import gtk Change to from gi.repository import Gtk The vmmGObject class is changed to always inherit from GObject.GObject There is no compelling reason to avoid a GObject dep for the virt-manager TUI & it horribly messed up the code. Signal declarations are changed from vmmChooseCD.signal_new(vmmChooseCD, "cdrom-chosen", [object, str]) To __gsignals__ = { "cdrom-chosen": (GObject.SignalFlags.RUN_FIRST, None, [object, str]) } which is required by new GObject bindings Most of the rest of the change is simply dealing with renamed constants / classes. Alot of legacy compat code was removed - ie helpers which check to see if certain GTK2 methods are available are no longer required since we're mandating GTK3 only. The event loop is replaced with LibvirtGLib's event loop. Still todo - Rip out all DBus stuff & make vmmEngine class inherit GtkApplication which provides unique support & DBus method handling - Switch to use LibvirtGConfig & LibvirtGObject for libvirt interaction - Possibly switch to Python 3 too ? - Figure out why GNOME keyring is missing Introspection support My suggestion is that the standalone GIT repo for virt-install only live on as a support branch for legacy platforms. A stable-0.9 branch of virt-manager can be kept for legacy PyGtk2 based virt-manager releases. The virt-manager master branch should exclusively use GObject inspection and ideally Python3 and contain both the virt-manager and virt-install codebases in one since they are intimately related to each other & using separate GIT repos has needlessly complicated life for everyone. crobinso: Some locking fixes Misc cleanups and dropping now-useless code Fix dbus usage Fix graph cell renderer regression Fix a couple tooltip issues
2012-05-14 17:24:56 +04:00
from gi.repository import Gio
from gi.repository import GLib
from virtinst import log
class vmmSecret(object):
def __init__(self, name, secret=None, attributes=None):
self.name = name
self.secret = secret
self.attributes = attributes
def get_secret(self):
return self.secret
def get_name(self):
return self.name
2012-11-08 17:15:02 +04:00
class vmmKeyring(object):
def __init__(self):
self._collection = None
2010-06-15 18:14:44 +04:00
try:
self._dbus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
self._service = Gio.DBusProxy.new_sync(self._dbus, 0, None,
"org.freedesktop.secrets",
"/org/freedesktop/secrets",
"org.freedesktop.Secret.Service", None)
self._session = self._service.OpenSession("(sv)", "plain",
GLib.Variant("s", ""))[1]
self._collection = Gio.DBusProxy.new_sync(self._dbus, 0, None,
"org.freedesktop.secrets",
"/org/freedesktop/secrets/aliases/default",
"org.freedesktop.Secret.Collection", None)
log.debug("Using keyring session %s", self._session)
except Exception:
log.exception("Error determining keyring")
##############
# Public API #
##############
def is_available(self):
return not (self._collection is None)
def add_secret(self, secret):
ret = None
try:
props = {
"org.freedesktop.Secret.Item.Label": GLib.Variant("s", secret.get_name()),
"org.freedesktop.Secret.Item.Attributes": GLib.Variant("a{ss}", secret.attributes),
}
params = (self._session, [],
[ord(v) for v in secret.get_secret()],
"text/plain; charset=utf8")
replace = True
_id = self._collection.CreateItem("(a{sv}(oayays)b)",
props, params, replace)[0]
ret = int(_id.rsplit("/")[-1])
except Exception:
log.exception("Failed to add keyring secret")
2010-06-15 18:14:44 +04:00
return ret
def del_secret(self, _id):
try:
path = self._collection.get_object_path() + "/" + str(_id)
iface = Gio.DBusProxy.new_sync(self._dbus, 0, None,
"org.freedesktop.secrets", path,
"org.freedesktop.Secret.Item", None)
iface.Delete("(s)", "/")
except Exception:
log.exception("Failed to delete keyring secret")
def get_secret(self, _id):
ret = None
try:
path = self._collection.get_object_path() + "/" + str(_id)
iface = Gio.DBusProxy.new_sync(self._dbus, 0, None,
"org.freedesktop.secrets", path,
"org.freedesktop.Secret.Item", None)
secretbytes = iface.GetSecret("(o)", self._session)[2]
label = iface.get_cached_property("Label").unpack().strip("'")
dbusattrs = iface.get_cached_property("Attributes").unpack()
2018-02-07 02:57:00 +03:00
secret = u"".join([chr(c) for c in secretbytes])
attrs = {}
for key, val in dbusattrs.items():
if key not in ["hvuri", "uuid"]:
continue
attrs["%s" % key] = "%s" % val
ret = vmmSecret(label, secret, attrs)
except Exception:
log.exception("Failed to get keyring secret id=%s", _id)
return ret