| 1 | from __future__ import annotations |
| 2 | |
| 3 | import json |
| 4 | from typing import Optional |
| 5 | |
| 6 | from consul.callback import CB |
| 7 | |
| 8 | |
| 9 | class Policy: |
| 10 | def __init__(self, agent) -> None: |
| 11 | self.agent = agent |
| 12 | |
| 13 | def list(self, token: str | None = None): |
| 14 | """ |
| 15 | Lists all the active ACL policies. This is a privileged endpoint, and |
| 16 | requires a management token. *token* will override this client's |
| 17 | default token. |
| 18 | Requires a token with acl:read capability. ACLPermissionDenied raised otherwise |
| 19 | """ |
| 20 | |
| 21 | headers = self.agent.prepare_headers(token) |
| 22 | return self.agent.http.get(CB.json(), "/v1/acl/policies", headers=headers) |
| 23 | |
| 24 | def read(self, uuid, token: str | None = None): |
| 25 | """ |
| 26 | Returns the policy information for *id*. Requires a token with acl:read capability. |
| 27 | :param uuid: Specifies the UUID of the policy you look up. |
| 28 | :param token: token with acl:read capability |
| 29 | :return: selected Polic information |
| 30 | """ |
| 31 | headers = self.agent.prepare_headers(token) |
| 32 | return self.agent.http.get(CB.json(), f"/v1/acl/policy/{uuid}", headers=headers) |
| 33 | |
| 34 | def create(self, name: str, token: str | None = None, description: Optional[str] = None, rules=None): |
| 35 | """ |
| 36 | Create a policy |
| 37 | This is a privileged endpoint, and requires a token with acl:write. |
| 38 | :param name: Specifies a name for the ACL policy. |
| 39 | :param token: token with acl:write capability |
| 40 | :param description: Free form human-readable description of the policy. |
| 41 | :param rules: Specifies rules for the ACL policy. |
| 42 | :return: The cloned token information |
| 43 | """ |
| 44 | json_data = {"name": name} |
| 45 | if rules: |
| 46 | json_data["rules"] = json.dumps(rules) |
| 47 | if description: |
| 48 | json_data["Description"] = description |
| 49 | headers = self.agent.prepare_headers(token) |
| 50 | return self.agent.http.put( |
| 51 | CB.json(), |
| 52 | "/v1/acl/policy", |
| 53 | headers=headers, |
| 54 | data=json.dumps(json_data), |
| 55 | ) |