From 6591e4dd0799f49481b3c37bc243740de1930990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Wed, 22 Jan 2025 12:07:56 +0000 Subject: [PATCH 1/3] Fix _pygit2.GitError: error loading known_hosts: issue on gitfs This commit ensures the right HOME value is set during Pygit2 remote initialization, otherwise there are chances to get exceptions as pygit2/libgit2 is relying on it. --- salt/utils/gitfs.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/salt/utils/gitfs.py b/salt/utils/gitfs.py index 9feda7a16771..e2a7f2d84ab2 100644 --- a/salt/utils/gitfs.py +++ b/salt/utils/gitfs.py @@ -2003,7 +2003,12 @@ def init_remote(self): """ # https://github.com/libgit2/pygit2/issues/339 # https://github.com/libgit2/libgit2/issues/2122 + # https://github.com/saltstack/salt/issues/64121 home = os.path.expanduser("~") + if "HOME" not in os.environ: + # Make sure $HOME env variable is set to prevent + # _pygit2.GitError: error loading known_hosts in some libgit2 versions. + os.environ["HOME"] = home pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = home new = False if not os.listdir(self._cachedir): From 426332a285170e01be74d417d089b0a93ef05fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Wed, 22 Jan 2025 12:23:25 +0000 Subject: [PATCH 2/3] Add changelog entry file --- changelog/64121.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/64121.fixed.md diff --git a/changelog/64121.fixed.md b/changelog/64121.fixed.md new file mode 100644 index 000000000000..e78bbd5b7f3e --- /dev/null +++ b/changelog/64121.fixed.md @@ -0,0 +1 @@ +Ensure the right HOME environment value is set during Pygit2 remote initialization. From c60fab1432308388731e3773c73972bbb2db1ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?= Date: Wed, 22 Jan 2025 12:36:27 +0000 Subject: [PATCH 3/3] Add test_checkout_pygit2_with_home_env_unset unit test --- tests/pytests/unit/utils/test_gitfs.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/pytests/unit/utils/test_gitfs.py b/tests/pytests/unit/utils/test_gitfs.py index 4b0c11afd60f..c4a9544b05aa 100644 --- a/tests/pytests/unit/utils/test_gitfs.py +++ b/tests/pytests/unit/utils/test_gitfs.py @@ -7,6 +7,7 @@ import salt.fileserver.gitfs import salt.utils.gitfs from salt.exceptions import FileserverConfigError +from tests.support.helpers import patched_environ from tests.support.mock import MagicMock, patch try: @@ -239,6 +240,20 @@ def test_checkout_pygit2(_prepare_provider): assert provider.checkout() is None +@pytest.mark.skipif(not HAS_PYGIT2, reason="This host lacks proper pygit2 support") +@pytest.mark.skip_on_windows( + reason="Skip Pygit2 on windows, due to pygit2 access error on windows" +) +def test_checkout_pygit2_with_home_env_unset(_prepare_provider): + provider = _prepare_provider + provider.remotecallbacks = None + provider.credentials = None + with patched_environ(__cleanup__=["HOME"]): + assert "HOME" not in os.environ + provider.init_remote() + assert "HOME" in os.environ + + @pytest.mark.skipif(not HAS_PYGIT2, reason="This host lacks proper pygit2 support") @pytest.mark.skip_on_windows( reason="Skip Pygit2 on windows, due to pygit2 access error on windows"