Exemple #1
0
        def cached(*args):
            if not cache[0]:
                if region is not None:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **kwargs)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            if skip_self:
                allargs = deco_args + args[1:]
            else:
                allargs = deco_args + args
            cache_key = " ".join(map(util.text_type, allargs))
            cache_key = cache_key.encode("utf8")
            if region:
                key_length = cache_regions[region]['key_length']
            else:
                key_length = kwargs.pop('key_length', 250)
            if len(cache_key) + len(namespace) > int(key_length):
                cache_key = sha1(cache_key).hexdigest()

            def go():
                return func(*args)

            return cache[0].get_value(cache_key, createfunc=go)
Exemple #2
0
def _cache_decorator_invalidate(cache, key_length, args):
    """Invalidate a cache key based on function arguments."""

    cache_key = " ".join(map(str, args))
    if len(cache_key) > key_length:
        cache_key = sha1(cache_key).hexdigest()
    cache.remove_value(cache_key)
Exemple #3
0
def _cache_decorator_invalidate(cache, key_length, args):
    """Invalidate a cache key based on function arguments."""

    cache_key = u_(" ").join(map(u_, args))
    if len(cache_key) + len(cache.namespace_name) > key_length:
        cache_key = sha1(cache_key.encode("utf-8")).hexdigest()
    cache.remove_value(cache_key)
Exemple #4
0
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode('ascii')
     formated_key = (self.namespace + '_' + key).replace(' ', '\302\267')
     if len(formated_key) > MAX_KEY_LENGTH:
         formated_key = sha1(formated_key).hexdigest()
     return formated_key
Exemple #5
0
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode("ascii")
     formated_key = (self.namespace + "_" + key).replace(" ", "\302\267")
     if len(formated_key) > MAX_KEY_LENGTH:
         formated_key = sha1(formated_key).hexdigest()
     return formated_key
Exemple #6
0
def _cache_decorator_invalidate(cache, key_length, args):
    """Invalidate a cache key based on function arguments."""

    cache_key = u_(" ").join(map(u_, args))
    if len(cache_key) + len(cache.namespace_name) > key_length:
        cache_key = sha1(cache_key.encode("utf-8")).hexdigest()
    cache.remove_value(cache_key)
Exemple #7
0
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode('ascii')
     formated_key = (self.namespace + '_' + key).replace(' ', '\302\267')
     if len(formated_key) > MAX_KEY_LENGTH:
         formated_key = sha1(formated_key).hexdigest()
     return formated_key
Exemple #8
0
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode('ascii')
     if len(key) > (self.MAX_KEY_LENGTH - len(self.namespace) - len('beaker_cache:') - 1):
         if not PY2:
             key = key.encode('utf-8')
         key = sha1(key).hexdigest()
     return 'beaker_cache:%s:%s' % (self.namespace, key)
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode('ascii')
     if len(key) > (self.MAX_KEY_LENGTH - len(self.namespace) - 1):
         if not PY2:
             key = key.encode('utf-8')
         key = sha1(key).hexdigest()
     return '%s:%s' % (self.namespace, key)
Exemple #10
0
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode("ascii")
     if len(key) > (self.MAX_KEY_LENGTH - len(self.namespace) -
                    len("beaker_cache:") - 1):
         if not PY2:
             key = key.encode("utf-8")
         key = sha1(key).hexdigest()
     return "beaker_cache:%s:%s" % (self.namespace, key)
Exemple #11
0
 def _format_key(self, key):
     if not isinstance(key, str):
         key = key.decode("ascii")
     formated_key = (self.namespace + "_" + key).replace(" ", "\302\267")
     if len(formated_key) > MAX_KEY_LENGTH:
         if not PY2:
             formated_key = formated_key.encode("utf-8")
         formated_key = sha1(formated_key).hexdigest()
     return formated_key
Exemple #12
0
def _cache_decorator_invalidate(cache, key_length, args):
    """Invalidate a cache key based on function arguments."""

    try:
        cache_key = " ".join(map(str, args))
    except UnicodeEncodeError:
        cache_key = " ".join(map(unicode, args))
    if len(cache_key) + len(cache.namespace_name) > key_length:
        cache_key = sha1(cache_key).hexdigest()
    cache.remove_value(cache_key)
Exemple #13
0
def _cache_decorator_invalidate(cache, key_length, args):
    """Invalidate a cache key based on function arguments."""

    try:
        cache_key = " ".join(map(str, args))
    except UnicodeEncodeError:
        cache_key = " ".join(map(str, args))
    if len(cache_key) + len(cache.namespace_name) > key_length:
        cache_key = sha1(cache_key).hexdigest()
    cache.remove_value(cache_key)
Exemple #14
0
    def _set_password(self, password):
        """Hash password on the fly."""
        if isinstance(password, unicode):
            password_8bit = password.encode('UTF-8')
        else:
            password_8bit = password

        salt = sha1()
        salt.update(os.urandom(60))
        hash = sha1()
        hash.update(password_8bit + salt.hexdigest())
        hashed_password = salt.hexdigest() + hash.hexdigest()

        # Make sure the hased password is an UTF-8 object at the end of the
        # process because SQLAlchemy _wants_ a unicode object for Unicode
        # fields
        if not isinstance(hashed_password, unicode):
            hashed_password = hashed_password.decode('UTF-8')

        self.password = hashed_password
Exemple #15
0
    def _set_password(self, password):
        """Hash password on the fly."""
        if isinstance(password, unicode):
            password_8bit = password.encode('UTF-8')
        else:
            password_8bit = password

        salt = sha1()
        salt.update(os.urandom(60))
        hash = sha1()
        hash.update(password_8bit + salt.hexdigest())
        hashed_password = salt.hexdigest() + hash.hexdigest()

        # Make sure the hased password is an UTF-8 object at the end of the
        # process because SQLAlchemy _wants_ a unicode object for Unicode
        # fields
        if not isinstance(hashed_password, unicode):
            hashed_password = hashed_password.decode('UTF-8')

        self.password = hashed_password
Exemple #16
0
        def cached(*args, **kwargs):
            if not cache[0]:
                if region is not None:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args, **kwargs)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **options)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            cache_key_kwargs = []
            if kwargs:
                # kwargs provided, merge them in positional args
                # to avoid having different cache keys.
                args, kwargs = bindfuncargs(signature, args, kwargs)
                cache_key_kwargs = [
                    u_(':').join((u_(key), u_(value)))
                    for key, value in kwargs.items()
                ]

            cache_key_args = args
            if skip_self:
                cache_key_args = args[1:]

            cache_key = u_(" ").join(
                map(u_, chain(deco_args, cache_key_args, cache_key_kwargs)))

            if region:
                cachereg = cache_regions[region]
                key_length = cachereg.get('key_length',
                                          util.DEFAULT_CACHE_KEY_LENGTH)
            else:
                key_length = options.pop('key_length',
                                         util.DEFAULT_CACHE_KEY_LENGTH)

            # TODO: This is probably a bug as length is checked before converting to UTF8
            # which will cause cache_key to grow in size.
            if len(cache_key) + len(namespace) > int(key_length):
                cache_key = sha1(cache_key.encode('utf-8')).hexdigest()

            def go():
                return func(*args, **kwargs)

            # save org function name
            go.__name__ = '_cached_%s' % (func.__name__, )

            return cache[0].get_value(cache_key, createfunc=go)
Exemple #17
0
    def validate_password(self, password):
        """
        Check the password against existing credentials.

        :param password: the password that was provided by the user to
            try and authenticate. This is the clear text version that we will
            need to match against the hashed one in the database.
        :type password: unicode object.
        :return: Whether the password is valid.
        :rtype: bool

        """
        hashed_pass = sha1()
        hashed_pass.update(password + self.password[:40])
        return self.password[40:] == hashed_pass.hexdigest()
Exemple #18
0
    def validate_password(self, password):
        """
        Check the password against existing credentials.

        :param password: the password that was provided by the user to
            try and authenticate. This is the clear text version that we will
            need to match against the hashed one in the database.
        :type password: unicode object.
        :return: Whether the password is valid.
        :rtype: bool

        """
        hashed_pass = sha1()
        hashed_pass.update(password + self.password[:40])
        return self.password[40:] == hashed_pass.hexdigest()
Exemple #19
0
        def cached(*args, **kwargs):
            if not cache[0]:
                if region is not None:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args, **kwargs)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **options)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            cache_key_kwargs = []
            if kwargs:
                # kwargs provided, merge them in positional args
                # to avoid having different cache keys.
                args, kwargs = bindfuncargs(signature, args, kwargs)
                cache_key_kwargs = [u_(':').join((u_(key), u_(value))) for key, value in kwargs.items()]

            cache_key_args = args
            if skip_self:
                cache_key_args = args[1:]

            cache_key = u_(" ").join(map(u_, chain(deco_args, cache_key_args, cache_key_kwargs)))

            if region:
                cachereg = cache_regions[region]
                key_length = cachereg.get('key_length', util.DEFAULT_CACHE_KEY_LENGTH)
            else:
                key_length = options.pop('key_length', util.DEFAULT_CACHE_KEY_LENGTH)

            # TODO: This is probably a bug as length is checked before converting to UTF8
            # which will cause cache_key to grow in size.
            if len(cache_key) + len(namespace) > int(key_length):
                cache_key = sha1(cache_key.encode('utf-8')).hexdigest()

            def go():
                return func(*args, **kwargs)
            # save org function name
            go.__name__ = '_cached_%s' % (func.__name__,)

            return cache[0].get_value(cache_key, createfunc=go)
Exemple #20
0
        def cached(*args):
            if not cache[0]:
                if region is not None:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **kwargs)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            if skip_self:
                try:
                    cache_key = " ".join(map(str, deco_args + args[1:]))
                except UnicodeEncodeError:
                    cache_key = " ".join(map(unicode, deco_args + args[1:]))
            else:
                try:
                    cache_key = " ".join(map(str, deco_args + args))
                except UnicodeEncodeError:
                    cache_key = " ".join(map(unicode, deco_args + args))
            if region:
                cachereg = cache_regions[region]
                key_length = cachereg.get('key_length', util.DEFAULT_CACHE_KEY_LENGTH)
            else:
                key_length = kwargs.pop('key_length', util.DEFAULT_CACHE_KEY_LENGTH)
            if len(cache_key) + len(namespace) > int(key_length):
                if util.py3k:
                    cache_key = cache_key.encode('utf-8')
                cache_key = sha1(cache_key).hexdigest()

            def go():
                return func(*args)

            return cache[0].get_value(cache_key, createfunc=go)
Exemple #21
0
        def cached(*args):
            if not cache[0]:
                if region is not None:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **kwargs)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            if skip_self:
                try:
                    cache_key = " ".join(map(str, deco_args + args[1:]))
                except UnicodeEncodeError:
                    cache_key = " ".join(map(unicode, deco_args + args[1:]))
            else:
                try:
                    cache_key = " ".join(map(str, deco_args + args))
                except UnicodeEncodeError:
                    cache_key = " ".join(map(unicode, deco_args + args))
            if region:
                key_length = cache_regions[region]['key_length']
            else:
                key_length = kwargs.pop('key_length', 250)
            if len(cache_key) + len(namespace) > int(key_length):
                if util.py3k:
                    cache_key = cache_key.encode('utf-8')
                cache_key = sha1(cache_key).hexdigest()

            def go():
                return func(*args)

            return cache[0].get_value(cache_key, createfunc=go)
        def cached(*args):
            if not cache[0]:
                if region is not None:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **kwargs)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            cache_key_args = args
            if skip_self:
                cache_key_args = args[1:]
            cache_key = u_(" ").join(map(u_, deco_args + cache_key_args))

            if region:
                cachereg = cache_regions[region]
                key_length = cachereg.get('key_length',
                                          util.DEFAULT_CACHE_KEY_LENGTH)
            else:
                key_length = kwargs.pop('key_length',
                                        util.DEFAULT_CACHE_KEY_LENGTH)

            # TODO: This is probably a bug as length is checked before converting to UTF8
            # which will cause cache_key to grow in size.
            if len(cache_key) + len(namespace) > int(key_length):
                cache_key = sha1(cache_key.encode('utf-8')).hexdigest()

            def go():
                return func(*args)

            return cache[0].get_value(cache_key, createfunc=go)
Exemple #23
0
        def cached(*args):
            if not cache[0]:
                if region is not None:
                    if region not in cache_regions:
                        raise BeakerException(
                            'Cache region not configured: %s' % region)
                    reg = cache_regions[region]
                    if not reg.get('enabled', True):
                        return func(*args)
                    cache[0] = Cache._get_cache(namespace, reg)
                elif manager:
                    cache[0] = manager.get_cache(namespace, **kwargs)
                else:
                    raise Exception("'manager + kwargs' or 'region' "
                                    "argument is required")

            cache_key_args = args
            if skip_self:
                cache_key_args = args[1:]
            cache_key = u_(" ").join(map(u_, deco_args + cache_key_args))

            if region:
                cachereg = cache_regions[region]
                key_length = cachereg.get('key_length', util.DEFAULT_CACHE_KEY_LENGTH)
            else:
                key_length = kwargs.pop('key_length', util.DEFAULT_CACHE_KEY_LENGTH)

            # TODO: This is probably a bug as length is checked before converting to UTF8
            # which will cause cache_key to grow in size.
            if len(cache_key) + len(namespace) > int(key_length):
                cache_key = sha1(cache_key.encode('utf-8')).hexdigest()

            def go():
                return func(*args)

            return cache[0].get_value(cache_key, createfunc=go)
Exemple #24
0
 def _format_key(self, key):
     formated_key = self.namespace + '_' + key.replace(' ', '\302\267')
     if len(formated_key) > MAX_KEY_LENGTH:
         formated_key = sha1(formated_key).hexdigest()
     return formated_key
Exemple #25
0
 def _format_key(self, key):
     formated_key = self.namespace + "_" + key.replace(" ", "\302\267")
     if len(formated_key) > MAX_KEY_LENGTH:
         formated_key = sha1(formated_key).hexdigest()
     return formated_key
Exemple #26
0
 def _format_key(self, key):
     formated_key = self.namespace + '_' + key.replace(' ', '\302\267')
     if len(formated_key) > MAX_KEY_LENGTH:
         formated_key = sha1(formated_key).hexdigest()
     return formated_key