-
Notifications
You must be signed in to change notification settings - Fork 22
/
steward_tech_check.py
executable file
·1172 lines (1126 loc) · 50 KB
/
steward_tech_check.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import os
import sys
import platform
import subprocess
import re
#Rules definition takes care of the following item in 7.1 of:
# https://sovrin.org/wp-content/uploads/2017/06/SovrinProvisionalTrustFramework2017-03-22.pdf
# 5.a - MUST run a server operating system that receives timely patches from its vendor or
# community. For Linux, less than 2.5 years old
# Rule: os_allowed
#
# 7.a - Run on a mainstream hypervisor
# Rule: machine_type_allowed
#
# 8 - Machine is dedicated to the validator, no other services
# Rule:ports_allowed
#
# 16 - Have 8 or more cores
# Rule: machine_hw_required
#
# 17 - Have at least 32GB of RAM and 1-2+TB of reliable disk space
# Rule: machine_hw_required
# [NOTE] Hardware raid detection is only checking if a card is present not setup
#
# 18 - Must be running NTP and maintain a system clock that is demonstrably in sync within
# two seconds.
# Rule: procs_required
# [NOTE] This only checks that ntpd is running, not that it is in-sync
#
# 22 - Run a firewall that disallows public ingress except on ports used by the validator
# node software or remote administration tools.
# Rule: firewalls_allowed
# [NOTE] This only checks IF there are any iptables rules, not that they are
# blocking anything. It does NOT check if there is an external device.
rules = {
"must": {
"os_allowed": {
"oses": {
"ubuntu": [
"PRETTY_NAME / LTS",
"VERSION_ID >= 14.04"
],
"debian": [
"VERSION_ID >= 7.0"
],
"sles": [
"VERSION_ID >= 11.0"
],
"opensuse": [
"VERSION_ID ranges 15-42.0,42.2-42.3"
],
"opensuse-leap": [
"VERSION_ID ranges 15-42.0,42.2-42.3"
],
"centos": [
"VERSION_ID >= 6.0"
],
"rhel": [
"VERSION_ID >= 6.0"
]
}
},
"ports_allowed": {
"ports": {
"tcp": [
{
'port': '= 22',
'prog': 'sshd'
},
{
'port': '= 123',
'prog': 'ntpd'
},
{
'port': '>= 9700',
'prog': 'python3'
}
],
"udp": [
{
'port': '= 68',
'prog': 'dhclient'
},
{
'port': '= 123',
'prog': 'ntpd'
}
]
}
},
"machine_type_allowed": {
"types": {
"vm": [ 'kvm','xen','vmware','virtualbox', 'microsoft'],
"container": [ 'docker','lxc','podman' ],
"metal": [ 'hardware' ]
}
},
"machine_resources_required": {
"resources": {
"memory": ">= 8000000000",
"disk": {
'path': "/var/lib/indy",
'size': ">= 250000000000",
'raid': True
},
'hardware': 'attested',
'single_machine': 'attested'
}
},
"procs_required": {
'comment': '',
'processes': [
['/usr/sbin/ntpd', '/lib/systemd/systemd-timesyncd']
]
},
"firewalls_allowed": {
'comment': '',
'firewalls': [
'iptables',
'attested'
]
},
"administration": {
'rules': {
'personel': 'attested',
'monitoring': 'attested',
'other_access': 'attested'
}
}
},
"should": {
"machine_resources_recommended": {
"resources": {
"cpu_cores": ">= 2"
}
},
"access": {
"rules": {
'locked_door': 'attested',
'isolated_network': 'attested',
'two_factor': 'attested'
}
},
"availability": {
"rules": {
'ups': 'attested',
'broadband': 'attested',
'dedicated_nics': 'attested',
'backups': 'attested'
}
},
"software": {
"rules": {
"patching": 'attested'
}
}
}
}
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Node:
def __init__(self):
self.vm_identifiers = ['kvm','xen','vmware','innotek']
self._os_name_map = {
'red hat enterprise linux server': 'rhel',
'suse linux enterprise server': 'sles'
}
self.os_info = self._get_os_info()
self.os_name = self._get_os_name()
self.os_vers = float(self.os_info['VERSION_ID'])
self.os_type = self.os_info['TYPE']
self.memory = self._get_memory()
self.cpu_cores = self._get_cpu_cores()
self.mach_type = 'Unknown'
self.mach_tech = None
self.is_vm = False
self.is_container = False
self.is_metal = False
self._set_mach_type()
def _get_os_info(self):
orel_path = "/etc/os-release"
data = {}
data['TYPE'] = platform.system()
if data['TYPE'] == 'Linux':
if os.access(orel_path, os.R_OK):
with open(orel_path,'r') as orf:
for l in orf.readlines():
lsplit = l.strip().split('=')
if len(lsplit) > 1:
data[lsplit[0]] = lsplit[1].replace('"','').strip()
else:
d = platform.linux_distribution()
data['VERSION_ID'] = d[1]
data['ID'] = d[0].lower().strip()
else:
print("ERROR! Unsupported OS: {}".format(data['TYPE']))
sys.exit(1)
return data
def _get_os_name(self):
name = ''
try:
name = self.os_info['ID']
for n in self._os_name_map:
if n == name:
name = self._os_name_map[n]
return name.lower()
except KeyError:
return None
def _get_memory(self):
with open("/proc/meminfo","r") as meminfo:
mi=meminfo.readline().strip().split()
return int(mi[1]) * 1024
def _get_cpu_cores(self):
count = 0
with open("/proc/cpuinfo","r") as cpuinfo:
for l in cpuinfo.readlines():
if l.startswith('processor'):
count+=1
return count
def _set_mach_type(self):
try:
output=subprocess.check_output("dmesg | grep 'Detected virtualization'", shell=True)
except:
# Not a VM
if os.path.isfile("/run/.containerenv"):
self.is_container = True
self.mach_type = 'container'
self.mach_tech = 'podman'
return
with open("/proc/1/cgroup","r") as cg:
for l in cg.readlines():
lsplit = l.strip().split(':')
if lsplit[2] == '/':
self.is_metal = True
self.mach_type = 'metal'
self.mach_tech = 'hardware'
return
self.is_container = True
self.mach_type = 'container'
self.mach_tech = lsplit[2].split('/')[1]
else:
outstr=output.decode()
match = re.search("Detected virtualization (.*)\.", outstr)
if match.group(1) == 'oracle':
vm_id = 'VirtualBox'
else:
vm_id = match.group(1).strip()
self.is_vm = True
self.mach_tech = vm_id.lower()
self.mach_type = 'vm'
def _address_in_network(self, ip, net):
ip = ip.split('%')[0]
ipaddr = int(''.join([ '%02x' % int(x) for x in ip.split('.') ]), 16)
netstr, bits = net.split('/')
netaddr = int(''.join([ '%02x' % int(x) for x in netstr.split('.') ]), 16)
mask = (0xffffffff << (32 - int(bits))) & 0xffffffff
return (ipaddr & mask) == (netaddr & mask)
def _run_cmd(self,path,args=[],split=True,suppress_error_out=False):
p = path
if isinstance(path,(list,tuple,set)):
in_path = False
for p in path:
if os.access(p, os.X_OK):
in_path = True
break
if not in_path:
if not suppress_error_out:
print("Error! Can't find executable here: {}".format(path))
return (-1,"Error! Can't find executable here: {}".format(path))
else:
if not os.access(p,os.X_OK):
if not suppress_error_out:
print("Error! Can't find executable here: {}".format(p))
return (-1,"Error! Can't find executable here: {}".format(p))
pr = subprocess.Popen([p] + args,shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
pr_out,stderr = pr.communicate()
if pr.returncode > 0:
if not suppress_error_out:
print("Error! running {} {}:".format(p,' '.join(args)))
print(stderr)
out = stderr.decode('ascii','ignore')
else:
out = pr_out.decode('ascii','ignore')
if split:
out = out.splitlines()
return (pr.returncode,out)
def iptables_running(self):
iptables_save_path = ['/sbin/iptables-save','/usr/sbin/iptables-save']
if os.getuid() != 0:
print("WARNING: You are not root, won't be able to check iptables")
return False
ipts_p = self._run_cmd(iptables_save_path)
if ipts_p[0] == 0:
for l in ipts_p[1]:
if l[0] in [':','#']:
continue
return True
return False
def mount_for_path(self,path):
cpath = os.path.realpath(path)
while not os.path.ismount(cpath):
cpath = os.path.dirname(cpath)
return cpath
def path_exists(self,path):
return os.path.exists(path)
def get_dev_for_mount(self,path):
dev = None
with open("/proc/mounts","r") as mounts:
mount_list=mounts.readlines()
for m in mount_list:
device,mount_point,fs,opts,d,co = m.strip().split()
if device[0] == '/' and path == mount_point:
dev = device
break
return dev
def get_dev_for_path(self,path):
m = self.mount_for_path(path)
dev = self.get_dev_for_mount(m)
if not dev:
print("Error, couldn't find device for path: {}".format(path))
return dev
def get_storage_size_for_path(self,path):
mount = self.mount_for_path(path)
return self.get_storage_size(mount)
def get_storage_size(self,mount):
df_paths = ['/usr/bin/df','/bin/df']
size = -1
s = self._run_cmd(df_paths,['-k',mount])
if s[0] == 0:
size = int(s[1][1].strip().split()[1]) * 1024
return size
def get_storage_details(self,dev):
mdstat = '/proc/mdstat'
lvdisplay_path = '/sbin/lvdisplay'
pvdisplay_path = '/sbin/pvdisplay'
dmraid_path = '/sbin/dmraid'
lspci_path = [ '/sbin/lspci', '/usr/bin/lspci' ]
data = {'type': 'standard'}
while True:
if os.access(mdstat,os.R_OK):
with open(mdstat,"r") as mdf:
found = False
for l in mdf.readlines():
line = l.strip()
if not ':' in line:
continue
lsplit = line.split(':')
det = lsplit[1].split()
d = lsplit[0].strip()
if dev == '/dev/{}'.format(d):
data['raid'] = {}
data['raid']['method'] = 'md'
data['raid']['level'] = det[1]
data['raid']['devices'] = det[2:]
data['type'] = 'raid'
found = True
break
if found:
break
lvd_p = self._run_cmd(lvdisplay_path,['-c',dev],suppress_error_out=True)
if lvd_p[0] == 0:
data['lvm'] = {}
data['type'] = 'lvm'
for l in lvd_p[1]:
lsplit = l.strip().split(':')
data['lvm']['vg'] = lsplit[1]
pvd_p = self._run_cmd(pvdisplay_path,['-c'],suppress_error_out=True)
if pvd_p[0] == 0:
data['lvm']['pv'] = []
for pl in pvd_p[1]:
plsplit = pl.strip().split(':')
if plsplit[1] == data['lvm']['vg']:
data['lvm']['pv'].append(plsplit[0])
for p in data['lvm']['pv']:
td = self.get_storage_details(p)
if 'raid' in td:
if not 'raid' in data['lvm']:
data['lvm']['raid'] ={}
data['lvm']['raid']['raided_pvs'] = []
data['lvm']['raid']['methods'] = []
data['lvm']['raid']['devices'] = []
data['lvm']['raid']['levels'] = []
data['lvm']['raid']['raided_pvs'].append(p)
data['lvm']['raid']['devices'] += td['raid']['devices']
data['lvm']['raid']['methods'].append(td['raid']['method'])
data['lvm']['raid']['levels'].append(td['raid']['level'])
if 'card' in td['raid']:
data['lvm']['raid']['card'] = td['raid']['card']
break
else:
if 'WARNING: Running as a non-root user' in ''.join(lvd_p[1]):
print("WARNING: Unable to check if storage is on LVM, run as root to check")
dmr_p = self._run_cmd(dmraid_path,['-r'],suppress_error_out=True)
if dmr_p[0] == 0:
data['raid'] = {}
data['raid']['method'] = 'fakeraid'
data['raid']['level'] = ''
data['raid']['devices'] =[]
data['type'] = 'raid'
for l in dmr_p[1]:
lsplit = l.strip().split(':')
det = lsplit[1].strip().split(',')
if '/dev/mapper/{}'.format(det[1].strip().strip('"')) == dev:
data['raid']['devices'].append(lsplit[0])
data['raid']['level'] = det['2']
break
else:
if dmr_p[1]:
if "Can't find executable" not in dmr_p[1]:
print("ERROR running 'dmraid' to check for fakeraid:")
print(dmr_p[1])
lspci_p = self._run_cmd(lspci_path,['-v'],suppress_error_out=True)
if lspci_p[0]:
for l in lspci_p[1]:
if 'raid' in l.lower():
data['raid'] = {}
data['raid']['method'] = 'guess_hwraid'
data['raid']['level'] = 'Unknown'
data['raid']['devices'] = []
data['raid']['card'] = l.strip()
data['type'] = 'raid'
break
break
break
return data
def running_procs(self):
ps_paths = ['/usr/bin/ps','/bin/ps']
procs = []
ps_p = self._run_cmd(ps_paths,['-eo','pid,cmd'])
if ps_p[0] == 0:
for line in ps_p[1][1:]:
lsplit = line.strip().split()
pid = lsplit[0]
cmd = ' '.join(lsplit[1:])
procs.append({'pid': pid, 'cmd': cmd})
return procs
def rpc_ports(self):
data = {}
rpcinfo_path = '/usr/sbin/rpcinfo'
rpc_p = self._run_cmd(rpcinfo_path,['-p'],suppress_error_out=True)
if rpc_p[0] == 0:
for line in rpc_p[1][1:]:
lsplit = line.strip().split()
proto,port,service = lsplit[2:5]
try:
data[proto][port] = service
except KeyError:
data[proto] = {}
data[proto][port] = service
else:
if not 'No such file or directory' in ''.join(rpc_p[1]):
print("Error running 'rpcinfo -p' to check additional rpc ports")
print(rpc_p[1])
return data
def listening_ports(self):
data = {}
ss = False
ss_to_ns_col = [0,2,3,4,5,1,6]
if self.os_type == "Linux":
netstat_path = '/bin/netstat'
ss_path = ['/bin/ss', '/usr/bin/ss']
protos = ['tcp','tcp6','udp', 'udp6']
ns_p = (-1,'')
if self.path_exists(netstat_path):
ns_p = self._run_cmd(netstat_path,['-ntulp'])
if ns_p[0] == -1:
ns_p = self._run_cmd(ss_path,['-ntulp'])
ss = True
if ns_p[0] == -1:
print("Error, can't find netstat OR ss")
return False
if ns_p[0] == 0:
rpc_loaded = False
rpc_data = {}
for line in ns_p[1]:
lsplit = line.strip().split()
if len(lsplit) > 1:
#Format ss's output to match netstat's
if ss:
#Make sure we have 7 columns, missing one means no pid/proc name
if len(lsplit) < 7:
lsplit.append('-')
else:
#Skip header row
if lsplit[6] == 'Peer':
continue
#Format pid/proc like netstat's
ss_p = lsplit[6].replace('users:((','').replace('))','').replace('"','').split(',')
lsplit[6] = '{}/{}'.format(ss_p[1].split('=')[1],ss_p[0])
#Reorder the columns
lsplit = [ lsplit[i] for i in ss_to_ns_col ]
#Replace * with 0.0.0.0, and strip out square brackets
lsplit[3] = lsplit[3].replace('*','0.0.0.0').replace('[','').replace(']','')
#Set proto correctly if ipv6 address
if lsplit[3].count(':') > 1:
lsplit[0]+='6'
proto = lsplit[0]
if proto in protos:
v6 = False
if proto.endswith('6'):
v6 = True
proto = proto.rstrip('6')
local = False
lface = lsplit[3].split(':')
iface = ':'.join(lface[:-1])
port = lface[-1]
if lsplit[5] in [ 'LISTEN', 'UNCONN']:
prog_pid = ' '.join(lsplit[6:]).split('/')
else:
prog_pid = ' '.join(lsplit[5:]).split('/')
if prog_pid[0] == '-':
prog = '-'
pid = '-'
if not rpc_loaded:
rpc_data = self.rpc_ports()
rpc_loaded = True
try:
prog = 'rpc:{}'.format(rpc_data[proto][port])
pid = '0'
except KeyError:
pass
else:
pid,prog = prog_pid
if iface == '::1':
local = True
elif iface == '::':
local = False
elif ':' in iface:
if iface.startswith('fe80:'):
local = True
else:
local = False
else:
local = self._address_in_network(iface,'127.0.0.1/8')
d = {
'iface': iface,
'local': local,
'port': port,
'prog': prog,
'pid': pid,
'v6': v6,
}
try:
data[proto].append(d)
except KeyError:
data[proto] = []
data[proto].append(d)
return data
class RuleValidator:
def __init__(self,rules,node):
self.must_rules_map = {
'firewalls_allowed': self.eval_fw_allowed,
'machine_resources_required': self.eval_mach_resources_required,
'machine_type_allowed': self.eval_mach_type_allowed,
'os_allowed': self.eval_os_allowed,
'ports_allowed': self.eval_ports_allowed,
'procs_required': self.eval_procs_req,
'administration': self.eval_administration
}
self.should_rules_map = {
'machine_resources_recommended': self.eval_mach_resources_recommended,
'access': self.eval_access,
'availability': self.eval_availability,
'software': self.eval_software
}
self.must_results = {}
self.should_results = {}
for k in rules["must"]:
if k not in self.must_rules_map:
raise ValueError("Unknown rule: {}".format(k))
for k in rules["should"]:
if k not in self.should_rules_map:
raise ValueError("Unknown rule: {}".format(k))
self.must_rules = rules["must"]
self.should_rules = rules["should"]
self.node = node
def _compare(self,lefthand,op,righthand):
if op in ['>','>=','<','<=','=']:
lh = float(lefthand)
rh = float(righthand)
if op == '>':
return lh > rh
elif op == '>=':
return lh >= rh
elif op == '<':
return lh < rh
elif op == '<=':
return lh <= rh
elif op == '=':
return lh == rh
elif op in [ '/', 'in' ]:
if isinstance(righthand,(list,tuple,set)):
rh = righthand
elif ',' in righthand:
rh = righthand.split(',')
else:
rh = str(righthand)
return rh in str(lefthand)
elif op == 'ranges':
ranges = righthand.split(',')
nir = False
lh = float(lefthand)
for r in ranges:
rl,rr = r.split('-')
if lh >= float(rl) and lh <= float(rr):
return True
return False
elif op == 'in':
return lefthand in righthand
else:
raise ValueError("Unknown operator: {}".format(op))
def _comp_exec(self,cur,comp_str,rule_str=''):
comp_args = comp_str.split()
if len(comp_args) < 2:
raise ValueError("Invalid Rule: {}: {}".format(rule_str,comp_str))
return self._compare(cur,comp_args[0],comp_args[1])
def _attest(self,prompt):
while True:
# DEBUG
#response = 'y'
response = input(prompt + " (y/n): ")
if response.lower() not in ('y', 'n'):
print("Please choose y (yes) or n (no)")
else:
break
if response.lower() in ('y'):
return True
else:
return False
def validate(self):
rk = list(self.must_rules.keys())
rk.sort()
for r in rk:
self.must_results[r] = self.must_rules_map[r](self.must_rules[r])
rk = list(self.should_rules.keys())
rk.sort()
for r in rk:
self.should_results[r] = self.should_rules_map[r](self.should_rules[r])
def print_report(self):
print(bcolors.BOLD + '== Results for "A Steward MUST" ==' + bcolors.ENDC)
rk = list(self.must_results.keys())
rk.sort()
indent_level=' '
rule_data = ['result', 'details', 'action_needed']
for r in rk:
print("Rule: {}:".format(r))
for rdk in rule_data:
rd = self.must_results[r][rdk]
if isinstance(rd,(list,tuple,set)):
print("{}{}:".format(indent_level,rdk))
for i in rd:
print('{il}{il}{d}'.format(il=indent_level,d=i))
else:
print("{}{}: {}".format(indent_level,rdk,rd))
print(bcolors.BOLD + '== Results for "A Steward SHOULD" ==' + bcolors.ENDC)
rk = list(self.should_results.keys())
rk.sort()
indent_level=' '
rule_data = ['result', 'details', 'action_needed']
for r in rk:
print("Rule: {}:".format(r))
for rdk in rule_data:
rd = self.should_results[r][rdk]
if isinstance(rd,(list,tuple,set)):
print("{}{}:".format(indent_level,rdk))
for i in rd:
print('{il}{il}{d}'.format(il=indent_level,d=i))
else:
print("{}{}: {}".format(indent_level,rdk,rd))
def eval_administration(self,criteria):
res = {
'result': 'UNKNOWN',
'action_needed': [],
'details': [],
'comment': 'None'
}
if 'personel' in criteria["rules"]:
if self._attest("At least one qualifed adminstrator is assigned to administer the node, and at least one other person has adequate access and training to administer the box in an emergency."):
res['result'] = bcolors.OKGREEN + "PASSED" + bcolors.ENDC
res['details'].append("Appropriate administrative personnel is assigned")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Assign qualified administrators")
res['details'].append("Inadequate administrative personnel assigned")
if 'other_access' in criteria['rules']:
if self._attest("Persons who are not designated administrators of the validator node have access to the system."):
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Modify access rules to only allow access by designated administrators")
res['details'].append("Access control rules are insufficiently restrictive")
else:
res['details'].append("Access control rules are appropriate")
if 'monitoring' in criteria['rules']:
if self._attest("Monitoring is in place that provides notification if the OS crashes, if the sovrin daemon abends, or if abnormal spikes in resource usage occur."):
res['details'].append("Node and service monitoring is in place")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Implement monitoring of the node and the Sovrin service")
res['details'].append("Monitoring is not implemented on the node or Sovrin service")
return res
def eval_access(self,criteria):
res = {
'result': 'UNKNOWN',
'action_needed': [],
'details': [],
'comment': 'None'
}
if 'locked_door' in criteria["rules"]:
if self._attest("Hardware is in a locked datacenter with at least one layer of keycard access."):
res['result'] = bcolors.OKGREEN + "PASSED" + bcolors.ENDC
res['details'].append("Physical security provided")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Move node to a secure datacenter")
res['details'].append("Inadequate physical security")
if 'isolated_network' in criteria['rules']:
if self._attest("The node is logically isolated from steward internal systems and networks."):
res['details'].append("Node is isolated from Steward internal networks")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Move node to an exterior-facing network")
res['details'].append("Node has logical access to internal resources that it should not have access to")
if 'two_factor' in criteria['rules']:
if self._attest("Two-factor authentication is required for all access to node."):
res['details'].append("Secure authenticaton policies applied")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Implement two-factor authentication policy")
res['details'].append("Authentication incorrectly does not require two factors.")
return res
def eval_availability(self,criteria):
res = {
'result': 'UNKNOWN',
'action_needed': [],
'details': [],
'comment': 'None'
}
if 'ups' in criteria["rules"]:
if self._attest("The system will remain functional through blackouts or brownouts up to 60 minutes duration."):
res['result'] = bcolors.OKGREEN + "PASSED" + bcolors.ENDC
res['details'].append("System power is resilient")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Provide robust power for the node")
res['details'].append("System is not able to remain functional through modest power supply interruptions")
if 'broadband' in criteria['rules']:
if self._attest("Internet connectivity is via high-reliabity, high-speed connection(s)."):
res['details'].append("Appropriate network connectivity is provided")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Provide enterprise-quality connection to the internet")
res['details'].append("Inadequate connection to the internet is provided")
if 'dedicated_nics' in criteria['rules']:
if self._attest("The node has two NICs, one each for validator traffic and for client traffic. "):
res['details'].append("Requested NICs provided")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Provide independent NICs for validator vs. client traffic")
res['details'].append("A single NIC, not two dedicated NICs, is provided.")
if 'backups' in criteria['rules']:
if self._attest("A snapshot or backup of the system is maintained, with the ability to restore in one hour."):
res['details'].append("Snapshot or backup of the system is maintained")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Implement snapshots or system backups")
res['details'].append("No system backup is maintained")
return res
def eval_software(self,criteria):
res = {
'result': 'UNKNOWN',
'action_needed': [],
'details': [],
'comment': 'None'
}
if 'patching' in criteria["rules"]:
if self._attest("Policies and practices provide for application of new security patches within 1 week of release."):
res['result'] = bcolors.OKGREEN + "PASSED" + bcolors.ENDC
res['details'].append("Patching occurs in timely fashion")
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'].append("Develop policies and practices for rapid deployment of patches")
res['details'].append("Software patches are not applied within a week")
return res
def eval_fw_allowed(self,criteria):
res = {
'result': 'UNKNOWN',
'action_needed': 'None',
'details': '',
'comment': 'None'
}
# default to failed
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['action_needed'] = "Protect your validator with a firewall"
res['details'] = "No iptables nor attested firewall"
if 'iptables' in criteria['firewalls']:
if self.node.iptables_running():
res['result'] = bcolors.OKGREEN + "PASSED" + bcolors.ENDC
res['action_needed'] = "Verify that iptables rules are blocking appropriately"
res['details'] = "Local firewall (iptables) detected"
if 'attested' in criteria['firewalls']:
if self._attest("Do you run an external firewall that protects the validator?"):
res['result'] = bcolors.OKGREEN + "PASSED" + bcolors.ENDC
res['action_needed'] = "Verify that your firewall rules are blocking appropriately"
if "No iptables" in res['details']:
res['details'] = "Administrator indicates that an external firewall is in use"
else:
res['details'] = res['details'] + ". Administrator also indicates that an external firewall is in use"
try:
res['comment'] = criteria['comment']
except KeyError:
pass
return res
def eval_mach_resources_required(self,criteria):
res = {
'result': 'UNKNOWN',
'action_needed': [],
'details': [],
'comment': 'None'
}
er = True
for r in criteria['resources']:
if r == "memory":
memory = self.node.memory
if not self._comp_exec(
self.node.memory,
criteria['resources'][r],
'mach_resources_required.memory'
):
if er:
er = False
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['details'].append("{}: Not enough memory, only found: {}".format(r,memory))
res['action_needed'].append("Change memory to be: {}".format(criteria['resources'][r]))
else:
res['details'].append("{}: Found enough memory: {}".format(r,memory))
elif r == "disk":
try:
d_mount = self.node.mount_for_path(
criteria['resources'][r]['path']
)
d_dev = self.node.get_dev_for_mount(d_mount)
d_size = self.node.get_storage_size(d_mount)
path_exists = self.node.path_exists(criteria['resources'][r]['path'])
if path_exists:
res['details'].append(
"{}: Found path: {} exists".format(
r,
criteria['resources'][r]['path']
)
)
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['details'].append(
"{}: Path: {} NOT found".format(
r,
criteria['resources'][r]['path']
)
)
res['action_needed'].append(
"{}: Create path: {}".format(
r,
criteria['resources'][r]['path']
)
)
if not self._comp_exec(
d_size,
criteria['resources'][r]['size'],
'mach_resources_required.disk.size'
):
if er:
er = False
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['details'].append(
"{}: Not enough storage space for path: {} mounted at: {} from device: {}. Only found: {} bytes".format(
r,
criteria['resources'][r]['path'],
d_mount,
d_dev,
d_size
)
)
res['action_needed'].append("Change storage space for path: {} mounted at: {} from device: {} to be {}".format(
criteria['resources'][r]['path'],
d_mount,
d_dev,
criteria['resources'][r]['size']
)
)
else:
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['details'].append("{}: Found enough storage space for path: {}, {} bytes".format(
r,
criteria['resources'][r]['path'],
d_size
)
)
except KeyError:
raise ValueError(
"Invalid Rule: {}: {}".format(
'machine_resources_required.resources.disk.path',
criteria['resources'][r]['path']
)
)
if 'raid' in criteria['resources'][r]:
d_dev = self.node.get_dev_for_path(
criteria['resources'][r]['path']
)
st_details = self.node.get_storage_details(d_dev)
raided = 'raid' in st_details
if criteria['resources'][r]['raid'] != raided:
if self._attest("This system utilizes an external RAID system for storage."):
res['details'].append("{}: An external RAID is in use".format(r))
else:
if er:
er = False
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['details'].append(
"{}: Storage space for path: {}, doesn't meet raid requirements, raided is: {}".format(
r,
criteria['resources'][r]['path'],
raided
)
)
res['action_needed'].append("Modify the storage for path: {} to be raided: {}".format(
criteria['resources'][r]['path'],
criteria['resources'][r]['raid']
)
)
else:
res['details'].append("{}: A RAID is used for storage".format(r))
elif r == 'hardware':
if self._attest("This node is on server-class hardware that is less than 4 years old?"):
res['details'].append("{}: Node is on current, server-class hardware".format(r))
else:
er = False
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['details'].append("{}: Node is not on current, server-class hardware".format(r))
res['action_needed'].append("Upgrade to current, server-class hardware")
elif r == 'single_machine':
if self._attest("This node is a single machine, not a cluster"):
res['details'].append("{}: Node is a single machine".format(r))
else:
er = False
res['result'] = bcolors.FAIL + "FAILED" + bcolors.ENDC
res['details'].append("{}: Node ia clustered. Should be a single machine".format(r))
res['action_needed'].append("Stand up a single-server replacement")
else:
raise ValueError(
"Invalid Rule: mach_resources_required.{}: {}".format(
r,
criteria['resources'][r]
)
)