Commit f4e4b76f authored by Daniel Silverstone's avatar Daniel Silverstone

_cachekey.py _artifactelement.py: Create new is_key() and use it

To more cleanly check if a given string could be a cache key, create
a new _cachekey.is_key() function and use that in _artifactelement's
verify_artifact_ref() function.
Signed-off-by: Daniel Silverstone's avatarDaniel Silverstone <daniel.silverstone@codethink.co.uk>
parent 3816dcf8
......@@ -83,8 +83,8 @@ class ArtifactElement(Element):
def verify_artifact_ref(ref):
try:
project, element, key = ref.split('/', 2) # This will raise a Value error if unable to split
# Explicitly raise a ValueError if the key lenght is not as expected
if len(key) != len(_cachekey.generate_key(_yaml.new_empty_node())):
# Explicitly raise a ValueError if the key length is not as expected
if not _cachekey.is_key(key):
raise ValueError
except ValueError:
raise ArtifactElementError("Artifact: {} is not of the expected format".format(ref))
......
......@@ -24,6 +24,31 @@ import ujson
from . import _yaml
# Internal record of the size of a cache key
_CACHEKEY_SIZE = len(hashlib.sha256().hexdigest())
# Hex digits
_HEX_DIGITS = "0123456789abcdef"
# is_key()
#
# Check if the passed in string *could be* a cache key. This basically checks
# that the length matches a sha256 hex digest, and that the string does not
# contain any non-hex characters and is fully lower case.
#
# Args:
# key (str): The string to check
#
# Returns:
# (bool): Whether or not `key` could be a cache key
#
def is_key(key):
if len(key) != _CACHEKEY_SIZE:
return False
return not any(ch not in _HEX_DIGITS for ch in key)
# generate_key()
#
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment