From f34bead7d2220b8ab0eeb7c2cdb45ce925878e42 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 9 Oct 2018 05:12:19 -0400 Subject: [PATCH] Improve index mirroring cache class to use the hash of the keys instead of the literal keys. Avoid warning about invalid key characters. Closes GitLab issue #518. Thanks to TheOneValen @ for the report. Signed-off-by: Roberto Rosario --- HISTORY.rst | 6 +++++- mayan/apps/mirroring/caches.py | 18 +++++++++++++++--- mayan/apps/mirroring/tests/literals.py | 1 + mayan/apps/mirroring/tests/test_caches.py | 17 ++++++++++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 11813952c1..67362baa80 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,8 +1,12 @@ 3.1.6 (2018-10-XX) ================== -* Improve index mounting value clean up code to remove the spaces at the +* Improve index mirroring value clean up code to remove the spaces at the starts and at the end of directories. Closes again GitLab issue #520 Thanks to TheOneValen @ for the report. +* Improve index mirroring cache class to use the hash of the keys + instead of the literal keys. Avoid warning about invalid key + characters. Closes GitLab issue #518. Thanks to TheOneValen @ for the + report. 3.1.5 (2018-10-08) ================== diff --git a/mayan/apps/mirroring/caches.py b/mayan/apps/mirroring/caches.py index aaef782938..affeaa003e 100644 --- a/mayan/apps/mirroring/caches.py +++ b/mayan/apps/mirroring/caches.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import hashlib + from django.core.cache import caches from .settings import ( @@ -8,17 +10,27 @@ from .settings import ( class IndexFilesystemCache(object): + @staticmethod + def get_key_hash(key): + return hashlib.sha256(key).hexdigest() + @staticmethod def get_document_key(document): - return 'document_pk_{}'.format(document.pk) + return IndexFilesystemCache.get_key_hash( + key='document_pk_{}'.format(document.pk) + ) @staticmethod def get_node_key(node): - return 'node_pk_{}'.format(node.pk) + return IndexFilesystemCache.get_key_hash( + key='node_pk_{}'.format(node.pk) + ) @staticmethod def get_path_key(path): - return 'path_{}'.format(path) + return IndexFilesystemCache.get_key_hash( + key='path_{}'.format(path) + ) def __init__(self, name='default'): self.cache = caches[name] diff --git a/mayan/apps/mirroring/tests/literals.py b/mayan/apps/mirroring/tests/literals.py index 19708b1341..08724ec148 100644 --- a/mayan/apps/mirroring/tests/literals.py +++ b/mayan/apps/mirroring/tests/literals.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, unicode_literals +TEST_CACHE_KEY_BAD_CHARACTERS = ' \r\n!@#$%^&*()+_{}|:"<>?-=[];\',./' TEST_DOCUMENT_PK = 99 TEST_NODE_EXPRESSION = 'level_1' TEST_NODE_EXPRESSION_MULTILINE = 'first\r\nsecond\r\nthird' diff --git a/mayan/apps/mirroring/tests/test_caches.py b/mayan/apps/mirroring/tests/test_caches.py index 4d5cceaa90..0e15b3df9b 100644 --- a/mayan/apps/mirroring/tests/test_caches.py +++ b/mayan/apps/mirroring/tests/test_caches.py @@ -1,10 +1,14 @@ from __future__ import absolute_import, unicode_literals +import warnings + from common.tests import BaseTestCase from ..caches import IndexFilesystemCache -from .literals import TEST_DOCUMENT_PK, TEST_NODE_PK, TEST_PATH +from .literals import ( + TEST_CACHE_KEY_BAD_CHARACTERS, TEST_DOCUMENT_PK, TEST_NODE_PK, TEST_PATH +) class MockDocument(object): @@ -47,3 +51,14 @@ class IndexFilesystemCacheTestCase(BaseTestCase): self.cache.clear_node(node=self.node) self.assertEquals(None, self.cache.get_path(path=TEST_PATH)) + + def test_valid_cache_key_characters(self): + with warnings.catch_warnings(record=True) as warning_list: + self.cache.cache.validate_key(TEST_CACHE_KEY_BAD_CHARACTERS) + self.assertTrue(len(warning_list) == 1) + + with warnings.catch_warnings(record=True) as warning_list: + self.cache.cache.validate_key( + self.cache.get_key_hash(key=TEST_CACHE_KEY_BAD_CHARACTERS) + ) + self.assertTrue(len(warning_list) == 0)