-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetadata_lib.py
More file actions
631 lines (540 loc) · 22.7 KB
/
metadata_lib.py
File metadata and controls
631 lines (540 loc) · 22.7 KB
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
"""
<Program Name>
metadata_lib.py
<Authors>
Team Totolly Secure
<Started>
November 2015.
<Purpose>
To provide a list of functions that may assist with parsing metadata data
structure with a given policy.
"""
from collections import Counter
import json, sys, os.path, re
def check_acl(metadata, acl_name, violation):
"""
<Purpose>
Checks metadata against a list of permissions.
ALGORITHM OF THIS FUNCTION IS DEFINITELY NOT OPTIMIZED.
ONE TO THREE SUB FUNCTION SHOULD HANDLE commit, merge, and write.
<Arguments>
metadata dictionary
acl_name or file name to permissions
violation is the output file name
<Exceptions>
None
<Returns>
Dict
"""
acl = {}
if not os.path.isfile(acl_name):
print "Permission List does not exist."
sys.exit()
else:
acl = read_json(acl_name)
unreviewed = detect_unreviewed_merges(metadata)
violations = {}
for commit in metadata:
errors = ""
if commit in unreviewed:
errors += "Merge not code reviewed. "
if metadata[commit]["author"] not in acl:
errors += "Author " + str(metadata[commit]["author"]) + " not in ACL. "
if metadata[commit]["committer"] not in acl:
errors += "Committer " + str(metadata[commit]["committer"]) + " not in ACL. "
if "merge" in metadata[commit]["commit_type"]:
errors += "Merger " + str(metadata[commit]["committer"]) + " not in ACL. "
if "merge" in metadata[commit]["commit_type"] and metadata[commit]["committer"] in acl:
if "merge" in acl[metadata[commit]["committer"]]:
if "start" in acl[metadata[commit]["committer"]]["merge"]:
if isinstance(acl[metadata[commit]["committer"]]["merge"]["start"], bool):
if acl[metadata[commit]["committer"]]["merge"]["start"] == True:
print "Merger of " + str(commit) + " has open date for start merge permission."
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge start permission. "
else:
if compare_time(acl[metadata[commit]["committer"]]["merge"]["start"], metadata[commit]["commit_timestamp"]) == False:
errors += "Merger " + str(metadata[commit]["committer"]) + " merged before permission was granted. "
else:
errors += "ACL does not have start merge time for " + metadata[commit]["committer"] + ". "
if "end" in acl[metadata[commit]["committer"]]["merge"]:
if isinstance(acl[metadata[commit]["committer"]]["merge"]["end"], bool):
if acl[metadata[commit]["committer"]]["merge"]["end"] == True:
print "Merger of " + str(commit) + " has open date for end merge permission."
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge end permission. "
else:
if compare_time(metadata[commit]["commit_timestamp"], acl[metadata[commit]["committer"]]["merge"]["end"]) == False:
errors += "Merger " + str(metadata[commit]["committer"]) + " merged after permission has expired. "
else:
errors += "ACL does not have end merge time for " + metadata[commit]["committer"] + ". "
if "layer" in acl[metadata[commit]["committer"]]["merge"]:
if isinstance(acl[metadata[commit]["committer"]]["merge"]["layer"], bool):
if acl[metadata[commit]["committer"]]["merge"]["layer"] == True:
print "Merger of " + str(commit) + " can merge in any layer."
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge permissions in layer " + str(metadata[commit]["layer"]) + ". "
else:
if str(metadata[commit]["layer"]) in acl[metadata[commit]["committer"]]["merge"]["layer"]:
print "Merger of " + str(commit) + " can merge in layer " + str(metadata[commit]["layer"])
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge permissions in layer " + str(metadata[commit]["layer"]) + ". "
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge permissions in layer " + str(metadata[commit]["layer"]) + ". "
if "branch" in acl[metadata[commit]["committer"]]["merge"]:
if isinstance(acl[metadata[commit]["committer"]]["merge"]["branch"], bool):
if acl[metadata[commit]["committer"]]["merge"]["branch"] == True:
print "Merger of " + str(commit) + " can write in any branch."
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge permissions in branch " + str(metadata[commit]["branch"]) + ". "
else:
if str(metadata[commit]["branch"]) in acl[metadata[commit]["committer"]]["merge"]["branch"]:
print "Merger of " + str(commit) + " can merge in branch " + str(metadata[commit]["branch"])
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge permissions in branch " + str(metadata[commit]["branch"]) + ". "
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have merge permissions in branch " + str(metadata[commit]["branch"]) + ". "
if metadata[commit]["author"] in acl:
if "write" in acl[metadata[commit]["author"]]:
if "start" in acl[metadata[commit]["author"]]["write"]:
if isinstance(acl[metadata[commit]["author"]]["write"]["start"], bool):
if acl[metadata[commit]["author"]]["write"]["start"] == True:
print "Author of " + str(commit) + " has open date for start write permission."
else:
errors += "Author " + str(metadata[commit]["author"]) + " does not have write start permission. "
else:
if compare_time(acl[metadata[commit]["author"]]["write"]["start"], metadata[commit]["author_timestamp"]) == False:
errors += "Author " + str(metadata[commit]["author"]) + " wrote before permission was granted. "
else:
errors += "ACL does not have start write time for " + metadata[commit]["author"] + ". "
if "end" in acl[metadata[commit]["author"]]["write"]:
if isinstance(acl[metadata[commit]["author"]]["write"]["end"], bool):
if acl[metadata[commit]["author"]]["write"]["end"] == True:
print "Author of " + str(commit) + " has open date for end write permission."
else:
errors += "Author " + str(metadata[commit]["committer"]) + " does not have write end permission. "
else:
if compare_time(metadata[commit]["author_timestamp"], acl[metadata[commit]["author"]]["write"]["end"]) == False:
errors += "Author " + str(metadata[commit]["author"]) + " wrote after permission has expired. "
else:
errors += "ACL does not have end write time for " + metadata[commit]["author"] + ". "
if "layer" in acl[metadata[commit]["author"]]["write"]:
if isinstance(acl[metadata[commit]["author"]]["write"]["layer"], bool):
if acl[metadata[commit]["author"]]["write"]["layer"] == True:
print "Author of " + str(commit) + " can write in any layer."
else:
errors += "Author " + str(metadata[commit]["author"]) + " does not have write permissions in layer " + str(metadata[commit]["layer"]) + ". "
else:
if str(metadata[commit]["layer"]) in acl[metadata[commit]["author"]]["write"]["layer"]:
print "Author of " + str(commit) + " can write in layer " + str(metadata[commit]["layer"])
else:
errors += "Author " + str(metadata[commit]["author"]) + " does not have write permissions in layer " + str(metadata[commit]["layer"]) + ". "
else:
errors += "Author " + str(metadata[commit]["author"]) + " does not have write permissions in layer " + str(metadata[commit]["layer"]) + ". "
if "branch" in acl[metadata[commit]["author"]]["write"]:
if isinstance(acl[metadata[commit]["author"]]["write"]["branch"], bool):
if acl[metadata[commit]["author"]]["write"]["branch"] == True:
print "Author of " + str(commit) + " can write in any branch."
else:
errors += "Author " + str(metadata[commit]["author"]) + " does not have write permissions in branch " + str(metadata[commit]["branch"]) + ". "
else:
if str(metadata[commit]["branch"]) in acl[metadata[commit]["author"]]["write"]["branch"]:
print "Author of " + str(commit) + " can write in branch " + str(metadata[commit]["branch"])
else:
errors += "Author " + str(metadata[commit]["author"]) + " does not have write permissions in branch " + str(metadata[commit]["branch"]) + ". "
else:
errors += "Author " + str(metadata[commit]["author"]) + " does not have write permissions in branch " + str(metadata[commit]["branch"]) + ". "
if metadata[commit]["committer"] in acl and "merge" not in metadata[commit]["commit_type"]:
if "commit" in acl[metadata[commit]["committer"]]:
if "start" in acl[metadata[commit]["committer"]]["commit"]:
if isinstance(acl[metadata[commit]["committer"]]["commit"]["start"], bool):
if acl[metadata[commit]["committer"]]["commit"]["start"] == True:
print "Committer of " + str(commit) + " has open date for start commit permission."
else:
errors += "Committer " + str(metadata[commit]["committer"]) + " does not have commit start permission. "
else:
if compare_time(acl[metadata[commit]["committer"]]["commit"]["start"], metadata[commit]["commit_timestamp"]) == False:
errors += "Committer " + str(metadata[commit]["committer"]) + " committed before permission was granted. "
else:
errors += "ACL does not have start commit time for " + metadata[commit]["committer"] + ". "
if "end" in acl[metadata[commit]["committer"]]["commit"]:
if isinstance(acl[metadata[commit]["committer"]]["commit"]["end"], bool):
if acl[metadata[commit]["committer"]]["commit"]["end"] == True:
print "Committer of " + str(commit) + " has open date for end write permission."
else:
errors += "Merger " + str(metadata[commit]["committer"]) + " does not have commit end permission. "
else:
if compare_time(metadata[commit]["commit_timestamp"], acl[metadata[commit]["committer"]]["commit"]["end"]) == False:
errors += "Committer " + str(metadata[commit]["committer"]) + " committed after permission has expired. "
else:
errors += "ACL does not have end commit time for " + metadata[commit]["committer"] + ". "
if "layer" in acl[metadata[commit]["committer"]]["commit"]:
if isinstance(acl[metadata[commit]["committer"]]["commit"]["layer"], bool):
if acl[metadata[commit]["committer"]]["commit"]["layer"] == True:
print "Committer of " + str(commit) + " can commit in any layer."
else:
errors += "Committer " + str(metadata[commit]["committer"]) + " does not have commit permissions in layer " + str(metadata[commit]["layer"]) + ". "
else:
if str(metadata[commit]["layer"]) in acl[metadata[commit]["committer"]]["commit"]["layer"]:
print "Committer of " + str(commit) + " can commit in layer " + str(metadata[commit]["layer"])
else:
errors += "Committer " + str(metadata[commit]["committer"]) + " does not have commit permissions in layer " + str(metadata[commit]["layer"]) + ". "
else:
errors += "Committer " + str(metadata[commit]["committer"]) + " does not have commit permissions in layer " + str(metadata[commit]["layer"]) + ". "
if "branch" in acl[metadata[commit]["committer"]]["commit"]:
if isinstance(acl[metadata[commit]["committer"]]["commit"]["branch"], bool):
if acl[metadata[commit]["committer"]]["commit"]["branch"] == True:
print "Committer of " + str(commit) + " can commit in any branch."
else:
errors += "Committer " + str(metadata[commit]["committer"]) + " does not have commit permissions in branch " + str(metadata[commit]["branch"]) + ". "
else:
if str(metadata[commit]["branch"]) in acl[metadata[commit]["committer"]]["commit"]["branch"]:
print "Committer of " + str(commit) + " can commit in branch " + str(metadata[commit]["branch"])
else:
errors += "Committer " + str(metadata[commit]["committer"]) + " does not have commit permissions in branch " + str(metadata[commit]["branch"]) + ". "
else:
errors += "Committer " + str(metadata[commit]["committer"]) + " does not have commit permissions in branch " + str(metadata[commit]["branch"]) + ". "
violations[commit] = errors
counter = 0
for i in violations:
if violations[i] != "":
counter += 1
print "There are " + str(counter) + " commits/hashes with violations."
write_json(violation, violations)
return violations
def compare_time(start, end):
"""
<Purpose>
Manually compares two times.
Returns True if end time is more recent than start time.
Returns False otherwise.
<Arguments>
start time
end time
<Exceptions>
None
<Returns>
Bool
"""
s = re.split('-|\+|:| ', start)
e = re.split('-|\+|:| ', end)
if s[0] > e[0]:
return False
if s[1] > e[1]:
return False
if s[2] > e[2]:
return False
if s[3] > e[3]:
return False
if s[4] > e[4]:
return False
if s[5] > e[5]:
return False
return True
def read_json(name):
"""
<Purpose>
Reads JSON file and returns it as a dictionary.
<Arguments>
Name of input file
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns dictionary
"""
with open(name) as f:
data = json.load(f)
return data
def write_json(name, data):
"""
<Purpose>
Writes dictionary to JSON file.
<Arguments>
Name of output file
Dictionary to write in file
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns Nothing
"""
with open(name, "w") as ofs :
ofs.write(json.dumps(data, indent = 4 ))
def return_hashes(metadata):
"""
<Purpose>
Returns metadata keys in a list.
<Arguments>
Metadata object
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
hashes = []
for key in metadata:
hashes.append(key)
return hashes
def check(sub_key, value, metadata):
"""
<Purpose>
Runs list of keys for sub_metadata that have a value that does not
match the value passed in.
<Arguments>
sub_key that can be "code_reviewed", "committer", "branch", etc...
Expected value for the above sub_key
Metadata object.
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
lst = []
for key in metadata:
if value not in metadata[key][sub_key]:
lst.append(key)
return lst
def check_with_type(sub_key, value, metadata, type_):
"""
<Purpose>
Runs list of keys for sub_metadata that have a value that does not
match the value passed in for a specific type of commit.
<Arguments>
sub_key that can be "code_reviewed", "committer", "branch", etc...
Expected value for the above sub_key
Metadata object.
type of commit to check for in the Metadata object.
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
lst = []
for key in metadata:
if value not in metadata[key][sub_key] and type_ in metadata[key]["commit_type"]:
lst.append(key)
return lst
# ______________________________________________________________________________
def list_merges(metadata):
"""
<Purpose>
Finds all merging commits.
<Arguments>
Metadata object.
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
lst = []
for key in metadata:
if "parent1" in metadata[key] and "parent2" in metadata[key]:
lst.append(key)
return lst
def list_branches(metadata):
"""
<Purpose>
Finds all branching/forking commits.
<Arguments>
Metadata object.
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
lst = []
for key in metadata:
if "child1" in metadata[key] and "child2" in metadata[key]:
lst.append(key)
return lst
# ______________________________________________________________________________
def author_committer_same(metadata):
"""
<Purpose>
Checks commits where author and committer are the same.
<Arguments>
Metadata object.
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
lst = []
for key in metadata:
if metadata[key]["author"] == metadata[key]["committer"]:
lst.append(key)
return lst
def author_committer_differ(metadata):
"""
<Purpose>
Checks commits where author and committer differ.
<Arguments>
Metadata object.
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
lst = []
for key in metadata:
if metadata[key]["author"] != metadata[key]["committer"]:
lst.append(key)
return lst
# ______________________________________________________________________________
def detect_unreviewed_merges(metadata):
"""
<Purpose>
Checks all merges that have not been code reviewed.
<Arguments>
Metadata object.
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a list of hashes.
"""
return check_with_type("code_reviewed", "True", metadata, "merge")
def detect_mergers(metadata):
"""
<Purpose>
Returns mergers and associated number of merges in
the form of a list of tuples. Also returns
number of unique mergers.
<Arguments>
Metadata object
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns list of tuples and length of list
"""
lst = []
for key in metadata:
lst.append(metadata[key]["committer"])
# Finding the mode merger and its count along with any other mergers
data = Counter(lst)
counters = data.most_common() # List of Tuples
return counters, len(lst)
def get_infamous_mergers(metadata):
"""
<Purpose>
Returns hashes to mergers that merges less than 10% of the time.
THIS FUNCTION HAS BEEN DEEMED USELESS
<Arguments>
Metadata object
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns list of hashes
"""
lst = []
counters, mergers = detect_mergers(metadata)
for merger in counters: # List of Tuples traversal
if merger[1] / mergers < 0.1:
print "Possible Infamous Merger: " + str(merger[0])
lst.append(merger[0])
infamous_hashes = []
for key in metadata:
if metadata[key]["committer"] in lst:
infamous_hashes.append(metadata[key])
return infamous_hashes
# ______________________________________________________________________________
# WORKFLOW FUNCTIONS
def __dictator_lieutenent_workflow_scrap(dictator, lieut_list, metadata):
"""
DO NOT USE
"""
print "Checking if Dictator-Lieutenent Workflow..."
is_workflow = True
infamous_mergers = get_infamous_mergers(metadata)
if len(infamous_mergers) != 1:
is_workflow = False
print "Not a dictator-lieutenent workflow"
return is_workflow
def dictator_lieutenent_driver(acl, dictator_names, lieut_names, metadata):
"""
<Purpose>
This function takes names of dictators and lieutenents and calls the
actual function that detects if repository follows a dictator-
lieutenent workflow.
<Arguments>
Access Control List (dictionary of dictionaries)
{
user1:{
write:"True",
commit:"True",
merge:(<str>,<str>)
},
user2:{
write:"True"
},
...
}
Dictator names (list of strings)
Liuetenent names (list of strings)
Metadata object
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a boolean value.
"""
dictator_dict = {}
lieut_dict = {}
for key in acl:
if key in dictator_names:
temp = {}
temp[key] = acl[key]
dictator_dict[key] = temp
elif key in dictator_names:
temp = {}
temp[key] = acl[key]
dictator_dict[key] = temp
return __dictator_lieutenent_workflow(dictator_dict, lieut_dict, metadata)
def __dictator_lieutenent_workflow(dictator_dict, lieut_dict, metadata):
"""
<Purpose>
Checks if merges in the master is done only by the dictator and
any repositories that are one merge away from the master has
merges that have been done by lieutenent.
<Arguments>
Dictator's dictionary from driver function
Liuetenent's dictionary from driver function
Metadata object
<Exceptions>
None. Program will fail silently if algorithm is not found.
<Returns>
Returns a boolean value.
"""
print "Checking if Dictator-Lieutenent Workflow..."
is_workflow = True
merges = list_merges(metadata) # Returns list of hashes for merges
for hashes in merges:
if metadata[hashes]["committer"] in dictator_dict and metadata[hashes]["layer"] == 0:
if "merge" in dictator_dict[metadata[hashes]["committer"]]:
if isinstance(dictator_dict[metadata[hashes]["committer"]]["merge"], tuple):
if metadata[hashes]["commit_timestamp"] > dictator_dict[metadata[hashes]["committer"]]["merge"][0] and metadata[hashes]["commit_timestamp"] < dictator_dict[metadata[hashes]["committer"]]["merge"][1]:
print "Dictator is fine"
else:
is_workflow = False
elif isinstance(dictator_dict[metadata[hashes]["committer"]]["merge"], str):
if dictator_dict[metadata[hashes]["committer"]]["merge"] == "True":
print "Dictator is fine"
else:
is_workflow = False
else:
is_workflow = False
for hashes in merges:
if metadata[hashes]["committer"] in lieut_dict and metadata[hashes]["layer"] == 1:
if "merge" in lieut_dict[metadata[hashes]["committer"]]:
if isinstance(lieut_dict[metadata[hashes]["committer"]]["merge"], tuple):
if metadata[hashes]["commit_timestamp"] > lieut_dict[metadata[hashes]["committer"]]["merge"][0] and metadata[hashes]["commit_timestamp"] < lieut_dict[metadata[hashes]["committer"]]["merge"][1]:
print "Lieutenent is fine"
else:
is_workflow = False
elif isinstance(lieut_dict[metadata[hashes]["committer"]]["merge"], str):
if lieut_dict[metadata[hashes]["committer"]]["merge"] == "True":
print "Lieutenent is fine"
else:
is_workflow = False
else:
is_workflow = False
if is_workflow == False:
print "Not Dictator-Lieutenent Workflow"
return is_workflow