Skip to content

Commit 5972888

Browse files
cmcmarrowMegan Wilhite
authored and
Megan Wilhite
committed
base64 hash
1 parent 8344fe5 commit 5972888

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

salt/utils/gitfs.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55

6+
import base64
67
import contextlib
78
import copy
89
import errno
@@ -16,6 +17,7 @@
1617
import shlex
1718
import shutil
1819
import stat
20+
import string
1921
import subprocess
2022
import time
2123
import weakref
@@ -457,7 +459,7 @@ def __init__(
457459

458460
hash_type = getattr(hashlib, self.opts.get("hash_type", "md5"))
459461
# Generate full id. The full id is made from these parts name-id-env-_root.
460-
# Full id stops collections in the gitfs cache.
462+
# Full id helps decrease the chances of collections in the gitfs cache.
461463
self._full_id = "-".join(
462464
[
463465
getattr(self, "name", ""),
@@ -468,7 +470,22 @@ def __init__(
468470
)
469471
# We loaded this data from yaml configuration files, so, its safe
470472
# to use UTF-8
471-
self.cachedir_basename = f"{getattr(self, 'name', '')}-{hash_type(self._full_id.encode('utf-8')).hexdigest()}"
473+
base64_hash = str(
474+
base64.b64encode(hash_type(self._full_id.encode("utf-8")).digest()),
475+
encoding="ascii", # base64 only outputs ascii
476+
).replace(
477+
"/", "_"
478+
) # replace "/" with "_" to not cause trouble with file system
479+
480+
# limit name length to 19, so we don't eat up all the path length for windows
481+
# this is due to pygit2 limitations
482+
# replace any unknown char with "_" to not cause trouble with file system
483+
name_chars = string.ascii_letters + string.digits + "-"
484+
cache_name = "".join(
485+
c if c in name_chars else "_" for c in getattr(self, "name", "")[:19]
486+
)
487+
488+
self.cachedir_basename = f"{cache_name}-{base64_hash}"
472489
self.cachedir = salt.utils.path.join(cache_root, self.cachedir_basename)
473490
self.linkdir = salt.utils.path.join(cache_root, "links", self.cachedir_basename)
474491

tests/pytests/unit/utils/test_gitfs.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ def test_full_id_pygit2(_prepare_provider):
254254
)
255255
def test_get_cachedir_basename_pygit2(_prepare_provider):
256256
basename = _prepare_provider.get_cachedir_basename()
257-
assert len(basename) > 1
257+
# Note: if you are changing the length of basename
258+
# keep in mind that pygit2 will error out on large file paths on Windows
259+
assert len(basename) == 45
258260
assert basename[0] == "-"
259-
# check that a valid hex is given
260-
assert all(c in string.hexdigits for c in basename[1:])
261+
# check that a valid base64 is given '/' -> '_'
262+
assert all(c in string.ascii_letters + string.digits + "+_=" for c in basename[1:])

tests/unit/utils/test_gitfs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ def test_full_id_with_name(self):
124124
def test_get_cachedir_basename(self):
125125
self.assertEqual(
126126
self.main_class.remotes[0].get_cachedir_basename(),
127-
"-b4dcbd51b08742ec23eaf96d192d29b417ec137ea7ca0c0de2515cfaf6e26860",
127+
"-tNy9UbCHQuwj6vltGS0ptBfsE36nygwN4lFc+vbiaGA=",
128128
)
129129

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

136136
def test_git_provider_mp_lock(self):

0 commit comments

Comments
 (0)