Skip to content

Commit 731c4e0

Browse files
committed
Add revoke_privilege method to the client.
1 parent 36e50ea commit 731c4e0

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

influxdb/client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,22 @@ def grant_privilege(self, privilege, database, username):
658658
username)
659659
self.query(text)
660660

661+
def revoke_privilege(self, privilege, database, username):
662+
"""Revoke a privilege on a database from an user.
663+
664+
:param privilege: the privilege to revoke, one of 'read', 'write'
665+
or 'all'. The string is case-insensitive
666+
:type privilege: str
667+
:param database: the database to revoke the privilege on
668+
:type database: str
669+
:param username: the username to revoke the privilege from
670+
:type username: str
671+
"""
672+
text = "REVOKE {} ON {} FROM {}".format(privilege,
673+
database,
674+
username)
675+
self.query(text)
676+
661677
def send_packet(self, packet):
662678
"""Send an UDP packet.
663679

tests/influxdb/client_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,28 @@ def test_grant_privilege_invalid(self):
690690
with _mocked_session(cli, 'get', 400):
691691
self.cli.grant_privilege('', 'testdb', 'test')
692692

693+
def test_revoke_privilege(self):
694+
example_response = '{"results":[{}]}'
695+
696+
with requests_mock.Mocker() as m:
697+
m.register_uri(
698+
requests_mock.GET,
699+
"http://localhost:8086/query",
700+
text=example_response
701+
)
702+
self.cli.revoke_privilege('read', 'testdb', 'test')
703+
704+
self.assertEqual(
705+
m.last_request.qs['q'][0],
706+
'revoke read on testdb from test'
707+
)
708+
709+
@raises(Exception)
710+
def test_revoke_privilege_invalid(self):
711+
cli = InfluxDBClient('host', 8086, 'username', 'password')
712+
with _mocked_session(cli, 'get', 400):
713+
self.cli.revoke_privilege('', 'testdb', 'test')
714+
693715

694716
class FakeClient(InfluxDBClient):
695717
fail = False

tests/influxdb/client_test_with_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,21 @@ def test_grant_privilege_invalid(self):
422422
self.assertIn('{"error":"error parsing query: ',
423423
ctx.exception.content)
424424

425+
def test_revoke_privilege(self):
426+
self.cli.create_user('test', 'test')
427+
self.cli.create_database('testdb')
428+
self.cli.revoke_privilege('all', 'testdb', 'test')
429+
# TODO: when supported by InfluxDB, check if privileges are revoked
430+
431+
def test_revoke_privilege_invalid(self):
432+
self.cli.create_user('test', 'test')
433+
self.cli.create_database('testdb')
434+
with self.assertRaises(InfluxDBClientError) as ctx:
435+
self.cli.revoke_privilege('', 'testdb', 'test')
436+
self.assertEqual(400, ctx.exception.code)
437+
self.assertIn('{"error":"error parsing query: ',
438+
ctx.exception.content)
439+
425440

426441
############################################################################
427442

0 commit comments

Comments
 (0)