40
40
import org .apache .cassandra .db .Keyspace ;
41
41
import org .apache .cassandra .db .marshal .UTF8Type ;
42
42
import org .apache .cassandra .exceptions .ConfigurationException ;
43
+ import org .apache .cassandra .exceptions .UnauthorizedException ;
43
44
import org .apache .cassandra .service .ClientState ;
45
+ import org .assertj .core .api .Assertions ;
44
46
45
47
import static org .apache .cassandra .auth .AuthKeyspace .NETWORK_PERMISSIONS ;
46
48
import static org .apache .cassandra .auth .RoleTestUtils .LocalCassandraRoleManager ;
@@ -113,7 +115,19 @@ private static ClientState getClientState()
113
115
return state ;
114
116
}
115
117
118
+ private static ClientState getClientState (String role )
119
+ {
120
+ ClientState state = ClientState .forInternalCalls ();
121
+ state .login (new AuthenticatedUser (role ));
122
+ return state ;
123
+ }
124
+
116
125
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 )
117
131
{
118
132
CQLStatement statement = QueryProcessor .parseStatement (String .format (query , args )).prepare (ClientState .forInternalCalls ());
119
133
assert statement instanceof CreateRoleStatement
@@ -123,7 +137,8 @@ private static void auth(String query, Object... args)
123
137
124
138
// invalidate roles cache so that any changes to the underlying roles are picked up
125
139
Roles .clearCache ();
126
- authStmt .execute (getClientState ());
140
+ authStmt .authorize (clientState );
141
+ authStmt .execute (clientState );
127
142
}
128
143
129
144
private static DCPermissions dcPerms (String username )
@@ -170,6 +185,27 @@ public void alter()
170
185
assertDcPermRow (username );
171
186
}
172
187
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
+
173
209
@ Test
174
210
public void drop ()
175
211
{
0 commit comments