def validate(self): # Validate policy itself cleaned = super(PolicyAPI, self).validate() # Validate policy parameters # pylint: disable=no-member policy_type_db = PolicyType.get_by_ref(cleaned.policy_type) if not policy_type_db: raise ValueError('Referenced policy_type "%s" doesnt exist' % (cleaned.policy_type)) parameters_schema = policy_type_db.parameters parameters = getattr(cleaned, 'parameters', {}) schema = util_schema.get_schema_for_resource_parameters( parameters_schema=parameters_schema) validator = util_schema.get_validator() cleaned_parameters = util_schema.validate(parameters, schema, validator, use_default=True, allow_default_none=True) cleaned.parameters = cleaned_parameters return cleaned
def _validate_config_values_against_schema(self): try: config_schema_db = ConfigSchema.get_by_pack(value=self.pack) except StackStormDBObjectNotFoundError: # Config schema is optional return # Note: We are doing optional validation so for now, we do allow additional properties instance = self.values or {} schema = config_schema_db.attributes schema = util_schema.get_schema_for_resource_parameters( parameters_schema=schema, allow_additional_properties=True) try: cleaned = util_schema.validate(instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True) except jsonschema.ValidationError as e: attribute = getattr(e, 'path', []) attribute = '.'.join(attribute) configs_path = os.path.join(cfg.CONF.system.base_path, 'configs/') config_path = os.path.join(configs_path, '%s.yaml' % (self.pack)) msg = ( 'Failed validating attribute "%s" in config for pack "%s" (%s): %s' % (attribute, self.pack, config_path, str(e))) raise jsonschema.ValidationError(msg) return cleaned
def _validate_config_values_against_schema(self): try: config_schema_db = ConfigSchema.get_by_pack(value=self.pack) except StackStormDBObjectNotFoundError: # Config schema is optional return # Note: We are doing optional validation so for now, we do allow additional properties instance = self.values or {} schema = config_schema_db.attributes schema = util_schema.get_schema_for_resource_parameters(parameters_schema=schema, allow_additional_properties=True) try: cleaned = util_schema.validate(instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True) except jsonschema.ValidationError as e: attribute = getattr(e, 'path', []) attribute = '.'.join(attribute) configs_path = os.path.join(cfg.CONF.system.base_path, 'configs/') config_path = os.path.join(configs_path, '%s.yaml' % (self.pack)) msg = ('Failed validating attribute "%s" in config for pack "%s" (%s): %s' % (attribute, self.pack, config_path, str(e))) raise jsonschema.ValidationError(msg) return cleaned
def validate_config_against_schema(config_schema, config_object, config_path, pack_name=None): """ Validate provided config dictionary against the provided config schema dictionary. """ pack_name = pack_name or 'unknown' schema = util_schema.get_schema_for_resource_parameters(parameters_schema=config_schema, allow_additional_properties=True) instance = config_object try: cleaned = util_schema.validate(instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True) except jsonschema.ValidationError as e: attribute = getattr(e, 'path', []) attribute = '.'.join(attribute) msg = ('Failed validating attribute "%s" in config for pack "%s" (%s): %s' % (attribute, pack_name, config_path, str(e))) raise jsonschema.ValidationError(msg) return cleaned
def validate_config_against_schema(config_schema, config_object, config_path, pack_name=None): """ Validate provided config dictionary against the provided config schema dictionary. """ pack_name = pack_name or 'unknown' schema = util_schema.get_schema_for_resource_parameters( parameters_schema=config_schema, allow_additional_properties=True) instance = config_object try: cleaned = util_schema.validate(instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True) except jsonschema.ValidationError as e: attribute = getattr(e, 'path', []) attribute = '.'.join(attribute) msg = ( 'Failed validating attribute "%s" in config for pack "%s" (%s): %s' % (attribute, pack_name, config_path, str(e))) raise jsonschema.ValidationError(msg) return cleaned
def validate_config_against_schema(config_schema, config_object, config_path, pack_name=None): """ Validate provided config dictionary against the provided config schema dictionary. """ # NOTE: Lazy improt to avoid performance overhead of importing this module when it's not used import jsonschema pack_name = pack_name or 'unknown' schema = util_schema.get_schema_for_resource_parameters(parameters_schema=config_schema, allow_additional_properties=True) instance = config_object try: cleaned = util_schema.validate(instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True) except jsonschema.ValidationError as e: attribute = getattr(e, 'path', []) if isinstance(attribute, (tuple, list, collections.Iterable)): attribute = [str(item) for item in attribute] attribute = '.'.join(attribute) else: attribute = str(attribute) msg = ('Failed validating attribute "%s" in config for pack "%s" (%s): %s' % (attribute, pack_name, config_path, six.text_type(e))) raise jsonschema.ValidationError(msg) return cleaned
def validate_config_against_schema(config_schema, config_object, config_path, pack_name=None): """ Validate provided config dictionary against the provided config schema dictionary. """ # NOTE: Lazy improt to avoid performance overhead of importing this module when it's not used import jsonschema pack_name = pack_name or 'unknown' schema = util_schema.get_schema_for_resource_parameters(parameters_schema=config_schema, allow_additional_properties=True) instance = config_object try: cleaned = util_schema.validate(instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True) except jsonschema.ValidationError as e: attribute = getattr(e, 'path', []) if isinstance(attribute, (tuple, list, collections.Iterable)): attribute = [str(item) for item in attribute] attribute = '.'.join(attribute) else: attribute = str(attribute) msg = ('Failed validating attribute "%s" in config for pack "%s" (%s): %s' % (attribute, pack_name, config_path, six.text_type(e))) raise jsonschema.ValidationError(msg) return cleaned
def validate_config_against_schema(config_schema, config_object, config_path, pack_name=None): """ Validate provided config dictionary against the provided config schema dictionary. """ # NOTE: Lazy improt to avoid performance overhead of importing this module when it's not used import jsonschema pack_name = pack_name or "unknown" schema = util_schema.get_schema_for_resource_parameters( parameters_schema=config_schema, allow_additional_properties=True) instance = config_object try: cleaned = util_schema.validate( instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True, ) for key in cleaned: if (jinja_utils.is_jinja_expression(value=cleaned.get(key)) and "decrypt_kv" in cleaned.get(key) and config_schema.get(key).get("secret")): raise ValueValidationException( 'Values specified as "secret: True" in config ' "schema are automatically decrypted by default. Use " 'of "decrypt_kv" jinja filter is not allowed for ' "such values. Please check the specified values in " "the config or the default values in the schema.") except jsonschema.ValidationError as e: attribute = getattr(e, "path", []) if isinstance(attribute, (tuple, list, Iterable)): attribute = [str(item) for item in attribute] attribute = ".".join(attribute) else: attribute = str(attribute) msg = 'Failed validating attribute "%s" in config for pack "%s" (%s): %s' % ( attribute, pack_name, config_path, six.text_type(e), ) raise jsonschema.ValidationError(msg) return cleaned
def validate(self): # Validate policy itself cleaned = super(PolicyAPI, self).validate() # Validate policy parameters policy_type_db = PolicyType.get_by_ref(cleaned.policy_type) if not policy_type_db: raise ValueError('Referenced policy_type "%s" doesnt exist' % (cleaned.policy_type)) parameters_schema = policy_type_db.parameters parameters = getattr(cleaned, 'parameters', {}) schema = util_schema.get_schema_for_resource_parameters( parameters_schema=parameters_schema) validator = util_schema.get_validator() cleaned_parameters = util_schema.validate(parameters, schema, validator, use_default=True, allow_default_none=True) cleaned.parameters = cleaned_parameters return cleaned
def _validate_config_values_against_schema(self): try: config_schema_db = ConfigSchema.get_by_pack(value=self.pack) except StackStormDBObjectNotFoundError: # Config schema is optional return # Note: We are doing optional validation so for now, we do allow additional properties instance = self.values or {} schema = config_schema_db.attributes schema = util_schema.get_schema_for_resource_parameters(parameters_schema=schema, allow_additional_properties=True) try: cleaned = util_schema.validate(instance=instance, schema=schema, cls=util_schema.CustomValidator, use_default=True, allow_default_none=True) except jsonschema.ValidationError as e: msg = 'Failed validating config for pack "%s": %s' % (self.pack, str(e)) raise jsonschema.ValidationError(msg) return cleaned