virt-manager/virtManager/keyring.py

123 lines
4.1 KiB
Python
Raw Normal View History

#
# Copyright (C) 2006 Red Hat, Inc.
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
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 08:24:56 -05:00
import logging
try:
from gi.repository import GnomeKeyring # pylint: disable=E0611
except:
GnomeKeyring = None
logging.debug("GnomeKeyring bindings not installed, no keyring support")
class vmmSecret(object):
def __init__(self, name, secret=None, attributes=None):
self.name = name
self.secret = secret
self.attributes = {}
if isinstance(attributes, dict):
self.attributes = attributes
elif isinstance(attributes, list):
for attr in attributes:
self.attributes[attr.name] = attr.get_string()
def get_secret(self):
return self.secret
def get_name(self):
return self.name
def get_attributes_for_keyring(self):
attrs = GnomeKeyring.attribute_list_new()
for key, value in self.attributes.items():
GnomeKeyring.attribute_list_append_string(attrs, key, value)
return attrs
2012-11-08 07:15:02 -06:00
class vmmKeyring(object):
def __init__(self):
2010-06-15 09:14:44 -05:00
self.keyring = None
if GnomeKeyring is None:
2010-06-15 09:14:44 -05:00
return
2010-06-15 09:14:44 -05:00
try:
result = GnomeKeyring.get_default_keyring_sync()
if result and result[0] == GnomeKeyring.Result.OK:
self.keyring = result[1]
2012-11-08 07:15:02 -06:00
if self.keyring is None:
2010-06-15 09:33:45 -05:00
self.keyring = 'default'
logging.debug("No default keyring, creating '%s'",
self.keyring)
2010-06-15 09:33:45 -05:00
try:
GnomeKeyring.create_sync(self.keyring, None)
except GnomeKeyring.AlreadyExistsError:
2010-06-15 09:33:45 -05:00
pass
2010-06-15 09:14:44 -05:00
except:
logging.exception("Error determining keyring")
self.keyring = None
def is_available(self):
2012-11-08 07:15:02 -06:00
return not (self.keyring is None)
def add_secret(self, secret):
2010-06-15 09:14:44 -05:00
_id = None
try:
result, _id = GnomeKeyring.item_create_sync(
self.keyring,
GnomeKeyring.ItemType.GENERIC_SECRET,
secret.get_name(),
secret.get_attributes_for_keyring(),
secret.get_secret(),
True)
if result != GnomeKeyring.Result.OK:
raise RuntimeError("Creating keyring item failed with: %s" %
repr(result))
except:
2010-06-15 09:14:44 -05:00
logging.exception("Failed to add keyring secret")
return _id
def get_secret(self, _id):
"""
ignore, item = GnomeKeyring.item_get_info_sync(self.keyring, _id)
if item is None:
return
2010-06-15 09:14:44 -05:00
sec = None
try:
result, attrs = GnomeKeyring.item_get_attributes_sync(
self.keyring, _id)
if result != GnomeKeyring.Result.OK:
raise RuntimeError("Fetching keyring attributes failed "
"with %s" % result)
2010-06-15 09:14:44 -05:00
sec = vmmSecret(item.get_display_name(), item.get_secret(), attrs)
except:
logging.exception("Failed to lookup keyring item %s", item)
2010-06-15 09:14:44 -05:00
return sec
"""
# FIXME: Uncomment this once gnome-keyring is fixed
# https://bugzilla.gnome.org/show_bug.cgi?id=691638
ignore = _id
return None