コード例 #1
0
    def loads(self, msg, encoding=None, raw=False):
        '''
        Run the correct loads serialization format

        :param encoding: Useful for Python 3 support. If the msgpack data
                         was encoded using "use_bin_type=True", this will
                         differentiate between the 'bytes' type and the
                         'str' type by decoding contents with 'str' type
                         to what the encoding was set as. Recommended
                         encoding is 'utf-8' when using Python 3.
                         If the msgpack data was not encoded using
                         "use_bin_type=True", it will try to decode
                         all 'bytes' and 'str' data (the distinction has
                         been lost in this case) to what the encoding is
                         set as. In this case, it will fail if any of
                         the contents cannot be converted.
        '''
        try:
            def ext_type_decoder(code, data):
                if code == 78:
                    data = salt.utils.stringutils.to_unicode(data)
                    return datetime.datetime.strptime(data, '%Y%m%dT%H:%M:%S.%f')
                return data

            gc.disable()  # performance optimization for msgpack
            if msgpack.version >= (0, 4, 0):
                # msgpack only supports 'encoding' starting in 0.4.0.
                # Due to this, if we don't need it, don't pass it at all so
                # that under Python 2 we can still work with older versions
                # of msgpack.
                try:
                    ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder, encoding=encoding)
                except UnicodeDecodeError:
                    # msg contains binary data
                    ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder)
            else:
                ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder)
            if six.PY3 and encoding is None and not raw:
                ret = salt.transport.frame.decode_embedded_strs(ret)
        except Exception as exc:
            log.critical(
                'Could not deserialize msgpack message. This often happens '
                'when trying to read a file not in binary mode. '
                'To see message payload, enable debug logging and retry. '
                'Exception: %s', exc
            )
            log.debug('Msgpack deserialization failure on message: %s', msg)
            gc.collect()
            raise six.raise_from(
                SaltDeserializationError(
                    'Could not deserialize msgpack message.'
                    ' See log for more info.'
                ),
                exc,
            )
        finally:
            gc.enable()
        return ret
コード例 #2
0
ファイル: test_auth.py プロジェクト: paususe/salt-saltstack
 def test_get_tok_with_broken_file_will_remove_bad_token(self):
     fake_get_token = MagicMock(side_effect=SaltDeserializationError("hi"))
     patch_opts = patch.dict(self.lauth.opts, {"eauth_tokens": "testfs"})
     patch_get_token = patch.dict(
         self.lauth.tokens, {"testfs.get_token": fake_get_token},
     )
     mock_rm_token = MagicMock()
     patch_rm_token = patch.object(self.lauth, "rm_token", mock_rm_token)
     with patch_opts, patch_get_token, patch_rm_token:
         expected_token = "fnord"
         self.lauth.get_tok(expected_token)
         mock_rm_token.assert_called_with(expected_token)
コード例 #3
0
 def test_get_tok_with_broken_file_will_remove_bad_token(self):
     fake_get_token = MagicMock(side_effect=SaltDeserializationError('hi'))
     patch_opts = patch.dict(self.lauth.opts, {'eauth_tokens': 'testfs'})
     patch_get_token = patch.dict(
         self.lauth.tokens,
         {'testfs.get_token': fake_get_token},
     )
     mock_rm_token = MagicMock()
     patch_rm_token = patch.object(self.lauth, 'rm_token', mock_rm_token)
     with patch_opts, patch_get_token, patch_rm_token:
         expected_token = 'fnord'
         self.lauth.get_tok(expected_token)
         mock_rm_token.assert_called_with(expected_token)
コード例 #4
0
ファイル: payload.py プロジェクト: steverweber/salt
    def loads(self, msg, encoding=None, raw=False):
        """
        Run the correct loads serialization format

        :param encoding: Useful for Python 3 support. If the msgpack data
                         was encoded using "use_bin_type=True", this will
                         differentiate between the 'bytes' type and the
                         'str' type by decoding contents with 'str' type
                         to what the encoding was set as. Recommended
                         encoding is 'utf-8' when using Python 3.
                         If the msgpack data was not encoded using
                         "use_bin_type=True", it will try to decode
                         all 'bytes' and 'str' data (the distinction has
                         been lost in this case) to what the encoding is
                         set as. In this case, it will fail if any of
                         the contents cannot be converted.
        """
        try:

            def ext_type_decoder(code, data):
                if code == 78:
                    data = salt.utils.stringutils.to_unicode(data)
                    return datetime.datetime.strptime(data,
                                                      "%Y%m%dT%H:%M:%S.%f")
                return data

            gc.disable()  # performance optimization for msgpack
            loads_kwargs = {"use_list": True, "ext_hook": ext_type_decoder}
            if salt.utils.msgpack.version >= (0, 4, 0):
                # msgpack only supports 'encoding' starting in 0.4.0.
                # Due to this, if we don't need it, don't pass it at all so
                # that under Python 2 we can still work with older versions
                # of msgpack.
                if salt.utils.msgpack.version >= (0, 5, 2):
                    if encoding is None:
                        loads_kwargs["raw"] = True
                    else:
                        loads_kwargs["raw"] = False
                else:
                    loads_kwargs["encoding"] = encoding
                try:
                    ret = salt.utils.msgpack.unpackb(msg, **loads_kwargs)
                except UnicodeDecodeError:
                    # msg contains binary data
                    loads_kwargs.pop("raw", None)
                    loads_kwargs.pop("encoding", None)
                    ret = salt.utils.msgpack.loads(msg, **loads_kwargs)
            else:
                ret = salt.utils.msgpack.loads(msg, **loads_kwargs)
            if encoding is None and not raw:
                ret = salt.transport.frame.decode_embedded_strs(ret)
        except Exception as exc:  # pylint: disable=broad-except
            log.critical(
                "Could not deserialize msgpack message. This often happens "
                "when trying to read a file not in binary mode. "
                "To see message payload, enable debug logging and retry. "
                "Exception: %s",
                exc,
            )
            log.debug("Msgpack deserialization failure on message: %s", msg)
            gc.collect()

            exc_msg = "Could not deserialize msgpack message. See log for more info."
            raise SaltDeserializationError(exc_msg) from exc
        finally:
            gc.enable()
        return ret