-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AuthScenariosInteractionTestBase.java
779 lines (715 loc) · 33 KB
/
AuthScenariosInteractionTestBase.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
/*
* 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.apache.commons.io.Charsets;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge;
import org.neo4j.test.DoubleLatch;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.neo4j.graphdb.security.AuthorizationViolationException.PERMISSION_DENIED;
import static org.neo4j.server.security.enterprise.auth.plugin.api.PredefinedRoles.ADMIN;
import static org.neo4j.server.security.enterprise.auth.plugin.api.PredefinedRoles.ARCHITECT;
import static org.neo4j.server.security.enterprise.auth.plugin.api.PredefinedRoles.PUBLISHER;
import static org.neo4j.server.security.enterprise.auth.plugin.api.PredefinedRoles.READER;
public abstract class AuthScenariosInteractionTestBase<S> extends ProcedureInteractionTestBase<S>
{
//---------- User creation -----------
@Test
public void readOperationsShouldNotBeAllowedWhenPasswordChangeRequired() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', true)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertPasswordChangeRequired( subject );
testFailRead( subject, 3, pwdReqErrMsg( READ_OPS_NOT_ALLOWED ) );
}
@Test
public void passwordChangeShouldEnableRolePermissions() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', true)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertPasswordChangeRequired( subject );
assertPasswordChangeWhenPasswordChangeRequired( subject, "foo" );
subject = neo.login( "Henrik", "foo" );
neo.assertAuthenticated( subject );
testFailWrite( subject );
testSuccessfulRead( subject, 3 );
}
@Test
public void loginShouldFailWithIncorrectPassword() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', true)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "foo" );
neo.assertInitFailed( subject );
}
/*
* Logging scenario smoke test
*/
@Test
public void shouldLogSecurityEvents() throws Exception
{
S mats = neo.login( "mats", "neo4j" );
// for REST, login doesn't happen until the subject does something
neo.executeQuery( mats, "UNWIND [] AS i RETURN 1", Collections.emptyMap(), r -> {} );
assertEmpty( adminSubject, "CALL dbms.security.createUser('mats', 'neo4j', false)" );
assertEmpty( adminSubject, "CALL dbms.security.createRole('role1')" );
assertEmpty( adminSubject, "CALL dbms.security.deleteRole('role1')" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('reader', 'mats')" );
mats = neo.login( "mats", "neo4j" );
assertEmpty( mats, "MATCH (n) WHERE id(n) < 0 RETURN 1" );
assertFail( mats, "CALL dbms.security.changeUserPassword('neo4j', 'hackerPassword')", PERMISSION_DENIED );
assertFail( mats, "CALL dbms.security.changeUserPassword('mats', '')", "A password cannot be empty." );
assertEmpty( mats, "CALL dbms.security.changeUserPassword('mats', 'hackerPassword')" );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('reader', 'mats')" );
assertEmpty( adminSubject, "CALL dbms.security.deleteUser('mats')" );
// flush log
neo.getLocalGraph().shutdown();
// assert on log content
FullLog log = new FullLog();
log.assertHasLine( "mats", "logged in" );
log.assertHasLine( "adminSubject", "created user `mats`" );
log.assertHasLine( "adminSubject", "created role `role1`" );
log.assertHasLine( "adminSubject", "deleted role `role1`" );
log.assertHasLine( "mats", "logged in" );
log.assertHasLine( "adminSubject", "added role `reader` to user `mats`" );
log.assertHasLine( "mats", "tried to change password for user `neo4j`: " + PERMISSION_DENIED);
log.assertHasLine( "mats", "tried to change password: A password cannot be empty.");
log.assertHasLine( "mats", "changed password");
log.assertHasLine( "adminSubject", "removed role `reader` from user `mats`");
log.assertHasLine( "adminSubject", "deleted user `mats`");
}
private class FullLog
{
List<String> lines;
FullLog() throws IOException
{
lines = new ArrayList<>();
BufferedReader bufferedReader = new BufferedReader(
neo.fileSystem().openAsReader( new File( securityLog.getAbsolutePath() ), Charsets.UTF_8 ) );
lines.add( bufferedReader.readLine() );
bufferedReader.lines().forEach( lines::add );
}
void assertHasLine( String subject, String msg )
{
assertThat( lines, hasItem( containsString( "[" + subject + "]: " + msg ) ) );
}
}
/*
Admin creates user Henrik with password bar
Henrik logs in with correct password (gets prompted to change - change to foo)
Henrik starts read transaction → permission denied
Admin adds user Henrik to role Reader
Henrik starts write transaction → permission denied
Henrik starts read transaction → ok
Henrik logs off
*/
@Test
public void userCreation2() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', true)" );
S subject = neo.login( "Henrik", "bar" );
neo.assertPasswordChangeRequired( subject );
assertPasswordChangeWhenPasswordChangeRequired( subject, "foo" );
subject = neo.login( "Henrik", "foo" );
neo.assertAuthenticated( subject );
testFailRead( subject, 3 );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
testFailWrite( subject );
testSuccessfulRead( subject, 3 );
}
/*
Admin creates user Henrik with password bar
Henrik logs in with correct password
Henrik starts read transaction → permission denied
Admin adds user Henrik to role Publisher
Henrik starts write transaction → ok
Henrik starts read transaction → ok
Henrik starts schema transaction → permission denied
Henrik logs off
*/
@Test
public void userCreation3() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testFailRead( subject, 3 );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
testSuccessfulWrite( subject );
testSuccessfulRead( subject, 4 );
testFailSchema( subject );
}
/*
Admin creates user Henrik with password bar
Henrik logs in with correct password
Henrik starts read transaction → permission denied
Henrik starts write transaction → permission denied
Henrik starts schema transaction → permission denied
Henrik creates user Craig → permission denied
Admin adds user Henrik to role Architect
Henrik starts write transaction → ok
Henrik starts read transaction → ok
Henrik starts schema transaction → ok
Henrik creates user Craig → permission denied
Henrik logs off
*/
@Test
public void userCreation4() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testFailRead( subject, 3 );
testFailWrite( subject );
testFailSchema( subject );
testFailCreateUser( subject, PERMISSION_DENIED );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + ARCHITECT + "', 'Henrik')" );
testSuccessfulWrite( subject );
testSuccessfulRead( subject, 4 );
testSuccessfulSchema( subject );
testFailCreateUser( subject, PERMISSION_DENIED );
}
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
Henrik logs in with correct password
Henrik creates user Craig → permission denied
Henrik logs off
*/
@Test
public void userCreation5() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
testFailCreateUser( subject, PERMISSION_DENIED );
}
//---------- User deletion -----------
/*
Admin creates user Henrik with password bar
Admin deletes user Henrik
Henrik logs in with correct password → fail
*/
@Test
public void userDeletion1() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.deleteUser('Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertInitFailed( subject );
}
/*
Admin creates user Henrik with password bar
Admin deletes user Henrik
Admin adds user Henrik to role Publisher → fail
*/
@Test
public void userDeletion2() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.deleteUser('Henrik')" );
assertFail( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')",
"User 'Henrik' does not exist" );
}
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
Admin deletes user Henrik
Admin removes user Henrik from role Publisher → fail
*/
@Test
public void userDeletion3() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
assertEmpty( adminSubject, "CALL dbms.security.deleteUser('Henrik')" );
assertFail( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'Henrik')",
"User 'Henrik' does not exist" );
}
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
User Henrik logs in with correct password → ok
Admin deletes user Henrik
Henrik starts transaction with read query → fail
Henrik tries to login again → fail
*/
@Test
public void userDeletion4() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S henrik = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( henrik );
assertEmpty( adminSubject, "CALL dbms.security.deleteUser('Henrik')" );
neo.assertSessionKilled( henrik );
henrik = neo.login( "Henrik", "bar" );
neo.assertInitFailed( henrik );
}
//---------- Role management -----------
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
Henrik logs in with correct password
Henrik starts transaction with write query → ok
Admin removes user Henrik from role Publisher
Henrik starts transaction with read query → permission denied
Admin adds Henrik to role Reader
Henrik starts transaction with write query → permission denied
Henrik starts transaction with read query → ok
*/
@Test
public void roleManagement1() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testSuccessfulWrite( subject );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'Henrik')" );
testFailRead( subject, 4 );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
testFailWrite( subject );
testSuccessfulRead( subject, 4 );
}
/*
Admin creates user Henrik with password bar
Henrik logs in with correct password
Henrik starts transaction with write query → permission denied
Admin adds user Henrik to role Publisher → ok
Admin adds user Henrik to role Publisher → ok
Henrik starts transaction with write query → ok
*/
@Test
public void roleManagement2() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testFailWrite( subject );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
testSuccessfulWrite( subject );
}
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
Henrik logs in with correct password
Admin adds user Henrik to role Reader
Henrik starts transaction with write query → ok
Henrik starts transaction with read query → ok
Admin removes user Henrik from role Publisher
Henrik starts transaction with write query → permission denied
Henrik starts transaction with read query → ok
*/
@Test
public void roleManagement3() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
testSuccessfulWrite( subject );
testSuccessfulRead( subject, 4 );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'Henrik')" );
testFailWrite( subject );
testSuccessfulRead( subject, 4 );
}
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
Henrik logs in with correct password
Admin adds user Henrik to role Reader
Henrik starts transaction with write query → ok
Henrik starts transaction with read query → ok
Admin removes user Henrik from all roles
Henrik starts transaction with write query → permission denied
Henrik starts transaction with read query → permission denied
*/
@Test
public void roleManagement4() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
testSuccessfulWrite( subject );
testSuccessfulRead( subject, 4 );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + READER + "', 'Henrik')" );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'Henrik')" );
testFailWrite( subject );
testFailRead( subject, 4 );
}
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
Henrik logs in with correct password
Henrik starts transaction with long running writing query Q
Admin removes user Henrik from role Publisher (while Q still running)
Q finishes and transaction is committed → ok
Henrik starts new transaction with write query → permission denied
*/
@Test
public void roleManagement5() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S henrik = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( henrik );
DoubleLatch latch = new DoubleLatch( 2 );
ThreadedTransaction<S> write = new ThreadedTransaction<>( neo, latch );
write.executeCreateNode( threading, henrik );
latch.startAndWaitForAllToStart();
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'Henrik')" );
latch.finishAndWaitForAllToFinish();
write.closeAndAssertSuccess();
testFailWrite( henrik );
}
/*
* Procedure 'test.allowedReadProcedure' with READ mode and 'allowed = role1' is loaded.
* Procedure 'test.allowedWriteProcedure' with WRITE mode and 'allowed = role1' is loaded.
* Procedure 'test.allowedSchemaProcedure' with SCHEMA mode and 'allowed = role1' is loaded.
* Admin creates a new user 'mats'.
* 'mats' logs in.
* 'mats' executes the procedures, access denied.
* Admin creates 'role1'.
* 'mats' executes the procedures, access denied.
* Admin adds role 'role1' to 'mats'.
* 'mats' executes the procedures successfully.
* Admin removes the role 'role1'.
* 'mats' executes the procedures, access denied.
* Admin creates the role 'role1' again (new).
* 'mats' executes the procedures, access denied.
* Admin adds role 'architect' to 'mats'.
* 'mats' executes the procedures successfully.
* Admin adds 'role1' to 'mats'.
* 'mats' executes the procedures successfully.
*/
@Test
public void customRoleWithProcedureAccess() throws Exception
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('mats', 'neo4j', false)" );
S mats = neo.login( "mats", "neo4j" );
testFailTestProcs( mats );
assertEmpty( adminSubject, "CALL dbms.security.createRole('role1')" );
testFailTestProcs( mats );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('role1', 'mats')" );
testSuccessfulTestProcs( mats );
assertEmpty( adminSubject, "CALL dbms.security.deleteRole('role1')" );
testFailTestProcs( mats );
assertEmpty( adminSubject, "CALL dbms.security.createRole('role1')" );
testFailTestProcs( mats );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('architect', 'mats')" );
testSuccessfulTestProcs( mats );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('role1', 'mats')" );
testSuccessfulTestProcs( mats );
}
//---------- User suspension -----------
/*
Admin creates user Henrik with password bar
Henrik logs in with correct password → ok
Henrik logs off
Admin suspends user Henrik
User Henrik logs in with correct password → fail
*/
@Test
public void userSuspension1() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
neo.logout( subject );
assertEmpty( adminSubject, "CALL dbms.security.suspendUser('Henrik')" );
subject = neo.login( "Henrik", "bar" );
neo.assertInitFailed( subject );
}
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Reader
Henrik logs in with correct password → ok
Henrik starts and completes transaction with read query → ok
Admin suspends user Henrik
Henrik’s session is terminated
Henrik logs in with correct password → fail
*/
@Test
public void userSuspension2() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testSuccessfulRead( subject, 3 );
assertEmpty( adminSubject, "CALL dbms.security.suspendUser('Henrik')" );
neo.assertSessionKilled( subject );
subject = neo.login( "Henrik", "bar" );
neo.assertInitFailed( subject );
}
//---------- User activation -----------
/*
Admin creates user Henrik with password bar
Admin suspends user Henrik
Henrik logs in with correct password → fail
Admin reinstates user Henrik
Henrik logs in with correct password → ok
*/
@Test
public void userActivation1() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.suspendUser('Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertInitFailed( subject );
assertEmpty( adminSubject, "CALL dbms.security.activateUser('Henrik', false)" );
subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
}
//---------- list users / roles -----------
/*
Admin lists all users → ok
Admin creates user Henrik with password bar
Admin lists all users → ok
Henrik logs in with correct password → ok
Henrik lists all users → permission denied
Admin adds user Henrik to role Admin
Henrik lists all users → ok
*/
@Test
public void userListing() throws Throwable
{
testSuccessfulListUsers( adminSubject, initialUsers );
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
testSuccessfulListUsers( adminSubject, with( initialUsers, "Henrik" ) );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testFailListUsers( subject, 6, PERMISSION_DENIED );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + ADMIN + "', 'Henrik')" );
testSuccessfulListUsers( subject, with( initialUsers, "Henrik" ) );
}
/*
Admin creates user Henrik with password bar
Henrik logs in with correct password → ok
Henrik lists all roles → permission denied
Admin lists all roles → ok
Admin adds user Henrik to role Admin
Henrik lists all roles → ok
*/
@Test
public void rolesListing() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testFailListRoles( subject, PERMISSION_DENIED);
testSuccessfulListRoles( adminSubject, initialRoles );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + ADMIN + "', 'Henrik')" );
testSuccessfulListRoles( subject, initialRoles );
}
/*
Admin creates user Henrik with password bar
Admin creates user Craig
Admin adds user Craig to role Publisher
Henrik logs in with correct password → ok
Henrik lists all roles for user Craig → permission denied
Admin lists all roles for user Craig → ok
Admin adds user Henrik to role Publisher
Craig logs in with correct password → ok
Craig lists all roles for user Craig → ok
*/
@Test
public void listingUserRoles() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.createUser('Craig', 'foo', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Craig')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testFailListUserRoles( subject, "Craig", PERMISSION_DENIED );
assertSuccess( adminSubject, "CALL dbms.security.listRolesForUser('Craig') YIELD value as roles RETURN roles",
r -> assertKeyIs( r, "roles", PUBLISHER ) );
S craigSubject = neo.login( "Craig", "foo" );
assertSuccess( craigSubject, "CALL dbms.security.listRolesForUser('Craig') YIELD value as roles RETURN roles",
r -> assertKeyIs( r, "roles", PUBLISHER ) );
}
/*
Admin creates user Henrik with password bar
Admin creates user Craig
Admin adds user Henrik to role Publisher
Admin adds user Craig to role Publisher
Henrik logs in with correct password → ok
Henrik lists all users for role Publisher → permission denied
Admin lists all users for role Publisher → ok
*/
@Test
public void listingRoleUsers() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.createUser('Craig', 'foo', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Craig')" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( subject );
testFailListRoleUsers( subject, PUBLISHER, PERMISSION_DENIED );
assertSuccess( adminSubject,
"CALL dbms.security.listUsersForRole('" + PUBLISHER + "') YIELD value as users RETURN users",
r -> assertKeyIs( r, "users", "Henrik", "Craig", "writeSubject" ) );
}
//---------- calling procedures -----------
/*
Admin creates user Henrik with password bar
Admin adds user Henrik to role Publisher
Henrik logs in with correct password → ok
Henrik calls procedure marked as read-only → ok
Henrik calls procedure marked as read-write → ok
Admin adds user Henrik to role Reader
Henrik calls procedure marked as read-only → ok
Henrik calls procedure marked as read-write → ok
Admin removes Henrik from role Publisher
Henrik calls procedure marked as read-only → ok
Henrik calls procedure marked as read-write → permission denied
*/
@Test
public void callProcedures1() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'bar', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + PUBLISHER + "', 'Henrik')" );
S henrik = neo.login( "Henrik", "bar" );
neo.assertAuthenticated( henrik );
assertEmpty( henrik, "CALL test.createNode()" );
assertSuccess( henrik, "CALL test.numNodes() YIELD count as count RETURN count",
r -> assertKeyIs( r, "count", "4" ) );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
assertEmpty( henrik, "CALL test.createNode()" );
assertSuccess( henrik, "CALL test.numNodes() YIELD count as count RETURN count",
r -> assertKeyIs( r, "count", "5" ) );
assertEmpty( adminSubject, "CALL dbms.security.removeRoleFromUser('" + PUBLISHER + "', 'Henrik')" );
assertFail( henrik, "CALL test.createNode()",
"Write operations are not allowed for user 'Henrik' with roles [reader]." );
}
//---------- change password -----------
/*
Admin creates user Henrik with password abc
Admin adds user Henrik to role Reader
Henrik logs in with correct password → ok
Henrik starts transaction with read query → ok
Henrik changes password to 123
Henrik starts transaction with read query → ok
Henrik logs out
Henrik logs in with password abc → fail
Henrik logs in with password 123 → ok
Henrik starts transaction with read query → ok
Henrik logs out
*/
@Test
public void changeUserPassword1() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'abc', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "abc" );
neo.assertAuthenticated( subject );
testSuccessfulRead( subject, 3 );
assertEmpty( subject, "CALL dbms.security.changeUserPassword('Henrik', '123', false)" );
neo.updateAuthToken( subject, "Henrik", "123" ); // Because RESTSubject caches an auth token that is sent with every request
testSuccessfulRead( subject, 3 );
neo.logout( subject );
subject = neo.login( "Henrik", "abc" );
neo.assertInitFailed( subject );
subject = neo.login( "Henrik", "123" );
neo.assertAuthenticated( subject );
testSuccessfulRead( subject, 3 );
}
/*
Admin creates user Henrik with password abc
Admin adds user Henrik to role Reader
Henrik logs in with password abc → ok
Henrik starts transaction with read query → ok
Admin changes user Henrik’s password to 123
Henrik logs out
Henrik logs in with password abc → fail
Henrik logs in with password 123 → ok
Henrik starts transaction with read query → ok
Henrik logs out
*/
@Test
public void changeUserPassword2() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'abc', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "abc" );
neo.assertAuthenticated( subject );
testSuccessfulRead( subject, 3 );
assertEmpty( adminSubject, "CALL dbms.security.changeUserPassword('Henrik', '123', false)" );
neo.logout( subject );
subject = neo.login( "Henrik", "abc" );
neo.assertInitFailed( subject );
subject = neo.login( "Henrik", "123" );
neo.assertAuthenticated( subject );
testSuccessfulRead( subject, 3 );
}
/*
Admin creates user Henrik with password abc
Admin creates user Craig
Admin adds user Henrik to role Reader
Henrik logs in with password abc → ok
Henrik starts transaction with read query → ok
Henrik changes Craig’s password to 123 → fail
*/
@Test
public void changeUserPassword3() throws Throwable
{
assertEmpty( adminSubject, "CALL dbms.security.createUser('Craig', 'abc', false)" );
assertEmpty( adminSubject, "CALL dbms.security.createUser('Henrik', 'abc', false)" );
assertEmpty( adminSubject, "CALL dbms.security.addRoleToUser('" + READER + "', 'Henrik')" );
S subject = neo.login( "Henrik", "abc" );
neo.assertAuthenticated( subject );
testSuccessfulRead( subject, 3 );
assertFail( subject, "CALL dbms.security.changeUserPassword('Craig', '123')", PERMISSION_DENIED );
}
// OTHER TESTS
@Test
public void shouldNotTryToCreateTokensWhenReading()
{
assertEmpty( adminSubject, "CREATE (:MyNode)" );
assertSuccess( readSubject, "MATCH (n:MyNode) WHERE n.nonExistent = 'foo' RETURN toString(count(*)) AS c",
r -> assertKeyIs( r, "c", "0" ) );
assertFail( readSubject, "MATCH (n:MyNode) SET n.nonExistent = 'foo' RETURN toString(count(*)) AS c",
WRITE_OPS_NOT_ALLOWED );
assertFail( readSubject, "MATCH (n:MyNode) SET n:Foo RETURN toString(count(*)) AS c",
WRITE_OPS_NOT_ALLOWED );
assertSuccess( writeSubject, "MATCH (n:MyNode) SET n.nonExistent = 'foo' RETURN toString(count(*)) AS c",
r -> assertKeyIs( r, "c", "1" ) );
assertSuccess( readSubject, "MATCH (n:MyNode) WHERE n.nonExistent = 'foo' RETURN toString(count(*)) AS c",
r -> assertKeyIs( r, "c", "1" ) );
}
}