def validate(options): if not os.path.exists(options.config): raise ValueError("Invalid path for config %r" % options.config) options.dryrun = True format = options.config.rsplit('.', 1)[-1] with open(options.config) as fh: if format in ('yml', 'yaml'): data = yaml.safe_load(fh.read()) if format in ('json',): data = json.load(fh) errors = schema.validate(data) if not errors: null_config = Bag(dryrun=True, log_group=None, cache=None, assume_role="na") for p in data.get('policies', ()): try: Policy(p, null_config, Bag()) except Exception as e: log.error("Policy: %s is invalid: %s" % ( p.get('name', 'unknown'), e)) sys.exit(1) return log.info("Config valid") return log.error("Invalid configuration") for e in errors: log.error(" %s" % e) sys.exit(1)
def validate(options): load_resources() if len(options.configs) < 1: log.error('no config files specified') sys.exit(1) used_policy_names = set() schm = schema.generate() errors = [] for config_file in options.configs: config_file = os.path.expanduser(config_file) if not os.path.exists(config_file): raise ValueError("Invalid path for config %r" % config_file) options.dryrun = True fmt = config_file.rsplit('.', 1)[-1] with open(config_file) as fh: if fmt in ('yml', 'yaml'): data = yaml.safe_load(fh.read()) elif fmt in ('json', ): data = json.load(fh) else: log.error("The config file must end in .json, .yml or .yaml.") raise ValueError( "The config file must end in .json, .yml or .yaml.") errors += schema.validate(data, schm) conf_policy_names = { p.get('name', 'unknown') for p in data.get('policies', ()) } dupes = conf_policy_names.intersection(used_policy_names) if len(dupes) >= 1: errors.append( ValueError( "Only one policy with a given name allowed, duplicates: %s" % (", ".join(dupes)))) used_policy_names = used_policy_names.union(conf_policy_names) if not errors: null_config = Bag(dryrun=True, log_group=None, cache=None, assume_role="na") for p in data.get('policies', ()): try: policy = Policy(p, null_config, Bag()) policy.validate() except Exception as e: msg = "Policy: %s is invalid: %s" % (p.get( 'name', 'unknown'), e) errors.append(msg) if not errors: log.info("Configuration valid: {}".format(config_file)) continue log.error("Configuration invalid: {}".format(config_file)) for e in errors: log.error("%s" % e) if errors: sys.exit(1)
def validate(options): load_resources() if options.config is not None: # support the old -c option options.configs.append(options.config) if len(options.configs) < 1: # no configs to test # We don't have the parser object, so fake ArgumentParser.error eprint('Error: no config files specified') sys.exit(1) used_policy_names = set() schm = schema.generate() errors = [] for config_file in options.configs: config_file = os.path.expanduser(config_file) if not os.path.exists(config_file): raise ValueError("Invalid path for config %r" % config_file) options.dryrun = True format = config_file.rsplit('.', 1)[-1] with open(config_file) as fh: if format in ('yml', 'yaml'): data = yaml.safe_load(fh.read()) if format in ('json', ): data = json.load(fh) errors = schema.validate(data, schm) conf_policy_names = {p['name'] for p in data.get('policies', ())} dupes = conf_policy_names.intersection(used_policy_names) if len(dupes) >= 1: errors.append( ValueError( "Only one policy with a given name allowed, duplicates: %s" % (", ".join(dupes)))) used_policy_names = used_policy_names.union(conf_policy_names) if not errors: null_config = Bag(dryrun=True, log_group=None, cache=None, assume_role="na") for p in data.get('policies', ()): try: Policy(p, null_config, Bag()) except Exception as e: msg = "Policy: %s is invalid: %s" % (p.get( 'name', 'unknown'), e) errors.append(msg) if not errors: log.info("Configuration valid: {}".format(config_file)) continue log.error("Configuration invalid: {}".format(config_file)) for e in errors: log.error(" %s" % e) if errors: sys.exit(1)