Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

add grant listing and granting admin #322

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@ def delete_series(self, database=None, measurement=None, tags=None):
for k, v in tags.items()])
self.query(query_str, database=database)

def grant_admin_privileges(self, username):
"""Grant administration privileges to an existing user.

:param username: the username to grant admin privileges to
:type username: str

.. note:: Only a cluster administrator can manage user privileges.
"""
text = "GRANT ALL PRIVILEGES TO {0}".format(username)
self.query(text)

def revoke_admin_privileges(self, username):
"""Revoke cluster administration privileges from an user.

Expand Down Expand Up @@ -756,6 +767,26 @@ def revoke_privilege(self, privilege, database, username):
username)
self.query(text)

def get_list_grants(self, username):
"""Get the list of privileges granted to a user.

:param username: the username whose grants to list
:type username: str

:returns: all privileges granted to the user in InfluxDB
:rtype: list of dictionaries

:Example:

::

>> grants = client.get_list_grants("todd")
>> grants
[{u'database': u'db1', u'privilege': u'READ'}, {u'database': u'db2', u'privilege': u'ALL PRIVILEGES'}]
"""
text = "SHOW GRANTS FOR {0}".format(username)
return list(self.query(text).get_points())

def send_packet(self, packet):
"""Send an UDP packet.

Expand Down Expand Up @@ -803,7 +834,7 @@ def __init__(self,
):
self.clients = [self] # Keep it backwards compatible
self.hosts = hosts
self.bad_hosts = [] # Corresponding server has failures in history
self.bad_hosts = [] # Corresponding server has failures in history
self.shuffle = shuffle
self.healing_delay = healing_delay
self._last_healing = time.time()
Expand Down
45 changes: 45 additions & 0 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,28 @@ def test_get_list_users_empty(self):

self.assertListEqual(self.cli.get_list_users(), [])

@raises(Exception)
def test_grant_admin_privileges_invalid(self):
cli = InfluxDBClient('host', 8086, 'username', 'password')
with _mocked_session(cli, 'get', 400):
self.cli.grant_admin_privileges('')

def test_grant_admin_privileges(self):
example_response = '{"results":[{}]}'

with requests_mock.Mocker() as m:
m.register_uri(
requests_mock.GET,
"http://localhost:8086/query",
text=example_response
)
self.cli.grant_admin_privileges('test')

self.assertEqual(
m.last_request.qs['q'][0],
'grant all privileges to test'
)

def test_revoke_admin_privileges(self):
example_response = '{"results":[{}]}'

Expand Down Expand Up @@ -819,6 +841,29 @@ def test_invalid_port_fails(self):
with self.assertRaises(ValueError):
InfluxDBClient('host', '80/redir', 'username', 'password')

def test_get_list_grants(self):
data = {'results': [
{'series': [
{'columns': ['database', 'privilege'],
'values': [
['db1', 'READ'],
['db2', 'ALL PRIVILEGES']]}
]}
]}

with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
self.assertListEqual(
self.cli.get_list_grants('test'),
[{'database': 'db1', 'privilege': 'READ'},
{'database': 'db2', 'privilege': 'ALL PRIVILEGES'}, ]
)

@raises(Exception)
def test_get_list_grants_fails(self):
cli = InfluxDBClient('host', 8086, 'username', 'password')
with _mocked_session(cli, 'get', 401):
cli.get_list_grants('test')


class FakeClient(InfluxDBClient):

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy