-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AuthProceduresLoggingTest.java
539 lines (450 loc) · 18.6 KB
/
AuthProceduresLoggingTest.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.security.enterprise.auth;
import java.io.IOException;
import java.util.stream.Stream;
import org.apache.shiro.mgt.SecurityManager;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.function.ThrowingAction;
import org.neo4j.graphdb.security.AuthorizationViolationException;
import org.neo4j.kernel.api.security.AuthSubject;
import org.neo4j.kernel.api.security.exception.InvalidArgumentsException;
import org.neo4j.kernel.impl.enterprise.SecurityLog;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.server.security.auth.AuthenticationStrategy;
import org.neo4j.server.security.auth.BasicPasswordPolicy;
import org.neo4j.server.security.auth.InMemoryUserRepository;
import org.neo4j.test.assertion.Assert;
import static org.mockito.Mockito.mock;
import static org.neo4j.logging.AssertableLogProvider.inLog;
import static org.neo4j.server.security.enterprise.auth.AuthProcedures.PERMISSION_DENIED;
import static org.neo4j.server.security.enterprise.auth.PredefinedRolesBuilder.ADMIN;
import static org.neo4j.server.security.enterprise.auth.PredefinedRolesBuilder.ARCHITECT;
import static org.neo4j.server.security.enterprise.auth.PredefinedRolesBuilder.READER;
public class AuthProceduresLoggingTest
{
private AuthProcedures authProcedures;
private AssertableLogProvider log = null;
private AuthSubject matsSubject = null;
@Before
public void setUp() throws Throwable
{
EnterpriseUserManager userManager = getUserManager();
AuthSubject adminSubject = new TestAuthSubject( "admin", true, userManager );
matsSubject = new TestAuthSubject( "mats", false, userManager );
log = new AssertableLogProvider();
authProcedures = new TestAuthProcedures();
authProcedures.authSubject = adminSubject;
authProcedures.securityLog = new SecurityLog( log.getLog( getClass() ) );
authProcedures.graph = mock( GraphDatabaseAPI.class );
}
private EnterpriseUserManager getUserManager() throws Throwable
{
InternalFlatFileRealm realm = new InternalFlatFileRealm( new InMemoryUserRepository(),
new InMemoryRoleRepository(),
new BasicPasswordPolicy(),
mock( AuthenticationStrategy.class ) );
realm.start(); // creates default user and roles
return realm;
}
@Test
public void shouldLogCreatingUser() throws Throwable
{
authProcedures.createUser( "andres", "el password", true );
authProcedures.createUser( "mats", "el password", false );
log.assertExactly(
info( "[admin]: created user `%s`", "andres" ),
info( "[admin]: created user `%s`", "mats" ) );
}
@Test
public void shouldLogFailureToCreateUser() throws Throwable
{
catchInvalidArguments( () -> authProcedures.createUser( null, "pw", true ) );
catchInvalidArguments( () -> authProcedures.createUser( "", "pw", true ) );
catchInvalidArguments( () -> authProcedures.createUser( "andres", "", true ) );
catchInvalidArguments( () -> authProcedures.createUser( "mats", null, true ) );
catchInvalidArguments( () -> authProcedures.createUser( "neo4j", "nonEmpty", true ) );
log.assertExactly(
error( "[admin]: tried to create user `%s`: %s", null, "The provided username is empty." ),
error( "[admin]: tried to create user `%s`: %s", "", "The provided username is empty." ),
error( "[admin]: tried to create user `%s`: %s", "andres", "A password cannot be empty." ),
error( "[admin]: tried to create user `%s`: %s", "mats", "A password cannot be empty." ),
error( "[admin]: tried to create user `%s`: %s", "neo4j", "The specified user 'neo4j' already exists." )
);
}
@Test
public void shouldLogUnauthorizedCreatingUser() throws Throwable
{
authProcedures.authSubject = matsSubject;
catchAuthorizationViolation( () -> authProcedures.createUser( "andres", "", true ) );
log.assertExactly( error( "[mats]: tried to create user `%s`: %s", "andres", PERMISSION_DENIED ) );
}
@Test
public void shouldLogDeletingUser() throws Throwable
{
authProcedures.createUser( "andres", "el password", false );
authProcedures.deleteUser( "andres" );
log.assertExactly(
info( "[admin]: created user `%s`", "andres" ),
info( "[admin]: deleted user `%s`", "andres" ) );
}
@Test
public void shouldLogDeletingNonExistentUser() throws Throwable
{
catchInvalidArguments( () -> authProcedures.deleteUser( "andres" ) );
log.assertExactly( error( "[admin]: tried to delete user `%s`: %s", "andres", "User 'andres' does not exist." ) );
}
@Test
public void shouldLogUnauthorizedDeleteUser() throws Throwable
{
authProcedures.authSubject = matsSubject;
catchAuthorizationViolation( () -> authProcedures.deleteUser( ADMIN ) );
log.assertExactly( error( "[mats]: tried to delete user `%s`: %s", ADMIN, PERMISSION_DENIED ) );
}
@Test
public void shouldLogAddingRoleToUser() throws Throwable
{
authProcedures.createUser( "mats", "neo4j", false );
authProcedures.addRoleToUser( ARCHITECT, "mats" );
log.assertExactly(
info( "[admin]: created user `%s`", "mats" ),
info( "[admin]: added role `%s` to user `%s`", ARCHITECT, "mats" ) );
}
@Test
public void shouldLogFailureToAddRoleToUser() throws Throwable
{
authProcedures.createUser( "mats", "neo4j", false );
catchInvalidArguments( () -> authProcedures.addRoleToUser( "null", "mats" ) );
log.assertExactly(
info( "[admin]: created user `%s`", "mats" ),
error( "[admin]: tried to add role `%s` to user `%s`: %s", "null", "mats", "Role 'null' does not exist." ) );
}
@Test
public void shouldLogUnauthorizedAddingRole() throws Throwable
{
authProcedures.authSubject = matsSubject;
catchAuthorizationViolation( () -> authProcedures.addRoleToUser( ADMIN, "mats" ) );
log.assertExactly( error( "[mats]: tried to add role `%s` to user `%s`: %s", ADMIN, "mats", PERMISSION_DENIED ) );
}
@Test
public void shouldLogRemovalOfRoleFromUser() throws Throwable
{
// Given
authProcedures.createUser( "mats", "neo4j", false );
authProcedures.addRoleToUser( READER, "mats" );
log.clear();
// When
authProcedures.removeRoleFromUser( READER, "mats" );
// Then
log.assertExactly( info( "[admin]: removed role `%s` from user `%s`", READER, "mats" ) );
}
@Test
public void shouldLogFailureToRemoveRoleFromUser() throws Throwable
{
// Given
authProcedures.createUser( "mats", "neo4j", false );
authProcedures.addRoleToUser( READER, "mats" );
log.clear();
// When
catchInvalidArguments( () -> authProcedures.removeRoleFromUser( "notReader", "mats" ) );
catchInvalidArguments( () -> authProcedures.removeRoleFromUser( READER, "notMats" ) );
// Then
log.assertExactly(
error( "[admin]: tried to remove role `%s` from user `%s`: %s", "notReader", "mats", "Role 'notReader' does not exist." ),
error( "[admin]: tried to remove role `%s` from user `%s`: %s", READER, "notMats", "User 'notMats' does not exist." )
);
}
@Test
public void shouldLogUnauthorizedRemovingRole() throws Throwable
{
authProcedures.authSubject = matsSubject;
catchAuthorizationViolation( () -> authProcedures.removeRoleFromUser( ADMIN, ADMIN ) );
log.assertExactly( error( "[mats]: tried to remove role `%s` from user `%s`: %s", ADMIN, ADMIN, PERMISSION_DENIED ) );
}
@Test
public void shouldLogPasswordChanges() throws IOException, InvalidArgumentsException
{
// Given
authProcedures.createUser( "mats", "neo4j", true );
log.clear();
// When
authProcedures.changeUserPassword( "mats", "longerPassword", false );
authProcedures.authSubject = matsSubject;
authProcedures.changeUserPassword( "mats", "evenLongerPassword", false );
// Then
log.assertExactly(
info( "[admin]: changed password for user `%s`", "mats" ),
info( "[mats]: changed own password" )
);
}
@Test
public void shouldLogFailureToChangePassword() throws Throwable
{
// Given
authProcedures.createUser( "andres", "neo4j", true );
log.clear();
// When
catchInvalidArguments( () -> authProcedures.changeUserPassword( "andres", "neo4j", false ) );
catchInvalidArguments( () -> authProcedures.changeUserPassword( "andres", "", false ) );
catchInvalidArguments( () -> authProcedures.changeUserPassword( "notAndres", "good password", false ) );
// Then
log.assertExactly(
error( "[admin]: tried to change %s: %s", "password for user `andres`", "Old password and new password cannot be the same." ),
error( "[admin]: tried to change %s: %s", "password for user `andres`", "A password cannot be empty." ),
error( "[admin]: tried to change %s: %s", "password for user `notAndres`", "User 'notAndres' does not exist." )
);
}
@Test
public void shouldLogFailureToChangeOwnPassword() throws Throwable
{
// Given
authProcedures.createUser( "mats", "neo4j", true );
authProcedures.authSubject = matsSubject;
log.clear();
// When
catchInvalidArguments( () -> authProcedures.changeUserPassword( "mats", "neo4j", false ) );
catchInvalidArguments( () -> authProcedures.changeUserPassword( "mats", "", false ) );
// Then
log.assertExactly(
error( "[mats]: tried to change %s: %s", "own password", "Old password and new password cannot be the same." ),
error( "[mats]: tried to change %s: %s", "own password", "A password cannot be empty." )
);
}
@Test
public void shouldLogUnauthorizedChangePassword() throws Throwable
{
// Given
authProcedures.createUser( "andres", "neo4j", true );
log.clear();
authProcedures.authSubject = matsSubject;
// When
catchAuthorizationViolation( () -> authProcedures.changeUserPassword( "andres", "otherPw", false ) );
// Then
log.assertExactly(
error( "[mats]: tried to change %s: %s", "password for user `andres`", PERMISSION_DENIED )
);
}
@Test
public void shouldLogSuspendUser() throws Throwable
{
// Given
authProcedures.createUser( "mats", "neo4j", false );
log.clear();
// When
authProcedures.suspendUser( "mats" );
authProcedures.suspendUser( "mats" );
// Then
log.assertExactly(
info( "[admin]: suspended user `%s`", "mats" ),
info( "[admin]: suspended user `%s`", "mats" )
);
}
@Test
public void shouldLogFailureToSuspendUser() throws Throwable
{
// Given
authProcedures.createUser( "mats", "neo4j", false );
log.clear();
// When
catchInvalidArguments( () -> authProcedures.suspendUser( "notMats" ) );
catchInvalidArguments( () -> authProcedures.suspendUser( ADMIN ) );
// Then
log.assertExactly(
error( "[admin]: tried to suspend user `%s`: %s", "notMats", "User 'notMats' does not exist." ),
error( "[admin]: tried to suspend user `%s`: %s", "admin", "Suspending yourself (user 'admin') is not allowed." )
);
}
@Test
public void shouldLogUnauthorizedSuspendUser() throws Throwable
{
// Given
authProcedures.authSubject = matsSubject;
// When
catchAuthorizationViolation( () -> authProcedures.suspendUser( ADMIN ) );
// Then
log.assertExactly(
error( "[mats]: tried to suspend user `%s`: %s", "admin", PERMISSION_DENIED )
);
}
@Test
public void shouldLogActivateUser() throws Throwable
{
// Given
authProcedures.createUser( "mats", "neo4j", false );
authProcedures.suspendUser( "mats" );
log.clear();
// When
authProcedures.activateUser( "mats", false );
authProcedures.activateUser( "mats", false );
// Then
log.assertExactly(
info( "[admin]: activated user `%s`", "mats" ),
info( "[admin]: activated user `%s`", "mats" )
);
}
@Test
public void shouldLogFailureToActivateUser() throws Throwable
{
// When
catchInvalidArguments( () -> authProcedures.activateUser( "notMats", false ) );
catchInvalidArguments( () -> authProcedures.activateUser( ADMIN, false ) );
// Then
log.assertExactly(
error( "[admin]: tried to activate user `%s`: %s", "notMats", "User 'notMats' does not exist." ),
error( "[admin]: tried to activate user `%s`: %s", ADMIN, "Activating yourself (user 'admin') is not allowed." )
);
}
@Test
public void shouldLogUnauthorizedActivateUser() throws Throwable
{
// Given
authProcedures.authSubject = matsSubject;
// When
catchAuthorizationViolation( () -> authProcedures.activateUser( "admin", true ) );
// Then
log.assertExactly(
error( "[mats]: tried to activate user `%s`: %s", "admin", PERMISSION_DENIED )
);
}
@Test
public void shouldLogCreatingRole() throws Throwable
{
// When
authProcedures.createRole( "role" );
// Then
log.assertExactly( info( "[admin]: created role `%s`", "role" ) );
}
@Test
public void shouldLogFailureToCreateRole() throws Throwable
{
// Given
authProcedures.createRole( "role" );
log.clear();
// When
catchInvalidArguments( () -> authProcedures.createRole( null ) );
catchInvalidArguments( () -> authProcedures.createRole( "" ) );
catchInvalidArguments( () -> authProcedures.createRole( "role" ) );
catchInvalidArguments( () -> authProcedures.createRole( "!@#$" ) );
// Then
log.assertExactly(
error( "[admin]: tried to create role `%s`: %s", null, "The provided role name is empty." ),
error( "[admin]: tried to create role `%s`: %s", "", "The provided role name is empty." ),
error( "[admin]: tried to create role `%s`: %s", "role", "The specified role 'role' already exists." ),
error( "[admin]: tried to create role `%s`: %s", "!@#$",
"Role name '!@#$' contains illegal characters. Use simple ascii characters and numbers." )
);
}
@Test
public void shouldLogUnauthorizedCreateRole() throws Exception
{
// Given
authProcedures.authSubject = matsSubject;
// When
catchAuthorizationViolation( () -> authProcedures.createRole( "role" ) );
// Then
log.assertExactly( error("[mats]: tried to create role `%s`: %s", "role", PERMISSION_DENIED) );
}
private void catchInvalidArguments( ThrowingAction<Exception> f ) throws Exception
{
Assert.assertException( f, InvalidArgumentsException.class, "" );
}
private void catchAuthorizationViolation( ThrowingAction<Exception> f ) throws Exception
{
Assert.assertException( f, AuthorizationViolationException.class, "" );
}
private AssertableLogProvider.LogMatcher info( String message, String... arguments )
{
if ( arguments.length == 0 )
{
return inLog( this.getClass() ).info( message );
}
return inLog( this.getClass() ).info( message, arguments );
}
private AssertableLogProvider.LogMatcher error( String message, String... arguments )
{
return inLog( this.getClass() ).error( message, arguments );
}
private static class TestAuthSubject extends EnterpriseAuthSubject
{
private final String name;
private final boolean isAdmin;
private final EnterpriseUserManager userManager;
TestAuthSubject( String name, boolean isAdmin, EnterpriseUserManager userManager )
{
super(null, new TestShiroSubject( name ));
this.name = name;
this.isAdmin = isAdmin;
this.userManager = userManager;
}
@Override
public String username()
{
return name;
}
@Override
public boolean isAdmin()
{
return isAdmin;
}
@Override
public EnterpriseUserManager getUserManager()
{
return userManager;
}
@Override
public boolean doesUsernameMatch( String username )
{
return name.equals( username );
}
}
private static class TestShiroSubject extends ShiroSubject
{
private final String name;
TestShiroSubject( String name )
{
super( mock( SecurityManager.class ), null );
this.name = name;
}
@Override
public Object getPrincipal()
{
return name;
}
}
private static class TestAuthProcedures extends AuthProcedures
{
@Override
void kickoutUser( String username, String reason )
{
}
@Override
Stream<TransactionTerminationResult> terminateTransactionsForValidUser( String username )
{
return Stream.empty();
}
@Override
Stream<ConnectionResult> terminateConnectionsForValidUser( String username )
{
return Stream.empty();
}
}
}