Cleanup gnome-keyring code (still broken with introspection)

This commit is contained in:
Cole Robinson 2012-11-09 16:08:15 +01:00
parent 5df0a108f8
commit cb512f61e7
5 changed files with 94 additions and 122 deletions

View File

@ -35,7 +35,6 @@ src/virtManager/network.py
src/virtManager/packageutils.py
src/virtManager/preferences.py
src/virtManager/remote.py
src/virtManager/secret.py
src/virtManager/serialcon.py
src/virtManager/storagebrowse.py
src/virtManager/storagepool.py

View File

@ -25,8 +25,8 @@ from gi.repository import GConf
import virtinst
from virtManager.keyring import vmmKeyring
from virtManager.secret import vmmSecret
from virtManager.keyring import vmmKeyring, vmmSecret
class vmmConfig(object):
@ -735,47 +735,31 @@ class vmmConfig(object):
def has_keyring(self):
if self.keyring is None:
logging.warning("Initializing keyring")
self.keyring = vmmKeyring()
return self.keyring.is_available()
def clear_console_password(self, vm):
_id = self.conf.get_int(self.conf_dir + "/console/passwords/" + vm.get_uuid())
if _id is not None:
if not(self.has_keyring()):
return
if self.keyring.clear_secret(_id):
self.conf.unset(self.conf_dir + "/console/passwords/" + vm.get_uuid())
def get_console_password(self, vm):
_id = self.conf.get_int(self.conf_dir + "/console/passwords/" + vm.get_uuid())
username = self.conf.get_string(self.conf_dir + "/console/usernames/" + vm.get_uuid())
keyid = self.conf.get_int(self.conf_dir +
"/console/passwords/" + vm.get_uuid())
username = self.conf.get_string(self.conf_dir +
"/console/usernames/" + vm.get_uuid())
if username is None:
username = ""
if keyid is None or not self.has_keyring():
return ("", "")
if _id is not None:
if not(self.has_keyring()):
return ("", "")
secret = self.keyring.get_secret(keyid)
if secret is None or secret.get_name() != self.get_secret_name(vm):
return ("", "")
secret = self.keyring.get_secret(_id)
if secret is not None and secret.get_name() == self.get_secret_name(vm):
if not(secret.has_attribute("hvuri")):
return ("", "")
if secret.get_attribute("hvuri") != vm.conn.get_uri():
return ("", "")
if not(secret.has_attribute("uuid")):
return ("", "")
if secret.get_attribute("uuid") != vm.get_uuid():
return ("", "")
if (secret.attributes.get("hvuri", None) != vm.conn.get_uri() or
secret.attributes.get("uuid", None) != vm.get_uuid()):
return ("", "")
return (secret.get_secret(), username)
return ("", username)
return (secret.get_secret(), username or "")
def set_console_password(self, vm, password, username=""):
if not(self.has_keyring()):
if not self.has_keyring():
return
# Nb, we don't bother to check if there is an existing
@ -786,7 +770,11 @@ class vmmConfig(object):
secret = vmmSecret(self.get_secret_name(vm), password,
{"uuid" : vm.get_uuid(),
"hvuri": vm.conn.get_uri()})
_id = self.keyring.add_secret(secret)
if _id is not None:
self.conf.set_int(self.conf_dir + "/console/passwords/" + vm.get_uuid(), _id)
self.conf.set_string(self.conf_dir + "/console/usernames/" + vm.get_uuid(), username)
keyid = self.keyring.add_secret(secret)
if keyid is None:
return
self.conf.set_int(self.conf_dir +
"/console/passwords/" + vm.get_uuid(), keyid)
self.conf.set_string(self.conf_dir +
"/console/usernames/" + vm.get_uuid(), username)

View File

@ -340,7 +340,12 @@ class VNCViewer(Viewer):
return self.desktop_resolution
def _auth_credential(self, src_ignore, credList):
for cred in credList:
# XXX shouldn't need this
values = []
for idx in range(int(credList.n_values)):
values.append(credList.get_nth(idx))
for cred in values:
if cred in [GtkVnc.DisplayCredential.PASSWORD,
GtkVnc.DisplayCredential.USERNAME,
GtkVnc.DisplayCredential.CLIENTNAME]:
@ -363,7 +368,7 @@ class VNCViewer(Viewer):
withUsername = False
withPassword = False
for cred in credList:
for cred in values:
logging.debug("Got credential request %s", cred)
if cred == GtkVnc.DisplayCredential.PASSWORD:
withPassword = True

View File

@ -18,33 +18,57 @@
# MA 02110-1301 USA.
#
from virtManager.secret import vmmSecret
import logging
haveKeyring = False
try:
import gnomekeyring
haveKeyring = True
from gi.repository import GnomeKeyring
except:
logging.warning("gnomekeyring bindings not installed, no keyring support")
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
class vmmKeyring(object):
def __init__(self):
self.keyring = None
if not haveKeyring:
if GnomeKeyring is None:
return
try:
self.keyring = gnomekeyring.get_default_keyring_sync()
result = GnomeKeyring.get_default_keyring_sync()
if result and result[0] == GnomeKeyring.Result.OK:
self.keyring = result[1]
if self.keyring is None:
# Code borrowed from
# http://trac.gajim.org/browser/src/common/passwords.py
self.keyring = 'default'
logging.debug("No default keyring, creating '%s'",
self.keyring)
try:
gnomekeyring.create_sync(self.keyring, None)
except gnomekeyring.AlreadyExistsError:
GnomeKeyring.create_sync(self.keyring, None)
except GnomeKeyring.AlreadyExistsError:
pass
except:
logging.exception("Error determining keyring")
@ -56,32 +80,43 @@ class vmmKeyring(object):
def add_secret(self, secret):
_id = None
try:
_id = gnomekeyring.item_create_sync(self.keyring,
gnomekeyring.ITEM_GENERIC_SECRET,
secret.get_name(),
secret.get_attributes(),
secret.get_secret(),
True)
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:
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
sec = None
try:
item = gnomekeyring.item_get_info_sync(self.keyring, _id)
attrs = gnomekeyring.item_get_attributes_sync(self.keyring, _id)
result, attrs = GnomeKeyring.item_get_attributes_sync(
self.keyring, _id)
if result != GnomeKeyring.Result.OK:
raise RuntimeError("Fetching keyring attributes failed "
"with %s" % result)
sec = vmmSecret(item.get_display_name(), item.get_secret(), attrs)
except:
pass
logging.exception("Failed to lookup keyring item %s", item)
return sec
def clear_secret(self, _id):
try:
gnomekeyring.item_delete_sync(self.keyring, _id)
return True
except:
return False
"""
# FIXME: Uncomment this once gnome-keyring is fixed
# https://bugzilla.gnome.org/show_bug.cgi?id=691638
ignore = _id
return None

View File

@ -1,55 +0,0 @@
#
# 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.
#
class vmmSecret(object):
def __init__(self, name, secret=None, attributes=None):
self.name = name
self.secret = secret
if attributes is None:
attributes = {}
self.attributes = attributes
def set_secret(self, data):
self.secret = data
def get_secret(self):
return self.secret
def get_name(self):
return self.name
def get_attributes(self):
return self.attributes
def has_attribute(self, key):
return key in self.attributes
def add_attribute(self, key, value):
if type(value) != str:
value = str(value)
self.attributes[key] = value
def list_attributes(self):
return self.attributes.keys()
def get_attribute(self, key):
return self.attributes[key]