Skip to content

Commit

Permalink
base64 hash
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcmarrow authored and Megan Wilhite committed Aug 2, 2023
1 parent 8344fe5 commit 5972888
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
21 changes: 19 additions & 2 deletions salt/utils/gitfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""


import base64
import contextlib
import copy
import errno
Expand All @@ -16,6 +17,7 @@
import shlex
import shutil
import stat
import string
import subprocess
import time
import weakref
Expand Down Expand Up @@ -457,7 +459,7 @@ def __init__(

hash_type = getattr(hashlib, self.opts.get("hash_type", "md5"))
# Generate full id. The full id is made from these parts name-id-env-_root.
# Full id stops collections in the gitfs cache.
# Full id helps decrease the chances of collections in the gitfs cache.
self._full_id = "-".join(
[
getattr(self, "name", ""),
Expand All @@ -468,7 +470,22 @@ def __init__(
)
# We loaded this data from yaml configuration files, so, its safe
# to use UTF-8
self.cachedir_basename = f"{getattr(self, 'name', '')}-{hash_type(self._full_id.encode('utf-8')).hexdigest()}"
base64_hash = str(
base64.b64encode(hash_type(self._full_id.encode("utf-8")).digest()),
encoding="ascii", # base64 only outputs ascii
).replace(
"/", "_"
) # replace "/" with "_" to not cause trouble with file system

# limit name length to 19, so we don't eat up all the path length for windows
# this is due to pygit2 limitations
# replace any unknown char with "_" to not cause trouble with file system
name_chars = string.ascii_letters + string.digits + "-"
cache_name = "".join(
c if c in name_chars else "_" for c in getattr(self, "name", "")[:19]
)

self.cachedir_basename = f"{cache_name}-{base64_hash}"
self.cachedir = salt.utils.path.join(cache_root, self.cachedir_basename)
self.linkdir = salt.utils.path.join(cache_root, "links", self.cachedir_basename)

Expand Down
8 changes: 5 additions & 3 deletions tests/pytests/unit/utils/test_gitfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ def test_full_id_pygit2(_prepare_provider):
)
def test_get_cachedir_basename_pygit2(_prepare_provider):
basename = _prepare_provider.get_cachedir_basename()
assert len(basename) > 1
# Note: if you are changing the length of basename
# keep in mind that pygit2 will error out on large file paths on Windows
assert len(basename) == 45
assert basename[0] == "-"
# check that a valid hex is given
assert all(c in string.hexdigits for c in basename[1:])
# check that a valid base64 is given '/' -> '_'
assert all(c in string.ascii_letters + string.digits + "+_=" for c in basename[1:])
4 changes: 2 additions & 2 deletions tests/unit/utils/test_gitfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ def test_full_id_with_name(self):
def test_get_cachedir_basename(self):
self.assertEqual(
self.main_class.remotes[0].get_cachedir_basename(),
"-b4dcbd51b08742ec23eaf96d192d29b417ec137ea7ca0c0de2515cfaf6e26860",
"-tNy9UbCHQuwj6vltGS0ptBfsE36nygwN4lFc+vbiaGA=",
)

def test_get_cachedir_base_with_name(self):
self.assertEqual(
self.main_class.remotes[1].get_cachedir_basename(),
"repo2-4218c2f8e303c6ea24cc541d8748e523d5b443c3050170a43a1a00be253b56aa",
"repo2-QhjC+OMDxuokzFQdh0jlI9W0Q8MFAXCkOhoAviU7Vqo=",
)

def test_git_provider_mp_lock(self):
Expand Down

0 comments on commit 5972888

Please sign in to comment.