Skip to content

Commit 6207a30

Browse files
committed
Tighten up permissions in dc authorizers
patch by Stefan Miklosovic; reviewed by Francisco Guerrero for CASSANDRA-20225
1 parent 3ddccf4 commit 6207a30

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
4.0.16
2+
* Tighten up permissions in dc authorizers (CASSANDRA-20225)
23
* CBUtil serialization of UTF8 does not handle all UTF8 properly (CASSANDRA-20234)
34
* Make hint expiry use request start time rather than timeout time for TTL (CASSANDRA-20014)
45
* Do not attach rows and partitions to CheckForAbort when already attached (CASSANDRA-20135)

src/java/org/apache/cassandra/cql3/statements/AlterRoleStatement.java

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public void authorize(ClientState state) throws UnauthorizedException
7878
if (opts.getSuperuser().isPresent() && !isSuper)
7979
throw new UnauthorizedException("Only superusers are allowed to alter superuser status");
8080

81+
if (dcPermissions != null && !isSuper)
82+
throw new UnauthorizedException("Only superusers are allowed to alter access to datacenters.");
83+
8184
// superusers can do whatever else they like
8285
if (isSuper)
8386
return;

test/unit/org/apache/cassandra/auth/CassandraNetworkAuthorizerTest.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.apache.cassandra.db.Keyspace;
4141
import org.apache.cassandra.db.marshal.UTF8Type;
4242
import org.apache.cassandra.exceptions.ConfigurationException;
43+
import org.apache.cassandra.exceptions.UnauthorizedException;
4344
import org.apache.cassandra.service.ClientState;
45+
import org.assertj.core.api.Assertions;
4446

4547
import static org.apache.cassandra.auth.AuthKeyspace.NETWORK_PERMISSIONS;
4648
import static org.apache.cassandra.auth.RoleTestUtils.LocalCassandraRoleManager;
@@ -113,7 +115,19 @@ private static ClientState getClientState()
113115
return state;
114116
}
115117

118+
private static ClientState getClientState(String role)
119+
{
120+
ClientState state = ClientState.forInternalCalls();
121+
state.login(new AuthenticatedUser(role));
122+
return state;
123+
}
124+
116125
private static void auth(String query, Object... args)
126+
{
127+
auth(query, getClientState(), args);
128+
}
129+
130+
private static void auth(String query, ClientState clientState, Object... args)
117131
{
118132
CQLStatement statement = QueryProcessor.parseStatement(String.format(query, args)).prepare(ClientState.forInternalCalls());
119133
assert statement instanceof CreateRoleStatement
@@ -123,7 +137,8 @@ private static void auth(String query, Object... args)
123137

124138
// invalidate roles cache so that any changes to the underlying roles are picked up
125139
Roles.clearCache();
126-
authStmt.execute(getClientState());
140+
authStmt.authorize(clientState);
141+
authStmt.execute(clientState);
127142
}
128143

129144
private static DCPermissions dcPerms(String username)
@@ -170,6 +185,27 @@ public void alter()
170185
assertDcPermRow(username);
171186
}
172187

188+
@Test
189+
public void alterAsUser()
190+
{
191+
String username = createName();
192+
193+
assertNoDcPermRow(username);
194+
auth("CREATE ROLE %s WITH PASSWORD = 'password' AND LOGIN = true AND ACCESS TO DATACENTERS {'dc1'}", username);
195+
Assert.assertEquals(DCPermissions.subset("dc1"), dcPerms(username));
196+
assertDcPermRow(username, "dc1");
197+
198+
// try to alter as a user
199+
ClientState userState = getClientState(username);
200+
Assertions.assertThatThrownBy(() -> auth("ALTER ROLE %s WITH ACCESS TO DATACENTERS {'dc1', 'dc2'}", userState, username))
201+
.hasMessage("Only superusers are allowed to alter access to datacenters.")
202+
.isInstanceOf(UnauthorizedException.class);
203+
204+
// nothing changed
205+
Assert.assertEquals(DCPermissions.subset("dc1"), dcPerms(username));
206+
assertDcPermRow(username, "dc1");
207+
}
208+
173209
@Test
174210
public void drop()
175211
{

0 commit comments

Comments
 (0)