Пример #1
0
    def build_sqlalchemy_uri(
        self, data: Dict[str, Any], **kwargs: Any
    ) -> Dict[str, Any]:
        """
        Build SQLAlchemy URI from separate parameters.

        This is used for databases that support being configured by individual
        parameters (eg, username, password, host, etc.), instead of requiring
        the constructed SQLAlchemy URI to be passed.
        """
        parameters = data.pop("parameters", {})

        # TODO (betodealmeida): remove second expression after making sure
        # frontend is not passing engine inside parameters
        engine = data.pop("engine", None) or parameters.pop("engine", None)

        configuration_method = data.get("configuration_method")
        if configuration_method == ConfigurationMethod.DYNAMIC_FORM:
            if not engine:
                raise ValidationError(
                    [
                        _(
                            "An engine must be specified when passing "
                            "individual parameters to a database."
                        )
                    ]
                )
            engine_specs = get_engine_specs()
            if engine not in engine_specs:
                raise ValidationError(
                    [_('Engine "%(engine)s" is not a valid engine.', engine=engine,)]
                )
            engine_spec = engine_specs[engine]

            if not hasattr(engine_spec, "build_sqlalchemy_uri") or not hasattr(
                engine_spec, "parameters_schema"
            ):
                raise ValidationError(
                    [
                        _(
                            'Engine spec "InvalidEngine" does not support '
                            "being configured via individual parameters."
                        )
                    ]
                )

            # validate parameters
            parameters = engine_spec.parameters_schema.load(parameters)  # type: ignore

            serialized_encrypted_extra = data.get("encrypted_extra", "{}")
            try:
                encrypted_extra = json.loads(serialized_encrypted_extra)
            except json.decoder.JSONDecodeError:
                encrypted_extra = {}

            data["sqlalchemy_uri"] = engine_spec.build_sqlalchemy_uri(  # type: ignore
                parameters, encrypted_extra
            )

        return data
Пример #2
0
def sqlalchemy_uri_validator(value: str) -> str:
    """
    Validate if it's a valid SQLAlchemy URI and refuse SQLLite by default
    """
    try:
        make_url(value.strip())
    except (ArgumentError, AttributeError):
        raise ValidationError(
            [
                _(
                    "Invalid connection string, a valid string usually follows:"
                    "'DRIVER://*****:*****@DB-HOST/DATABASE-NAME'"
                    "<p>"
                    "Example:'postgresql://*****:*****@your-postgres-db/database'"
                    "</p>"
                )
            ]
        )
    if app.config["PREVENT_UNSAFE_DB_CONNECTIONS"] and value:
        if value.startswith("sqlite"):
            raise ValidationError(
                [
                    _(
                        "SQLite database cannot be used as a data source for "
                        "security reasons."
                    )
                ]
            )

    return value
Пример #3
0
 def check_publisher(self, data, **_):  # noqa
     if data['publisher_id']:
         if not current_user.is_admin:
             raise ValidationError('Shouldn\'t be filled', field_name='publisher_id')
     else:
         if current_user.is_admin:
             raise ValidationError('Required', field_name='publisher_id')
Пример #4
0
def sqlalchemy_uri_validator(value: str) -> str:
    """
    Validate if it's a valid SQLAlchemy URI and refuse SQLLite by default
    """
    try:
        make_url(value.strip())
    except (ArgumentError, AttributeError, ValueError):
        raise ValidationError(
            [
                _(
                    "Invalid connection string, a valid string usually follows: "
                    "driver://*****:*****@database-host/database-name"
                )
            ]
        )
    if current_app.config.get("PREVENT_UNSAFE_DB_CONNECTIONS", True) and value:
        if value.startswith("sqlite"):
            raise ValidationError(
                [
                    _(
                        "SQLite database cannot be used as a data source for "
                        "security reasons."
                    )
                ]
            )

    return value
Пример #5
0
def extra_validator(value: str) -> str:
    """
    Validate that extra is a valid JSON string, and that metadata_params
    keys are on the call signature for SQLAlchemy Metadata
    """
    if value:
        try:
            extra_ = json.loads(value)
        except json.JSONDecodeError as ex:
            raise ValidationError(
                [_("Field cannot be decoded by JSON. %(msg)s", msg=str(ex))]
            )
        else:
            metadata_signature = inspect.signature(MetaData)
            for key in extra_.get("metadata_params", {}):
                if key not in metadata_signature.parameters:
                    raise ValidationError(
                        [
                            _(
                                "The metadata_params in Extra field "
                                "is not configured correctly. The key "
                                "%(key)s is invalid.",
                                key=key,
                            )
                        ]
                    )
    return value
Пример #6
0
    def build_sqlalchemy_uri(self, data: Dict[str, Any],
                             **kwargs: Any) -> Dict[str, Any]:
        """
        Build SQLAlchemy URI from separate parameters.

        This is used for databases that support being configured by individual
        parameters (eg, username, password, host, etc.), instead of requiring
        the constructed SQLAlchemy URI to be passed.
        """
        parameters = data.pop("parameters", None)
        if parameters:
            if "engine" not in parameters:
                raise ValidationError([
                    _("An engine must be specified when passing "
                      "individual parameters to a database.")
                ])
            engine = parameters["engine"]

            engine_specs = get_engine_specs()
            if engine not in engine_specs:
                raise ValidationError([
                    _(
                        'Engine "%(engine)s" is not a valid engine.',
                        engine=engine,
                    )
                ])
            engine_spec = engine_specs[engine]
            if hasattr(engine_spec, "build_sqlalchemy_uri"):
                data[
                    "sqlalchemy_uri"] = engine_spec.build_sqlalchemy_uri(  # type: ignore
                        parameters)
        return data
Пример #7
0
    def __call__(self, value) -> dt:
        try:
            if type(value) is not dt:
                raise ValidationError(self._format_error(value))
        except TypeError as error:
            raise ValidationError(self._format_error(value)) from error

        return value
Пример #8
0
    def check_schema(self, data, **_):  # noqa
        if not any([v for v in data.values()]):
            raise ValidationError('All fields are empty')

        status = data['status']
        if status and not current_user.is_admin:
            if status not in [STATUS_CREATED, STATUS_MODERATION]:
                raise ValidationError(f'Status {status} not allowed', field_name='status')
Пример #9
0
def validate_json_metadata(value: Union[bytes, bytearray, str]) -> None:
    if not value:
        return
    try:
        value_obj = json.loads(value)
    except json.decoder.JSONDecodeError as ex:
        raise ValidationError("JSON not valid") from ex
    errors = DashboardJSONMetadataSchema().validate(value_obj, partial=False)
    if errors:
        raise ValidationError(errors)
Пример #10
0
def validate_json_metadata(value):
    if not value:
        return
    try:
        value_obj = json.loads(value)
    except json.decoder.JSONDecodeError:
        raise ValidationError("JSON not valid")
    errors = DashboardJSONMetadataSchema(strict=True).validate(value_obj,
                                                               partial=False)
    if errors:
        raise ValidationError(errors)
Пример #11
0
    def validate_fields(self, data, **kwargs):
        """Ensure that both indexes and quality band is present in attribute 'bands'.

        Seeks for quality_band in attribute 'bands' and set as `common_name`.

        Raises:
            ValidationError when a band inside indexes or quality_band is duplicated with attribute bands.
        """
        indexes = data['indexes']

        band_names = [b['name'] for b in data['bands']]

        for band_index in indexes:
            if band_index['name'] in band_names:
                raise ValidationError(
                    f'Duplicated band name in indices {band_index["name"]}')

        if data['composite_function'] != 'IDT' and data.get(
                'quality_band') is None:
            raise ValidationError(
                f'Quality band is required for {data["composite_function"]}.')

        if 'quality_band' in data and data.get('quality_band') is not None:
            if data['quality_band'] not in band_names:
                raise ValidationError(
                    f'Quality band "{data["quality_band"]}" not found in key "bands"'
                )

            band = next(
                filter(lambda band: band['name'] == data['quality_band'],
                       data['bands']))
            band['common_name'] = 'quality'

        if 'temporal_schema' in data:
            import json
            import pkgutil

            import bdc_catalog
            from jsonschema import draft7_format_checker, validate
            content = pkgutil.get_data(
                bdc_catalog.__name__,
                'jsonschemas/collection-temporal-composition-schema.json')
            schema = json.loads(content)
            try:
                schema['$id'] = schema['$id'].replace('#', '')
                validate(instance=data['temporal_schema'],
                         schema=schema,
                         format_checker=draft7_format_checker)
            except Exception as e:
                raise

        return data
Пример #12
0
    def __call__(self, value) -> Any:
        if not (type(value) is int or type(value) is float):
            raise ValidationError(f"value {value} isn't an int or float")

        if self.max_timestamp:
            if value > self.max_timestamp:
                raise ValidationError(
                    f"value {value} seems to be either not in seconds, or further than year 3000."
                )
        if self.min_timestmap:
            if value < self.min_timestmap:
                raise ValidationError(
                    f"value {value} seems to be too far in the past")
Пример #13
0
    def __call__(self, value: typing.Any) -> typing.Any:
        try:
            attr = getattr(value, self.attribute)
            if self.is_getter_method:
                attr = attr()
        except Exception as e:
            raise ValidationError(str(e)) from e

        try:
            return all(validator(attr) for validator in self.validate)
        except Exception as e:
            raise ValidationError(
                f"Invalid {self.attribute} ({value}): {str(e)}") from e
Пример #14
0
def get_engine_spec(engine: Optional[str]) -> Type[BaseEngineSpec]:
    if not engine:
        raise ValidationError([
            _("An engine must be specified when passing "
              "individual parameters to a database.")
        ])
    engine_specs = get_engine_specs()
    if engine not in engine_specs:
        raise ValidationError(
            [_(
                'Engine "%(engine)s" is not a valid engine.',
                engine=engine,
            )])
    return engine_specs[engine]
Пример #15
0
 def _deserialize(self, value, attr, data):
     """
     反序列化成為 Enum
     """
     if self._enum_cls is None:
         err_msg = 'Couldn\'t deserialize, not assign Enum type',
         raise ValidationError(err_msg, field_names=[attr])
     if isinstance(value, self._enum_cls):
         return value
     values = [item.value for item in self._enum_cls]
     if value in values:
         return self._enum_cls(value)
     else:
         err_msg = 'The value not belong to {}'.format(self._enum_cls)
         raise ValidationError(err_msg)
Пример #16
0
    def __call__(self, value: dict, **kwargs) -> Any:
        non_numeric = dict()
        if type(value) is not dict:
            raise ValidationError(f"fields input isn't a dict. got {value}")
        for key, val in value.items():
            # blank dicts are fine
            if type(val) is dict:
                if len(val) == 0:
                    return
                else:
                    ValidationError(
                        f"Don't know what to do with {value} for key {key}")

            if not (type(val) is float or type(val) is int):
                non_numeric[key] = val
Пример #17
0
        async def decorated(*args, **kwargs):
            request = None
            for arg in args:
                if hasattr(arg, "app") and hasattr(arg.app, "models"):
                    request = arg
                    break

            if request is None:
                raise InvalidRoute(func.__name__)

            modelObj = (getattr(request.app.models, model) if isinstance(
                model, str) else model)(many=many)
            payload = getter(getattr(request, from_)) if getter else getattr(
                request, from_)
            modelObj.load(payload, many=many)
            errors = modelObj.get_errors()
            if errors:
                raise ValidationError(errors)
            else:
                listargs = list(args)
                listargs.append(modelObj)
                args = tuple(listargs)

            result = await func(*args, **kwargs)
            return result
Пример #18
0
    def __call__(self, *values):
        resource_manager = self.resource_manager or current_restlib.ResourceManager(
            self.model_class)
        criteria = dict(zip(self.fields, values))

        if not resource_manager.exists(criteria):
            value = ', '.join(f'{n}={v}' for n, v in criteria.items())
            raise ValidationError(self.error.format(value=value))
Пример #19
0
    def validate_birth_date(self, value: date) -> None:
        """
        Валидация на то, что дата рождения не может быть датой из будущего.

        :param value: дата для валидации
        """
        if value > date.today():
            raise ValidationError("Birth date can not be in future")
Пример #20
0
 def __call__(self, value):
     if value.endswith("cm"):
         Range(min=150, max=193)(int(value[:-2]))
     elif value.endswith("in"):
         Range(min=59, max=76)(int(value[:-2]))
     else:
         raise ValidationError("height must be cm or in")
     return value
Пример #21
0
def validate_hash_digest(value):
    """
       Validate that the given value is in the format of a sha3/256 digest
       :param value: The value to be validated
       :raises: ValidationError
       """
    if not value:
        raise ValidationError('hash digest cannot be None')

    if len(value) < BaseConfig.SHA3_256_DIGEST_LENGTH:
        raise ValidationError('hash digest too short')

    if len(value) > BaseConfig.SHA3_256_DIGEST_LENGTH:
        raise ValidationError('hash digest too long')

    if calculate_entropy(value) < 1:
        raise ValidationError('hash digest not random enough')
Пример #22
0
    def validate_relatives_unique(self, value: list) -> None:
        """
        Валидация на уникальной id-шников родственников.

        :param value: список id-шников родственников
        """
        if len(value) != len(set(value)):
            raise ValidationError("Relatives must be unique")
Пример #23
0
    def pack_person(self, data):
        pp(super().validate(data)
           )  # maybe correct direct field validation call during serialization

        if not data['name']:
            raise ValidationError('Incorrect name', ['name'])
        else:
            return data
Пример #24
0
 def validate_unique_value(self, value):
     #base query
     qs = Inventory.query.filter(Inventory.value == value)
     #if pass id in context, exclude id from query
     if (self.context.get('id')):
         qs = qs.filter(Inventory.id != self.context['id'])
     if (qs.count()):
         raise ValidationError('The value is need unique')
 def validate_report_references(  # pylint: disable=unused-argument,no-self-use
     self, data: Dict[str, Any], **kwargs: Any
 ) -> None:
     if data["type"] == ReportScheduleType.REPORT:
         if "database" in data:
             raise ValidationError(
                 {"database": ["Database reference is not allowed on a report"]}
             )
Пример #26
0
def sqlalchemy_uri_validator(value: str) -> str:
    """
    Validate if it's a valid SQLAlchemy URI and refuse SQLLite by default
    """
    try:
        uri = make_url(value.strip())
    except (ArgumentError, AttributeError, ValueError):
        raise ValidationError([
            _("Invalid connection string, a valid string usually follows: "
              "driver://*****:*****@database-host/database-name")
        ])
    if current_app.config.get("PREVENT_UNSAFE_DB_CONNECTIONS", True):
        try:
            check_sqlalchemy_uri(uri)
        except SupersetSecurityException as ex:
            raise ValidationError([str(ex)])
    return value
Пример #27
0
 def check_confirm_password(self, data):
     if data.get("password") and data.get("confirm_password"):
         if data["password"] == data["confirm_password"]:
             return data
         else:
             raise ValidationError(
                 "password and confirm_password should be equal.",
                 field_names=["password", "confirm_password"])
Пример #28
0
    async def set_name(self, request, schema):
        if schema.__data__["name"] == "Exception":
            raise Exception("It can crash from a regular Exception")

        if schema.__data__["name"] == "MakeItCrash":
            raise ValidationError("Crash test")

        self.__data__["name"] = schema.__data__["name"]
        return self.__data__["name"]
Пример #29
0
 def _serialize(self, value, attr, obj):
     """
     Enum 序列化成 Enum 的值
     """
     if value is None:
         return None
     elif isinstance(value, enum.Enum):
         return None if value.name in ['Null', 'None'] else value.value
     raise ValidationError("The type is not Enum")
Пример #30
0
 def __call__(self, value: dict, **kwargs) -> Any:
     missing_keys = []
     keys = value.keys()
     for key in self.important_keys:
         if key not in keys:
             missing_keys.append(key)
     if len(missing_keys) > 0:
         raise ValidationError(
             f"Missing important keys {missing_keys} from {value}")