-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
AuditLoggerAuthTest.java
313 lines (277 loc) · 12.9 KB
/
AuditLoggerAuthTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.audit;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.AuthenticationException;
import com.datastax.driver.core.exceptions.SyntaxError;
import com.datastax.driver.core.exceptions.UnauthorizedException;
import org.apache.cassandra.OrderedJUnit4ClassRunner;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.config.OverrideConfigurationLoader;
import org.apache.cassandra.config.ParameterizedClass;
import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.cql3.PasswordObfuscator;
import org.apache.cassandra.locator.InetAddressAndPort;
import org.apache.cassandra.service.EmbeddedCassandraService;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* AuditLoggerAuthTest class is responsible for covering test cases for Authenticated user (LOGIN) audits.
* Non authenticated tests are covered in {@link AuditLoggerTest}
*/
@RunWith(OrderedJUnit4ClassRunner.class)
public class AuditLoggerAuthTest
{
private static EmbeddedCassandraService embedded;
private static final String TEST_USER = "testuser";
private static final String TEST_ROLE = "testrole";
private static final String TEST_PW = "testpassword";
private static final String CASS_USER = "cassandra";
private static final String CASS_PW = "cassandra";
@BeforeClass
public static void setup() throws Exception
{
OverrideConfigurationLoader.override((config) -> {
config.authenticator = "PasswordAuthenticator";
config.role_manager = "CassandraRoleManager";
config.authorizer = "CassandraAuthorizer";
config.audit_logging_options.enabled = true;
config.audit_logging_options.logger = new ParameterizedClass("InMemoryAuditLogger", null);
});
CQLTester.prepareServer();
System.setProperty("cassandra.superuser_setup_delay_ms", "0");
embedded = new EmbeddedCassandraService();
embedded.start();
executeWithCredentials(
Arrays.asList(getCreateRoleCql(TEST_USER, true, false, false),
getCreateRoleCql("testuser_nologin", false, false, false),
"CREATE KEYSPACE testks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}",
"CREATE TABLE testks.table1 (key text PRIMARY KEY, col1 int, col2 int)"),
"cassandra", "cassandra", null);
}
@AfterClass
public static void shutdown()
{
embedded.stop();
}
@Before
public void clearInMemoryLogger()
{
getInMemAuditLogger().clear();
}
@Test
public void testCqlLoginAuditing() throws Throwable
{
executeWithCredentials(Collections.emptyList(), TEST_USER, "wrongpassword",
AuditLogEntryType.LOGIN_ERROR);
assertEquals(0, getInMemAuditLogger().size());
clearInMemoryLogger();
executeWithCredentials(Collections.emptyList(), "wronguser", TEST_PW, AuditLogEntryType.LOGIN_ERROR);
assertEquals(0, getInMemAuditLogger().size());
clearInMemoryLogger();
executeWithCredentials(Collections.emptyList(), "testuser_nologin",
TEST_PW, AuditLogEntryType.LOGIN_ERROR);
assertEquals(0, getInMemAuditLogger().size());
clearInMemoryLogger();
executeWithCredentials(Collections.emptyList(), TEST_USER, TEST_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertEquals(0, getInMemAuditLogger().size());
clearInMemoryLogger();
}
@Test
public void testCqlCreateRoleAuditing()
{
createTestRole();
}
@Test
public void testCqlCreateRoleSyntaxError()
{
String createTestRoleCQL = String.format("CREATE ROLE %s WITH LOGIN = %s ANDSUPERUSER = %s AND PASSWORD",
TEST_ROLE, true, false) + CASS_PW;
String createTestRoleCQLExpected = String.format("CREATE ROLE %s WITH LOGIN = %s ANDSUPERUSER = %s AND PASSWORD",
TEST_ROLE, true, false) + PasswordObfuscator.OBFUSCATION_TOKEN;
executeWithCredentials(Arrays.asList(createTestRoleCQL), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.REQUEST_FAILURE, createTestRoleCQLExpected, CASS_USER);
assertEquals(0, getInMemAuditLogger().size());
}
@Test
public void testCqlALTERRoleAuditing()
{
createTestRole();
String cql = "ALTER ROLE " + TEST_ROLE + " WITH PASSWORD = 'foo_bar'";
executeWithCredentials(Arrays.asList(cql), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.ALTER_ROLE, "ALTER ROLE " + TEST_ROLE + " WITH PASSWORD" + PasswordObfuscator.OBFUSCATION_TOKEN, CASS_USER);
assertEquals(0, getInMemAuditLogger().size());
}
@Test
public void testCqlDROPRoleAuditing()
{
createTestRole();
String cql = "DROP ROLE " + TEST_ROLE;
executeWithCredentials(Arrays.asList(cql), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.DROP_ROLE, cql, CASS_USER);
assertEquals(0, getInMemAuditLogger().size());
}
@Test
public void testCqlLISTROLESAuditing()
{
String cql = "LIST ROLES";
executeWithCredentials(Arrays.asList(cql), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.LIST_ROLES, cql, CASS_USER);
}
@Test
public void testCqlLISTPERMISSIONSAuditing()
{
String cql = "LIST ALL";
executeWithCredentials(Arrays.asList(cql), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.LIST_PERMISSIONS, cql, CASS_USER);
}
@Test
public void testCqlGRANTAuditing()
{
createTestRole();
String cql = "GRANT SELECT ON ALL KEYSPACES TO " + TEST_ROLE;
executeWithCredentials(Arrays.asList(cql), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.GRANT, cql, CASS_USER);
}
@Test
public void testCqlREVOKEAuditing()
{
createTestRole();
String cql = "REVOKE ALTER ON ALL KEYSPACES FROM " + TEST_ROLE;
executeWithCredentials(Arrays.asList(cql), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.REVOKE, cql, CASS_USER);
}
@Test
public void testUNAUTHORIZED_ATTEMPTAuditing()
{
createTestRole();
String cql = "ALTER ROLE " + TEST_ROLE + " WITH superuser = true";
executeWithCredentials(Arrays.asList(cql), TEST_USER, TEST_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.UNAUTHORIZED_ATTEMPT, cql, TEST_USER);
assertEquals(0, getInMemAuditLogger().size());
}
/**
* Helper methods
*/
private static void executeWithCredentials(List<String> queries, String username, String password,
AuditLogEntryType expectedType)
{
boolean authFailed = false;
try (Cluster cluster = Cluster.builder().addContactPoints(InetAddress.getLoopbackAddress())
.withoutJMXReporting()
.withCredentials(username, password)
.withPort(DatabaseDescriptor.getNativeTransportPort()).build())
{
try (Session session = cluster.connect())
{
for (String query : queries)
session.execute(query);
}
catch (AuthenticationException e)
{
authFailed = true;
}
catch (UnauthorizedException ue)
{
//no-op, taken care by caller
}
catch (SyntaxError se)
{
// no-op, taken care of by caller
}
}
if (expectedType != null)
{
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertEquals(expectedType, logEntry.getType());
assertTrue(!authFailed || logEntry.getType() == AuditLogEntryType.LOGIN_ERROR);
assertSource(logEntry, username);
// drain all remaining login related events, as there's no specification how connections and login attempts
// should be handled by the driver, so we can't assert a fixed number of login events
getInMemAuditLogger()
.removeIf(auditLogEntry -> auditLogEntry.getType() == AuditLogEntryType.LOGIN_ERROR
|| auditLogEntry.getType() == AuditLogEntryType.LOGIN_SUCCESS);
}
}
private static Queue<AuditLogEntry> getInMemAuditLogger()
{
return ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue;
}
private static void assertLogEntry(AuditLogEntry logEntry, AuditLogEntryType type, String cql, String username)
{
assertSource(logEntry, username);
assertNotEquals(0, logEntry.getTimestamp());
assertEquals(type, logEntry.getType());
if (null != cql && !cql.isEmpty())
{
assertThat(logEntry.getOperation(), containsString(cql));
}
}
private static void assertSource(AuditLogEntry logEntry, String username)
{
assertEquals(InetAddressAndPort.getLoopbackAddress().address, logEntry.getSource().address);
assertTrue(logEntry.getSource().port > 0);
if (logEntry.getType() != AuditLogEntryType.LOGIN_ERROR)
assertEquals(username, logEntry.getUser());
}
private static String getCreateRoleCql(String role, boolean login, boolean superUser, boolean isPasswordObfuscated)
{
String baseQueryString = String.format("CREATE ROLE IF NOT EXISTS %s WITH LOGIN = %s AND SUPERUSER = %s AND PASSWORD", role, login, superUser);
return isPasswordObfuscated ? baseQueryString + PasswordObfuscator.OBFUSCATION_TOKEN : baseQueryString + String.format(" = '%s'", TEST_PW);
}
private static void createTestRole()
{
String createTestRoleCQL = getCreateRoleCql(TEST_ROLE, true, false, false);
executeWithCredentials(Arrays.asList(createTestRoleCQL), CASS_USER, CASS_PW, AuditLogEntryType.LOGIN_SUCCESS);
assertTrue(getInMemAuditLogger().size() > 0);
AuditLogEntry logEntry = getInMemAuditLogger().poll();
assertLogEntry(logEntry, AuditLogEntryType.CREATE_ROLE, getCreateRoleCql(TEST_ROLE, true, false, true), CASS_USER);
assertEquals(0, getInMemAuditLogger().size());
}
}