forked from SeattleTestbed/seattlelib_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmclient.r2py
498 lines (370 loc) · 16.3 KB
/
nmclient.r2py
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
"""
Author: Justin Cappos
Module: Routines that interact with a node manager to perform actions on
nodes. A simple front end can be added to make this a functional
experiment manager.
Start date: September 7th 2008
The design goals of this version are to be secure, simple, and reliable (in
that order).
"""
# for signing the data we send to the node manager
signeddata = dy_import_module("signeddata.r2py")
rsa = dy_import_module("rsa.r2py")
# session wrapper (breaks the stream into messages)
# an abstracted "itemized data communication" in a separate API
session = dy_import_module("session.r2py")
# makes connections time out
sockettimeout = dy_import_module("sockettimeout.r2py")
# Import the random library so we can use the randomfloat() function.
random = dy_import_module("random.r2py")
# We need the time library for various time functinoalities
# that we call later on.
time = dy_import_module("time.r2py")
# The idea is that this module returns "node manager handles". A handle
# may be used to communicate with a node manager and issue commands. If the
# caller wants to have a set of node managers with the same state, this can
# be done by something like:
#
#
# myid = # some unique, non-repeating value
# nmhandles = []
# for nm in nodemanagers:
# nmhandles.append(nmclient_createhandle(nm, sequenceid = myid))
#
#
# def do_action(action):
# for nmhandle in nmhandles:
# nmclient_doaction(nmhandle, ... )
#
#
# The above code snippet will ensure that none of the nmhandles perform the
# actions called in do_action() out of order. A node that "misses" an action
# (perhaps due to a network or node failure) will not perform later actions
# unless the sequenceid is reset.
#
# Note that the above calls to nmclient_createhandle and nmclient_doaction
# should really be wrapped in try except blocks for NMClientExceptions
# Thrown when a failure occurs when trying to communicate with a node
class NMClientException(Exception):
pass
# This holds all of the client handles. A client handle is merely a
# string that is the key to this dict. All of the information is stored in
# the dictionary value (a dict with keys for IP, port, sessionID, timestamp,
# identity, expirationtime, public key, private key, and vesselID).
nmclient_handledict = {}
# BUG: How do I do this and have it be portable across repy <-> python?
# needed when assigning new handles to prevent race conditions...
nmclient_handledictlock = createlock()
# Note: I open a new connection for every request. Is this really what I want
# to do? It seemed easiest but likely has performance implications
def _do_timeout_openconnection(destip, destport, localip=None,
localport=None, timeout=5):
"""
This is a reimplementation of sockettimeout's timeout_openconnection.
We need to have it here because our and sockettimeout's references to
Repy's openconnection function might be different, and we will want
to use our's.
(This situation arises when an Affix stack is built, and the Repy
networking calls should be overridden. Affixes often use sockettimeout,
so they import it, and by this make it use the original Repy calls.
We import it later, but it's already there then, so we just get a
reference to the existing instance with the original calls in place.
"""
# Initialize localip and localport (if not set by caller)
if localip is None:
localip = getmyip()
if localport is None:
portrange = list(getresources()[0]['connport'])
random.random_shuffle(portrange)
else:
portrange = [localport]
for myport in portrange:
try:
realsocketlikeobject = openconnection(destip, destport, localip, myport, timeout)
# Heureka, we found a usable port. Continue outside of the for loop.
break
except (DuplicateTupleError, CleanupInProgressError,
AlreadyListeningError, AddressBindingError):
# These are "legitimate" in the sense that there is something
# in our very program already using the port we just tried.
pass
except ResourceForbiddenError:
# The port is not in the restrictions file. This can only
# happen if the user supplied us with the ``localport'' argument.
raise
except Exception:
# E.g. RepyArgumentError due to using a FQDN instead of an
# IP address argument -- the user's fault, really.
raise
else:
# Checked the whole port range without success
raise AddressBindingError, "Could not find any usable source port in the range " + str(portrange) + " on IP address " + str(localip) + "."
thissocketlikeobject = sockettimeout.timeout_socket(realsocketlikeobject, timeout)
return thissocketlikeobject
# Sends data to a node (opens the connection, writes the
# communication header, sends all the data, receives the result, and returns
# the result)...
def nmclient_rawcommunicate(nmhandle, *args):
try:
thisconnobject = _do_timeout_openconnection(
nmclient_handledict[nmhandle]['IP'],
nmclient_handledict[nmhandle]['port'], getmyip(),
timeout=nmclient_handledict[nmhandle]['timeout'])
except Exception, e:
raise NMClientException, repr(e)
# always close the connobject
try:
# send the args separated by '|' chars (as is expected by the node manager)
session.session_sendmessage(thisconnobject, '|'.join(args))
return session.session_recvmessage(thisconnobject)
except Exception, e:
raise NMClientException, str(e)
finally:
thisconnobject.close()
# Sends data to a node (opens the connection, writes the
# communication header, sends all the data, receives the result, and returns
# the result)...
def nmclient_signedcommunicate(nmhandle, *args):
# need to check lots of the nmhandle settings...
if nmclient_handledict[nmhandle]['timestamp'] == True:
# set the time based upon the current time...
timestamp = time.time_gettime()
elif not nmclient_handledict[nmhandle]['timestamp']:
# we're false, so set to None
timestamp = None
else:
# For some reason, the caller wanted a specific time...
timestamp = nmclient_handledict[nmhandle]['timestamp']
if nmclient_handledict[nmhandle]['publickey']:
publickey = nmclient_handledict[nmhandle]['publickey']
else:
raise NMClientException, "Must have public key for signed communication"
if nmclient_handledict[nmhandle]['privatekey']:
privatekey = nmclient_handledict[nmhandle]['privatekey']
else:
raise NMClientException, "Must have private key for signed communication"
# use this blindly (None or a value are both okay)
sequenceid = nmclient_handledict[nmhandle]['sequenceid']
if nmclient_handledict[nmhandle]['expiration']:
if timestamp == None:
# highly dubious. However, it's technically valid, so let's allow it.
expirationtime = nmclient_handledict[nmhandle]['expiration']
else:
expirationtime = timestamp + nmclient_handledict[nmhandle]['expiration']
else:
# they don't want this to expire
expirationtime = nmclient_handledict[nmhandle]['expiration']
# use this blindly (None or a value are both okay)
identity = nmclient_handledict[nmhandle]['identity']
# build the data to send. Ideally we'd do: datatosend = '|'.join(args)
# we can't do this because some args may be non-strings...
datatosend = args[0]
for arg in args[1:]:
datatosend = datatosend + '|' + str(arg)
try:
thisconnobject = _do_timeout_openconnection(
nmclient_handledict[nmhandle]['IP'],
nmclient_handledict[nmhandle]['port'], getmyip(),
timeout=nmclient_handledict[nmhandle]['timeout'])
except Exception, e:
raise NMClientException, repr(e)
# always close the connobject afterwards...
try:
try:
mysigneddata = signeddata.signeddata_signdata(datatosend, privatekey, publickey, timestamp, expirationtime, sequenceid, identity)
except ValueError, e:
raise NMClientException, str(e)
try:
session.session_sendmessage(thisconnobject, mysigneddata)
except Exception, e:
# label the exception and change the type...
raise NMClientException, "signedcommunicate failed on session_sendmessage with error '"+str(e)+"'"
try:
message = session.session_recvmessage(thisconnobject)
except Exception, e:
# label the exception and change the type...
raise NMClientException, "signedcommunicate failed on session_recvmessage with error '"+str(e)+"'"
return message
finally:
thisconnobject.close()
def nmclient_safelygethandle():
# I lock to prevent a race when adding handles to the dictionary. I don't
# need a lock when removing because a race is benign (it prevents reuse)
nmclient_handledictlock.acquire(True)
try:
potentialhandle = random.randomfloat()
while potentialhandle in nmclient_handledict:
potentialhandle = random.randomfloat()
return potentialhandle
finally:
nmclient_handledictlock.release()
# Create a new handle, the IP, port must be provided but others are optional.
# The default is to have no sequenceID, timestamps on, expiration time of 1
# hour, and the program should set and use the identity of the node. The
# public key, private key, and vesselids are left uninitialized unless
# specified elsewhere. Regardless, the keys and vesselid are not used to
# create the handle and so are merely transfered to the created handle.
# Per #537, the default timeout (15) should be greater than the wait period
# for starting a vessel.
def nmclient_createhandle(nmIP, nmport, sequenceid = None, timestamp=True, identity = True, expirationtime = 60*60, publickey = None, privatekey = None, vesselid = None, timeout=15):
thisentry = {}
thisentry['IP'] = nmIP
thisentry['port'] = nmport
thisentry['sequenceid'] = sequenceid
thisentry['timestamp'] = timestamp
thisentry['expiration'] = expirationtime
thisentry['publickey'] = publickey
thisentry['privatekey'] = privatekey
thisentry['vesselid'] = vesselid
thisentry['timeout'] = timeout
newhandle = nmclient_safelygethandle()
nmclient_handledict[newhandle] = thisentry
# Use GetVessels as a "hello" test (and for identity reasons as shown below)
try:
response = nmclient_rawsay(newhandle, 'GetVessels')
except (ValueError, NMClientException, KeyError), e:
del nmclient_handledict[newhandle]
raise NMClientException, e
# set up the identity
if identity == True:
for line in response.split('\n'):
if line.startswith('Nodekey: '):
# get everything after the Nodekey as the identity
nmclient_handledict[newhandle]['identity'] = line[len('Nodekey: '):]
break
else:
del nmclient_handledict[newhandle]
raise NMClientException, "Do not understand node manager identity in identification"
else:
nmclient_handledict[newhandle]['identity'] = identity
# it worked!
return newhandle
def nmclient_duplicatehandle(nmhandle):
newhandle = nmclient_safelygethandle()
nmclient_handledict[newhandle] = nmclient_handledict[nmhandle].copy()
return newhandle
# public. Use this to clean up a handle
def nmclient_destroyhandle(nmhandle):
try:
del nmclient_handledict[nmhandle]
except KeyError:
return False
return True
# public. Use these to get / set attributes about the handles...
def nmclient_get_handle_info(nmhandle):
if nmhandle not in nmclient_handledict:
raise NMClientException, "Unknown nmhandle: '"+str(nmhandle)+"'"
return nmclient_handledict[nmhandle].copy()
def nmclient_set_handle_info(nmhandle, dict):
if nmhandle not in nmclient_handledict:
raise NMClientException, "Unknown nmhandle: '"+str(nmhandle)+"'"
nmclient_handledict[nmhandle] = dict
# Public: Use this for non-signed operations...
def nmclient_rawsay(nmhandle, *args):
fullresponse = nmclient_rawcommunicate(nmhandle, *args)
try:
(response, status) = fullresponse.rsplit('\n',1)
except KeyError:
raise NMClientException, "Communication error '"+fullresponse+"'"
if status == 'Success':
return response
elif status == 'Error':
raise NMClientException, "Node Manager error '"+response+"'"
elif status == 'Warning':
raise NMClientException, "Node Manager warning '"+response+"'"
else:
raise NMClientException, "Unknown status '"+fullresponse+"'"
# Public: Use this for signed operations...
def nmclient_signedsay(nmhandle, *args):
fullresponse = nmclient_signedcommunicate(nmhandle, *args)
try:
(response, status) = fullresponse.rsplit('\n',1)
except KeyError:
raise NMClientException, "Communication error '"+fullresponse+"'"
if status == 'Success':
return response
elif status == 'Error':
raise NMClientException, "Node Manager error '"+response+"'"
elif status == 'Warning':
raise NMClientException, "Node Manager warning '"+response+"'"
else:
raise NMClientException, "Unknown status '"+fullresponse+"'"
# public, use this to do raw communication with a vessel
def nmclient_rawsaytovessel(nmhandle, call, *args):
vesselid = nmclient_handledict[nmhandle]['vesselid']
if not vesselid:
raise NMClientException, "Must set vesselid to communicate with a vessel"
return nmclient_rawsay(nmhandle,call, vesselid,*args)
# public, use this to do a signed communication with a vessel
def nmclient_signedsaytovessel(nmhandle, call, *args):
vesselid = nmclient_handledict[nmhandle]['vesselid']
if not vesselid:
raise NMClientException, "Must set vesselid to communicate with a vessel"
return nmclient_signedsay(nmhandle,call, vesselid,*args)
# public, lists the vessels that the provided key owns or can use
def nmclient_listaccessiblevessels(nmhandle, publickey):
vesselinfo = nmclient_getvesseldict(nmhandle)
# these will be filled with relevant vessel names...
ownervessels = []
uservessels = []
for vesselname in vesselinfo['vessels']:
if publickey == vesselinfo['vessels'][vesselname]['ownerkey']:
ownervessels.append(vesselname)
if 'userkeys' in vesselinfo['vessels'][vesselname] and publickey in vesselinfo['vessels'][vesselname]['userkeys']:
uservessels.append(vesselname)
return (ownervessels, uservessels)
#public, parse a node manager's vessel information and return it to the user...
def nmclient_getvesseldict(nmhandle):
response = nmclient_rawsay(nmhandle, 'GetVessels')
retdict = {}
retdict['vessels'] = {}
# here we loop through the response and set the dicts as appropriate
lastvesselname = None
for line in response.split('\n'):
if not line:
# empty line. Let's allow it...
pass
elif line.startswith('Version: '):
retdict['version'] = line[len('Version: '):]
elif line.startswith('Nodename: '):
retdict['nodename'] = line[len('Nodename: '):]
elif line.startswith('Nodekey: '):
retdict['nodekey'] = rsa.rsa_string_to_publickey(line[len('Nodekey: '):])
# start of a vessel
elif line.startswith('Name: '):
# if there is a previous vessel write it to the dict...
if lastvesselname:
retdict['vessels'][lastvesselname] = thisvessel
thisvessel = {}
# NOTE:I'm changing this so that userkeys will always exist even if there
# are no user keys (in this case it has an empty list). I think this is
# the right functionality.
thisvessel['userkeys'] = []
lastvesselname = line[len('Name: '):]
elif line.startswith('OwnerKey: '):
thiskeystring = line[len('OwnerKey: '):]
thiskey = rsa.rsa_string_to_publickey(thiskeystring)
thisvessel['ownerkey'] = thiskey
elif line.startswith('OwnerInfo: '):
thisownerstring = line[len('OwnerInfo: '):]
thisvessel['ownerinfo'] = thisownerstring
elif line.startswith('Status: '):
thisstatus = line[len('Status: '):]
thisvessel['status'] = thisstatus
elif line.startswith('Advertise: '):
thisadvertise = line[len('Advertise: '):]
if thisadvertise == 'True':
thisvessel['advertise'] = True
elif thisadvertise == 'False':
thisvessel['advertise'] = False
else:
raise NMClientException, "Unknown advertise type '"+thisadvertise+"'"
elif line.startswith('UserKey: '):
thiskeystring = line[len('UserKey: '):]
thiskey = rsa.rsa_string_to_publickey(thiskeystring)
thisvessel['userkeys'].append(thiskey)
else:
raise NMClientException, "Unknown line in GetVessels response '"+line+"'"
if lastvesselname:
retdict['vessels'][lastvesselname] = thisvessel
return retdict