def validate_rule_constraints(yml_files=None, cache_path=None): orm_docs = parser.parse_rules(yml_files) print("Validating additional ORM constraints...") if not validate_constraints_domain_default(orm_docs["rules"]): return False if not validate_constraints_rule_collision(orm_docs["rules"], cache_path=cache_path): return False return True
def main(): # pylint:disable=too-many-branches,too-many-statements """ Main function! """ arg_parser = argparse.ArgumentParser() arg_parser.add_argument("-o", "--output-dir", type=str, help="Output file directory.") arg_parser.add_argument( "-r", "--orm-rules-path", type=str, default="namespaces/**/*.yml", help="Glob specifying ORM rule files.", ) arg_parser.add_argument( "-G", "--globals-path", type=str, default=None, help="Path to ORM global settings file.", ) arg_parser.add_argument( "--cache-path", type=str, default=None, help="Path to collision check cache file. Caching " "is disabled if no path is specified.", ) arg_parser.add_argument( "-c", "--check", help="Only validate rules, no config generated.", action="store_true", default=False, ) arg_parser.add_argument( "-C", "--no-check", help="Do not validate rules.", action="store_true", default=False, ) arg_parser.add_argument( "-t", "--test-target", help="Run tests against the provided URL.", metavar="URL", type=str, default=None, ) arg_parser.add_argument( "-k", "--test-target-insecure", help="Do not verify test target certificates.", action="store_true", default=False, ) args = arg_parser.parse_args() yml_files = parser.list_rules_files(args.orm_rules_path) if not yml_files: print("ERROR: Found no files using glob: {}".format( args.orm_rules_path)) exit(1) if args.globals_path: if not validator.validate_globals_file(args.globals_path): print("ERROR: Global settings not valid") exit(1) if args.check and args.no_check: print("ERROR: --check together with --no-check does not make sense.") exit(1) if not args.no_check: print("Validating ORM rule files...") if not validator.validate_rule_files(yml_files=yml_files, cache_path=args.cache_path): print("ERROR: Not valid") exit(1) if args.check: print("All checks passed.") exit(0) parsed_globals = None defaults = None if args.globals_path: parsed_globals = parser.parse_globals(args.globals_path) defaults = parsed_globals.get("defaults", None) parsed_rules = parser.parse_rules(yml_files=yml_files, defaults=defaults) domain_rules = parsed_rules["rules"] tests = parsed_rules["tests"] if args.test_target: print("Run tests against %s" % args.test_target) run_tests( tests=tests, target=args.test_target, verify_certs=(not args.test_target_insecure), ) exit(0) print("Rendering Varnish config...") render_varnish = RenderVarnish(rule_docs=domain_rules, globals_doc=parsed_globals) print("Rendering HAProxy config...") render_haproxy = RenderHAProxy(rule_docs=domain_rules, globals_doc=parsed_globals) if not args.output_dir: render_varnish.print_config() render_haproxy.print_config() else: render_varnish.write_config_to_file(args.output_dir) render_haproxy.write_config_to_file(args.output_dir) print("Config written to " + args.output_dir)
def test_parse_rules(self): # Test merging rule files exp_docs = { 'rules': { 'example.com': [{ '_orm_source_file': self.merge_files[0], '_rule_id': 'foo', 'description': 'foo', 'domains': ['example.com', 'site.example.com'] }, { '_orm_source_file': self.merge_files[0], '_rule_id': 'bar', 'description': 'bar', 'domains': ['example.com'] }, { '_orm_source_file': self.merge_files[1], '_rule_id': 'foo_3', 'description': 'foo', 'domains': ['example.com'] }, { '_orm_source_file': self.merge_files[1], '_rule_id': 'baz', 'description': 'baz', 'domains': ['example.com'] }], 'site.example.com': [{ '_orm_source_file': self.merge_files[0], '_rule_id': 'foo_2', 'description': 'foo', 'domains': ['example.com', 'site.example.com'] }] }, 'tests': [] } docs = parser.parse_rules(self.merge_files) self.assertEqual(docs, exp_docs) # Test https_redirection default defaults = {'https_redirection': True} exp_docs = { 'rules': { 'example.com': [ { '_orm_source_file': self.defaults_file, '_rule_id': 'foo', 'description': 'foo', 'domains': ['example.com'], 'actions': { 'https_redirection': True } }, { '_orm_source_file': self.defaults_file, '_rule_id': 'bar', 'description': 'bar', 'domains': ['example.com'], 'actions': { 'https_redirection': False } }, { '_orm_source_file': self.defaults_file, '_rule_id': 'lizard', 'description': 'lizard', 'domains': ['example.com'], 'actions': { 'https_redirection': True } }, { '_orm_source_file': self.defaults_file, '_rule_id': 'snek', 'description': 'snek', 'domains': ['example.com'], 'actions': { # There should be no https_redirection here 'redirect': { 'type': 'temporary', 'url': 'internet' } } } ] }, 'tests': [] } docs = parser.parse_rules([self.defaults_file], defaults=defaults) self.assertEqual(docs, exp_docs)