diff --git a/influxdb/client.py b/influxdb/client.py index be28a39e..bd336f8b 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -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. @@ -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. @@ -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() diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index 90f6964a..a8a184cb 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -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":[{}]}' @@ -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):
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: