This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
delegatetools.py
672 lines (525 loc) · 22.4 KB
/
delegatetools.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
from amsoil.core import serviceinterface
from amsoil.config import expand_amsoil_path
import amsoil.core.pluginmanager as pm
import amsoil.core.log
from delegateexceptions import *
from apiexceptionsv2 import *
import os.path
import json
import uuid
import pyrfc3339, datetime, pytz
import re
logger = amsoil.core.log.getLogger('delegatetools')
class DelegateTools(object):
"""
Common tools used to implement a ClearingHouse (CH) delegate.
"""
JSON_COMMENT = "__comment" #: delimeter for comments in loaded JSON files (config.json and defaults.json)
REQUIRED_METHOD_KEYS = ['members_to_add', 'members_to_change', 'members_to_remove'] #: list of valid keys to be passed as 'options' in a 'modify_membership' call
GET_VERSION_FIELDS = ['URN', 'IMPLEMENTATION', 'SERVICES', 'CREDENTIAL_TYPES', 'ROLES', 'SERVICE_TYPES', 'API_VERSIONS'] #: list of fields possible in a 'get_version' API call response
def __init__(self):
"""
Load configuration files. Combine the default field names with the supplemenary fields to form a combined list.
"""
self.STATIC = {} #: holds static configuration and settings loaded from JSON files (config.json and defaults.json)
self._load_files()
self._combine_fields()
def _load_files(self):
"""
Load JSON configuration and default files.
Raises:
MalformedConfigFile: An error occured when loading the JSON file.
"""
paths = self._get_paths()
for path_key, path_value in paths.iteritems():
if not os.path.exists(path_value):
raise ConfigFileMissing(path_value)
try:
self.STATIC[path_key] = self._strip_comments(json.load(open(path_value)))
except Exception:
raise MalformedConfigFile(path_value, '')
def _combine_fields(self):
"""
Combine default fields with supplementary fields to form a combined set.
Supplementary fields can also overwrite exsiting default fields.
"""
self.STATIC['COMBINED'] = self.STATIC['DEFAULTS']
for type_key, type_value in self.STATIC['SUPPLEMENTARY_FIELDS'].iteritems():
if type_key not in DelegateTools.JSON_COMMENT:
for field_key, field_value in type_value.iteritems():
self.STATIC['COMBINED'][type_key.upper()][field_key.upper()] = field_value
def _strip_comments(self, json):
"""
Recursively strip comments out of loaded JSON files.
The delimiter used to define a comment is defined in a global variable (JSON_COMMENT).
Args:
json: JSON content to strip comments from
Returns:
JSON content with comments removed
"""
if type(json) in [str, unicode, int, float, bool, type(None)]:
return json
if isinstance(json, list):
return [self._strip_comments(j) for j in json if not ((type(j) in [str, unicode]) and j.startswith(DelegateTools.JSON_COMMENT))]
if isinstance(json, dict):
return dict( (k, self._strip_comments(v)) for (k,v) in json.iteritems() if k != DelegateTools.JSON_COMMENT) # there would be a dict comprehension from 2.7
def _get_paths(self):
"""
Get full file paths for JSON files to load (config.json and defaults.json).
Returns:
dictionary containing the loaded JSON content
"""
config = pm.getService("config")
config_path = config.get("delegatetools.config_path")
supplemetary_fields_path = config.get("delegatetools.supplemetary_fileds_path")
service_registry_path = config.get("delegatetools.service_registry_path")
defaults_path = config.get("delegatetools.defaults_path")
return {'CONFIG' : expand_amsoil_path(config_path),
'DEFAULTS' : expand_amsoil_path(defaults_path),
'SUPPLEMENTARY_FIELDS' : expand_amsoil_path(supplemetary_fields_path),
'REGISTRY' : expand_amsoil_path(service_registry_path)}
@serviceinterface
def validate_expiration_time(self, original_value, value_in_question, type_=None):
"""
Validate the expiration time value passed to Update or Create Methods.
Args:
original_value: The original value that needs to be compared (e.g., SLICE creation date)
value_in_question: The value that is doubted for correctness (e.g., Expiry time update date)
Returns:
a boolean value to indicate whether the expiration time valid or not
"""
parsed_original_value = pyrfc3339.parse(original_value)
parsed_value_in_question = pyrfc3339.parse(value_in_question)
if type_:
maximum_expansion_duration = self.STATIC['CONFIG'][type_]['max_%s_extension_time' %type_.lower()]
configuration_delta = datetime.timedelta(days=maximum_expansion_duration)
delta_time_days = parsed_value_in_question - parsed_original_value
return True if parsed_original_value < parsed_value_in_question and delta_time_days < configuration_delta else False
else:
return parsed_original_value < parsed_value_in_question
@serviceinterface
def get_fields(self, type_):
"""
Get combined fields for a given object type.
Args:
type_: the type of object
Returns:
the combined fields
"""
return self.STATIC['COMBINED'][type_]
@serviceinterface
def get_supplementary_fields(self, types):
"""
Get supplementary fields for a set of given object types.
Args:
types: a list of object types
Returns:
the supplementary fields
"""
result = {}
for _type in types:
try:
result.update(self.STATIC['SUPPLEMENTARY_FIELDS'][_type])
except KeyError:
pass
return result
@serviceinterface
def get_config(self, type_):
"""
Get configuration fields for a given object type.
Args:
type_: the type of object
Returns:
the configuration fields
"""
return self.STATIC['CONFIG'].get(type_, {})
@serviceinterface
def get_registry(self):
"""
Get REGISTRY fields for a given object type.
Args:
type_: the type of object
Returns:
the REGISTRY fields
"""
return self.STATIC['REGISTRY']
@staticmethod
@serviceinterface
def get_version(resource_manager):
"""
Get details for a 'get_version' response by calling relevant methods in the corresponding resource manager.
If the method is not available, it implies that this field is not required/supported.
Args:
resource_manager: Resource Manager instance to request information from
Returns:
complete 'get_version' response
"""
version = {}
for field in DelegateTools.GET_VERSION_FIELDS:
if hasattr(resource_manager, field.lower()):
version[field] = getattr(resource_manager, field.lower())()
return version
@serviceinterface
def get_whitelist(self, type_):
"""
Forms a number of whitelists for a given object type.
Lists formed include:
* create_whitelist: fields that can be passed in 'create' operation
* create_required: fields that must be passed in a 'create' operation
* update_whitelist: fields that can be passed in an 'update' operation
* lookup_match: fields that can be passed in a 'lookup' operation's 'match' field
* lookup_protected: fields that must be protected in a 'lookup' operation
* lookup_identifying: fields that identify a user in a 'lookup' operation
Protected and identify information is to be given out according to
implementation-specific privileges of requesting user
Args:
type_: the type of object
Returns:
dictionary of whitelists
"""
combined_fields = self.STATIC['COMBINED'][type_]
whitelist = {'create_whitelist' : [], 'create_required' : [],
'update_whitelist' : [], 'lookup_match' : [], 'lookup_private' : [],
'lookup_identifying' : []}
for field_key, field_value in combined_fields.iteritems():
whitelist = self._get_fields_values(whitelist, field_key, field_value)
return whitelist
def _get_fields_values(self, whitelist, field_key, field_value):
"""
Set whitelist values for a given field.
Fields and defaults as per: http://groups.geni.net/geni/wiki/CommonFederationAPIv2#APIget_versionmethods
Args:
whitelist: dictionary of whitelists to update
field_key: key for field
field_value: value for field
Returns:
dictionary of whitelists
"""
if field_value.get('CREATE', 'NOT ALLOWED') in ['REQUIRED', 'ALLOWED']:
whitelist['create_whitelist'].append(field_key)
if field_value.get('CREATE', False) is 'REQUIRED':
whitelist['create_required'].append(field_key)
if field_value.get('MATCH', True):
whitelist['lookup_match'].append(field_key)
if field_value.get('UPDATE', False):
whitelist['update_whitelist'].append(field_key)
protect = field_value.get('PROTECT', 'PUBLIC')
if protect is 'IDENTIFYING':
whitelist['lookup_identifying'].append(field_key)
if protect is 'PRIVATE':
whitelist['lookup_private'].append(field_key)
return whitelist
@staticmethod
@serviceinterface
def member_check(required_field_keys, options):
"""
Check correctly formed options for 'modify_membership' method.
Ensures method is one of 'members_to_add', 'members_to_change' or 'members_to_remove'.
Args:
required_field_keys: keys to add, change or remove are dependent on object type (Slice Member or Project Member)
options: call parameters to check
Raises:
GFedv2ArgumentError: There is an inconsitency in the options given
"""
for method_key, method_value in options.iteritems():
if method_key in DelegateTools.REQUIRED_METHOD_KEYS:
for item in method_value:
for field_key in item.iterkeys():
if not field_key in required_field_keys:
raise GFedv2ArgumentError("Member key to modify not of required type. Offending key is: " +
str(field_key) + ". Should be one of these types: " +
str(required_field_keys))
else:
raise GFedv2ArgumentError("Member method key not found. Offending key is: " + str(method_key) +
". Should be one of these types: " +
str(DelegateTools.REQUIRED_METHOD_KEYS))
@staticmethod
@serviceinterface
def slice_name_check(slice_name):
if not re.match(r'^[a-zA-Z0-9][A-Za-z0-9-]{1,19}$', slice_name):
raise GFedv2ArgumentError('SLICE_NAME field must be <= 19 characters, must only contain alphanumeric '
'characters or hyphens and those hyphens must not be leading.')
@staticmethod
@serviceinterface
def object_creation_check(fields, whitelist):
"""
Check if the given fields can be used in creating an object.
Args:
fields: fields to verify
whitelist: field names to check against
Raises:
GFedv2ArgumentError: There is a required field missing or it is not possible to pass this field during object creation.
"""
required = set(whitelist.get('create_required')).difference(set(fields))
whitelist = set(fields).difference(set(whitelist.get('create_whitelist')))
if required:
raise GFedv2ArgumentError('Required key(s) missing for object creation: ' + ', '.join(required))
if whitelist:
raise GFedv2ArgumentError('Cannot pass the following key(s) when creating an object : ' +
', '.join(whitelist))
@staticmethod
@serviceinterface
def object_update_check(fields, whitelist):
"""
Check if the given fields can be used in updating an object.
Args:
fields: field names to verify
whitelist: field names to check against
Raises:
GFedv2ArgumentError: It is not possible to pass this field during an object update.
"""
whitelist = set(fields).difference(set(whitelist.get('update_whitelist')))
if whitelist:
raise GFedv2ArgumentError('Cannot pass the following key(s) when updating an object : ' + ', '.join(whitelist))
@serviceinterface
def object_consistency_check(self, type_, fields):
"""
Check that fields conform to a predefined type by calling a corresponding method.
Args:
type_: the type of object
fields: fields to verify
Raises:
GFedv2ArgumentError: Inconsistency found between a field value and the required type.
"""
combined = self.STATIC['COMBINED'][type_]
for key, value in fields.iteritems():
if DelegateTools.JSON_COMMENT not in key and value is not None:
field_type = combined[key.upper()].get('TYPE')
try:
getattr(TypeCheck, 'check_' + field_type.lower())(value)
except AttributeError:
raise GFedv2ArgumentError('No type check available for: ' + field_type + '. Please check your supplementary fields for valid data types. ' +
'See http://groups.geni.net/geni/wiki/CommonFederationAPIv2#AppendixB:APIDataTypes for more details.')
except Exception:
raise GFedv2ArgumentError('Field {' + key + ' : ' + str(value) + '} is not of type ' + field_type)
@staticmethod
@serviceinterface
def decompose_slice_urns(match_value_to_decompose):
"""
Create individual SLICE_URN entries to match from a list of SLICE_URN Values. For example,
input = {SLICE_URN: [urn1, unr2, urn3}
output =[{SLICE_URN: urn1}, {SLICE_URN:urn2}, {SLICE_URN:urn3}]
Args:
match_value_to_decompose: dictionary of individual slice URN list with other match values
Returns:
match_urn_list: A list of dictionaries for each slice URN
"""
match_urn_list=[]
if 'SLICE_URN' in match_value_to_decompose:
get_result=match_value_to_decompose.get('SLICE_URN')
if isinstance(get_result,list):
urns_to_query=match_value_to_decompose.pop('SLICE_URN')
for value in urns_to_query:
match_value_to_decompose_copy=match_value_to_decompose.copy()
match_value_to_decompose['SLICE_URN']=value
match_urn_list.append(match_value_to_decompose_copy)
else:
match_urn_list.append(match_value_to_decompose)
else:
match_urn_list.append(match_value_to_decompose)
return match_urn_list
@staticmethod
@serviceinterface
def to_keyed_dict(list_, key):
"""
Convert a list to a dictionary, keyed to given key.
Args:
list_: list object use for conversion
key: field to use as dictionary key
Returns:
keyed dictionary
"""
for d in list_:
if not key in d:
raise ValueError("Can not convert to dict because the key_field name (%s) is not in the dictionary (%s)" % (key, d))
return { d[key] : dict(filter(lambda (k,v): (k != key), d.iteritems())) for d in list_}
@serviceinterface
def match_and_filter(self, list_of_dicts, field_filter, field_match):
"""
Takes a list of dicts and applies the given filter and matches the results (please GENI Federation API on how matching and filtering works).
if field_filter is None the unfiltered list is returned.
"""
return [self._filter_fields(d, field_filter) for d in list_of_dicts if self._does_match_fields(d, field_match)]
def _does_match_fields(self, d, field_match):
"""
Returns if the given dictionary matches the {field_match}.
{field_match} may look like: { 'must_be' : 'this', 'and_any_of_' : ['tho', 'se']}
"""
if not field_match:
return True
for mk, mv in field_match.iteritems(): # each matches must be fulfilled
val = d[mk]
if isinstance(mv, list): # any of those values (OR)
found_any = False;
for mvv in mv:
if val == mvv:
found_any = True
if not found_any:
return False
else: # or explicitly this one
if not val == mv:
return False
return True
def _filter_fields(self, d, field_filter):
"""Takes a dictionary and applies the filter. Returns the unfiltered d if None is given."""
if not field_filter:
return d
result = {}
for f in field_filter:
result[f] = d[f]
return result
class TypeCheck():
"""
Used as a holder for various type checks used in 'object_consistency_check' method.
"""
@staticmethod
def check_urn(value):
"""
Check if value is a valid URN string
See wiki for more details: http://groups.geni.net/geni/wiki/GeniApiIdentifiers
Args:
value: item to check
Raises:
Exception: value is not of valid URN string
"""
if not re.match(r"^urn:publicid:IDN+\+[A-Za-z0-9\._:-]+\+[A-Za-z0-9]+\+[A-Za-z0-9\._+:-]*$", value):
raise Exception
@staticmethod
def check_url(value):
"""
Check if value is a valid URL string
Args:
value: item to check
Raises:
Exception: value is not of valid URL string
"""
regex = re.compile(
r'^(?:http)s?://'
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
r'(?::\d+)?'
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
if not re.match(regex, value):
raise Exception
@staticmethod
def check_uid(value):
"""
Check if value is a valid UUID string.
Args:
value: item to check
Raises:
Exception: value is not of valid UID string
"""
uuid.UUID(value)
@staticmethod
def check_string(value):
"""
Check if value is a valid String.
Args:
value: item to check
Raises:
Exception: value is not of type String
"""
if not isinstance(value, basestring):
raise Exception
@staticmethod
def check_integer(value):
"""
Check if value is a valid Integer.
Args:
value: item to check
Raises:
Exception: value is not of type Integer
"""
if not isinstance(value, int):
raise Exception
@staticmethod
def check_datetime(value):
"""
Check if value is a valid RFC3339 string.
See RFC3339 for more details: http://www.ietf.org/rfc/rfc3339.txt
Args:
value: item to check
Raises:
Exception: value is not of valid RFC3339 string
"""
pyrfc3339.parse(value)
@staticmethod
def check_email(value):
"""
Check if value is a valid email string.
Args:
value: item to check
Raises:
Exception: value is not of valid email string
"""
if not re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$", value):
raise Exception
@staticmethod
def check_key(value):
"""
Check if value is a valid key string.
Should be contents, rather than filename.
Incomplete implementation. Only very basic object check currently.
Args:
value: item to check
Raises:
Exception: value is not of valid key string
"""
# TODO: check key validity - SSH or SSL public or private key
if not isinstance(value, basestring):
raise Exception
@staticmethod
def check_boolean(value):
"""
Check if value is a valid Boolean.
Args:
value: item to check
Raises:
Exception: value is not of type Boolean
"""
if not isinstance(value, bool):
raise Exception
@staticmethod
def check_credentials(value):
"""
Check if value is a valid credential string.
Should be contents, rather than filename.
Incomplete implementation. Only very basic object check currently.
Args:
value: item to check
Raises:
Exception: value is not of valid credential string
"""
# TODO: check credential validity with geni_trust
if not isinstance(value, basestring):
raise Exception
@staticmethod
def check_certificate(value):
"""
Check if value is a valid certificate string.
Should be contents, rather than filename.
Incomplete implementation. Only very basic object check currently.
Args:
value: item to check
Raises:
Exception: value is not of valid certificate string
"""
# TODO: check certificate validity with geni_trust
if not isinstance(value, basestring):
raise Exception
@staticmethod
def check_list_of_dictionaries(value):
"""
Check if value is a valid list of dictionaries.
Args:
value: item to check
Raises:
Exception: value is not of valid list of dictionaries
"""
if not isinstance(value, list):
raise Exception
for dictionary in value:
if not isinstance(dictionary, dict):
raise Exception