-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AuthProceduresTestLogic.java
1343 lines (1132 loc) · 57.7 KB
/
AuthProceduresTestLogic.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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.hamcrest.Matcher;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
import org.neo4j.bolt.v1.transport.integration.TransportTestUtil;
import org.neo4j.bolt.v1.transport.socket.client.TransportConnection;
import org.neo4j.bolt.v1.transport.socket.client.SocketConnection;
import org.neo4j.helpers.HostnamePort;
import org.neo4j.kernel.api.security.exception.InvalidArgumentsException;
import org.neo4j.test.DoubleLatch;
import org.neo4j.test.rule.concurrent.ThreadingRule;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.bolt.v1.messaging.message.InitMessage.init;
import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.msgSuccess;
import static org.neo4j.bolt.v1.transport.integration.TransportTestUtil.eventuallyReceives;
import static org.neo4j.helpers.collection.MapUtil.map;
import static org.neo4j.kernel.api.security.AuthenticationResult.PASSWORD_CHANGE_REQUIRED;
import static org.neo4j.server.security.enterprise.auth.AuthProcedures.PERMISSION_DENIED;
import static org.neo4j.server.security.enterprise.auth.InternalFlatFileRealm.IS_SUSPENDED;
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;
public abstract class AuthProceduresTestLogic<S> extends AuthTestBase<S>
{
private static final String PWD_CHANGE = PASSWORD_CHANGE_REQUIRED.name().toLowerCase();
@Rule
public final ThreadingRule threading = new ThreadingRule();
//---------- General tests over all procedures -----------
@Test
public void shouldHaveDescriptionsOnAllSecurityProcedures() throws Throwable
{
assertSuccess( readSubject, "CALL dbms.procedures", r -> {
Stream<Map<String, Object>> securityProcedures = r.stream().filter( s -> {
String name = s.get( "name" ).toString();
String description = s.get( "description" ).toString();
// TODO: remove filter for Transaction and Connection once those procedures are removed
if ( name.contains( "dbms.security" ) &&
!(name.contains( "Transaction" ) || name.contains( "Connection" )) )
{
assertThat( "Description for '" + name + "' should not be empty", description.trim().length(),
greaterThan( 0 ) );
return true;
}
return false;
} );
assertThat( securityProcedures.count(), equalTo( 15L ) );
} );
}
//---------- Change own password -----------
// Enterprise version of test in BuiltInProceduresIT.callChangePasswordWithAccessModeInDbmsMode.
// Uses community edition procedure in BuiltInProcedures
@Test
public void shouldChangeOwnPassword() throws Throwable
{
assertEmpty( readSubject, "CALL dbms.security.changePassword( '321' )" );
neo.updateAuthToken( readSubject, "readSubject", "321" ); // Because RESTSubject caches an auth token that is sent with every request
neo.assertAuthenticated( readSubject );
testSuccessfulRead( readSubject, 3 );
}
@Test
public void shouldChangeOwnPasswordEvenIfHasNoAuthorization() throws Throwable
{
neo.assertAuthenticated( noneSubject );
assertEmpty( noneSubject, "CALL dbms.security.changePassword( '321' )" );
neo.updateAuthToken( noneSubject, "noneSubject", "321" ); // Because RESTSubject caches an auth token that is sent with every request
neo.assertAuthenticated( noneSubject );
}
@Test
public void shouldNotChangeOwnPasswordIfNewPasswordInvalid() throws Exception
{
assertFail( readSubject, "CALL dbms.security.changePassword( '' )", "A password cannot be empty." );
assertFail( readSubject, "CALL dbms.security.changePassword( '123' )",
"Old password and new password cannot be the same." );
}
//---------- list running transactions -----------
@Test
public void shouldListSelfTransaction()
{
assertSuccess( adminSubject, "CALL dbms.security.listTransactions()",
r -> assertKeyIsMap( r, "username", "activeTransactions", map( "adminSubject", "1" ) ) );
}
@Test
public void shouldNotListTransactionsIfNotAdmin()
{
assertFail( noneSubject, "CALL dbms.security.listTransactions()", PERMISSION_DENIED );
assertFail( readSubject, "CALL dbms.security.listTransactions()", PERMISSION_DENIED );
assertFail( writeSubject, "CALL dbms.security.listTransactions()", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.listTransactions()", PERMISSION_DENIED );
}
@Test
public void shouldListTransactions() throws Throwable
{
DoubleLatch latch = new DoubleLatch( 3 );
ThreadedTransactionCreate<S> write1 = new ThreadedTransactionCreate<>( neo, latch );
ThreadedTransactionCreate<S> write2 = new ThreadedTransactionCreate<>( neo, latch );
String q1 = write1.execute( threading, writeSubject );
String q2 = write2.execute( threading, writeSubject );
latch.startAndWaitForAllToStart();
assertSuccess( adminSubject, "CALL dbms.security.listTransactions()",
r -> assertKeyIsMap( r, "username", "activeTransactions",
map( "adminSubject", "1", "writeSubject", "2" ) ) );
write1.finish();
write2.finish();
latch.finishAndWaitForAllToFinish();
write1.closeAndAssertSuccess();
write2.closeAndAssertSuccess();
}
@Test
public void shouldListRestrictedTransaction()
{
final DoubleLatch doubleLatch = new DoubleLatch( 2 );
ClassWithProcedures.setTestLatch( new ClassWithProcedures.LatchedRunnables( doubleLatch, () -> {}, () -> {} ) );
new Thread( () -> assertEmpty( writeSubject, "CALL test.waitForLatch()" ) ).start();
doubleLatch.start();
try
{
assertSuccess( adminSubject, "CALL dbms.security.listTransactions()",
r -> assertKeyIsMap( r, "username", "activeTransactions",
map( "adminSubject", "1", "writeSubject", "1" ) ) );
}
finally
{
doubleLatch.finish();
doubleLatch.awaitFinish();
}
}
@Ignore
@Test
public void shouldListQueries() throws Throwable
{
DoubleLatch latch = new DoubleLatch( 3 );
ThreadedTransactionCreate<S> write1 = new ThreadedTransactionCreate<>( neo, latch );
ThreadedTransactionCreate<S> write2 = new ThreadedTransactionCreate<>( neo, latch );
String q1 = write1.execute( threading, writeSubject );
String q2 = write2.execute( threading, writeSubject );
latch.startAndWaitForAllToStart();
assertSuccess( adminSubject, "CALL dbms.listQueries()", r ->
r.forEachRemaining( m ->
{
// TODO: Make sure each matches exactly once
Matcher<Map<String,Object>> matcher1 =
allOf( hasQuery( q1 ),
hasUsername( "writeSubject" ),
hasQueryId(),
hasStartTimeAfter( 0L ),
hasNoParameters() );
Matcher<Map<String,Object>> matcher2 =
allOf( hasQuery( q2 ),
hasUsername( "writeSubject" ),
hasQueryId(),
hasStartTimeAfter( 0L ),
hasNoParameters() );
assertThat( m, anyOf( matcher1, matcher2 ) );
}
) );
write1.finish();
write2.finish();
latch.finishAndWaitForAllToFinish();
write1.closeAndAssertSuccess();
write2.closeAndAssertSuccess();
}
//---------- terminate transactions for user -----------
@Test
public void shouldTerminateTransactionForUser() throws Throwable
{
DoubleLatch latch = new DoubleLatch( 2 );
ThreadedTransactionCreate<S> write = new ThreadedTransactionCreate<>( neo, latch );
write.execute( threading, writeSubject );
latch.startAndWaitForAllToStart();
assertSuccess( adminSubject, "CALL dbms.security.terminateTransactionsForUser( 'writeSubject' )",
r -> assertKeyIsMap( r, "username", "transactionsTerminated", map( "writeSubject", "1" ) ) );
assertSuccess( adminSubject, "CALL dbms.security.listTransactions()",
r -> assertKeyIsMap( r, "username", "activeTransactions", map( "adminSubject", "1" ) ) );
write.finish();
latch.finishAndWaitForAllToFinish();
write.closeAndAssertTransactionTermination();
assertEmpty( adminSubject, "MATCH (n:Test) RETURN n.name AS name" );
}
@Test
public void shouldTerminateOnlyGivenUsersTransaction() throws Throwable
{
DoubleLatch latch = new DoubleLatch( 3 );
ThreadedTransactionCreate<S> schema = new ThreadedTransactionCreate<>( neo, latch );
ThreadedTransactionCreate<S> write = new ThreadedTransactionCreate<>( neo, latch );
schema.execute( threading, schemaSubject );
write.execute( threading, writeSubject );
latch.startAndWaitForAllToStart();
assertSuccess( adminSubject, "CALL dbms.security.terminateTransactionsForUser( 'schemaSubject' )",
r -> assertKeyIsMap( r, "username", "transactionsTerminated", map( "schemaSubject", "1" ) ) );
assertSuccess( adminSubject, "CALL dbms.security.listTransactions()",
r -> assertKeyIsMap( r, "username", "activeTransactions",
map( "adminSubject", "1", "writeSubject", "1" ) ) );
schema.finish();
write.finish();
latch.finishAndWaitForAllToFinish();
schema.closeAndAssertTransactionTermination();
write.closeAndAssertSuccess();
assertSuccess( adminSubject, "MATCH (n:Test) RETURN n.name AS name",
r -> assertKeyIs( r, "name", "writeSubject-node" ) );
}
@Test
public void shouldTerminateAllTransactionsForGivenUser() throws Throwable
{
DoubleLatch latch = new DoubleLatch( 3 );
ThreadedTransactionCreate<S> schema1 = new ThreadedTransactionCreate<>( neo, latch );
ThreadedTransactionCreate<S> schema2 = new ThreadedTransactionCreate<>( neo, latch );
schema1.execute( threading, schemaSubject );
schema2.execute( threading, schemaSubject );
latch.startAndWaitForAllToStart();
assertSuccess( adminSubject, "CALL dbms.security.terminateTransactionsForUser( 'schemaSubject' )",
r -> assertKeyIsMap( r, "username", "transactionsTerminated", map( "schemaSubject", "2" ) ) );
assertSuccess( adminSubject, "CALL dbms.security.listTransactions()",
r -> assertKeyIsMap( r, "username", "activeTransactions", map( "adminSubject", "1" ) ) );
schema1.finish();
schema2.finish();
latch.finishAndWaitForAllToFinish();
schema1.closeAndAssertTransactionTermination();
schema2.closeAndAssertTransactionTermination();
assertEmpty( adminSubject, "MATCH (n:Test) RETURN n.name AS name" );
}
@Test
public void shouldNotTerminateTerminationTransaction() throws InterruptedException, ExecutionException
{
assertSuccess( adminSubject, "CALL dbms.security.terminateTransactionsForUser( 'adminSubject' )",
r -> assertKeyIsMap( r, "username", "transactionsTerminated", map( "adminSubject", "0" ) ) );
assertSuccess( readSubject, "CALL dbms.security.terminateTransactionsForUser( 'readSubject' )",
r -> assertKeyIsMap( r, "username", "transactionsTerminated", map( "readSubject", "0" ) ) );
}
@Test
public void shouldTerminateSelfTransactionsExceptTerminationTransactionIfAdmin() throws Throwable
{
shouldTerminateSelfTransactionsExceptTerminationTransaction( adminSubject );
}
@Test
public void shouldTerminateSelfTransactionsExceptTerminationTransactionIfNotAdmin() throws Throwable
{
shouldTerminateSelfTransactionsExceptTerminationTransaction( writeSubject );
}
private void shouldTerminateSelfTransactionsExceptTerminationTransaction( S subject ) throws Throwable
{
DoubleLatch latch = new DoubleLatch( 2 );
ThreadedTransactionCreate<S> create = new ThreadedTransactionCreate<>( neo, latch );
create.execute( threading, subject );
latch.startAndWaitForAllToStart();
String subjectName = neo.nameOf( subject );
assertSuccess( subject, "CALL dbms.security.terminateTransactionsForUser( '" + subjectName + "' )",
r -> assertKeyIsMap( r, "username", "transactionsTerminated", map( subjectName, "1" ) ) );
create.finish();
latch.finishAndWaitForAllToFinish();
create.closeAndAssertTransactionTermination();
assertEmpty( adminSubject, "MATCH (n:Test) RETURN n.name AS name" );
}
@Test
public void shouldNotTerminateTransactionsIfNonExistentUser() throws InterruptedException, ExecutionException
{
assertFail( adminSubject, "CALL dbms.security.terminateTransactionsForUser( 'Petra' )", "User 'Petra' does not exist" );
assertFail( adminSubject, "CALL dbms.security.terminateTransactionsForUser( '' )", "User '' does not exist" );
}
@Test
public void shouldNotTerminateTransactionsIfNotAdmin() throws Throwable
{
DoubleLatch latch = new DoubleLatch( 2 );
ThreadedTransactionCreate<S> write = new ThreadedTransactionCreate<>( neo, latch );
write.execute( threading, writeSubject );
latch.startAndWaitForAllToStart();
assertFail( noneSubject, "CALL dbms.security.terminateTransactionsForUser( 'writeSubject' )", PERMISSION_DENIED );
assertFail( pwdSubject, "CALL dbms.security.terminateTransactionsForUser( 'writeSubject' )", CHANGE_PWD_ERR_MSG );
assertFail( readSubject, "CALL dbms.security.terminateTransactionsForUser( 'writeSubject' )", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.terminateTransactionsForUser( 'writeSubject' )", PERMISSION_DENIED );
assertSuccess( adminSubject, "CALL dbms.security.listTransactions()",
r -> assertKeyIs( r, "username", "adminSubject", "writeSubject" ) );
write.finish();
latch.finishAndWaitForAllToFinish();
write.closeAndAssertSuccess();
assertSuccess( adminSubject, "MATCH (n:Test) RETURN n.name AS name",
r -> assertKeyIs( r, "name", "writeSubject-node" ) );
}
@Test
public void shouldTerminateRestrictedTransaction()
{
final DoubleLatch doubleLatch = new DoubleLatch( 2 );
ClassWithProcedures.setTestLatch( new ClassWithProcedures.LatchedRunnables( doubleLatch, () -> {}, () -> {} ) );
new Thread( () -> assertFail( writeSubject, "CALL test.waitForLatch()", "Explicitly terminated by the user." ) )
.start();
doubleLatch.start();
try
{
assertSuccess( adminSubject, "CALL dbms.security.terminateTransactionsForUser( 'writeSubject' )",
r -> assertKeyIsMap( r, "username", "transactionsTerminated", map( "writeSubject", "1" ) ) );
}
finally
{
doubleLatch.finish();
doubleLatch.awaitFinish();
}
}
//---------- change user password -----------
// Should change password for admin subject and valid user
@Test
public void shouldChangeUserPassword() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '321', false )" );
// TODO: uncomment and fix
// testUnAuthenticated( readSubject );
neo.assertInitFailed( neo.login( "readSubject", "123" ) );
neo.assertAuthenticated( neo.login( "readSubject", "321" ) );
}
@Test
public void shouldChangeUserPasswordAndRequirePasswordChangeOnNextLoginByDefault() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '321' )" );
neo.assertInitFailed( neo.login( "readSubject", "123" ) );
neo.assertPasswordChangeRequired( neo.login( "readSubject", "321" ) );
}
@Test
public void shouldChangeUserPasswordAndRequirePasswordChangeOnNextLoginOnRequest() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '321', true )" );
neo.assertInitFailed( neo.login( "readSubject", "123" ) );
neo.assertPasswordChangeRequired( neo.login( "readSubject", "321" ) );
}
// Should fail vaguely to change password for non-admin subject, regardless of user and password
@Test
public void shouldNotChangeUserPasswordIfNotAdmin() throws Exception
{
assertFail( schemaSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '321' )", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.changeUserPassword( 'jake', '321' )", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '' )", PERMISSION_DENIED );
}
// Should change own password for non-admin or admin subject
@Test
public void shouldChangeUserPasswordIfSameUser() throws Throwable
{
assertEmpty( readSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '321', false )" );
neo.updateAuthToken( readSubject, "readSubject", "321" ); // Because RESTSubject caches an auth token that is sent with every request
neo.assertAuthenticated( readSubject );
testSuccessfulRead( readSubject, 3 );
assertEmpty( adminSubject, "CALL dbms.security.changeUserPassword( 'adminSubject', 'cba', false )" );
neo.updateAuthToken( adminSubject, "adminSubject", "cba" ); // Because RESTSubject caches an auth token that is sent with every request
neo.assertAuthenticated( adminSubject );
testSuccessfulRead( adminSubject, 3 );
}
// Should fail nicely to change own password for non-admin or admin subject if password invalid
@Test
public void shouldFailToChangeUserPasswordIfSameUserButInvalidPassword() throws Exception
{
assertFail( readSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '123' )",
"Old password and new password cannot be the same." );
assertFail( adminSubject, "CALL dbms.security.changeUserPassword( 'adminSubject', 'abc' )",
"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 shouldNotChangeUserPasswordIfNonExistentUser() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.changeUserPassword( 'jake', '321' )",
"User 'jake' does not exist." );
}
// Should fail nicely to change password for admin subject and empty password
@Test
public void shouldNotChangeUserPasswordIfEmptyPassword() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '' )",
"A password cannot be empty." );
}
// Should fail to change password for admin subject and same password
@Test
public void shouldNotChangeUserPasswordIfSamePassword() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.changeUserPassword( 'readSubject', '123' )",
"Old password and new password cannot be the same." );
}
@Test
public void shouldTerminateTransactionsOnChangeUserPassword() throws Throwable
{
shouldTerminateTransactionsForUser( writeSubject, "dbms.security.changeUserPassword( '%s', 'newPassword' )" );
}
@Test
public void shouldTerminateConnectionsOnChangeUserPassword() throws Exception
{
TransportConnection conn = startBoltSession( "writeSubject", "abc" );
assertSuccess( adminSubject, "CALL dbms.security.listConnections() YIELD username, connectionCount " +
"WITH username, connectionCount WHERE username = 'writeSubject' RETURN username, connectionCount",
r -> assertKeyIsMap( r, "username", "connectionCount", map( "writeSubject", IS_BOLT ? "2" : "1" ) ) );
assertEmpty( adminSubject, "CALL dbms.security.changeUserPassword( 'writeSubject', 'newPassword' )" );
assertEmpty( adminSubject, "CALL dbms.security.listConnections() YIELD username, connectionCount " +
"WITH username, connectionCount WHERE username = 'writeSubject' RETURN username, connectionCount");
conn.disconnect();
}
//---------- create user -----------
@Test
public void shouldCreateUserAndRequirePasswordChangeByDefault() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('craig', '1234' )" );
userManager.getUser( "craig" );
neo.assertInitFailed( neo.login( "craig", "321" ) );
neo.assertPasswordChangeRequired( neo.login( "craig", "1234" ) );
}
@Test
public void shouldCreateUserAndRequirePasswordChangeIfRequested() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('craig', '1234', true)" );
userManager.getUser( "craig" );
neo.assertInitFailed( neo.login( "craig", "321" ) );
neo.assertPasswordChangeRequired( neo.login( "craig", "1234" ) );
}
@Test
public void shouldCreateUserAndRequireNoPasswordChangeIfRequested() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('craig', '1234', false)" );
userManager.getUser( "craig" );
neo.assertAuthenticated( neo.login( "craig", "1234" ) );
}
@Test
public void shouldNotCreateUserIfInvalidUsername() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.createUser('', '1234', true)",
"The provided user name is empty." );
assertFail( adminSubject, "CALL dbms.security.createUser('&%ss!', '1234', true)",
"User name '&%ss!' contains illegal characters." );
assertFail( adminSubject, "CALL dbms.security.createUser('&%ss!', '', true)",
"User name '&%ss!' contains illegal characters." );
}
@Test
public void shouldNotCreateUserIfInvalidPassword() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.createUser('craig', '', true)", "A password cannot be empty." );
}
@Test
public void shouldNotCreateExistingUser() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.createUser('readSubject', '1234', true)",
"The specified user 'readSubject' already exists" );
assertFail( adminSubject, "CALL dbms.security.createUser('readSubject', '', true)",
"A password cannot be empty." );
}
@Test
public void shouldNotAllowNonAdminCreateUser() throws Exception
{
testFailCreateUser( pwdSubject, CHANGE_PWD_ERR_MSG );
testFailCreateUser( readSubject, PERMISSION_DENIED );
testFailCreateUser( writeSubject, PERMISSION_DENIED );
testFailCreateUser( schemaSubject, PERMISSION_DENIED );
}
//---------- delete user -----------
@Test
public void shouldDeleteUser() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.deleteUser('noneSubject')" );
try
{
userManager.getUser( "noneSubject" );
fail( "User noneSubject should not exist" );
}
catch ( InvalidArgumentsException e )
{
assertTrue( "User noneSubject should not exist",
e.getMessage().contains( "User 'noneSubject' does not exist." ) );
}
userManager.addRoleToUser( PUBLISHER, "readSubject" );
assertEmpty( adminSubject, "CALL dbms.security.deleteUser('readSubject')" );
try
{
userManager.getUser( "readSubject" );
fail( "User readSubject should not exist" );
}
catch ( InvalidArgumentsException e )
{
assertTrue( "User readSubject should not exist",
e.getMessage().contains( "User 'readSubject' does not exist." ) );
}
assertFalse( userManager.getUsernamesForRole( READER ).contains( "readSubject" ) );
assertFalse( userManager.getUsernamesForRole( PUBLISHER ).contains( "readSubject" ) );
}
@Test
public void shouldNotDeleteUserIfNotAdmin() throws Exception
{
testFailDeleteUser( pwdSubject, "readSubject", CHANGE_PWD_ERR_MSG );
testFailDeleteUser( readSubject, "readSubject", PERMISSION_DENIED );
testFailDeleteUser( writeSubject, "readSubject", PERMISSION_DENIED );
testFailDeleteUser( schemaSubject, "readSubject", PERMISSION_DENIED );
testFailDeleteUser( schemaSubject, "Craig", PERMISSION_DENIED );
testFailDeleteUser( schemaSubject, "", PERMISSION_DENIED );
}
@Test
public void shouldNotAllowDeletingNonExistentUser() throws Exception
{
testFailDeleteUser( adminSubject, "Craig", "User 'Craig' does not exist." );
testFailDeleteUser( adminSubject, "", "User '' does not exist." );
}
@Test
public void shouldNotAllowDeletingYourself() throws Exception
{
testFailDeleteUser( adminSubject, "adminSubject", "Deleting yourself (user 'adminSubject') is not allowed." );
}
@Test
public void shouldTerminateTransactionsOnUserDeletion() throws Throwable
{
shouldTerminateTransactionsForUser( writeSubject, "dbms.security.deleteUser( '%s' )" );
}
@Test
public void shouldTerminateConnectionsOnUserDeletion() throws Exception
{
TransportConnection conn = startBoltSession( "writeSubject", "abc" );
assertSuccess( adminSubject, "CALL dbms.security.listConnections() YIELD username, connectionCount " +
"WITH username, connectionCount WHERE username = 'writeSubject' RETURN username, connectionCount",
r -> assertKeyIsMap( r, "username", "connectionCount", map( "writeSubject", IS_BOLT ? "2" : "1" ) ) );
assertEmpty( adminSubject, "CALL dbms.security.deleteUser( 'writeSubject' )" );
assertEmpty( adminSubject, "CALL dbms.security.listConnections() YIELD username, connectionCount " +
"WITH username, connectionCount WHERE username = 'writeSubject' RETURN username, connectionCount");
conn.disconnect();
}
//---------- suspend user -----------
@Test
public void shouldSuspendUser() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.suspendUser('readSubject')" );
assertTrue( userManager.getUser( "readSubject" ).hasFlag( IS_SUSPENDED ) );
}
@Test
public void shouldSuspendSuspendedUser() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.suspendUser('readSubject')" );
assertEmpty( adminSubject, "CALL dbms.security.suspendUser('readSubject')" );
assertTrue( userManager.getUser( "readSubject" ).hasFlag( IS_SUSPENDED ) );
}
@Test
public void shouldFailToSuspendNonExistentUser() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.suspendUser('Craig')", "User 'Craig' does not exist." );
}
@Test
public void shouldFailToSuspendIfNotAdmin() throws Exception
{
assertFail( schemaSubject, "CALL dbms.security.suspendUser('readSubject')", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.suspendUser('Craig')", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.suspendUser('')", PERMISSION_DENIED );
}
@Test
public void shouldFailToSuspendYourself() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.suspendUser('adminSubject')",
"Suspending yourself (user 'adminSubject') is not allowed." );
}
@Test
public void shouldTerminateTransactionsOnUserSuspension() throws Throwable
{
shouldTerminateTransactionsForUser( writeSubject, "dbms.security.suspendUser( '%s' )" );
}
@Test
public void shouldTerminateConnectionsOnUserSuspension() throws Exception
{
TransportConnection conn = startBoltSession( "writeSubject", "abc" );
assertSuccess( adminSubject, "CALL dbms.security.listConnections() YIELD username, connectionCount " +
"WITH username, connectionCount WHERE username = 'writeSubject' RETURN username, connectionCount",
r -> assertKeyIsMap( r, "username", "connectionCount", map( "writeSubject", IS_BOLT ? "2" : "1" ) ) );
assertEmpty( adminSubject, "CALL dbms.security.suspendUser( 'writeSubject' )" );
assertEmpty( adminSubject, "CALL dbms.security.listConnections() YIELD username, connectionCount " +
"WITH username, connectionCount WHERE username = 'writeSubject' RETURN username, connectionCount");
conn.disconnect();
}
//---------- activate user -----------
@Test
public void shouldActivateUserAndRequirePasswordChangeByDefault() throws Exception
{
userManager.suspendUser( "readSubject" );
assertEmpty( adminSubject, "CALL dbms.security.activateUser('readSubject')" );
neo.assertInitFailed( neo.login( "readSubject", "321" ) );
neo.assertPasswordChangeRequired( neo.login( "readSubject", "123" ) );
assertFalse( userManager.getUser( "readSubject" ).hasFlag( IS_SUSPENDED ) );
}
@Test
public void shouldActivateUserAndRequirePasswordChangeIfRequested() throws Exception
{
userManager.suspendUser( "readSubject" );
assertEmpty( adminSubject, "CALL dbms.security.activateUser('readSubject', true)" );
neo.assertInitFailed( neo.login( "readSubject", "321" ) );
neo.assertPasswordChangeRequired( neo.login( "readSubject", "123" ) );
assertFalse( userManager.getUser( "readSubject" ).hasFlag( IS_SUSPENDED ) );
}
@Test
public void shouldActivateUserAndRequireNoPasswordChangeIfRequested() throws Exception
{
userManager.suspendUser( "readSubject" );
assertEmpty( adminSubject, "CALL dbms.security.activateUser('readSubject', false)" );
assertFalse( userManager.getUser( "readSubject" ).hasFlag( IS_SUSPENDED ) );
}
@Test
public void shouldActivateActiveUser() throws Exception
{
userManager.suspendUser( "readSubject" );
assertEmpty( adminSubject, "CALL dbms.security.activateUser('readSubject')" );
assertEmpty( adminSubject, "CALL dbms.security.activateUser('readSubject')" );
assertFalse( userManager.getUser( "readSubject" ).hasFlag( IS_SUSPENDED ) );
}
@Test
public void shouldFailToActivateNonExistentUser() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.activateUser('Craig')", "User 'Craig' does not exist." );
}
@Test
public void shouldFailToActivateIfNotAdmin() throws Exception
{
userManager.suspendUser( "readSubject" );
assertFail( schemaSubject, "CALL dbms.security.activateUser('readSubject')", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.activateUser('Craig')", PERMISSION_DENIED );
assertFail( schemaSubject, "CALL dbms.security.activateUser('')", PERMISSION_DENIED );
}
@Test
public void shouldFailToActivateYourself() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.activateUser('adminSubject')",
"Activating yourself (user 'adminSubject') is not allowed." );
}
//---------- add user to role -----------
@Test
public void shouldAddRoleToUser() throws Exception
{
assertFalse( "Should not have role publisher", userHasRole( "readSubject", PUBLISHER ) );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'readSubject' )" );
assertTrue( "Should have role publisher", userHasRole( "readSubject", PUBLISHER ) );
}
@Test
public void shouldAddRetainUserInRole() throws Exception
{
assertTrue( "Should have role reader", userHasRole( "readSubject", READER ) );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'readSubject')" );
assertTrue( "Should have still have role reader", userHasRole( "readSubject", READER ) );
}
@Test
public void shouldFailToAddNonExistentUserToRole() throws Exception
{
testFailAddRoleToUser( adminSubject, PUBLISHER, "Olivia", "User 'Olivia' does not exist." );
testFailAddRoleToUser( adminSubject, "thisRoleDoesNotExist", "Olivia", "User 'Olivia' does not exist." );
testFailAddRoleToUser( adminSubject, "", "Olivia", "The provided role name is empty." );
}
@Test
public void shouldFailToAddUserToNonExistentRole() throws Exception
{
testFailAddRoleToUser( adminSubject, "thisRoleDoesNotExist", "readSubject",
"Role 'thisRoleDoesNotExist' does not exist." );
testFailAddRoleToUser( adminSubject, "", "readSubject", "The provided role name is empty." );
}
@Test
public void shouldFailToAddRoleToUserIfNotAdmin() throws Exception
{
testFailAddRoleToUser( pwdSubject, PUBLISHER, "readSubject", CHANGE_PWD_ERR_MSG );
testFailAddRoleToUser( readSubject, PUBLISHER, "readSubject", PERMISSION_DENIED );
testFailAddRoleToUser( writeSubject, PUBLISHER, "readSubject", PERMISSION_DENIED );
testFailAddRoleToUser( schemaSubject, PUBLISHER, "readSubject", PERMISSION_DENIED );
testFailAddRoleToUser( schemaSubject, PUBLISHER, "Olivia", PERMISSION_DENIED );
testFailAddRoleToUser( schemaSubject, "thisRoleDoesNotExist", "Olivia", PERMISSION_DENIED );
}
//---------- remove user from role -----------
@Test
public void shouldRemoveRoleFromUser() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + READER + "', 'readSubject')" );
assertFalse( "Should not have role reader", userHasRole( "readSubject", READER ) );
}
@Test
public void shouldKeepUserOutOfRole() throws Exception
{
assertFalse( "Should not have role publisher", userHasRole( "readSubject", PUBLISHER ) );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'readSubject')" );
assertFalse( "Should not have role publisher", userHasRole( "readSubject", PUBLISHER ) );
}
@Test
public void shouldFailToRemoveNonExistentUserFromRole() throws Exception
{
testFailRemoveRoleFromUser( adminSubject, PUBLISHER, "Olivia", "User 'Olivia' does not exist." );
testFailRemoveRoleFromUser( adminSubject, "thisRoleDoesNotExist", "Olivia", "User 'Olivia' does not exist." );
testFailRemoveRoleFromUser( adminSubject, "", "Olivia", "The provided role name is empty." );
testFailRemoveRoleFromUser( adminSubject, "", "", "The provided role name is empty." );
testFailRemoveRoleFromUser( adminSubject, PUBLISHER, "", "The provided user name is empty." );
}
@Test
public void shouldFailToRemoveUserFromNonExistentRole() throws Exception
{
testFailRemoveRoleFromUser( adminSubject, "thisRoleDoesNotExist", "readSubject",
"Role 'thisRoleDoesNotExist' does not exist." );
testFailRemoveRoleFromUser( adminSubject, "", "readSubject", "The provided role name is empty." );
}
@Test
public void shouldFailToRemoveRoleFromUserIfNotAdmin() throws Exception
{
testFailRemoveRoleFromUser( pwdSubject, PUBLISHER, "readSubject",CHANGE_PWD_ERR_MSG );
testFailRemoveRoleFromUser( readSubject, PUBLISHER, "readSubject", PERMISSION_DENIED );
testFailRemoveRoleFromUser( writeSubject, PUBLISHER, "readSubject", PERMISSION_DENIED );
testFailRemoveRoleFromUser( schemaSubject, READER, "readSubject", PERMISSION_DENIED );
testFailRemoveRoleFromUser( schemaSubject, READER, "Olivia", PERMISSION_DENIED );
testFailRemoveRoleFromUser( schemaSubject, "thisRoleDoesNotExist", "Olivia", PERMISSION_DENIED );
}
@Test
public void shouldFailToRemoveYourselfFromAdminRole() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.removeRoleFromUser('" + ADMIN + "', 'adminSubject')",
"Removing yourself (user 'adminSubject') from the admin role is not allowed." );
}
//---------- manage multiple roles -----------
@Test
public void shouldAllowAddingAndRemovingUserFromMultipleRoles() throws Exception
{
assertFalse( "Should not have role publisher", userHasRole( "readSubject", PUBLISHER ) );
assertFalse( "Should not have role architect", userHasRole( "readSubject", ARCHITECT ) );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'readSubject')" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + ARCHITECT + "', 'readSubject')" );
assertTrue( "Should have role publisher", userHasRole( "readSubject", PUBLISHER ) );
assertTrue( "Should have role architect", userHasRole( "readSubject", ARCHITECT ) );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'readSubject')" );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + ARCHITECT + "', 'readSubject')" );
assertFalse( "Should not have role publisher", userHasRole( "readSubject", PUBLISHER ) );
assertFalse( "Should not have role architect", userHasRole( "readSubject", ARCHITECT ) );
}
//---------- create role -----------
@Test
public void shouldCreateRole() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.createRole('new_role')" );
userManager.getRole( "new_role" );
}
@Test
public void shouldNotCreateRoleIfInvalidRoleName() throws Exception
{
assertFail( adminSubject, "CALL dbms.security.createRole('')", "The provided role name is empty." );
assertFail( adminSubject, "CALL dbms.security.createRole('&%ss!')",
"Role name '&%ss!' contains illegal characters. Use simple ascii characters and numbers." );
assertFail( adminSubject, "CALL dbms.security.createRole('åäöø')",
"Role name 'åäöø' contains illegal characters. Use simple ascii characters and numbers" );
}
@Test
public void shouldNotCreateExistingRole() throws Exception
{
assertFail( adminSubject, format( "CALL dbms.security.createRole('%s')", ARCHITECT),
"The specified role 'architect' already exists" );
assertEmpty( adminSubject, "CALL dbms.security.createRole('new_role')" );
assertFail( adminSubject, "CALL dbms.security.createRole('new_role')",
"The specified role 'new_role' already exists" );
}
@Test
public void shouldNotAllowNonAdminCreateRole() throws Exception
{
testFailCreateRole( pwdSubject, CHANGE_PWD_ERR_MSG );
testFailCreateRole( readSubject, PERMISSION_DENIED );
testFailCreateRole( writeSubject, PERMISSION_DENIED );
testFailCreateRole( schemaSubject, PERMISSION_DENIED );
}
//---------- delete role -----------
@Test
public void shouldThrowIfTryingToDeletePredefinedRole() throws Exception
{
testFailDeleteRole( adminSubject, ADMIN,
format( "'%s' is a predefined role and can not be deleted.", ADMIN ) );
testFailDeleteRole( adminSubject, ARCHITECT,
format( "'%s' is a predefined role and can not be deleted.", ARCHITECT ) );
testFailDeleteRole( adminSubject, PUBLISHER,
format( "'%s' is a predefined role and can not be deleted.", PUBLISHER ) );
testFailDeleteRole( adminSubject, READER,
format( "'%s' is a predefined role and can not be deleted.", READER ) );
}
@Test
public void shouldThrowIfNonAdminTryingToDeleteRole() throws Exception
{
assertEmpty( adminSubject, format("CALL dbms.security.createRole('%s')", "new_role" ) );
testFailDeleteRole( schemaSubject, "new_role", PERMISSION_DENIED);
testFailDeleteRole( writeSubject, "new_role", PERMISSION_DENIED);
testFailDeleteRole( readSubject, "new_role", PERMISSION_DENIED);
testFailDeleteRole( noneSubject, "new_role", PERMISSION_DENIED);
}
@Test
public void shouldThrowIfDeletingNonExistentRole()
{
testFailDeleteRole( adminSubject, "nonExistent", "Role 'nonExistent' does not exist." );
}
@Test
public void shouldDeleteRole() throws Exception
{
neo.getManager().newRole( "new_role" );
assertEmpty( adminSubject, format("CALL dbms.security.deleteRole('%s')", "new_role") );
assertThat( userManager.getAllRoleNames(), not( contains( "new_role" ) ) );
}
@Test
public void deletingRoleAssignedToSelfShouldWork() throws Exception
{
assertEmpty( adminSubject, format( "CALL dbms.security.createRole('%s')", "new_role" ) );
assertEmpty( adminSubject, format( "CALL dbms.security.addRoleToUser('%s', '%s')", "new_role", "adminSubject" ) );
assertThat( userManager.getRoleNamesForUser( "adminSubject" ), hasItem( "new_role" ) );
assertEmpty( this.adminSubject, format( "CALL dbms.security.deleteRole('%s')", "new_role" ) );
assertThat( userManager.getRoleNamesForUser( "adminSubject" ), not( hasItem( "new_role" ) ) );
assertThat( userManager.getAllRoleNames(), not( contains( "new_role" ) ) );
}
//---------- list users -----------
@Test
public void shouldListUsers() throws Exception
{
assertSuccess( adminSubject, "CALL dbms.security.listUsers() YIELD username",
r -> assertKeyIs( r, "username", initialUsers ) );
}
@Test
public void shouldReturnUsersWithRoles() throws Exception
{