From e408a96d6f0981c944593feb34dffc1d0ddd2b3b Mon Sep 17 00:00:00 2001 From: nicholasmhughes Date: Sat, 13 Jan 2024 13:53:07 -0500 Subject: [PATCH] fix Rules attribute formatting to allow for proper creation --- changelog/7.fixed.md | 1 + docs/ref/modules/index.rst | 1 - .../saltext.consul.modules.consul_mod.rst | 5 --- docs/ref/states/index.rst | 1 - .../saltext.consul.states.consul_mod.rst | 5 --- src/saltext/consul/modules/consul.py | 36 ++++++++++++---- src/saltext/consul/modules/consul_mod.py | 27 ------------ src/saltext/consul/states/consul.py | 14 +++++-- src/saltext/consul/states/consul_mod.py | 30 ------------- tests/unit/modules/test_consul.py | 42 ++++++++++++++++++- 10 files changed, 82 insertions(+), 80 deletions(-) create mode 100644 changelog/7.fixed.md delete mode 100644 docs/ref/modules/saltext.consul.modules.consul_mod.rst delete mode 100644 docs/ref/states/saltext.consul.states.consul_mod.rst delete mode 100644 src/saltext/consul/modules/consul_mod.py delete mode 100644 src/saltext/consul/states/consul_mod.py diff --git a/changelog/7.fixed.md b/changelog/7.fixed.md new file mode 100644 index 0000000..b024483 --- /dev/null +++ b/changelog/7.fixed.md @@ -0,0 +1 @@ +fixed Rules attribute formatting to allow for proper creation diff --git a/docs/ref/modules/index.rst b/docs/ref/modules/index.rst index 09009cb..e7c98cc 100644 --- a/docs/ref/modules/index.rst +++ b/docs/ref/modules/index.rst @@ -10,4 +10,3 @@ _________________ :toctree: consul - consul_mod diff --git a/docs/ref/modules/saltext.consul.modules.consul_mod.rst b/docs/ref/modules/saltext.consul.modules.consul_mod.rst deleted file mode 100644 index 4277ad6..0000000 --- a/docs/ref/modules/saltext.consul.modules.consul_mod.rst +++ /dev/null @@ -1,5 +0,0 @@ -``consul`` -========== - -.. automodule:: saltext.consul.modules.consul_mod - :members: diff --git a/docs/ref/states/index.rst b/docs/ref/states/index.rst index f93509a..c0b1435 100644 --- a/docs/ref/states/index.rst +++ b/docs/ref/states/index.rst @@ -10,4 +10,3 @@ _____________ :toctree: consul - consul_mod diff --git a/docs/ref/states/saltext.consul.states.consul_mod.rst b/docs/ref/states/saltext.consul.states.consul_mod.rst deleted file mode 100644 index 1ca37da..0000000 --- a/docs/ref/states/saltext.consul.states.consul_mod.rst +++ /dev/null @@ -1,5 +0,0 @@ -``consul`` -========== - -.. automodule:: saltext.consul.states.consul_mod - :members: diff --git a/src/saltext/consul/modules/consul.py b/src/saltext/consul/modules/consul.py index 6ceac04..ec50b78 100644 --- a/src/saltext/consul/modules/consul.py +++ b/src/saltext/consul/modules/consul.py @@ -44,6 +44,8 @@ def _query( api_version="v1", data=None, query_params=None, + decode=True, + text=False, ): """ Consul object method function to construct and execute on the API URL. @@ -53,6 +55,8 @@ def _query( :param function: The Consul api function to perform. :param method: The HTTP method, e.g. GET or POST. :param data: The data to be sent for POST method. This param is ignored for GET requests. + :param decode: Decode + :param text: Text :return: The json response from the API call or False. """ @@ -977,7 +981,7 @@ def agent_check_fail(consul_url=None, token=None, checkid=None, **kwargs): return ret -def agent_service_register(consul_url=None, token=None, **kwargs): +def agent_service_register(consul_url=None, token=None, decode=False, text=True, **kwargs): """ The used to add a new service, with an optional health check, to the local agent. @@ -1000,6 +1004,8 @@ def agent_service_register(consul_url=None, token=None, **kwargs): endpoint must be used periodically to update the state of the check. :param check_interval: Interval at which the check should run. + :param decode: Decode + :param text: Text :return: Boolean and message indicating success or failure. CLI Example: @@ -1077,7 +1083,15 @@ def agent_service_register(consul_url=None, token=None, **kwargs): data["Check"] = check_dd # if empty, ignore it function = "agent/service/register" - res = _query(consul_url=consul_url, function=function, token=token, method="PUT", data=data) + res = _query( + consul_url=consul_url, + function=function, + token=token, + method="PUT", + data=data, + decode=decode, + text=text, + ) if res["res"]: ret["res"] = True ret[ @@ -1095,12 +1109,14 @@ def agent_service_register(consul_url=None, token=None, **kwargs): return ret -def agent_service_deregister(consul_url=None, token=None, serviceid=None): +def agent_service_deregister(consul_url=None, token=None, serviceid=None, decode=False, text=True): """ Used to remove a service. :param consul_url: The Consul server URL. :param serviceid: A serviceid describing the service. + :param decode: Decode + :param text: Text :return: Boolean and message indicating success or failure. CLI Example: @@ -2080,9 +2096,6 @@ def acl_create(consul_url=None, token=None, **kwargs): ret["res"] = False return ret - if "id" in kwargs: - data["id"] = kwargs["id"] - if "name" in kwargs: data["Name"] = kwargs["name"] else: @@ -2092,7 +2105,15 @@ def acl_create(consul_url=None, token=None, **kwargs): data["Type"] = kwargs["type"] if "rules" in kwargs: - data["Rules"] = kwargs["rules"] + rules_str = "" + rules = kwargs["rules"] + for item in rules: + for key, val in item.items(): + if key != "policy": + rules_str += f'{key} "{val}" {{\n' + else: + rules_str += f' {key} = "{val}"\n}}\n' + data["Rules"] = rules_str function = "acl/create" res = _query(consul_url=consul_url, token=token, data=data, method="PUT", function=function) @@ -2225,6 +2246,7 @@ def acl_delete(consul_url=None, token=None, **kwargs): ] = "Removing ACL {} failed.".format( # pylint: disable=consider-using-f-string kwargs["id"] ) + ret["changes"] = res return ret diff --git a/src/saltext/consul/modules/consul_mod.py b/src/saltext/consul/modules/consul_mod.py deleted file mode 100644 index 501a21f..0000000 --- a/src/saltext/consul/modules/consul_mod.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -Salt execution module -""" -import logging - -log = logging.getLogger(__name__) - -__virtualname__ = "consul" - - -def __virtual__(): - # To force a module not to load return something like: - # return (False, "The consul execution module is not implemented yet") - return __virtualname__ - - -def example_function(text): - """ - This example function should be replaced - - CLI Example: - - .. code-block:: bash - - salt '*' consul.example_function text="foo bar" - """ - return __salt__["test.echo"](text) diff --git a/src/saltext/consul/states/consul.py b/src/saltext/consul/states/consul.py index f6e425c..285e848 100644 --- a/src/saltext/consul/states/consul.py +++ b/src/saltext/consul/states/consul.py @@ -10,11 +10,19 @@ acl_present: consul.acl_present: - - id: 38AC8470-4A83-4140-8DFD-F924CD32917F - name: acl_name - - rules: node "" {policy = "write"} service "" {policy = "read"} key "_rexec" {policy = "write"} - - type: client - consul_url: http://localhost:8500 + - rules: + - node: "host.example.local" + policy: "write" + - agent: "host.example.local" + policy: "write" + - session: "host.example.local" + policy: "write" + - key: "" + policy: "read" + - service: "" + policy: "read" acl_delete: consul.acl_absent: diff --git a/src/saltext/consul/states/consul_mod.py b/src/saltext/consul/states/consul_mod.py deleted file mode 100644 index 8e9db1f..0000000 --- a/src/saltext/consul/states/consul_mod.py +++ /dev/null @@ -1,30 +0,0 @@ -""" -Salt state module -""" -import logging - -log = logging.getLogger(__name__) - -__virtualname__ = "consul" - - -def __virtual__(): - # To force a module not to load return something like: - # return (False, "The consul state module is not implemented yet") - - # Replace this with your own logic - if "consul.example_function" not in __salt__: - return False, "The 'consul' execution module is not available" - return __virtualname__ - - -def exampled(name): - """ - This example function should be replaced - """ - ret = {"name": name, "changes": {}, "result": False, "comment": ""} - value = __salt__["consul.example_function"](name) - if value == name: - ret["result"] = True - ret["comment"] = f"The 'consul.example_function' returned: '{value}'" - return ret diff --git a/tests/unit/modules/test_consul.py b/tests/unit/modules/test_consul.py index d61f3ef..f64e731 100644 --- a/tests/unit/modules/test_consul.py +++ b/tests/unit/modules/test_consul.py @@ -1471,12 +1471,52 @@ def test_acl_create(): consul_url=consul_url, ) - with patch.object(salt.utils.http, "query", return_value=mock_http_result): + mock_http_query = MagicMock(return_value=mock_http_result) + with patch.object(salt.utils.http, "query", mock_http_query): with patch.dict(consul.__salt__, {"config.get": mock_url}): result = consul.acl_create(consul_url=consul_url, token=token, name=name) expected = {"message": f"ACL {name} created.", "res": True} assert expected == result + mock_http_query.reset_mock() + + # acl_create with rules + rules = [ + { + "node": "host.example.local", + "policy": "write", + }, + { + "agent": "host.example.local", + "policy": "write", + }, + { + "session": "host.example.local", + "policy": "write", + }, + { + "key": "", + "policy": "read", + }, + { + "service": "", + "policy": "read", + }, + ] + result = consul.acl_create(consul_url=consul_url, token=token, name=name, rules=rules) + mock_http_query.assert_called_with( + "http://localhost:1313/v1/acl/create", + method="PUT", + params={}, + data='{"Name": "name1", "Rules": [{"node": "host.example.local", "policy": "write"}, {"agent": "host.example.local", "policy": "write"}, {"session": "host.example.local", "policy": "write"}, {"key": "", "policy": "read"}, {"service": "", "policy": "read"}]}', + decode=True, + status=True, + header_dict={"X-Consul-Token": "randomtoken", "Content-Type": "application/json"}, + opts={"consul": {"url": "http://127.0.0.1", "token": "test_token"}}, + ) + expected = {"message": f"ACL {name} created.", "res": True} + assert expected == result + def test_acl_update(): """