From 5558c546328d317f2abaf91fa4378969d737f0f0 Mon Sep 17 00:00:00 2001 From: reyesj2 <94730068+reyesj2@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:44:58 -0600 Subject: [PATCH] so-yaml: treat non-mapping parents as missing keys getKeyValue and removeKey now return early when a parent of the key is null or not a mapping, so get, remove and purgelistitem report the key as missing instead of crashing on a hand-edited pillar such as `firewall: null`. --- salt/manager/tools/sbin/so-yaml.py | 4 ++ salt/manager/tools/sbin/so-yaml_test.py | 61 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/salt/manager/tools/sbin/so-yaml.py b/salt/manager/tools/sbin/so-yaml.py index a4126bedf..c38d523db 100755 --- a/salt/manager/tools/sbin/so-yaml.py +++ b/salt/manager/tools/sbin/so-yaml.py @@ -319,6 +319,8 @@ def add(args): def removeKey(content, key): + if not isinstance(content, dict): + return pieces = key.split(".", 1) if len(pieces) > 1: if pieces[0] in content: @@ -362,6 +364,8 @@ def replace(args): def getKeyValue(content, key): + if not isinstance(content, dict): + return None pieces = key.split(".", 1) if len(pieces) > 1 and pieces[0] in content: return getKeyValue(content[pieces[0]], pieces[1]) diff --git a/salt/manager/tools/sbin/so-yaml_test.py b/salt/manager/tools/sbin/so-yaml_test.py index 72a82f44e..71f748703 100644 --- a/salt/manager/tools/sbin/so-yaml_test.py +++ b/salt/manager/tools/sbin/so-yaml_test.py @@ -128,6 +128,22 @@ class TestRemove(unittest.TestCase): sysmock.assert_called_once_with(1) self.assertIn("Missing filename or key arg\n", mock_stderr.getvalue()) + def test_remove_null_parent(self): + filename = "/tmp/so-yaml_test-remove.yaml" + file = open(filename, "w") + file.write("key1: null\n") + file.close() + + result = soyaml.remove([filename, "key1.child1.deep1"]) + self.assertEqual(result, 0) + + file = open(filename, "r") + actual = file.read() + file.close() + + expected = "key1: null\n" + self.assertEqual(actual, expected) + def test_append_missing_arg(self): with patch('sys.exit', new=MagicMock()) as sysmock: with patch('sys.stderr', new=StringIO()) as mock_stderr: @@ -565,6 +581,19 @@ class TestRemove(unittest.TestCase): self.assertEqual(result, 2) self.assertEqual("", mock_stdout.getvalue()) + def test_get_null_parent(self): + with patch('sys.stdout', new=StringIO()) as mock_stdout: + with patch('sys.stderr', new=StringIO()) as mock_stderr: + filename = "/tmp/so-yaml_test-get.yaml" + file = open(filename, "w") + file.write("key1: null\n") + file.close() + + result = soyaml.get([filename, "key1.child1"]) + self.assertEqual(result, 2) + self.assertEqual("", mock_stdout.getvalue()) + self.assertIn("Key 'key1.child1' not found by so-yaml.py", mock_stderr.getvalue()) + def test_get_empty_file(self): with patch('sys.stdout', new=StringIO()) as mock_stdout: with patch('sys.stderr', new=StringIO()) as mock_stderr: @@ -925,6 +954,38 @@ class TestPurgeListItem(unittest.TestCase): expected = "key1:\n child2:\n - b\nkey2:\n group1:\n items:\n - x\n - y\n group2:\n items: []\n" self.assertEqual(actual, expected) + def test_purgelistitem_null_parent(self): + filename = "/tmp/so-yaml_test-purgelistitem.yaml" + file = open(filename, "w") + file.write("key1: null\n") + file.close() + + result = soyaml.purgelistitem([filename, "key1.child1", "b"]) + self.assertEqual(result, 0) + + file = open(filename, "r") + actual = file.read() + file.close() + + expected = "key1: null\n" + self.assertEqual(actual, expected) + + def test_purgelistitem_non_dict_parent(self): + filename = "/tmp/so-yaml_test-purgelistitem.yaml" + file = open(filename, "w") + file.write("key1:\n- a\n- b\n") + file.close() + + result = soyaml.purgelistitem([filename, "key1.child1", "b"]) + self.assertEqual(result, 0) + + file = open(filename, "r") + actual = file.read() + file.close() + + expected = "key1:\n- a\n- b\n" + self.assertEqual(actual, expected) + class TestAppendListObject(unittest.TestCase):