Skip to content

Commit 1f5aefd

Browse files
azotcsitSam Tunnicliffe
authored andcommitted
Add JMX auth test
Patch by Aleksei Zotov; reviewed by Sam Tunnicliffe for CASSANDRA-16404
1 parent 0ef8be4 commit 1f5aefd

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

auth_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import pytest
99
import logging
1010

11-
from cassandra import AuthenticationFailed, InvalidRequest, Unauthorized, Unavailable
11+
from cassandra import AuthenticationFailed, InvalidRequest, Unauthorized
1212
from cassandra.cluster import NoHostAvailable
13-
from cassandra.protocol import ServerError, SyntaxException
13+
from cassandra.protocol import SyntaxException
1414

1515
from dtest_setup_overrides import DTestSetupOverrides
1616
from dtest import Tester
@@ -24,6 +24,7 @@
2424
since = pytest.mark.since
2525
logger = logging.getLogger(__name__)
2626

27+
2728
class TestAuth(Tester):
2829

2930
@pytest.fixture(autouse=True)
@@ -3047,8 +3048,7 @@ def fixture_setup_auth(self, fixture_dtest_setup):
30473048
fixture_dtest_setup.superuser.execute("CREATE TABLE ks.tbl (k int primary key, v int)")
30483049

30493050
def username(self):
3050-
return ''.join(random.choice(string.ascii_lowercase) for _ in range(8));
3051-
3051+
return ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
30523052

30533053
def create_user(self, query_fmt, username):
30543054
"""

jmx_auth_test.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import random
2+
import string
13
import pytest
24
import logging
35
from distutils.version import LooseVersion
@@ -12,12 +14,14 @@
1214

1315
@since('3.6')
1416
class TestJMXAuth(Tester):
17+
"""
18+
Uses nodetool as a means of exercising the JMX interface as JolokiaAgent
19+
exposes its own connector which bypasses the in-built security features
20+
"""
1521

1622
def test_basic_auth(self):
1723
"""
1824
Some basic smoke testing of JMX authentication and authorization.
19-
Uses nodetool as a means of exercising the JMX interface as JolokiaAgent
20-
exposes its own connector which bypasses the in-built security features
2125
@jira_ticket CASSANDRA-10091
2226
"""
2327
self.prepare()
@@ -55,6 +59,42 @@ def test_basic_auth(self):
5559
# superuser status applies to JMX authz too
5660
node.nodetool('-u cassandra -pw cassandra gossipinfo')
5761

62+
@since('4.1')
63+
def test_revoked_jmx_access(self):
64+
"""
65+
if a user's access to a JMX MBean is revoked while they're connected,
66+
all of their requests should fail once the cache is cleared.
67+
@jira_ticket CASSANDRA-16404
68+
"""
69+
self.prepare(permissions_validity=60000)
70+
[node] = self.cluster.nodelist()
71+
72+
def test_revoked_access(cache_name):
73+
logger.debug('Testing with cache name: %s' % cache_name)
74+
username = self.username()
75+
session = self.patient_cql_connection(node, user='cassandra', password='cassandra')
76+
session.execute("CREATE ROLE %s WITH LOGIN=true AND PASSWORD='abc123'" % username)
77+
session.execute("GRANT SELECT ON MBEAN 'org.apache.cassandra.net:type=FailureDetector' TO %s" % username)
78+
session.execute("GRANT DESCRIBE ON ALL MBEANS TO %s" % username)
79+
80+
# works fine
81+
node.nodetool('-u %s -pw abc123 gossipinfo' % username)
82+
83+
session.execute("REVOKE SELECT ON MBEAN 'org.apache.cassandra.net:type=FailureDetector' FROM %s" % username)
84+
# works fine because the JMX permission is cached
85+
node.nodetool('-u %s -pw abc123 gossipinfo' % username)
86+
87+
node.nodetool('-u cassandra -pw cassandra invalidatejmxpermissionscache')
88+
# the user has no permissions to the JMX resource anymore
89+
with pytest.raises(ToolError, match='Access Denied'):
90+
node.nodetool('-u %s -pw abc123 gossipinfo' % username)
91+
92+
test_revoked_access("JmxPermissionsCache")
93+
94+
# deprecated cache name, scheduled for removal in 5.0
95+
if self.dtest_config.cassandra_version_from_build < '5.0':
96+
test_revoked_access("JMXPermissionsCache")
97+
5898
def prepare(self, nodes=1, permissions_validity=0):
5999
config = {'authenticator': 'org.apache.cassandra.auth.PasswordAuthenticator',
60100
'authorizer': 'org.apache.cassandra.auth.CassandraAuthorizer',
@@ -69,3 +109,6 @@ def prepare(self, nodes=1, permissions_validity=0):
69109
def authentication_fail_message(self, node, username):
70110
return "Provided username {user} and/or password are incorrect".format(user=username) \
71111
if node.cluster.version() >= LooseVersion('3.10') else "Username and/or password are incorrect"
112+
113+
def username(self):
114+
return ''.join(random.choice(string.ascii_lowercase) for _ in range(8))

0 commit comments

Comments
 (0)