root / lib / serializer.py @ 4884f187
History | View | Annotate | Download (8.6 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2007, 2008, 2014 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
"""Serializer abstraction module
|
22 |
|
23 |
This module introduces a simple abstraction over the serialization
|
24 |
backend (currently json).
|
25 |
|
26 |
"""
|
27 |
# pylint: disable=C0103
|
28 |
|
29 |
# C0103: Invalid name, since pylint doesn't see that Dump points to a
|
30 |
# function and not a constant
|
31 |
|
32 |
import re |
33 |
|
34 |
# Python 2.6 and above contain a JSON module based on simplejson. Unfortunately
|
35 |
# the standard library version is significantly slower than the external
|
36 |
# module. While it should be better from at least Python 3.2 on (see Python
|
37 |
# issue 7451), for now Ganeti needs to work well with older Python versions
|
38 |
# too.
|
39 |
import simplejson |
40 |
|
41 |
from ganeti import errors |
42 |
from ganeti import utils |
43 |
|
44 |
|
45 |
_RE_EOLSP = re.compile("[ \t]+$", re.MULTILINE)
|
46 |
|
47 |
|
48 |
def DumpJson(data): |
49 |
"""Serialize a given object.
|
50 |
|
51 |
@param data: the data to serialize
|
52 |
@return: the string representation of data
|
53 |
|
54 |
"""
|
55 |
encoded = simplejson.dumps(data) |
56 |
|
57 |
txt = _RE_EOLSP.sub("", encoded)
|
58 |
if not txt.endswith("\n"): |
59 |
txt += "\n"
|
60 |
|
61 |
return txt
|
62 |
|
63 |
|
64 |
def LoadJson(txt): |
65 |
"""Unserialize data from a string.
|
66 |
|
67 |
@param txt: the json-encoded form
|
68 |
@return: the original data
|
69 |
@raise JSONDecodeError: if L{txt} is not a valid JSON document
|
70 |
|
71 |
"""
|
72 |
return simplejson.loads(txt)
|
73 |
|
74 |
|
75 |
def DumpSignedJson(data, key, salt=None, key_selector=None): |
76 |
"""Serialize a given object and authenticate it.
|
77 |
|
78 |
@param data: the data to serialize
|
79 |
@param key: shared hmac key
|
80 |
@param key_selector: name/id that identifies the key (in case there are
|
81 |
multiple keys in use, e.g. in a multi-cluster environment)
|
82 |
@return: the string representation of data signed by the hmac key
|
83 |
|
84 |
"""
|
85 |
txt = DumpJson(data) |
86 |
if salt is None: |
87 |
salt = ""
|
88 |
signed_dict = { |
89 |
"msg": txt,
|
90 |
"salt": salt,
|
91 |
} |
92 |
|
93 |
if key_selector:
|
94 |
signed_dict["key_selector"] = key_selector
|
95 |
else:
|
96 |
key_selector = ""
|
97 |
|
98 |
signed_dict["hmac"] = utils.Sha1Hmac(key, txt, salt=salt + key_selector)
|
99 |
|
100 |
return DumpJson(signed_dict)
|
101 |
|
102 |
|
103 |
def LoadSignedJson(txt, key): |
104 |
"""Verify that a given message was signed with the given key, and load it.
|
105 |
|
106 |
@param txt: json-encoded hmac-signed message
|
107 |
@param key: the shared hmac key or a callable taking one argument (the key
|
108 |
selector), which returns the hmac key belonging to the key selector.
|
109 |
Typical usage is to pass a reference to the get method of a dict.
|
110 |
@rtype: tuple of original data, string
|
111 |
@return: original data, salt
|
112 |
@raises errors.SignatureError: if the message signature doesn't verify
|
113 |
|
114 |
"""
|
115 |
signed_dict = LoadJson(txt) |
116 |
if not isinstance(signed_dict, dict): |
117 |
raise errors.SignatureError("Invalid external message") |
118 |
try:
|
119 |
msg = signed_dict["msg"]
|
120 |
salt = signed_dict["salt"]
|
121 |
hmac_sign = signed_dict["hmac"]
|
122 |
except KeyError: |
123 |
raise errors.SignatureError("Invalid external message") |
124 |
|
125 |
if callable(key): |
126 |
# pylint: disable=E1103
|
127 |
key_selector = signed_dict.get("key_selector", None) |
128 |
hmac_key = key(key_selector) |
129 |
if not hmac_key: |
130 |
raise errors.SignatureError("No key with key selector '%s' found" % |
131 |
key_selector) |
132 |
else:
|
133 |
key_selector = ""
|
134 |
hmac_key = key |
135 |
|
136 |
if not utils.VerifySha1Hmac(hmac_key, msg, hmac_sign, |
137 |
salt=salt + key_selector): |
138 |
raise errors.SignatureError("Invalid Signature") |
139 |
|
140 |
return LoadJson(msg), salt
|
141 |
|
142 |
|
143 |
def LoadAndVerifyJson(raw, verify_fn): |
144 |
"""Parses and verifies JSON data.
|
145 |
|
146 |
@type raw: string
|
147 |
@param raw: Input data in JSON format
|
148 |
@type verify_fn: callable
|
149 |
@param verify_fn: Verification function, usually from L{ht}
|
150 |
@return: De-serialized data
|
151 |
|
152 |
"""
|
153 |
try:
|
154 |
data = LoadJson(raw) |
155 |
except Exception, err: |
156 |
raise errors.ParseError("Can't parse input data: %s" % err) |
157 |
|
158 |
if not verify_fn(data): |
159 |
raise errors.ParseError("Data does not match expected format: %s" % |
160 |
verify_fn) |
161 |
|
162 |
return data
|
163 |
|
164 |
|
165 |
Dump = DumpJson |
166 |
Load = LoadJson |
167 |
DumpSigned = DumpSignedJson |
168 |
LoadSigned = LoadSignedJson |
169 |
|
170 |
|
171 |
class Private(object): |
172 |
"""Wrap a value so it is hard to leak it accidentally.
|
173 |
|
174 |
>>> x = Private("foo")
|
175 |
>>> print "Value: %s" % x
|
176 |
Value: <redacted>
|
177 |
>>> print "Value: {0}".format(x)
|
178 |
Value: <redacted>
|
179 |
>>> x.upper() == "FOO"
|
180 |
True
|
181 |
|
182 |
"""
|
183 |
def __init__(self, item, descr="redacted"): |
184 |
if isinstance(item, Private): |
185 |
raise ValueError("Attempted to nest Private values.") |
186 |
self._item = item
|
187 |
self._descr = descr
|
188 |
|
189 |
def Get(self): |
190 |
"Return the wrapped value."
|
191 |
return self._item |
192 |
|
193 |
def __str__(self): |
194 |
return "<{._descr}>".format(self) |
195 |
|
196 |
def __repr__(self): |
197 |
return "Private(?, descr='{._descr}')".format(self) |
198 |
|
199 |
# pylint: disable=W0212
|
200 |
# If it doesn't access _item directly, the call will go through __getattr__
|
201 |
# because this class defines __slots__ and "item" is not in it.
|
202 |
# OTOH, if we do add it there, we'd risk shadowing an "item" attribute.
|
203 |
def __eq__(self, other): |
204 |
if isinstance(other, Private): |
205 |
return self._item == other._item |
206 |
else:
|
207 |
return self._item == other |
208 |
|
209 |
def __hash__(self): |
210 |
return hash(self._item) |
211 |
|
212 |
def __format__(self, *_1, **_2): |
213 |
return self.__str__() |
214 |
|
215 |
def __getattr__(self, attr): |
216 |
return Private(getattr(self._item, attr), |
217 |
descr="%s.%s" % (self._descr, attr)) |
218 |
|
219 |
def __call__(self, *args, **kwargs): |
220 |
return Private(self._item(*args, **kwargs), |
221 |
descr="%s()" % self._descr) |
222 |
|
223 |
# pylint: disable=R0201
|
224 |
# While this could get away with being a function, it needs to be a method.
|
225 |
# Required by the copy.deepcopy function used by FillDict.
|
226 |
def __getnewargs__(self): |
227 |
return tuple() |
228 |
|
229 |
def __nonzero__(self): |
230 |
return bool(self._item) |
231 |
|
232 |
# Get in the way of Pickle by implementing __slots__ but not __getstate__
|
233 |
# ...and get a performance boost, too.
|
234 |
__slots__ = ["_item", "_descr"] |
235 |
|
236 |
|
237 |
class PrivateDict(dict): |
238 |
"""A dictionary that turns its values to private fields.
|
239 |
|
240 |
>>> PrivateDict()
|
241 |
{}
|
242 |
>>> supersekkrit = PrivateDict({"password": "foobar"})
|
243 |
>>> print supersekkrit["password"]
|
244 |
<password>
|
245 |
>>> supersekkrit["password"].Get()
|
246 |
'foobar'
|
247 |
>>> supersekkrit.GetPrivate("password")
|
248 |
'foobar'
|
249 |
>>> supersekkrit["user"] = "eggspam"
|
250 |
>>> supersekkrit.Unprivate()
|
251 |
{'password': 'foobar', 'user': 'eggspam'}
|
252 |
|
253 |
"""
|
254 |
def __init__(self, data=None): |
255 |
dict.__init__(self) |
256 |
self.update(data)
|
257 |
|
258 |
def __setitem__(self, item, value): |
259 |
if not isinstance(value, Private): |
260 |
if not isinstance(item, dict): |
261 |
value = Private(value, descr=item) |
262 |
else:
|
263 |
value = PrivateDict(value) |
264 |
dict.__setitem__(self, item, value) |
265 |
|
266 |
# The actual conversion to Private containers is done by __setitem__
|
267 |
|
268 |
# copied straight from cpython/Lib/UserDict.py
|
269 |
# Copyright (c) 2001-2014 Python Software Foundation; All Rights Reserved
|
270 |
def update(self, other=None, **kwargs): |
271 |
# Make progressively weaker assumptions about "other"
|
272 |
if other is None: |
273 |
pass
|
274 |
elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups |
275 |
for k, v in other.iteritems(): |
276 |
self[k] = v
|
277 |
elif hasattr(other, 'keys'): |
278 |
for k in other.keys(): |
279 |
self[k] = other[k]
|
280 |
else:
|
281 |
for k, v in other: |
282 |
self[k] = v
|
283 |
if kwargs:
|
284 |
self.update(kwargs)
|
285 |
|
286 |
def GetPrivate(self, *args): |
287 |
"""Like dict.get, but extracting the value in the process.
|
288 |
|
289 |
Arguments are semantically equivalent to ``dict.get``
|
290 |
|
291 |
>>> PrivateDict({"foo": "bar"}).GetPrivate("foo")
|
292 |
'bar'
|
293 |
>>> PrivateDict({"foo": "bar"}).GetPrivate("baz", "spam")
|
294 |
'spam'
|
295 |
|
296 |
"""
|
297 |
if len(args) == 1: |
298 |
key, = args |
299 |
return self[key].Get() |
300 |
elif len(args) == 2: |
301 |
key, default = args |
302 |
if key not in self: |
303 |
return default
|
304 |
else:
|
305 |
return self[key].Get() |
306 |
else:
|
307 |
raise TypeError("GetPrivate() takes 2 arguments (%d given)" % len(args)) |
308 |
|
309 |
def Unprivate(self): |
310 |
"""Turn this dict of Private() values to a dict of values.
|
311 |
|
312 |
>>> PrivateDict({"foo": "bar"}).Unprivate()
|
313 |
{'foo': 'bar'}
|
314 |
|
315 |
@rtype: dict
|
316 |
|
317 |
"""
|
318 |
returndict = {} |
319 |
for key in self: |
320 |
returndict[key] = self[key].Get()
|
321 |
return returndict
|