2013-10-28 00:59:46 +04:00
# Copyright (C) 2006, 2013 Red Hat, Inc.
2006-07-19 22:33:23 +04:00
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2012-05-14 17:24:56 +04:00
2013-04-17 00:20:58 +04:00
from gi . repository import Gio
from gi . repository import GLib
2012-11-09 19:08:15 +04:00
2019-06-17 04:12:39 +03:00
from virtinst import log
2012-11-09 19:08:15 +04:00
class vmmSecret ( object ) :
def __init__ ( self , name , secret = None , attributes = None ) :
self . name = name
self . secret = secret
2013-04-17 00:20:58 +04:00
self . attributes = attributes
2012-11-09 19:08:15 +04:00
def get_secret ( self ) :
return self . secret
def get_name ( self ) :
return self . name
2012-11-08 17:15:02 +04:00
2011-04-18 20:39:53 +04:00
class vmmKeyring ( object ) :
2013-04-17 00:20:58 +04:00
2006-07-19 22:33:23 +04:00
def __init__ ( self ) :
2013-04-17 00:20:58 +04:00
self . _collection = None
2006-07-19 22:33:23 +04:00
2010-06-15 18:14:44 +04:00
try :
2013-04-17 00:20:58 +04:00
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 )
2019-06-17 04:12:39 +03:00
log . debug ( " Using keyring session %s " , self . _session )
2017-07-24 11:26:48 +03:00
except Exception :
2019-06-17 04:12:39 +03:00
log . exception ( " Error determining keyring " )
2013-04-17 00:20:58 +04:00
##############
# Public API #
##############
2006-07-19 22:33:23 +04:00
2006-08-16 00:07:17 +04:00
def is_available ( self ) :
2013-04-17 00:20:58 +04:00
return not ( self . _collection is None )
2006-07-19 22:33:23 +04:00
def add_secret ( self , secret ) :
2013-04-17 00:20:58 +04:00
ret = None
2006-08-17 00:12:50 +04:00
try :
2013-04-17 00:20:58 +04:00
props = {
2017-08-05 09:39:32 +03:00
" org.freedesktop.Secret.Item.Label " : GLib . Variant ( " s " , secret . get_name ( ) ) ,
" org.freedesktop.Secret.Item.Attributes " : GLib . Variant ( " a {ss} " , secret . attributes ) ,
2013-04-17 00:20:58 +04:00
}
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 ] )
2017-07-24 11:26:48 +03:00
except Exception :
2019-06-17 04:12:39 +03:00
log . exception ( " Failed to add keyring secret " )
2010-06-15 18:14:44 +04:00
2013-04-17 00:20:58 +04:00
return ret
2006-07-19 22:33:23 +04:00
2016-06-07 16:25:55 +03:00
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) " , " / " )
2017-07-24 11:26:48 +03:00
except Exception :
2019-06-17 04:12:39 +03:00
log . exception ( " Failed to delete keyring secret " )
2016-06-07 16:25:55 +03:00
2008-11-18 23:42:51 +03:00
def get_secret ( self , _id ) :
2013-04-17 00:20:58 +04:00
ret = None
2006-08-17 00:12:50 +04:00
try :
2013-04-17 00:20:58 +04:00
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 ( )
2012-11-09 19:08:15 +04:00
2018-02-07 02:57:00 +03:00
secret = u " " . join ( [ chr ( c ) for c in secretbytes ] )
2013-04-17 00:20:58 +04:00
attrs = { }
for key , val in dbusattrs . items ( ) :
if key not in [ " hvuri " , " uuid " ] :
continue
attrs [ " %s " % key ] = " %s " % val
ret = vmmSecret ( label , secret , attrs )
2017-07-24 11:26:48 +03:00
except Exception :
2019-06-17 04:12:39 +03:00
log . exception ( " Failed to get keyring secret id= %s " , _id )
2013-04-17 00:20:58 +04:00
return ret