From 02689668a35e55fd523830ece514b22dd58c7c2e Mon Sep 17 00:00:00 2001 From: Antoine Barbare Date: Mon, 10 Sep 2018 16:23:10 +0200 Subject: [PATCH 1/3] feat: Add ACL token on create session method --- consul/base.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/consul/base.py b/consul/base.py index ee6ab254..1aad5819 100755 --- a/consul/base.py +++ b/consul/base.py @@ -358,6 +358,7 @@ class Event(object): practice, this means you cannot rely on the order of message delivery. An advantage however is that events can still be used even in the absence of server nodes or during an outage.""" + def __init__(self, agent): self.agent = agent @@ -1716,7 +1717,8 @@ def create( lock_delay=15, behavior='release', ttl=None, - dc=None): + dc=None, + token=None): """ Creates a new session. There is more documentation for sessions `here `_. @@ -1747,12 +1749,16 @@ def create( By default the session will be created in the current datacenter but an optional *dc* can be provided. + *token* is an optional `ACL token`_ to apply to this request. + Returns the string *session_id* for the session. """ params = [] dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + if token: + params.append(('token', token)) data = {} if name: data['name'] = name @@ -1780,22 +1786,26 @@ def create( params=params, data=data) - def destroy(self, session_id, dc=None): + def destroy(self, session_id, dc=None, token=None): """ Destroys the session *session_id* + *token* is an optional `ACL token`_ to apply to this request. + Returns *True* on success. """ params = [] dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + if token: + params.append(('token', token)) return self.agent.http.put( CB.bool(), '/v1/session/destroy/%s' % session_id, params=params) - def list(self, index=None, wait=None, consistency=None, dc=None): + def list(self, index=None, wait=None, consistency=None, dc=None, token=None): """ Returns a tuple of (*index*, *sessions*) of all active sessions in the *dc* datacenter. *dc* defaults to the current datacenter of @@ -1812,6 +1822,8 @@ def list(self, index=None, wait=None, consistency=None, dc=None): not specified *consistency* will the consistency level this client was configured with. + *token* is an optional `ACL token`_ to apply to this request. + The response looks like this:: (index, [ @@ -1831,6 +1843,8 @@ def list(self, index=None, wait=None, consistency=None, dc=None): dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + if token: + params.append(('token', token)) if index: params.append(('index', index)) if wait: @@ -1841,7 +1855,7 @@ def list(self, index=None, wait=None, consistency=None, dc=None): return self.agent.http.get( CB.json(index=True), '/v1/session/list', params=params) - def node(self, node, index=None, wait=None, consistency=None, dc=None): + def node(self, node, index=None, wait=None, consistency=None, dc=None, token=None): """ Returns a tuple of (*index*, *sessions*) as per *session.list*, but filters the sessions returned to only those active for *node*. @@ -1856,11 +1870,15 @@ def node(self, node, index=None, wait=None, consistency=None, dc=None): *consistency* can be either 'default', 'consistent' or 'stale'. if not specified *consistency* will the consistency level this client was configured with. + + *token* is an optional `ACL token`_ to apply to this request. """ params = [] dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + if token: + params.append(('token', token)) if index: params.append(('index', index)) if wait: @@ -1877,7 +1895,8 @@ def info(self, index=None, wait=None, consistency=None, - dc=None): + dc=None, + token=None): """ Returns a tuple of (*index*, *session*) for the session *session_id* in the *dc* datacenter. *dc* defaults to the current @@ -1893,11 +1912,15 @@ def info(self, *consistency* can be either 'default', 'consistent' or 'stale'. if not specified *consistency* will the consistency level this client was configured with. + + *token* is an optional `ACL token`_ to apply to this request. """ params = [] dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + if token: + params.append(('token', token)) if index: params.append(('index', index)) if wait: @@ -1910,7 +1933,7 @@ def info(self, '/v1/session/info/%s' % session_id, params=params) - def renew(self, session_id, dc=None): + def renew(self, session_id, dc=None, token=None): """ This is used with sessions that have a TTL, and it extends the expiration by the TTL. @@ -1918,12 +1941,16 @@ def renew(self, session_id, dc=None): *dc* is the optional datacenter that you wish to communicate with. If None is provided, defaults to the agent's datacenter. + *token* is an optional `ACL token`_ to apply to this request. + Returns the session. """ params = [] dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + if token: + params.append(('token', token)) return self.agent.http.put( CB.json(one=True, allow_404=False), '/v1/session/renew/%s' % session_id, From 5cbe163d6a9bfd2c3bd1848f8ab9abbc20fb30d7 Mon Sep 17 00:00:00 2001 From: Antoine Barbare Date: Tue, 11 Sep 2018 11:58:30 +0200 Subject: [PATCH 2/3] pep8 --- consul/base.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/consul/base.py b/consul/base.py index 1aad5819..6f89e3e8 100755 --- a/consul/base.py +++ b/consul/base.py @@ -466,6 +466,7 @@ class KV(object): used to store service configurations or other meta data in a simple way. """ + def __init__(self, agent): self.agent = agent @@ -663,6 +664,7 @@ class Txn(object): The Transactions endpoints manage updates or fetches of multiple keys inside a single, atomic transaction. """ + def __init__(self, agent): self.agent = agent @@ -698,6 +700,7 @@ class Agent(object): takes on the burden of registering with the Catalog and performing anti-entropy to recover from outages. """ + def __init__(self, agent): self.agent = agent self.service = Consul.Agent.Service(agent) @@ -1805,7 +1808,13 @@ def destroy(self, session_id, dc=None, token=None): '/v1/session/destroy/%s' % session_id, params=params) - def list(self, index=None, wait=None, consistency=None, dc=None, token=None): + def list( + self, + index=None, + wait=None, + consistency=None, + dc=None, + token=None): """ Returns a tuple of (*index*, *sessions*) of all active sessions in the *dc* datacenter. *dc* defaults to the current datacenter of @@ -1855,7 +1864,14 @@ def list(self, index=None, wait=None, consistency=None, dc=None, token=None): return self.agent.http.get( CB.json(index=True), '/v1/session/list', params=params) - def node(self, node, index=None, wait=None, consistency=None, dc=None, token=None): + def node( + self, + node, + index=None, + wait=None, + consistency=None, + dc=None, + token=None): """ Returns a tuple of (*index*, *sessions*) as per *session.list*, but filters the sessions returned to only those active for *node*. @@ -2138,6 +2154,7 @@ class Status(object): The Status endpoints are used to get information about the status of the Consul cluster. """ + def __init__(self, agent): self.agent = agent From 088b67c61ef9934d3cd1dc0182d0ce4e8c507f72 Mon Sep 17 00:00:00 2001 From: Antoine Barbare Date: Fri, 20 Dec 2019 10:38:45 +0100 Subject: [PATCH 3/3] fix: use token if already set at agent level --- consul/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/consul/base.py b/consul/base.py index 6f89e3e8..11a21d5d 100755 --- a/consul/base.py +++ b/consul/base.py @@ -1760,6 +1760,7 @@ def create( dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + token = token or self.agent.token if token: params.append(('token', token)) data = {} @@ -1801,6 +1802,7 @@ def destroy(self, session_id, dc=None, token=None): dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + token = token or self.agent.token if token: params.append(('token', token)) return self.agent.http.put( @@ -1852,6 +1854,7 @@ def list( dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + token = token or self.agent.token if token: params.append(('token', token)) if index: @@ -1893,6 +1896,7 @@ def node( dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + token = token or self.agent.token if token: params.append(('token', token)) if index: @@ -1935,6 +1939,7 @@ def info(self, dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + token = token or self.agent.token if token: params.append(('token', token)) if index: @@ -1965,6 +1970,7 @@ def renew(self, session_id, dc=None, token=None): dc = dc or self.agent.dc if dc: params.append(('dc', dc)) + token = token or self.agent.token if token: params.append(('token', token)) return self.agent.http.put(