2006-07-19 22:33:23 +04:00
#
# 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
2007-11-20 19:12:20 +03:00
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
2006-07-19 22:33:23 +04:00
#
2008-11-03 23:15:11 +03:00
from virtManager . secret import vmmSecret
2006-07-20 22:56:27 +04:00
2006-08-16 00:07:17 +04:00
import sys
2006-09-26 02:22:27 +04:00
import logging
2006-07-19 22:33:23 +04:00
2006-08-16 00:07:17 +04:00
haveKeyring = False
2006-08-17 00:12:50 +04:00
2006-08-16 00:07:17 +04:00
try :
import gnomekeyring
haveKeyring = True
except :
2006-09-26 02:22:27 +04:00
logging . warning ( " No support for gnome-keyring " )
2006-08-16 00:07:17 +04:00
pass
2006-07-19 22:33:23 +04:00
class vmmKeyring :
def __init__ ( self ) :
2006-08-16 00:07:17 +04:00
if haveKeyring :
2006-08-16 22:44:35 +04:00
try :
2006-08-17 00:12:50 +04:00
if not ( " default " in gnomekeyring . list_keyring_names_sync ( ) ) :
gnomekeyring . create_sync ( " default " , None )
2006-08-16 22:44:35 +04:00
self . keyring = gnomekeyring . get_default_keyring_sync ( )
except :
2006-09-26 02:22:27 +04:00
logging . warning ( ( " Keyring unavailable: ' %s ' " ) % ( str ( ( sys . exc_info ( ) ) [ 0 ] ) + " " + str ( ( sys . exc_info ( ) ) [ 1 ] ) ) )
2006-08-16 22:44:35 +04:00
self . keyring = None
2006-08-16 00:07:17 +04:00
else :
self . keyring = None
2006-07-19 22:33:23 +04:00
2006-08-16 00:07:17 +04:00
def is_available ( self ) :
2006-07-19 22:33:23 +04:00
if self . keyring == None :
2006-08-16 00:07:17 +04:00
return False
return True
2006-07-19 22:33:23 +04:00
def add_secret ( self , secret ) :
2006-08-17 00:12:50 +04:00
try :
id = gnomekeyring . item_create_sync ( self . keyring ,
gnomekeyring . ITEM_GENERIC_SECRET ,
secret . get_name ( ) ,
secret . get_attributes ( ) ,
secret . get_secret ( ) ,
True )
return id
except :
return None
2006-07-19 22:33:23 +04:00
def get_secret ( self , id ) :
2006-08-17 00:12:50 +04:00
try :
item = gnomekeyring . item_get_info_sync ( self . keyring , id )
attrs = gnomekeyring . item_get_attributes_sync ( self . keyring , id )
return vmmSecret ( item . get_display_name ( ) , item . get_secret ( ) , attrs )
except :
return None
2006-08-16 00:07:17 +04:00
2006-07-19 22:33:23 +04:00
def clear_secret ( self , id ) :
2006-08-17 00:12:50 +04:00
try :
gnomekeyring . item_delete_sync ( self . keyring , id )
return True
except :
return False
2006-07-19 22:33:23 +04:00