Debian Codegraph @

consul/api/acl/token.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import json
4 import typing
5
6 from consul.callback import CB
7
8 if typing.TYPE_CHECKING:
9 import builtins
10
11
12 class Token:
13 def __init__(self, agent) -> None:
14 self.agent = agent
15
16 def list(self, token: str | None = None):
17 """
18 Lists all the active ACL tokens. This is a privileged endpoint, and
19 requires a management token. *token* will override this client's
20 default token.
21 Requires a token with acl:read capability. ACLPermissionDenied raised otherwise
22 """
23 headers = self.agent.prepare_headers(token)
24 return self.agent.http.get(CB.json(), "/v1/acl/tokens", headers=headers)
25
26 def read(self, accessor_id: str, token: str | None = None):
27 """
28 Returns the token information for *accessor_id*. Requires a token with acl:read capability.
29 :param accessor_id: The accessor ID of the token to read
30 :param token: token with acl:read capability
31 :return: selected token information
32 """
33 headers = self.agent.prepare_headers(token)
34 return self.agent.http.get(CB.json(), f"/v1/acl/token/{accessor_id}", headers=headers)
35
36 def delete(self, accessor_id: str, token: str | None = None):
37 """
38 Deletes the token with *accessor_id*. This is a privileged endpoint, and requires a token with acl:write.
39 :param accessor_id: The accessor ID of the token to delete
40 :param token: token with acl:write capability
41 :return: True if the token was deleted
42 """
43 headers = self.agent.prepare_headers(token)
44 return self.agent.http.delete(CB.boolean(), f"/v1/acl/token/{accessor_id}", headers=headers)
45
46 def clone(self, accessor_id: str, token: str | None = None, description: str = ""):
47 """
48 Clones the token identified by *accessor_id*. This is a privileged endpoint, and requires a token with acl:write.
49 :param accessor_id: The accessor ID of the token to clone
50 :param token: token with acl:write capability
51 :param description: Optional new token description
52 :return: The cloned token information
53 """
54
55 json_data = {"Description": description}
56 headers = self.agent.prepare_headers(token)
57 return self.agent.http.put(
58 CB.json(),
59 f"/v1/acl/token/{accessor_id}/clone",
60 headers=headers,
61 data=json.dumps(json_data),
62 )
63
64 def create(
65 self,
66 token: str | None = None,
67 accessor_id: str | None = None,
68 secret_id: str | None = None,
69 policies_id: builtins.list[str] | None = None,
70 description: str = "",
71 ):
72 """
73 Create a token (optionally identified by *secret_id* and *accessor_id*).
74 This is a privileged endpoint, and requires a token with acl:write.
75 :param token: token with acl:write capability
76 :param accessor_id: The accessor ID of the token to create
77 :param secret_id: The secret ID of the token to create
78 :param description: Optional new token description
79 :param policies_id: Optional list of policies id
80 :return: The cloned token information
81 """
82
83 json_data: dict[str, typing.Any] = {}
84 if accessor_id:
85 json_data["AccessorID"] = accessor_id
86 if secret_id:
87 json_data["SecretID"] = secret_id
88 if description:
89 json_data["Description"] = description
90 if policies_id:
91 json_data["Policies"] = [{"ID": policy} for policy in policies_id]
92
93 headers = self.agent.prepare_headers(token)
94 return self.agent.http.put(
95 CB.json(),
96 "/v1/acl/token",
97 headers=headers,
98 data=json.dumps(json_data),
99 )
100
101 def update(self, accessor_id: str, token: str | None = None, secret_id: str | None = None, description: str = ""):
102 """
103 Update a token (optionally identified by *secret_id* and *accessor_id*).
104 This is a privileged endpoint, and requires a token with acl:write.
105 :param accessor_id: The accessor ID of the token to update
106 :param token: token with acl:write capability
107 :param secret_id: Optional secret ID of the token to update
108 :param description: Optional new token description
109 :return: The updated token information
110 """
111
112 json_data = {"AccessorID": accessor_id}
113 if secret_id:
114 json_data["SecretID"] = secret_id
115 if description:
116 json_data["Description"] = description
117 headers = self.agent.prepare_headers(token)
118 return self.agent.http.put(
119 CB.json(),
120 f"/v1/acl/token/{accessor_id}",
121 headers=headers,
122 data=json.dumps(json_data),
123 )