From ae4e1d1cc15b3c510bdd774a1dfeff67c522324a Mon Sep 17 00:00:00 2001 From: Marek Czernek Date: Tue, 17 Oct 2023 13:05:00 +0200 Subject: [PATCH] Allow all primitive grain types for autosign_grains (#607) * Allow all primitive grain types for autosign_grains Signed-off-by: Marek Czernek * blacken daemons/masterapi.py and its test_auto_key Signed-off-by: Marek Czernek --------- Signed-off-by: Marek Czernek Co-authored-by: Alexander Graul --- changelog/61416.fixed.md | 1 + changelog/63708.fixed.md | 1 + salt/daemons/masterapi.py | 2 +- .../pytests/unit/daemons/masterapi/test_auto_key.py | 13 +++++++------ 4 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 changelog/61416.fixed.md create mode 100644 changelog/63708.fixed.md diff --git a/changelog/61416.fixed.md b/changelog/61416.fixed.md new file mode 100644 index 0000000000..3203a0a1c6 --- /dev/null +++ b/changelog/61416.fixed.md @@ -0,0 +1 @@ +Allow all primitive grain types for autosign_grains diff --git a/changelog/63708.fixed.md b/changelog/63708.fixed.md new file mode 100644 index 0000000000..3203a0a1c6 --- /dev/null +++ b/changelog/63708.fixed.md @@ -0,0 +1 @@ +Allow all primitive grain types for autosign_grains diff --git a/salt/daemons/masterapi.py b/salt/daemons/masterapi.py index 3716c63d99..54aca64a76 100644 --- a/salt/daemons/masterapi.py +++ b/salt/daemons/masterapi.py @@ -366,7 +366,7 @@ class AutoKey: line = salt.utils.stringutils.to_unicode(line).strip() if line.startswith("#"): continue - if autosign_grains[grain] == line: + if str(autosign_grains[grain]) == line: return True return False diff --git a/tests/pytests/unit/daemons/masterapi/test_auto_key.py b/tests/pytests/unit/daemons/masterapi/test_auto_key.py index b3657b7f1b..54c3f22d2a 100644 --- a/tests/pytests/unit/daemons/masterapi/test_auto_key.py +++ b/tests/pytests/unit/daemons/masterapi/test_auto_key.py @@ -17,11 +17,11 @@ def gen_permissions(owner="", group="", others=""): """ ret = 0 for c in owner: - ret |= getattr(stat, "S_I{}USR".format(c.upper()), 0) + ret |= getattr(stat, f"S_I{c.upper()}USR", 0) for c in group: - ret |= getattr(stat, "S_I{}GRP".format(c.upper()), 0) + ret |= getattr(stat, f"S_I{c.upper()}GRP", 0) for c in others: - ret |= getattr(stat, "S_I{}OTH".format(c.upper()), 0) + ret |= getattr(stat, f"S_I{c.upper()}OTH", 0) return ret @@ -256,16 +256,17 @@ def test_check_autosign_grains_no_autosign_grains_dir(auto_key): _test_check_autosign_grains(test_func, auto_key, autosign_grains_dir=None) -def test_check_autosign_grains_accept(auto_key): +@pytest.mark.parametrize("grain_value", ["test_value", 123, True]) +def test_check_autosign_grains_accept(grain_value, auto_key): """ Asserts that autosigning from grains passes when a matching grain value is in an autosign_grain file. """ def test_func(*args): - assert auto_key.check_autosign_grains({"test_grain": "test_value"}) is True + assert auto_key.check_autosign_grains({"test_grain": grain_value}) is True - file_content = "#test_ignore\ntest_value" + file_content = f"#test_ignore\n{grain_value}" _test_check_autosign_grains(test_func, auto_key, file_content=file_content) -- 2.42.0