-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient.py
400 lines (332 loc) · 14.4 KB
/
client.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
""":mod:`geofrontcli.client` --- Client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import collections
import contextlib
import io
import json
import logging
import re
import sys
import uuid
from keyring import get_password, set_password
from six import string_types
from six.moves.urllib.error import HTTPError
from six.moves.urllib.parse import urljoin
from six.moves.urllib.request import OpenerDirector, Request, build_opener
from .key import PublicKey
from .ssl import create_urllib_https_handler
from .version import MAX_PROTOCOL_VERSION, MIN_PROTOCOL_VERSION, VERSION
__all__ = ('REMOTE_PATTERN', 'BufferedResponse',
'Client', 'ExpiredTokenIdError',
'MasterKeyError', 'NoTokenIdError', 'ProtocolVersionError',
'RemoteAliasError', 'RemoteError', 'RemoteStateError',
'TokenIdError', 'UnfinishedAuthenticationError',
'parse_mimetype')
#: (:class:`re.RegexObject`) The pattern that matches to the remote string
#: look like ``'user@host:port'``.
REMOTE_PATTERN = re.compile(r'^(?:(?P<user>[^@]+)@)?'
r'(?P<host>[^:]+)'
r'(?::(?P<port>\d+))?$')
def parse_mimetype(content_type):
"""Parse :mailheader:`Content-Type` header and return the actual mimetype
and its options.
>>> parse_mimetype('text/html; charset=utf-8')
('text/html', ['charset=utf-8'])
"""
values = [v.strip() for v in content_type.split(';')]
return values[0], values[1:]
class Client(object):
"""Client for a configured Geofront server."""
#: (:class:`PublicKeyDict`) Public keys registered to Geofront server.
public_keys = None
def __init__(self, server_url, opener=None):
self.logger = logging.getLogger(__name__ + '.Client')
self.server_url = server_url
if opener is None:
opener = build_opener(create_urllib_https_handler())
elif not isinstance(opener, OpenerDirector):
raise TypeError('opener must be {0.__module__}.{0.__name__}, not '
'{1!r}'.format(OpenerDirector, opener))
self.opener = opener
self.public_keys = PublicKeyDict(self)
@contextlib.contextmanager
def request(self, method, url, data=None, headers={}):
logger = self.logger.getChild('request')
if isinstance(url, tuple):
url = './{0}/'.format('/'.join(url))
url = urljoin(self.server_url, url)
h = {
'User-Agent': 'geofront-cli/{0} (Python-urllib/{1})'.format(
VERSION, sys.version[:3]
),
'Accept': 'application/json'
}
h.update(headers)
request = Request(url, method=method, data=data, headers=h)
try:
response = self.opener.open(request)
except HTTPError as e:
logger.exception(e)
response = e
server_version = response.headers.get('X-Geofront-Version')
if server_version:
try:
server_version_info = tuple(
map(int, server_version.strip().split('.'))
)
except ValueError:
raise ProtocolVersionError(
None,
'the protocol version number the server sent is not '
'a valid format: ' + repr(server_version)
)
else:
if not (MIN_PROTOCOL_VERSION <=
server_version_info <=
MAX_PROTOCOL_VERSION):
raise ProtocolVersionError(
server_version_info,
'the server protocol version ({0}) is '
'incompatible'.format(server_version)
)
else:
raise ProtocolVersionError(
None,
'the server did not send the protocol version '
'(X-Geofront-Version)'
)
mimetype, _ = parse_mimetype(response.headers['Content-Type'])
if mimetype == 'application/json' and 400 <= response.code < 500:
read = response.read()
body = json.loads(read.decode('utf-8'))
response.close()
error = isinstance(body, dict) and body.get('error')
if response.code == 404 and error == 'token-not-found' or \
response.code == 410 and error == 'expired-token':
raise ExpiredTokenIdError('token id seems expired')
elif response.code == 412 and error == 'unfinished-authentication':
raise UnfinishedAuthenticationError(body['message'])
buffered = BufferedResponse(response.code, response.headers, read)
yield buffered
buffered.close()
return
yield response
response.close()
@property
def token_id(self):
"""(:class:`str`) The previously authenticated token id stored
in the system password store (e.g. Keychain of Mac).
"""
token_id = get_password('geofront-cli', self.server_url)
if token_id:
return token_id
raise NoTokenIdError('no configured token id')
@token_id.setter
def token_id(self, token_id):
set_password('geofront-cli', self.server_url, token_id)
@contextlib.contextmanager
def authenticate(self):
"""Authenticate and then store the :attr:`token_id`."""
token_id = uuid.uuid1().hex
with self.request('PUT', ('tokens', token_id)) as response:
assert response.code == 202
result = json.loads(response.read().decode('utf-8'))
yield result['next_url']
self.token_id = token_id
@property
def identity(self):
"""(:class:`tuple`) A pair of ``(team_type, identifier)``."""
with self.request('GET', ('tokens', self.token_id)) as r:
assert r.code == 200
mimetype, _ = parse_mimetype(r.headers['Content-Type'])
assert mimetype == 'application/json'
result = json.loads(r.read().decode('utf-8'))
return result['team_type'], result['identifier']
@property
def master_key(self):
"""(:class:`~.key.PublicKey`) The current master key."""
path = ('tokens', self.token_id, 'masterkey')
headers = {'Accept': 'text/plain'}
with self.request('GET', path, headers=headers) as r:
if r.code == 200:
mimetype, _ = parse_mimetype(r.headers['Content-Type'])
if mimetype == 'text/plain':
return PublicKey.parse_line(r.read())
raise MasterKeyError('server failed to show the master key')
@property
def remotes(self):
"""(:class:`collections.Mapping`) The map of aliases to remote
addresses.
"""
logger = self.logger.getChild('remotes')
logger.info('Loading the list of remotes from the Geofront server...',
extra={'user_waiting': True})
try:
path = ('tokens', self.token_id, 'remotes')
with self.request('GET', path) as r:
assert r.code == 200
mimetype, _ = parse_mimetype(r.headers['Content-Type'])
assert mimetype == 'application/json'
result = json.loads(r.read().decode('utf-8'))
fmt = '{0[user]}@{0[host]}:{0[port]}'.format
logger.info('Total %d remotes.', len(result),
extra={'user_waiting': False})
return dict((alias, fmt(remote))
for alias, remote in result.items())
except Exception:
logger.info('Failed to fetch the list of remotes.',
extra={'user_waiting': False})
raise
def authorize(self, alias):
"""Temporarily authorize you to access the given remote ``alias``.
A made authorization keeps alive in a minute, and then will be expired.
"""
logger = self.logger.getChild('authorize')
logger.info('Letting the Geofront server to authorize you to access '
'to %s...', alias, extra={'user_waiting': True})
try:
path = ('tokens', self.token_id, 'remotes', alias)
with self.request('POST', path) as r:
mimetype, _ = parse_mimetype(r.headers['Content-Type'])
assert mimetype == 'application/json'
result = json.loads(r.read().decode('utf-8'))
if r.code == 404 and result.get('error') == 'not-found':
raise RemoteAliasError(result.get('message'))
elif (r.code == 500 and
result.get('error') == 'connection-failure'):
raise RemoteStateError(result.get('message'))
assert r.code == 200
assert result['success'] == 'authorized'
except TokenIdError:
logger.info('Authentication is required.',
extra={'user_waiting': False})
raise
except Exception:
logger.info('Authorization to %s has failed.', alias,
extra={'user_waiting': False})
raise
else:
logger.info('Access to %s has authorized! The access will be '
'available only for a time.', alias,
extra={'user_waiting': False})
return '{0[user]}@{0[host]}:{0[port]}'.format(result['remote'])
def __repr__(self):
return '{0.__module__}.{0.__name__}({1!r})'.format(
type(self), self.server_url
)
class BufferedResponse(io.BytesIO):
""":class:`io.BytesIO` subclass that mimics some interface of
:class:`http.client.HTTPResponse`.
"""
def __init__(self, code, headers, *args, **kwargs):
super(BufferedResponse, self).__init__(*args, **kwargs)
self.code = code
self.headers = headers
class PublicKeyDict(collections.MutableMapping):
""":class:`dict`-like object that contains public keys."""
def __init__(self, client):
self.client = client
def _request(self, path=(), method='GET', data=None, headers={}):
path = ('tokens', self.client.token_id, 'keys') + path
with self.client.request(method, path, data, headers) as resp:
mimetype, _ = parse_mimetype(resp.headers['Content-Type'])
body = resp.read()
if mimetype == 'application/json':
body = json.loads(body.decode('utf-8'))
error = isinstance(body, dict) and body.get('error')
else:
error = None
return resp.code, body, error
def __len__(self):
code, body, error = self._request()
assert code == 200
return len(body)
def __iter__(self):
code, body, error = self._request()
assert code == 200
return iter(body)
def __getitem__(self, fprint):
if isinstance(fprint, string_types):
code, body, error = self._request((fprint,))
if not (code == 404 and error == 'not-found'):
return PublicKey.parse_line(body)
raise KeyError(fprint)
def __setitem__(self, fprint, pkey):
if not isinstance(pkey, PublicKey):
raise TypeError('expected {0.__module__}.{0.__name__}, not '
'{1!r}'.format(PublicKey, pkey))
if fprint != pkey.fingerprint:
raise ValueError(
'{0} is not a valid fingerprint of {1!r}'.format(fprint, pkey)
)
code, body, error = self._request(
method='POST',
data=bytes(pkey),
headers={'Content-Type': 'text/plain'}
)
if code == 400 and error == 'duplicate-key':
if fprint in self:
return
raise ValueError(fprint + ' is already used by other')
assert code == 201, 'error: ' + error
def __delitem__(self, fprint):
if isinstance(fprint, string_types):
code, body, error = self._request((fprint,), method='DELETE')
if not (code == 404 and error == 'not-found'):
return
raise KeyError(fprint)
def items(self):
code, body, error = self._request()
assert code == 200
return [(fprint, PublicKey.parse_line(pkey))
for fprint, pkey in body.items()]
def values(self):
code, body, error = self._request()
assert code == 200
return map(PublicKey.parse_line, body.values())
class ProtocolVersionError(Exception):
"""Exception that rises when the server version is not compatibile."""
#: (:class:`tuple`) The protocol version triple the server sent.
#: Might be :const:`None`.
server_version_info = None
def __init__(self, server_version_info, *args, **kwargs):
super(ProtocolVersionError, self).__init__(*args, **kwargs)
self.server_version_info = server_version_info
@property
def server_version(self):
"""(:class:`str`) The server version in string."""
v = self.server_version_info
return v and '{0}.{1}.{2}'.format(*v)
class TokenIdError(Exception):
"""Exception related to token id."""
class NoTokenIdError(TokenIdError, AttributeError):
"""Exception that rises when there's no configured token id."""
class ExpiredTokenIdError(TokenIdError):
"""Exception that rises when the used token id is expired."""
class UnfinishedAuthenticationError(TokenIdError):
"""Exception that rises when the used token id is not finished
authentication.
"""
class MasterKeyError(Exception):
"""Exception related to the master key."""
class RemoteError(Exception):
"""Exception related to remote."""
class RemoteAliasError(RemoteError, LookupError):
"""Exception that rises when the given remote alias doesn't exist."""
class RemoteStateError(RemoteError):
"""Exception that rises when the status of the remote is unavailable."""
if sys.version_info < (3, 3):
class Request(Request):
superclass = Request
def __init__(self, url, data=None, headers={}, method=None):
if isinstance(Request, type):
super(Request, self).__init__(url, data, headers)
else:
self.superclass.__init__(self, url, data, headers)
if method is not None:
self.method = method
def get_method(self):
if hasattr(self, 'method'):
return self.method
return 'GET' if self.data is None else 'POST'