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
#
2012-05-14 17:24:56 +04:00
2006-09-26 02:22:27 +04:00
import logging
2006-07-19 22:33:23 +04:00
2013-04-17 00:20:58 +04:00
# pylint: disable=E0611
from gi . repository import Gio
from gi . repository import GLib
# pylint: enable=E0611
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 )
logging . debug ( " Using keyring session %s " , self . _session )
2010-06-15 18:14:44 +04:00
except :
logging . 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 = {
" 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 ] )
2006-08-17 00:12:50 +04:00
except :
2010-06-15 18:14:44 +04:00
logging . exception ( " Failed to add keyring secret " )
2013-04-17 00:20:58 +04:00
return ret
2006-07-19 22:33:23 +04: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
2013-04-17 00:20:58 +04:00
secret = u " " . join ( [ unichr ( 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 )
2006-08-17 00:12:50 +04:00
except :
2013-04-17 00:20:58 +04:00
logging . exception ( " Failed to get keyring secret id= %s " , _id )
return ret