1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# Copyright (C) 2006, 2013 Red Hat, Inc.
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
from gi.repository import Gio
from gi.repository import GLib
from virtinst import log
from ..baseclass import vmmGObject
class _vmmSecret(object):
def __init__(self, name, secret=None, attributes=None):
self.name = name
self.secret = secret
self.attributes = attributes
def get_secret(self):
return self.secret
def get_name(self):
return self.name
class vmmKeyring(vmmGObject):
"""
freedesktop Secret API abstraction
"""
@classmethod
def get_instance(cls):
if not cls._instance:
cls._instance = vmmKeyring()
return cls._instance
def __init__(self):
vmmGObject.__init__(self)
self._collection = None
try:
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)
log.debug("Using keyring session %s", self._session)
except Exception: # pragma: no cover
log.exception("Error determining keyring")
def _cleanup(self):
pass # pragma: no cover
def _find_secret_item_path(self, uuid, hvuri):
attributes = {
"uuid": uuid,
"hvuri": hvuri,
}
unlocked, locked = self._service.SearchItems("(a{ss})", attributes)
if not unlocked:
if locked:
log.warning("Item found, but it's locked")
return None
return unlocked[0]
def _add_secret(self, secret):
try:
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
self._collection.CreateItem("(a{sv}(oayays)b)",
props, params, replace)
except Exception: # pragma: no cover
log.exception("Failed to add keyring secret")
def _del_secret(self, uuid, hvuri):
try:
path = self._find_secret_item_path(uuid, hvuri)
if path is None:
return None
iface = Gio.DBusProxy.new_sync(self._dbus, 0, None,
"org.freedesktop.secrets", path,
"org.freedesktop.Secret.Item", None)
prompt = iface.Delete()
if prompt != "/":
iface = Gio.DBusProxy.new_sync(self._dbus, 0, None,
"org.freedesktop.secrets", prompt,
"org.freedesktop.Secret.Prompt", None)
iface.Prompt("(s)", "")
except Exception:
log.exception("Failed to delete keyring secret")
def _get_secret(self, uuid, hvuri):
ret = None
try:
path = self._find_secret_item_path(uuid, hvuri)
if path is None:
return None
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()
secret = u"".join([chr(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)
except Exception: # pragma: no cover
log.exception("Failed to get keyring secret uuid=%r hvuri=%r", uuid, hvuri)
return ret
##############
# Public API #
##############
def is_available(self):
return not (self._collection is None)
def _get_secret_name(self, vm):
return "vm-console-" + vm.get_uuid()
def get_console_password(self, vm):
if not self.is_available():
return ("", "") # pragma: no cover
secret = self._get_secret(vm.get_uuid(), vm.conn.get_uri())
if secret is None:
return ("", "") # pragma: no cover
return (secret.get_secret(), vm.get_console_username() or "")
def set_console_password(self, vm, password, username=""):
if not self.is_available():
return # pragma: no cover
secret = _vmmSecret(self._get_secret_name(vm), password,
{"uuid": vm.get_uuid(),
"hvuri": vm.conn.get_uri()})
vm.set_console_username(username)
self._add_secret(secret)
def del_console_password(self, vm):
if not self.is_available():
return # pragma: no cover
self._del_secret(vm.get_uuid(), vm.conn.get_uri())
vm.del_console_username()
|