Skip to content

Commit

Permalink
fix Rules attribute formatting to allow for proper creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasmhughes committed Jan 13, 2024
1 parent d1ca6c1 commit e408a96
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 80 deletions.
1 change: 1 addition & 0 deletions changelog/7.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixed Rules attribute formatting to allow for proper creation
1 change: 0 additions & 1 deletion docs/ref/modules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ _________________
:toctree:

consul
consul_mod
5 changes: 0 additions & 5 deletions docs/ref/modules/saltext.consul.modules.consul_mod.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/ref/states/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ _____________
:toctree:

consul
consul_mod
5 changes: 0 additions & 5 deletions docs/ref/states/saltext.consul.states.consul_mod.rst

This file was deleted.

36 changes: 29 additions & 7 deletions src/saltext/consul/modules/consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
"""

Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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[
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
27 changes: 0 additions & 27 deletions src/saltext/consul/modules/consul_mod.py

This file was deleted.

14 changes: 11 additions & 3 deletions src/saltext/consul/states/consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 0 additions & 30 deletions src/saltext/consul/states/consul_mod.py

This file was deleted.

42 changes: 41 additions & 1 deletion tests/unit/modules/test_consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down

0 comments on commit e408a96

Please sign in to comment.