def test_entity_io(self): # entity = user filename = "tests/tmp/test_entity_save" usr = self.usr x.to_file(filename, usr) usr2 = x.load_usr(filename) self.assertEqual(usr.ID, usr2.ID) keys1 = usr.key_pair keys2 = usr2.key_pair self.assertEqual(keys1, keys2) for k in usr.database.keys(): self.assertEqual(usr.database[k], usr2.database[k]) for k in usr2.database.keys(): self.assertEqual(usr.database[k], usr2.database[k]) # entity = service src = self.src x.to_file(filename, src) src2 = x.load_src(filename) self.assertEqual(src.ID, src2.ID) keys1 = src.key_pair keys2 = src2.key_pair self.assertEqual(keys1, keys2) for k in src.database.keys(): self.assertEqual(src.database[k], src2.database[k]) for k in src2.database.keys(): self.assertEqual(src.database[k], src2.database[k]) # entity = inspector insp = self.insp x.to_file(filename, insp) insp2 = x.load_insp(filename) self.assertEqual(insp.ID, insp2.ID) self.assertEqual(insp.scope, insp2.scope) self.assertEqual(insp.vko_pair, insp2.vko_pair) self.assertEqual(insp.sign_pair, insp2.sign_pair) for k in insp.database.keys(): self.assertEqual(insp.database[k], insp2.database[k]) for k in insp2.database.keys(): self.assertEqual(insp.database[k], insp2.database[k]) # entity = auth center auth = self.auth x.to_file(filename, auth) auth2 = x.load_auth(filename) self.assertEqual(auth.total_ids, auth2.total_ids) self.assertEqual(auth.users, auth2.users) self.assertEqual(auth.id_scope, auth2.id_scope) self.assertEqual(auth.services, auth2.services) self.assertEqual(auth.inspectors_sig, auth2.inspectors_sig) self.assertEqual(auth.inspectors_vko, auth2.inspectors_vko)
def load_src(args): if args.src is None: src_path = default.DEFAULT_SERVICE msg = noexplicit("Service") printv(msg, args.verbose) else: src_path = args.src try: src = x.load_src(src_path) src.AUTH = x.load_auth(auth_path(args)) return src, src_path except x.SrcLoadError: print("Error when trying to load Src file") print("Service path: ", src_path) raise except x.AuthLoadError: print("Error when trying to load Auth file") print("Auth path: ", auth_path(args)) raise
def load_insp(args): if args.insp is None: insp_path = default.DEFAULT_INSPECTOR msg = noexplicit("Inspector") printv(msg, args.verbose) else: insp_path = args.insp try: insp = x.load_insp(insp_path) insp.AUTH = x.load_auth(auth_path(args)) return insp, insp_path except x.InspLoadError: print("Error when trying to load Inspector file") print("Inspector path: ", insp_path) raise except x.AuthLoadError: print("Error when trying to load Auth file") print("Auth path: ", auth_path(args)) raise
def load_usr(args): if args.usr is None: usr_path = default.DEFAULT_USER msg = noexplicit("User") printv(msg, args.verbose) else: usr_path = args.usr try: usr = x.load_usr(usr_path) usr.AUTH = x.load_auth(auth_path(args)) return usr, usr_path except x.UsrLoadError: print("Error when trying to load User file") print("User path: ", usr_path) raise except x.AuthLoadError: print("Error when trying to load Auth file") print("Auth path: ", auth_path(args)) raise
if args.database: print() print() print("INSPECTOR DATABASE") print() for k in insp.database.keys(): print(k) print() print(insp.database[k]) print() print() except: print("Wrong inspector file was given.") elif args.auth is not None: try: auth = x.load_auth(args.auth) print() print() print("AuthCenter DATABASE") print("USERS:") for k in auth.users.keys(): print(k) print(auth.users[k]) print() print("SERVICES:") for k in auth.services.keys(): print(k) print(auth.services[k]) print() print("INSPECTORS SIGNATURE KEYS:") for k in auth.inspectors_sig.keys():
def parse_CLI(arguments): parser = argparse.ArgumentParser(description='Registration script') parser.add_argument('--service', '-s', action="store_true", dest="src") parser.add_argument('--user', '-u', action="store_true", dest="usr") parser.add_argument('--inspector', '-i', action="store_true", dest="insp") parser.add_argument('--scope', '-S', action="store", dest="scope") parser.add_argument('--auth', '-a', action="store", dest="AUTH") parser.add_argument('--key' '-k', action="store", dest="key") parser.add_argument('--output', '-o', action="store", dest="output") parser.add_argument('--verbose', '-v', action="store_true") args = parser.parse_args(arguments) check = check_args(args) if check: return args else: return None if __name__ == "__main__": args = parse_CLI(sys.argv[1:]) if args is not None: AUTH = x.load_auth(auth_path(args)) if args.src: create_service(args, AUTH) elif args.usr: create_user(args, AUTH) else: create_inspector(args, AUTH) x.to_file(auth_path(args),AUTH)