Ejemplo n.º 1
0
 def check_fields(self, data, **kwargs):
     if data['new_name'] is None and data['new_description']\
             is None and data['new_end_date'] is None and data['new_status'] is None:
         raise ma.ValidationError('No parameter specified')
     if data['new_status'] is not None and data[
             'new_status'] not in PERMISSIBLE_STATUS:
         raise ma.ValidationError('Incorrect new status')
Ejemplo n.º 2
0
    def unwrap_request(self, data, many):
        if 'data' not in data:
            raise ma.ValidationError('Object must include `data` key.')

        data = data['data']
        data_is_collection = ma.utils.is_collection(data)
        if many:
            if not data_is_collection:
                raise ma.ValidationError([{
                    'detail': '`data` object must be a collection.',
                    'source': {
                        'pointer': '/data'
                    }
                }])

            return [self.unwrap_item(each) for each in data]

        if data_is_collection:
            raise ma.ValidationError([{
                'detail': '`data` object must be an object, not a collection.',
                'source': {
                    'pointer': '/data'
                }
            }])

        if data is None:
            # When updating relationships we need to specially handle the primary data being null.
            # http://jsonapi.org/format/1.1/#crud-updating-to-one-relationships
            raise NullPrimaryData()

        return self.unwrap_item(data)
Ejemplo n.º 3
0
            def _validate_ros_type(self, data):
                # extracting members from ROS type (we do not check internal type, we will just try conversion - python style)
                if hasattr(self._valid_ros_type, '__slots__'):  # ROS style
                    slots = []
                    ancestors = inspect.getmro(self._valid_ros_type)
                    for a in ancestors:
                        slots += a.__slots__ if hasattr(a, '__slots__') else []
                    # Remove special ROS slots
                    if '_connection_header' in slots:
                        slots.remove('_connection_header')
                    rtkeys = slots
                elif hasattr(self._valid_ros_type, '__dict__'):  # generic python object (for tests)
                    rtkeys = [k for k in vars(self._valid_ros_type).keys() if not k.startswith('_')]
                else:  # this is a basic python type (including dict)
                    rtkeys = ['data']  # this is intended to support decorating nested fields (????)
                    # OR except ???

                # here data is always a dict
                for dk, dv in data.iteritems():
                    if dk not in rtkeys:
                        raise marshmallow.ValidationError(
                            'loaded data has unexpected field {dk}'.format(**locals()))
                for k in rtkeys:
                    if k not in data:
                        raise marshmallow.ValidationError('loaded data missing field {k}'.format(**locals()))
Ejemplo n.º 4
0
    def _validate(self, value):
        """

        Parameters
        ----------
        value :
            

        Returns
        -------

        """
        try:
            path = os.path.dirname(value)
        except Exception as e:
            raise mm.ValidationError("%s cannot be os.path.dirname-ed" % value)
        try:
            with tempfile.TemporaryFile(mode='w', dir=path) as tfile:
                tfile.write('0')
        except Exception as e:
            if isinstance(e, OSError):
                if e.errno == errno.ENOENT:
                    raise mm.ValidationError(
                        "%s is not in a directory that exists" % value)
                elif e.errno == errno.EACCES:
                    raise mm.ValidationError(
                        "%s does not appear you can write to path" % value)
                else:
                    raise mm.ValidationError("Unknown OSError: {}".format(
                        e.message))
            else:
                raise mm.ValidationError("Unknown Exception: {}".format(
                    e.message))
Ejemplo n.º 5
0
 def _deserialize(self, value, attr, data, **kwargs):
     if value is None:
         return None
     if isinstance(value, DBRef):
         if self._document_cls.collection.name != value.collection:
             raise ma.ValidationError(
                 _("DBRef must be on collection `{collection}`.").format(
                     self._document_cls.collection.name))
         value = value.id
     elif isinstance(value, Reference):
         if value.document_cls != self.document_cls:
             raise ma.ValidationError(
                 _("`{document}` reference expected.").format(
                     document=self.document_cls.__name__))
         if not isinstance(value, self.reference_cls):
             value = self.reference_cls(value.document_cls, value.pk)
         return value
     elif isinstance(value, self.document_cls):
         if not value.is_created:
             raise ma.ValidationError(
                 _("Cannot reference a document that has not been created yet."
                   ))
         value = value.pk
     elif isinstance(value, self._document_implementation_cls):
         raise ma.ValidationError(
             _("`{document}` reference expected.").format(
                 document=self.document_cls.__name__))
     value = super()._deserialize(value, attr, data, **kwargs)
     return self.reference_cls(self.document_cls, value)
Ejemplo n.º 6
0
 def _deserialize(self, value, attr, data, **kwargs):
     if value is None:
         return None
     if isinstance(value, Reference):
         if not isinstance(value, self.reference_cls):
             value = self.reference_cls(value.document_cls, value.pk)
         return value
     if isinstance(value, self._document_implementation_cls):
         if not value.is_created:
             raise ma.ValidationError(
                 _("Cannot reference a document that has not been created yet."
                   ))
         return self.reference_cls(value.__class__, value.pk)
     if isinstance(value, dict):
         if value.keys() != {'cls', 'id'}:
             raise ma.ValidationError(
                 _("Generic reference must have `id` and `cls` fields."))
         try:
             _id = ObjectId(value['id'])
         except ValueError:
             raise ma.ValidationError(_("Invalid `id` field."))
         document_cls = self._document_cls(value['cls'])
         return self.reference_cls(document_cls, _id)
     raise ma.ValidationError(
         _("Invalid value for generic reference field."))
Ejemplo n.º 7
0
 def _validate(self, value):
     if not os.path.isdir(value):
         try:
             os.makedirs(value)
             if self.mode is not None:
                 os.chmod(value, self.mode)
         except OSError as e:
             if e.errno == errno.EEXIST:
                 pass
             else:
                 raise mm.ValidationError(
                     "{} is not a directory and you cannot create it".
                     format(value))
     if self.mode is not None:
         try:
             assert ((os.stat(value).st_mode & 0o777) == self.mode)
         except AssertionError:
             raise mm.ValidationError(
                 "{} does not have the mode  ({}) that was specified ".
                 format(value, self.mode))
         except os.error:
             raise mm.ValidationError(
                 "cannot get os.stat of {}".format(value))
     # use outputfile to test that a file in this location is a valid path
     validate_outpath(value)
Ejemplo n.º 8
0
    def validate_zones(self, data):
        if not data.get('zone') and not data.get('zones'):
            raise ma.ValidationError('Must specify one or more GCE zones',
                                     ['zone', 'zones'])

        if data.get('zone') and data.get('zones'):
            raise ma.ValidationError(
                'Must specify only one of the parameters `zone`, `zones`',
                ['zone', 'zones'])

        if len(data.get('zones', [])) == 1:
            raise ma.ValidationError(
                'Please use the `zone` argument for zonal instance groups (`zones` is for regional groups)',
                ['zones'])

        region = data.get('region')
        if not region:
            # will be handled by the `required` validator
            return

        zones = [_f for _f in [data.get('zone')] + data.get('zones', []) if _f]

        bad_zones = [
            zone for zone in zones if not zone.startswith(data.get('region'))
        ]
        if bad_zones:
            raise ma.ValidationError(
                'One or more zones are incompatible with the specified region `{}`: `{}`'
                .format(region, bad_zones))
Ejemplo n.º 9
0
def password(value):
    if len(value) < 8 or len(value) > 32:
        raise ms.ValidationError('Password should be 8-32 characters')
    pattern = r'^[0-9a-zA-Z!"#\$%&\'()*+,-\./:;<=>?@[\\\]^_`{|}~]{8,32}$'
    if not re.match(pattern, value):
        raise ms.ValidationError(
            'Password should not contain special characters')
    return value
Ejemplo n.º 10
0
 def validate_result(self, data: dict, many: bool, partial: bool) -> None:
     assert not many and not partial, "Use of `many` and `partial` with schema unsupported."
     if data["done"] and data.get("result") is None:  # pragma: no cover
         raise marshmallow.ValidationError(
             "If `done` then `result` must be set.", "result")
     if not data["done"] and data.get("result"):  # pragma: no cover
         raise marshmallow.ValidationError(
             "If not `done` then `result` must be empty.", "result")
Ejemplo n.º 11
0
    def validate_stats_or_error_field_is_present(self, data, **kwargs):
        if "stats" not in data and "error" not in data:
            raise marshmallow.ValidationError(
                "Either stats or error field is required")

        if "stats" in data and "error" in data:
            raise marshmallow.ValidationError(
                "stats and error fields can not be used at the same time")
Ejemplo n.º 12
0
 def unwrap_item(self, item):
     if 'type' not in item:
         raise ma.ValidationError('`data` object must include `type` key.')
     if item['type'] != self.opts.type_:
         raise IncorrectTypeError(actual=item['type'], expected=self.opts.type_)
     if 'attributes' not in item:
         raise ma.ValidationError('`data` object must include `attributes` key.')
     return item['attributes']
Ejemplo n.º 13
0
    def validate_hardware_info_fields(self, data, **kwargs):
        if "machine_info" not in data and "cluster_info" not in data:
            raise marshmallow.ValidationError(
                "Either machine_info or cluster_info field is required")

        if "machine_info" in data and "cluster_info" in data:
            raise marshmallow.ValidationError(
                "machine_info and cluster_info fields can not be used at the same time"
            )
Ejemplo n.º 14
0
def validate_input_affine(data):
    has_affine_dict = bool(data.get('affine_dict'))
    has_affine_list = bool(data.get('affine_list'))
    if has_affine_list and has_affine_dict:
        err_msg = 'Provide either affine_list or affine_dict, not both'
        raise mm.ValidationError(err_msg)
    elif not has_affine_list and not has_affine_dict:
        err_msg = 'Provide either affine_list or affine_dict'
        raise mm.ValidationError(err_msg)
Ejemplo n.º 15
0
    def translate(self, item):
        if isinstance(item, dict):
            return {
                key: self.translate(value)
                for (key, value) in six.iteritems(item)
            }
        elif isinstance(item, list):
            return list(map(self.translate, item))

        if not isinstance(item, six.string_types):
            raise ma.ValidationError(
                'Cannot translate object {} of type {}'.format(
                    item, type(item)))

        hits = list(self.OOZIE_MACRO_RE.finditer(item))
        if not hits:
            return item

        split = self.OOZIE_MACRO_RE.split(item)
        # Some sanity checking to make sure that the split gave us what we expected.
        # Given that we know there's at least one hit, we expect that there should be
        # at least one more item in splits than in hits.  If the text field both
        # begins and ends with a hit, then there can potentially be 2 more items
        # in the split array.
        if not (2 + len(hits) >= len(split) > len(hits)):
            raise ma.ValidationError(
                'Bad split on input text `{}`: hits == `{}` / split == `{}`'.
                format(item, hits, split))

        if not (split[-1] == '' or len(split) == 1 + len(hits)):
            raise ma.ValidationError(
                'Bad split on input text `{}`: hits == `{}` / split == `{}`'.
                format(item, hits, split))

        result = split[0]
        for (idx, hit) in enumerate(hits):
            # Must discard the first 2 and final 1 characters from the hit, because
            # these represent the '${' and '}'.  Note that we could use capture groups
            # in the regex but then these will contaminate the output of the split()
            # function (see: https://docs.python.org/2/library/re.html#re.split)
            assert hit.group(0).startswith('${') and hit.group(0).endswith('}')
            oozie_macro = hit.group(0)[2:-1]
            translation = self.macros.get(oozie_macro)

            if translation is None:
                raise ma.ValidationError(
                    'Could not translate {}: unrecognized macro `{}`'.format(
                        item, oozie_macro))

            # if we've made it here, either translation was successful or
            # unsuccessful translations are permitted.  In the latter case,
            # we reinsert the oozie macro.
            result += translation or hit.group(0)
            result += split[1 + idx]

        return result
Ejemplo n.º 16
0
def validate_input_affine(data):
    has_slice_transform_dict = bool(data.get('slice_transform_dict'))
    has_slice_transform_list = bool(data.get('slice_transform_list'))
    if has_slice_transform_list and has_slice_transform_dict:
        err_msg = ('Provide either slice_transform_list or '
                   'slice_transform_dict, not both')
        raise mm.ValidationError(err_msg)
    elif not has_slice_transform_list and not has_slice_transform_dict:
        err_msg = 'Provide either slice_transform_list or slice_transform_dict'
        raise mm.ValidationError(err_msg)
Ejemplo n.º 17
0
    def check_dims(self, data):
        lres = np.array([len(i) for i in data['residuals']])
        lpos = np.array([len(i) for i in data['positions']])
        if (not np.all(lres == 3)) | (not np.all(lpos == 3)):
            raise mm.ValidationError("all list entries should be length 3")
        if lres.size != lpos.size:
            raise mm.ValidationError(
                "residuals and positions should be same length")

        data['residuals'] = np.array(data['residuals'])
        data['positions'] = np.array(data['positions'])
Ejemplo n.º 18
0
    def __call__(self, value):
        message = self._format_error(value, self.allowed_file_extensions)
        if not isinstance(value, FileStorage):
            raise ma.ValidationError(message)

        if len(self.allowed_file_extensions) > 0:
            filename = value.filename.lower()
            if not filename.endswith(self.allowed_file_extensions):
                raise ma.ValidationError(message)

        return value
Ejemplo n.º 19
0
 def put_id_validator(self, data, original):
     """Ensure that an 'id' field is present when parsing many data"""
     if m.utils.is_collection(original):
         if data.get('id'):
             try:
                 data.update(self.__update__(data)._data)
             except exc.APIException:
                 raise m.ValidationError(db_helpers.CORRESPONDING, 'id')
         else:
             raise m.ValidationError(
                 m.fields.Field.default_error_messages['required'], 'id')
Ejemplo n.º 20
0
 def check_migration_have_correct_source_and_dict(self, data):
     clouds = data['clouds']
     migrations = data.get('migrations', {})
     for migration_name, migration in migrations.items():
         if migration.source not in clouds:
             raise marshmallow.ValidationError(
                 'Migration "{0}" source "{1}" should be defined '
                 'in clouds'.format(migration_name, migration.source))
         if migration.destination not in clouds:
             raise marshmallow.ValidationError(
                 'Migration "{0}" destination "{1}" should be defined '
                 'in clouds'.format(migration_name, migration.destination))
Ejemplo n.º 21
0
 def _deserialize(self, value, attr, data, **kwargs):
     if not isinstance(value, dict):
         raise ma.ValidationError(
             _("Invalid value for generic reference field."))
     if value.keys() != {'cls', 'id'}:
         raise ma.ValidationError(
             _("Generic reference must have `id` and `cls` fields."))
     try:
         _id = bson.ObjectId(value['id'])
     except ValueError:
         raise ma.ValidationError(_("Invalid `id` field."))
     return {'cls': value['cls'], 'id': _id}
Ejemplo n.º 22
0
    def validate_date(self, data, **kwargs):
        if not data['start'] <= data['end']:
            raise marshmallow.ValidationError(
                'start should not be less than end', 'start')

        if data['start'] < datetime.today().date():
            raise marshmallow.ValidationError('start can not be in the past',
                                              'start')

        if not Reservation.is_free(data['start'], data['end']):
            raise marshmallow.ValidationError(
                'there is already an existing reservation', 'start')
Ejemplo n.º 23
0
def int_range(value, min_value=None, max_value=None):
    try:
        x = int(value)
    except ValueError:
        raise ms.ValidationError('Must be integer')
    if min_value is not None and x < min_value:
        raise ms.ValidationError(
            'Must not be smaller than {}'.format(min_value))
    if max_value is not None and x > max_value:
        raise ms.ValidationError(
            'Must not be bigger than {}'.format(max_value))
    return x
Ejemplo n.º 24
0
    def encrypt(self, data):
        if not self.context.get('reencrypt_secrets'):
            logging.debug('Not re-encrypting secret %s because '
                    'reencrypt_secrets is not specified in the serializer '
                    'context')
            return data

        encrypt_method = self.context.get('encrypt_method', _encrypt_with_kms)

        if encrypt_method == _encrypt_with_kms:
            kms_key = _get_kms_key(data) or _get_kms_key(self.context)

            # If key exists, encrypt the plaintext with the key. If key doesn't exist, ciphertext is expected to be in the data. In that case, let ciphertext pass through without doing anything.
            if not kms_key and 'ciphertext' not in data:
                raise ma.ValidationError(('Cannot serialize secret {}: KMS key info not defined '
                    'and ciphertext not present!').format(data.get('name')))

            if 'ciphertext' not in data and 'plaintext' not in data:
                raise ma.ValidationError(('Cannot serialize secret {}: plaintext and ciphertext '
                    'are both missing!').format(data.get('name')))

            if kms_key:
                if 'plaintext' in data:
                    # Encrypt the plaintext if it is present, even if the ciphertext is
                    # already present. This could come up for instance if we implement
                    # some kind of key rotation, where perhaps we load the ciphertext
                    # with one key and then dump it with another key.
                    logging.info('Encrypting plaintext for secret %s', data['name'])

                    ciphertext = _encrypt_with_kms(kms_key.get('project'), kms_key.get('location'), kms_key.get('keyring'), kms_key.get('key'), data['plaintext'])
                elif 'output_filename' in data and os.path.isfile(data['output_filename']):
                    # To give consistent behavior to the plaintext-reencryption that
                    # is done in the above block, we re-encrypt the plaintext file
                    # if it is present, even if the ciphertext is already present.

                    logging.info('Encrypting file %s for secret %s', data['output_filename'], data['name'])

                    plaintext = None
                    with open(data['output_filename']) as _in:
                        plaintext = _in.read()

                    ciphertext = _encrypt_with_kms(kms_key.get('project'), kms_key.get('location'), kms_key.get('keyring'), kms_key.get('key'), data['plaintext'])
            else:
                ciphertext = data.get('ciphertext')

        else:
            ciphertext = encrypt_method(data)

        # return the data with ciphertext added
        copy = { key: value for (key, value) in six.iteritems(data) if key != 'plaintext' }
        copy['ciphertext'] = ciphertext
        return copy
Ejemplo n.º 25
0
    def check_fields(self, data):
        defaults = data.get('defaults')
        if defaults:
            disallowed_list = ['roles_path', 'library', 'filter_plugins']
            if any(disallowed in defaults for disallowed in disallowed_list):
                raise marshmallow.ValidationError(
                    'Disallowed user provided config option', defaults)

        privilege_escalation = data.get('privilege_escalation')
        if privilege_escalation:
            raise marshmallow.ValidationError(
                'Disallowed user provided config option',
                'privilege_escalation')
Ejemplo n.º 26
0
def must_be_time_dict(data):
    """checks that data is days of the week"""
    try:
        if not sorted([key.lower() for key in data.keys()]) == sorted([
                'monday', 'tuesday', 'wednesday', 'thursday', 'friday',
                'saturday', 'sunday'
        ]):
            raise marshmallow.ValidationError(
                'Must be an object with the days of the week as keys.')
    except Exception as error:
        print(error)
        raise marshmallow.ValidationError(
            'Must be an object with the days of the week as keys.')
Ejemplo n.º 27
0
    def _validate(self, value):
        if not os.path.isdir(value):
            raise mm.ValidationError("%s is not a directory")

        if sys.platform == "win32":
            try:
                x = list(os.scandir(value))
            except PermissionError:
                raise mm.ValidationError("%s is not a readable directory" %
                                         value)
        else:
            if not os.access(value, os.R_OK):
                raise mm.ValidationError("%s is not a readable directory" %
                                         value)
Ejemplo n.º 28
0
def validate_input_path(value):
    if not os.path.isfile(value):
        raise mm.ValidationError("%s is not a file" % value)
    else:
        if sys.platform == "win32":
            try:
                with open(value) as f:
                    s = f.read()
            except IOError as x:
                if x.errno == errno.EACCES:
                    raise mm.ValidationError("%s is not readable" % value)
        else:
            if not os.access(value, os.R_OK):
                raise mm.ValidationError("%s is not readable" % value)
Ejemplo n.º 29
0
 def check_override_rules(self, data):
     overrides = data['overrides']
     for object_type, ovr in list(overrides.items()):
         try:
             model_cls = model.get_model(object_type)
         except ImportError:
             raise marshmallow.ValidationError(
                 'Invalid object type "{0}"'.format(object_type))
         schema = model_cls.schema_class()
         for attribute, _ in list(ovr.items()):
             if attribute not in schema.fields:
                 raise marshmallow.ValidationError(
                     'Invalid override rule: "{0}" schema don\'t have '
                     '"{1}" attribute.'.format(object_type, attribute))
Ejemplo n.º 30
0
 def __call__(self, value):
     try:
         shape = value.shape
     except AttributeError:
         raise mm.ValidationError("{} is not a valid array, does not have "
                                  "attribute `shape`.".format(value))
     if len(shape) != len(self.shape):
         raise mm.ValidationError("Dimension mismatch: input shape {} does "
                                  "not match {}.".format(shape, self.shape))
     valid = all(
         [a == b for a, b in zip(shape, self.shape) if b is not None])
     if not valid:
         raise mm.ValidationError("Array shape {} does not match required "
                                  "shape {}.".format(shape, self.shape))
     return valid