2015-11-25 21:00:33 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
REQUIRED_FIELDS = ['publicKey', 'password', 'contact']
|
2015-11-25 22:57:26 +00:00
|
|
|
RECOMMENDED_FIELDS = ['gpg']
|
2015-11-25 21:00:33 +00:00
|
|
|
|
2015-11-25 21:17:57 +00:00
|
|
|
RED = '\x1b[01;31m'
|
|
|
|
GREEN = '\x1b[01;32m'
|
2015-11-25 22:57:26 +00:00
|
|
|
YELLOW = '\x1b[01;33m'
|
2015-11-25 21:17:57 +00:00
|
|
|
END = '\x1b[0m'
|
2015-11-25 21:00:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate(path):
|
|
|
|
print("Validating %s" % path)
|
|
|
|
try:
|
|
|
|
creds = open(path).read()
|
|
|
|
peers = json.loads("{%s}" % creds)
|
|
|
|
hosts = peers.keys()
|
2015-11-25 22:57:26 +00:00
|
|
|
warning = False
|
2015-11-25 21:00:33 +00:00
|
|
|
for host in hosts:
|
|
|
|
for field in REQUIRED_FIELDS:
|
|
|
|
if not field in peers[host]:
|
2015-11-25 22:57:26 +00:00
|
|
|
print(" %sHost %s is missing the required field %s%s" % (RED, host,
|
|
|
|
field, END))
|
2015-11-25 21:00:33 +00:00
|
|
|
return False
|
2015-11-25 22:57:26 +00:00
|
|
|
for field in RECOMMENDED_FIELDS:
|
|
|
|
if not field in peers[host]:
|
|
|
|
warning = True
|
|
|
|
print(" %sHost %s is missing the recommended field %s%s" % (YELLOW, host,
|
|
|
|
field, END))
|
|
|
|
if warning:
|
|
|
|
print(" %sSuccess, but missing recommended fields%s" % (YELLOW, END))
|
|
|
|
else:
|
|
|
|
print(" %sSuccess!%s" % (YELLOW, END))
|
2015-11-25 21:00:33 +00:00
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
print(" %sInvalid JSON!%s" % (RED, END))
|
|
|
|
return False
|
|
|
|
|
2015-11-26 03:09:59 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
success = True
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
success = validate(sys.argv[1])
|
|
|
|
else:
|
|
|
|
for directory, subdirs, files in os.walk('.'):
|
|
|
|
if len(files) > 0:
|
|
|
|
for f in files:
|
|
|
|
if f.endswith('.k'):
|
|
|
|
result = validate("%s/%s" % (directory, f))
|
|
|
|
if not result:
|
|
|
|
success = False
|
|
|
|
if not success:
|
|
|
|
sys.exit(1)
|