Skip to content

Commit

Permalink
Enhance Token Creation with Policy Support (#76)
Browse files Browse the repository at this point in the history
- Implement creation of policies.
- Integrate policy addition during token creation.
  • Loading branch information
cpaillet authored May 23, 2024
1 parent 93df72e commit 4042528
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
28 changes: 28 additions & 0 deletions consul/api/acl/policy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from consul.callback import CB


Expand Down Expand Up @@ -30,3 +32,29 @@ def read(self, uuid, token=None):
if token:
params.append(("token", token))
return self.agent.http.get(CB.json(), f"/v1/acl/policy/{uuid}", params=params)

def create(self, name, token=None, description=None, rules=None):
"""
Create a policy
This is a privileged endpoint, and requires a token with acl:write.
:param name: Specifies a name for the ACL policy.
:param token: token with acl:write capability
:param description: Free form human readable description of the policy.
:param rules: Specifies rules for the ACL policy.
:return: The cloned token information
"""
params = []
token = token or self.agent.token
if token:
params.append(("token", token))
json_data = {"name": name}
if rules:
json_data["rules"] = json.dumps(rules)
if description:
json_data["Description"] = description
return self.agent.http.put(
CB.json(),
"/v1/acl/policy",
params=params,
data=json.dumps(json_data),
)
6 changes: 5 additions & 1 deletion consul/api/acl/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ def clone(self, accessor_id, token=None, description=""):
data=json.dumps(json_data),
)

def create(self, token=None, accessor_id=None, secret_id=None, description=""):
def create(self, token=None, accessor_id=None, secret_id=None, policies_id=None, description=""):
"""
Create a token (optionally identified by *secret_id* and *accessor_id*).
This is a privileged endpoint, and requires a token with acl:write.
:param token: token with acl:write capability
:param accessor_id: The accessor ID of the token to create
:param secret_id: The secret ID of the token to create
:param description: Optional new token description
:param policies: Optional list of policies id
:return: The cloned token information
"""
params = []
Expand All @@ -89,6 +90,9 @@ def create(self, token=None, accessor_id=None, secret_id=None, description=""):
json_data["SecretID"] = secret_id
if description:
json_data["Description"] = description
if policies_id:
json_data["Policies"] = [{"ID": policy} for policy in policies_id]

return self.agent.http.put(
CB.json(),
"/v1/acl/token",
Expand Down

0 comments on commit 4042528

Please sign in to comment.