-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AuthProceduresTest.java
744 lines (650 loc) · 31.3 KB
/
AuthProceduresTest.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
* 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 org.junit.Test;
import java.util.Map;
import org.neo4j.graphdb.QueryExecutionException;
import org.neo4j.kernel.api.security.AuthenticationResult;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.neo4j.helpers.collection.MapUtil.map;
import static org.neo4j.server.security.auth.SecurityTestUtils.authToken;
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.PUBLISHER;
import static org.neo4j.server.security.enterprise.auth.PredefinedRolesBuilder.READER;
// TODO: homogenize "'' does not exist" type error messages. In short, add quotes in the right places
public class AuthProceduresTest extends AuthProcedureTestBase
{
//---------- Change own password -----------
// Enterprise version of test in BuiltInProceduresIT.callChangePasswordWithAccessModeInDbmsMode.
// Uses community edition procedure in BuiltInProcedures
@Test
public void shouldChangeOwnPassword() throws Exception
{
testCallEmpty( readSubject, "CALL dbms.changePassword( '321' )" );
testUnAunthenticated( readSubject );
ShiroAuthSubject subject = manager.login( authToken( "readSubject", "321" ) );
assertEquals( AuthenticationResult.SUCCESS, subject.getAuthenticationResult() );
}
/*
TODO: uncomment and fix
@Test
public void shouldNotChangeOwnPasswordIfNewPasswordInvalid() throws Exception
{
testCallFail( readSubject, "CALL dbms.changePassword( '' )", QueryExecutionException.class,
"Password cannot be empty" );
testCallFail( readSubject, "CALL dbms.changePassword( '321' )", QueryExecutionException.class,
"Old password and new password cannot be the same" );
}
*/
//---------- change user password -----------
// Should change password for admin subject and valid user
@Test
public void shouldChangeUserPassword() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.changeUserPassword( 'readSubject', '321' )" );
assertEquals( AuthenticationResult.FAILURE, manager.login( authToken( "readSubject", "123" ) )
.getAuthenticationResult() );
assertEquals( AuthenticationResult.SUCCESS, manager.login( authToken( "readSubject", "321" ) )
.getAuthenticationResult() );
}
// Should fail vaguely to change password for non-admin subject, regardless of user and password
@Test
public void shouldNotChangeUserPasswordIfNotAdmin() throws Exception
{
testCallFail( schemaSubject, "CALL dbms.changeUserPassword( 'readSubject', '321' )",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.changeUserPassword( 'jake', '321' )",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.changeUserPassword( 'readSubject', '' )",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
}
// Should change own password for non-admin or admin subject
@Test
public void shouldChangeUserPasswordIfSameUser() throws Exception
{
testCallEmpty( readSubject, "CALL dbms.changeUserPassword( 'readSubject', '321' )" );
assertEquals( AuthenticationResult.FAILURE, manager.login( authToken( "readSubject", "123" ) )
.getAuthenticationResult() );
assertEquals( AuthenticationResult.SUCCESS, manager.login( authToken( "readSubject", "321" ) )
.getAuthenticationResult() );
testCallEmpty( adminSubject, "CALL dbms.changeUserPassword( 'adminSubject', 'cba' )" );
assertEquals( AuthenticationResult.FAILURE, manager.login( authToken( "adminSubject", "abc" ) )
.getAuthenticationResult() );
assertEquals( AuthenticationResult.SUCCESS, manager.login( authToken( "adminSubject", "cba" ) )
.getAuthenticationResult() );
}
// Should fail nicely to change own password for non-admin or admin subject if password invalid
@Test
public void shouldFailToChangeUserPasswordIfSameUserButInvalidPassword() throws Exception
{
testCallFail( readSubject, "CALL dbms.changeUserPassword( 'readSubject', '123' )",
QueryExecutionException.class, "Old password and new password cannot be the same" );
testCallFail( adminSubject, "CALL dbms.changeUserPassword( 'adminSubject', 'abc' )",
QueryExecutionException.class, "Old password and new password cannot be the same" );
}
// Should fail nicely to change password for admin subject and non-existing user
@Test
public void shouldNotChangeUserPasswordIfNonExistingUser() throws Exception
{
testCallFail( adminSubject, "CALL dbms.changeUserPassword( 'jake', '321' )",
QueryExecutionException.class, "User jake does not exist" );
}
// Should fail nicely to change password for admin subject and empty password
@Test
public void shouldNotChangeUserPasswordIfEmptyPassword() throws Exception
{
testCallFail( adminSubject, "CALL dbms.changeUserPassword( 'readSubject', '' )",
QueryExecutionException.class, "Password cannot be empty" );
}
// Should fail to change password for admin subject and same password
@Test
public void shouldNotChangeUserPasswordIfSamePassword() throws Exception
{
testCallFail( adminSubject, "CALL dbms.changeUserPassword( 'readSubject', '123' )",
QueryExecutionException.class, "Old password and new password cannot be the same" );
}
//---------- create user -----------
@Test
public void shouldCreateUser() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.createUser('craig', '1234', true)" );
assertNotNull( "User craig should exist", manager.getUser( "craig" ) );
}
/*
TODO: uncomment and fix
@Test
public void shouldNotCreateUserIfInvalidUsername() throws Exception
{
testCallFail( adminSubject, "CALL dbms.createUser('', '1234', true)", QueryExecutionException.class,
"Username cannot be empty" );
testCallFail( adminSubject, "CALL dbms.createUser('&%ss!', '1234', true)", QueryExecutionException.class,
"Username cannot be empty" );
testCallFail( adminSubject, "CALL dbms.createUser('&%ss!', '', true)", QueryExecutionException.class,
"Username cannot be empty" );
}
*/
@Test
public void shouldNotCreateUserIfInvalidPassword() throws Exception
{
testCallFail( adminSubject, "CALL dbms.createUser('craig', '', true)",
QueryExecutionException.class, "Password cannot be empty" );
}
@Test
public void shouldNotCreateExistingUser() throws Exception
{
testCallFail( adminSubject, "CALL dbms.createUser('readSubject', '1234', true)",
QueryExecutionException.class, "The specified user already exists" );
testCallFail( adminSubject, "CALL dbms.createUser('readSubject', 'abc', true)",
QueryExecutionException.class, "The specified user already exists" );
}
@Test
public void shouldNotAllowNonAdminCreateUser() throws Exception
{
testFailCreateUser( pwdSubject );
testFailCreateUser( readSubject );
testFailCreateUser( writeSubject );
testFailCreateUser( schemaSubject );
}
//---------- delete user -----------
@Test
public void shouldDeleteUser() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.deleteUser('noneSubject')" );
assertNull( "User noneSubject should not exist", manager.getUser( "noneSubject" ) );
manager.addUserToRole( "readSubject", PUBLISHER );
testCallEmpty( adminSubject, "CALL dbms.deleteUser('readSubject')" );
assertNull( "User readSubject should not exist", manager.getUser( "readSubject" ) );
assertFalse( manager.getUsernamesForRole( READER ).contains( "readSubject" ) );
assertFalse( manager.getUsernamesForRole( PUBLISHER ).contains( "readSubject" ) );
}
@Test
public void shouldNotDeleteUserIfNotAdmin() throws Exception
{
testFailDeleteUser( pwdSubject );
testFailDeleteUser( readSubject );
testFailDeleteUser( writeSubject );
testFailDeleteUser( schemaSubject );
}
@Test
public void shouldNotAllowDeletingNonExistingUser() throws Exception
{
testCallFail( adminSubject, "CALL dbms.deleteUser('Craig')",
QueryExecutionException.class, "The user 'Craig' does not exist" );
}
/*
TODO: uncomment and fix
@Test
public void shouldNotAllowDeletingYourself() throws Exception
{
testCallFail( adminSubject, "CALL dbms.deleteUser('" + adminSubject.name() + "')",
QueryExecutionException.class, "Deleting yourself is not allowed" );
}
*/
//---------- suspend user -----------
@Test
public void shouldSuspendUser() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.suspendUser('readSubject')" );
assertTrue( manager.getUser( "readSubject" ).hasFlag( InternalFlatFileRealm.IS_SUSPENDED ) );
}
@Test
public void shouldSuspendSuspendedUser() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.suspendUser('readSubject')" );
testCallEmpty( adminSubject, "CALL dbms.suspendUser('readSubject')" );
assertTrue( manager.getUser( "readSubject" ).hasFlag( InternalFlatFileRealm.IS_SUSPENDED ) );
}
@Test
public void shouldFailToSuspendNonExistingUser() throws Exception
{
testCallFail( adminSubject, "CALL dbms.suspendUser('Craig')",
QueryExecutionException.class, "User Craig does not exist" );
}
@Test
public void shouldFailToSuspendIfNotAdmin() throws Exception
{
testCallFail( schemaSubject, "CALL dbms.suspendUser('readSubject')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.suspendUser('Craig')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.suspendUser('')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
}
/*
TODO: uncomment and fix
@Test
public void shouldFailToSuspendYourself() throws Exception
{
testCallFail( adminSubject, "CALL dbms.suspendUser('" + adminSubject.name() + "')",
QueryExecutionException.class, "Suspending yourself is not allowed" );
}
*/
//---------- activate user -----------
@Test
public void shouldActivateUser() throws Exception
{
manager.suspendUser( "readSubject" );
testCallEmpty( adminSubject, "CALL dbms.activateUser('readSubject')" );
assertFalse( manager.getUser( "readSubject" ).hasFlag( InternalFlatFileRealm.IS_SUSPENDED ) );
}
@Test
public void shouldActivateActiveUser() throws Exception
{
manager.suspendUser( "readSubject" );
testCallEmpty( adminSubject, "CALL dbms.activateUser('readSubject')" );
testCallEmpty( adminSubject, "CALL dbms.activateUser('readSubject')" );
assertFalse( manager.getUser( "readSubject" ).hasFlag( InternalFlatFileRealm.IS_SUSPENDED ) );
}
/*
TODO: uncomment and fix
@Test
public void shouldFailToActivateNonExistingUser() throws Exception
{
testCallFail( adminSubject, "CALL dbms.activateUser('Craig')",
QueryExecutionException.class, "User 'Craig' does not exist" );
}
*/
@Test
public void shouldFailToActivateIfNotAdmin() throws Exception
{
manager.suspendUser( "readSubject" );
testCallFail( schemaSubject, "CALL dbms.activateUser('readSubject')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.activateUser('Craig')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.activateUser('')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
}
/*
TODO: uncomment and fix
@Test
public void shouldFailToActivateYourself() throws Exception
{
testCallFail( adminSubject, "CALL dbms.activateUser('" + adminSubject.name() + "')",
QueryExecutionException.class, "Activating yourself is not allowed" );
}
*/
//---------- add user to role -----------
@Test
public void shouldAddUserToRole() throws Exception
{
assertFalse( "Should not have role publisher", readSubject.getSubject().hasRole( PUBLISHER ) );
testCallEmpty( adminSubject, "CALL dbms.addUserToRole('readSubject', '" + PUBLISHER + "')" );
assertTrue( "Should have role publisher", readSubject.getSubject().hasRole( PUBLISHER ) );
}
@Test
public void shouldAddRetainUserInRole() throws Exception
{
assertTrue( "Should have role reader", readSubject.getSubject().hasRole( READER ) );
testCallEmpty( adminSubject, "CALL dbms.addUserToRole('readSubject', '" + READER + "')" );
assertTrue( "Should have still have role reader", readSubject.getSubject().hasRole( READER ) );
}
@Test
public void shouldFailToAddNonExistingUserToRole() throws Exception
{
testCallFail( adminSubject, "CALL dbms.addUserToRole('Olivia', '" + PUBLISHER + "')",
QueryExecutionException.class, "User Olivia does not exist" );
testCallFail( adminSubject, "CALL dbms.addUserToRole('Olivia', 'thisRoleDoesNotExist')",
QueryExecutionException.class, "User Olivia does not exist" );
testCallFail( adminSubject, "CALL dbms.addUserToRole('Olivia', '')",
QueryExecutionException.class, "User Olivia does not exist" );
}
@Test
public void shouldFailToAddUserToNonExistingRole() throws Exception
{
testCallFail( adminSubject, "CALL dbms.addUserToRole('readSubject', 'thisRoleDoesNotExist')",
QueryExecutionException.class, "Role thisRoleDoesNotExist does not exist" );
testCallFail( adminSubject, "CALL dbms.addUserToRole('readSubject', '')",
QueryExecutionException.class, "Role does not exist" );
}
@Test
public void shouldFailToAddUserToRoleIfNotAdmin() throws Exception
{
testFailAddUserToRole( pwdSubject );
testFailAddUserToRole( readSubject );
testFailAddUserToRole( writeSubject );
testCallFail( schemaSubject, "CALL dbms.addUserToRole('readSubject', '" + PUBLISHER + "')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.addUserToRole('Olivia', '" + PUBLISHER + "')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.addUserToRole('Olivia', 'thisRoleDoesNotExist')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
}
//---------- remove user from role -----------
@Test
public void shouldRemoveUserFromRole() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.removeUserFromRole('readSubject', '" + READER + "')" );
assertFalse( "Should not have role reader", readSubject.getSubject().hasRole( READER ) );
}
@Test
public void shouldKeepUserOutOfRole() throws Exception
{
assertFalse( "Should not have role publisher", readSubject.getSubject().hasRole( PUBLISHER ) );
testCallEmpty( adminSubject, "CALL dbms.removeUserFromRole('readSubject', '" + PUBLISHER + "')" );
assertFalse( "Should not have role publisher", readSubject.getSubject().hasRole( PUBLISHER ) );
}
@Test
public void shouldFailToRemoveNonExistingUserFromRole() throws Exception
{
testCallFail( adminSubject, "CALL dbms.removeUserFromRole('Olivia', '" + PUBLISHER + "')",
QueryExecutionException.class, "User Olivia does not exist" );
testCallFail( adminSubject, "CALL dbms.removeUserFromRole('Olivia', 'thisRoleDoesNotExist')",
QueryExecutionException.class, "User Olivia does not exist" );
testCallFail( adminSubject, "CALL dbms.removeUserFromRole('Olivia', '')",
QueryExecutionException.class, "User Olivia does not exist" );
}
@Test
public void shouldFailToRemoveUserFromNonExistingRole() throws Exception
{
testCallFail( adminSubject, "CALL dbms.removeUserFromRole('readSubject', 'thisRoleDoesNotExist')",
QueryExecutionException.class, "Role thisRoleDoesNotExist does not exist" );
testCallFail( adminSubject, "CALL dbms.removeUserFromRole('readSubject', '')",
QueryExecutionException.class, "Role does not exist" );
}
@Test
public void shouldFailToRemoveUserFromRoleIfNotAdmin() throws Exception
{
testFailRemoveUserFromRole( pwdSubject );
testFailRemoveUserFromRole( readSubject );
testFailRemoveUserFromRole( writeSubject );
testCallFail( schemaSubject, "CALL dbms.removeUserFromRole('readSubject', '" + READER + "')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.removeUserFromRole('Olivia', '" + READER + "')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallFail( schemaSubject, "CALL dbms.removeUserFromRole('Olivia', 'thisRoleDoesNotExist')",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
}
/*
TODO: uncomment and fix
@Test
public void shouldFailToRemoveYourselfFromAdminRole() throws Exception
{
testCallFail( adminSubject, "CALL dbms.removeUserFromRole('" + adminSubject.name() + "', '" + ADMIN + "')",
QueryExecutionException.class, "Remove yourself from admin role is not allowed" );
}
*/
//---------- manage multiple roles -----------
@Test
public void shouldAllowAddingAndRemovingUserFromMultipleRoles() throws Exception
{
assertFalse( "Should not have role publisher",
ShiroAuthSubject.castOrFail( readSubject ).getSubject().hasRole( PUBLISHER ) );
assertFalse( "Should not have role architect",
ShiroAuthSubject.castOrFail( readSubject ).getSubject().hasRole( ARCHITECT ) );
testCallEmpty( adminSubject, "CALL dbms.addUserToRole('readSubject', '" + PUBLISHER + "')" );
testCallEmpty( adminSubject, "CALL dbms.addUserToRole('readSubject', '" + ARCHITECT + "')" );
assertTrue( "Should have role publisher",
ShiroAuthSubject.castOrFail( readSubject ).getSubject().hasRole( PUBLISHER ) );
assertTrue( "Should have role architect",
ShiroAuthSubject.castOrFail( readSubject ).getSubject().hasRole( ARCHITECT ) );
testCallEmpty( adminSubject, "CALL dbms.removeUserFromRole('readSubject', '" + PUBLISHER + "')" );
testCallEmpty( adminSubject, "CALL dbms.removeUserFromRole('readSubject', '" + ARCHITECT + "')" );
assertFalse( "Should not have role publisher",
ShiroAuthSubject.castOrFail( readSubject ).getSubject().hasRole( PUBLISHER ) );
assertFalse( "Should not have role architect",
ShiroAuthSubject.castOrFail( readSubject ).getSubject().hasRole( ARCHITECT ) );
}
//---------- list users -----------
@Test
public void shouldListUsers() throws Exception
{
testResult( adminSubject, "CALL dbms.listUsers() YIELD username",
r -> assertKeyIs( r, "username", initialUsers ) );
}
@Test
public void shouldReturnUsersWithRoles() throws Exception
{
Map<String, Object> expected = map(
"adminSubject", listOf( ADMIN ),
"readSubject", listOf( READER ),
"schemaSubject", listOf( ARCHITECT ),
"readWriteSubject", listOf( READER, PUBLISHER ),
"pwdSubject", listOf( ),
"noneSubject", listOf( ),
"neo4j", listOf( ADMIN )
);
manager.addUserToRole( "readWriteSubject", READER );
testResult( adminSubject, "CALL dbms.listUsers()",
r -> resultContainsMap( r, "username", "roles", expected ) );
}
@Test
public void shouldShowCurrentUser() throws Exception
{
manager.addUserToRole( "readWriteSubject", READER );
testResult( adminSubject, "CALL dbms.showCurrentUser()",
r -> resultContainsMap( r, "username", "roles", map( "adminSubject", listOf( ADMIN ) ) ) );
testResult( readSubject, "CALL dbms.showCurrentUser()",
r -> resultContainsMap( r, "username", "roles", map( "readSubject", listOf( READER ) ) ) );
testResult( schemaSubject, "CALL dbms.showCurrentUser()",
r -> resultContainsMap( r, "username", "roles", map( "schemaSubject", listOf( ARCHITECT ) ) ) );
testResult( writeSubject, "CALL dbms.showCurrentUser()",
r -> resultContainsMap( r, "username", "roles",
map( "readWriteSubject", listOf( READER, PUBLISHER ) ) ) );
testResult( noneSubject, "CALL dbms.showCurrentUser()",
r -> resultContainsMap( r, "username", "roles", map( "noneSubject", listOf() ) ) );
}
@Test
public void shouldNotAllowNonAdminListUsers() throws Exception
{
testFailListUsers( pwdSubject, 5 );
testFailListUsers( readSubject, 5 );
testFailListUsers( writeSubject, 5 );
testFailListUsers( schemaSubject, 5 );
}
//---------- list roles -----------
@Test
public void shouldListRoles() throws Exception
{
testResult( adminSubject, "CALL dbms.listRoles() YIELD role AS roles RETURN roles",
r -> assertKeyIs( r, "roles", initialRoles ) );
}
@Test
public void shouldReturnRolesWithUsers() throws Exception
{
Map<String,Object> expected = map(
ADMIN, listOf( "adminSubject", "neo4j" ),
READER, listOf( "readSubject" ),
ARCHITECT, listOf( "schemaSubject" ),
PUBLISHER, listOf( "readWriteSubject" ),
"empty", listOf()
);
testResult( adminSubject, "CALL dbms.listRoles()",
r -> resultContainsMap( r, "role", "users", expected ) );
}
@Test
public void shouldNotAllowNonAdminListRoles() throws Exception
{
testFailListRoles( pwdSubject );
testFailListRoles( readSubject );
testFailListRoles( writeSubject );
testFailListRoles( schemaSubject );
}
//---------- list roles for user -----------
@Test
public void shouldListRolesForUser() throws Exception
{
testResult( adminSubject, "CALL dbms.listRolesForUser('adminSubject') YIELD value as roles RETURN roles",
r -> assertKeyIs( r, "roles", ADMIN ) );
testResult( adminSubject, "CALL dbms.listRolesForUser('readSubject') YIELD value as roles RETURN roles",
r -> assertKeyIs( r, "roles", READER ) );
}
@Test
public void shouldListNoRolesForUserWithNoRoles() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.createUser('Henrik', 'bar', false)" );
testCallEmpty( adminSubject, "CALL dbms.listRolesForUser('Henrik') YIELD value as roles RETURN roles" );
}
@Test
public void shouldNotListRolesForNonExistingUser() throws Exception
{
testCallFail( adminSubject, "CALL dbms.listRolesForUser('Petra') YIELD value as roles RETURN roles",
QueryExecutionException.class, "User Petra does not exist" );
testCallFail( adminSubject, "CALL dbms.listRolesForUser('') YIELD value as roles RETURN roles",
QueryExecutionException.class, "User does not exist" );
}
/*
TODO: uncommend and fix
@Test
public void shouldListOwnRolesRoles() throws Exception
{
testResult( adminSubject, "CALL dbms.listRolesForUser('adminSubject') YIELD value as roles RETURN roles",
r -> assertKeyIs( r, "roles", ADMIN ) );
testResult( readSubject, "CALL dbms.listRolesForUser('readSubject') YIELD value as roles RETURN roles",
r -> assertKeyIs( r, "roles", READER ) );
}
*/
@Test
public void shouldNotAllowNonAdminListUserRoles() throws Exception
{
testFailListUserRoles( pwdSubject, "adminSubject" );
testFailListUserRoles( readSubject, "adminSubject" );
testFailListUserRoles( writeSubject, "adminSubject" );
testFailListUserRoles( schemaSubject, "adminSubject" );
}
//---------- list users for role -----------
@Test
public void shouldListUsersForRole() throws Exception
{
testResult( adminSubject, "CALL dbms.listUsersForRole('admin') YIELD value as users RETURN users",
r -> assertKeyIs( r, "users", adminSubject.name(), "neo4j" ) );
}
@Test
public void shouldListNoUsersForRoleWithNoUsers() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.listUsersForRole('empty') YIELD value as users RETURN users" );
}
@Test
public void shouldNotListUsersForNonExistingRole() throws Exception
{
testCallFail( adminSubject, "CALL dbms.listUsersForRole('poodle') YIELD value as users RETURN users",
QueryExecutionException.class, "Role poodle does not exist" );
testCallFail( adminSubject, "CALL dbms.listUsersForRole('') YIELD value as users RETURN users",
QueryExecutionException.class, "Role does not exist" );
}
@Test
public void shouldNotListUsersForRoleIfNotAdmin() throws Exception
{
testFailListRoleUsers( pwdSubject, ADMIN );
testFailListRoleUsers( readSubject, ADMIN );
testFailListRoleUsers( writeSubject, ADMIN );
testFailListRoleUsers( schemaSubject, ADMIN );
}
//---------- permissions -----------
/*
TODO: uncomment and fix un-authentication handling
@Test
public void shouldSetCorrectUnAuthenticatedPermissions() throws Exception
{
pwdSubject.logout();
testUnAunthenticated( pwdSubject, "MATCH (n) RETURN n" );
testUnAunthenticated( pwdSubject, "CREATE (:Node)" );
testUnAunthenticated( pwdSubject, "CREATE INDEX ON :Node(number)" );
testUnAunthenticated( pwdSubject, "CALL dbms.changePassword( '321' )" );
testUnAunthenticated( pwdSubject, "CALL dbms.createUser('Henrik', 'bar', true)" );
}
*/
@Test
public void shouldSetCorrectPasswordChangeRequiredPermissions() throws Exception
{
testFailRead( pwdSubject, 3 );
testFailWrite( pwdSubject );
testFailSchema( pwdSubject );
testCallEmpty( pwdSubject, "CALL dbms.changePassword( '321' )" );
testCallEmpty( adminSubject, "CALL dbms.createUser('Henrik', 'bar', true)" );
testCallEmpty( adminSubject, "CALL dbms.addUserToRole('Henrik', '" + ARCHITECT + "')" );
ShiroAuthSubject henrik = manager.login( authToken( "Henrik", "bar" ) );
assertEquals( AuthenticationResult.PASSWORD_CHANGE_REQUIRED, henrik.getAuthenticationResult() );
testFailRead( henrik, 3 );
testFailWrite( henrik );
testFailSchema( henrik );
testCallEmpty( henrik, "CALL dbms.changePassword( '321' )" );
testCallEmpty( adminSubject, "CALL dbms.createUser('Olivia', 'bar', true)" );
testCallEmpty( adminSubject, "CALL dbms.addUserToRole('Olivia', '" + ADMIN + "')" );
ShiroAuthSubject olivia = manager.login( authToken( "Olivia", "bar" ) );
assertEquals( AuthenticationResult.PASSWORD_CHANGE_REQUIRED, olivia.getAuthenticationResult() );
testFailRead( olivia, 3 );
testFailWrite( olivia );
testFailSchema( olivia );
testCallFail( olivia, "CALL dbms.createUser('OliviasFriend', 'bar', false)",
QueryExecutionException.class, AuthProcedures.PERMISSION_DENIED );
testCallEmpty( olivia, "CALL dbms.changePassword( '321' )" );
}
@Test
public void shouldSetCorrectNoRolePermissions() throws Exception
{
testFailRead( noneSubject, 3 );
testFailWrite( noneSubject );
testFailSchema( noneSubject );
testFailCreateUser( noneSubject );
testCallEmpty( noneSubject, "CALL dbms.changePassword( '321' )" );
}
@Test
public void shouldSetCorrectReaderPermissions() throws Exception
{
testSuccessfulRead( readSubject, 3 );
testFailWrite( readSubject );
testFailSchema( readSubject );
testFailCreateUser( readSubject );
testCallEmpty( readSubject, "CALL dbms.changePassword( '321' )" );
}
@Test
public void shouldSetCorrectPublisherPermissions() throws Exception
{
testSuccessfulRead( writeSubject, 3 );
testSuccessfulWrite( writeSubject );
testFailSchema( writeSubject );
testFailCreateUser( writeSubject );
testCallEmpty( writeSubject, "CALL dbms.changePassword( '321' )" );
}
@Test
public void shouldSetCorrectSchemaPermissions() throws Exception
{
testSuccessfulRead( schemaSubject, 3 );
testSuccessfulWrite( schemaSubject );
testSuccessfulSchema( schemaSubject );
testFailCreateUser( schemaSubject );
testCallEmpty( schemaSubject, "CALL dbms.changePassword( '321' )" );
}
@Test
public void shouldSetCorrectAdminPermissions() throws Exception
{
testSuccessfulRead( adminSubject, 3 );
testSuccessfulWrite( adminSubject );
testSuccessfulSchema( adminSubject );
testCallEmpty( adminSubject, "CALL dbms.createUser('Olivia', 'bar', true)" );
testCallEmpty( adminSubject, "CALL dbms.changePassword( '321' )" );
}
@Test
public void shouldSetCorrectMultiRolePermissions() throws Exception
{
testCallEmpty( adminSubject, "CALL dbms.addUserToRole('schemaSubject', '" + READER + "')" );
testSuccessfulRead( schemaSubject, 3 );
testSuccessfulWrite( schemaSubject );
testSuccessfulSchema( schemaSubject );
testFailCreateUser( schemaSubject );
testCallEmpty( schemaSubject, "CALL dbms.changePassword( '321' )" );
}
}