-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AuthProcedures.java
618 lines (557 loc) · 23 KB
/
AuthProcedures.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
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.security.enterprise.auth;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.neo4j.graphdb.security.AuthorizationViolationException;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.KernelTransactionHandle;
import org.neo4j.kernel.api.bolt.BoltConnectionTracker;
import org.neo4j.kernel.api.bolt.ManagedBoltStateMachine;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.security.AuthSubject;
import org.neo4j.kernel.api.security.exception.InvalidArgumentsException;
import org.neo4j.kernel.impl.api.KernelTransactions;
import org.neo4j.kernel.impl.enterprise.SecurityLog;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;
import static java.util.function.Predicate.isEqual;
import static java.lang.String.format;
import static org.neo4j.kernel.impl.api.security.OverriddenAccessMode.getUsernameFromAccessMode;
import static org.neo4j.procedure.Mode.DBMS;
@SuppressWarnings( "unused" )
public class AuthProcedures
{
public static final String PERMISSION_DENIED = "Permission denied.";
@Context
public AuthSubject authSubject;
@Context
public GraphDatabaseAPI graph;
@Context
public KernelTransaction tx;
@Context
public SecurityLog securityLog;
@Description( "Create a new user." )
@Procedure( name = "dbms.security.createUser", mode = DBMS )
public void createUser(
@Name( "username" ) String username,
@Name( "password" ) String password,
@Name( value = "requirePasswordChange", defaultValue = "true" ) boolean requirePasswordChange
)
throws InvalidArgumentsException, IOException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
adminSubject.getUserManager().newUser( username, password, requirePasswordChange );
securityLog.info( authSubject, "created user `%s`", username );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to create user `%s`: %s", username, e.getMessage() );
throw e;
}
}
@Description( "Change the current user's password." )
@Procedure( name = "dbms.security.changeUserPassword", mode = DBMS )
public void changeUserPassword(
@Name( "username" ) String username,
@Name( "newPassword" ) String newPassword,
@Name( value = "requirePasswordChange", defaultValue = "true" ) boolean requirePasswordChange
)
throws InvalidArgumentsException, IOException
{
String ownOrOther = format("password for user `%s`", username );
try
{
StandardEnterpriseAuthSubject enterpriseSubject = StandardEnterpriseAuthSubject.castOrFail( authSubject );
if ( enterpriseSubject.doesUsernameMatch( username ) )
{
ownOrOther = "own password";
enterpriseSubject.setPassword( newPassword, requirePasswordChange );
securityLog.info( authSubject, "changed own password" );
}
else if ( !enterpriseSubject.isAdmin() )
{
throw new AuthorizationViolationException( PERMISSION_DENIED );
}
else
{
enterpriseSubject.getUserManager().setUserPassword( username, newPassword, requirePasswordChange );
terminateTransactionsForValidUser( username );
terminateConnectionsForValidUser( username );
securityLog.info( authSubject, "changed password for user `%s`", username );
}
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to change %s: %s", ownOrOther, e.getMessage() );
throw e;
}
}
@Description( "Assign a role to the user." )
@Procedure( name = "dbms.security.addRoleToUser", mode = DBMS )
public void addRoleToUser(@Name( "roleName" ) String roleName, @Name( "username" ) String username )
throws IOException, InvalidArgumentsException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
adminSubject.getUserManager().addRoleToUser( roleName, username );
securityLog.info( authSubject, "added role `%s` to user `%s`", roleName, username );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to add role `%s` to user `%s`: %s",
roleName, username, e.getMessage() );
throw e;
}
}
@Description( "Unassign a role from the user." )
@Procedure( name = "dbms.security.removeRoleFromUser", mode = DBMS )
public void removeRoleFromUser( @Name( "roleName" ) String roleName, @Name( "username" ) String username )
throws InvalidArgumentsException, IOException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
if ( adminSubject.doesUsernameMatch( username ) && roleName.equals( PredefinedRolesBuilder.ADMIN ) )
{
throw new InvalidArgumentsException(
"Removing yourself (user '" + username + "') from the admin role is not allowed." );
}
adminSubject.getUserManager().removeRoleFromUser( roleName, username );
securityLog.info( authSubject, "removed role `%s` from user `%s`", roleName, username );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to remove role `%s` from user `%s`: %s", roleName, username, e
.getMessage() );
throw e;
}
}
@Description( "Delete the specified user." )
@Procedure( name = "dbms.security.deleteUser", mode = DBMS )
public void deleteUser( @Name( "username" ) String username ) throws InvalidArgumentsException, IOException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
if ( adminSubject.doesUsernameMatch( username ) )
{
throw new InvalidArgumentsException( "Deleting yourself (user '" + username + "') is not allowed." );
}
adminSubject.getUserManager().deleteUser( username );
securityLog.info( authSubject, "deleted user `%s`", username );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to delete user `%s`: %s", username, e.getMessage() );
throw e;
}
kickoutUser( username, "deletion" );
}
private void kickoutUser( String username, String reason )
{
try
{
terminateTransactionsForValidUser( username );
terminateConnectionsForValidUser( username );
}
catch ( Exception e )
{
securityLog.error( authSubject, "failed to terminate running transaction and bolt connections for " +
"user `%s` following %s: %s", username, reason, e.getMessage() );
throw e;
}
}
@Description( "Suspend the specified user." )
@Procedure( name = "dbms.security.suspendUser", mode = DBMS )
public void suspendUser( @Name( "username" ) String username ) throws IOException, InvalidArgumentsException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
if ( adminSubject.doesUsernameMatch( username ) )
{
throw new InvalidArgumentsException( "Suspending yourself (user '" + username +
"') is not allowed." );
}
adminSubject.getUserManager().suspendUser( username );
securityLog.info( authSubject, "suspended user `%s`", username );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to suspend user `%s`: %s", username, e.getMessage() );
throw e;
}
kickoutUser( username, "suspension" );
}
@Description( "Activate a suspended user." )
@Procedure( name = "dbms.security.activateUser", mode = DBMS )
public void activateUser(
@Name( "username" ) String username,
@Name( value = "requirePasswordChange", defaultValue = "true" ) boolean requirePasswordChange
) throws IOException, InvalidArgumentsException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
if ( adminSubject.doesUsernameMatch( username ) )
{
throw new InvalidArgumentsException( "Activating yourself (user '" + username +
"') is not allowed." );
}
adminSubject.getUserManager().activateUser( username, requirePasswordChange );
securityLog.info( authSubject, "activated user `%s`", username );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to activate user `%s`: %s", username, e.getMessage() );
throw e;
}
}
@Description( "Show the current user." )
@Procedure( name = "dbms.security.showCurrentUser", mode = DBMS )
public Stream<UserResult> showCurrentUser() throws InvalidArgumentsException, IOException
{
StandardEnterpriseAuthSubject enterpriseSubject = StandardEnterpriseAuthSubject.castOrFail( authSubject );
EnterpriseUserManager userManager = enterpriseSubject.getUserManager();
return Stream.of( new UserResult( enterpriseSubject.username(),
userManager.getRoleNamesForUser( enterpriseSubject.username() ),
userManager.getUser( enterpriseSubject.username() ).getFlags() ) );
}
@Description( "List all local users." )
@Procedure( name = "dbms.security.listUsers", mode = DBMS )
public Stream<UserResult> listUsers() throws InvalidArgumentsException, IOException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
EnterpriseUserManager userManager = adminSubject.getUserManager();
Set<String> users = userManager.getAllUsernames();
List<UserResult> results = new ArrayList<>();
for ( String u : users )
{
results.add(
new UserResult( u, userManager.getRoleNamesForUser( u ), userManager.getUser( u ).getFlags() ) );
}
return results.stream();
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to list users: %s", e.getMessage() );
throw e;
}
}
@Description( "List all available roles." )
@Procedure( name = "dbms.security.listRoles", mode = DBMS )
public Stream<RoleResult> listRoles() throws InvalidArgumentsException, IOException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
EnterpriseUserManager userManager = adminSubject.getUserManager();
Set<String> roles = userManager.getAllRoleNames();
List<RoleResult> results = new ArrayList<>();
for ( String r : roles )
{
results.add( new RoleResult( r, userManager.getUsernamesForRole( r ) ) );
}
return results.stream();
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to list roles: %s", e.getMessage() );
throw e;
}
}
@Description( "List all roles assigned to the specified user." )
@Procedure( name = "dbms.security.listRolesForUser", mode = DBMS )
public Stream<StringResult> listRolesForUser( @Name( "username" ) String username )
throws InvalidArgumentsException, IOException
{
try
{
StandardEnterpriseAuthSubject subject = ensureSelfOrAdminAuthSubject( username );
return subject.getUserManager().getRoleNamesForUser( username ).stream().map( StringResult::new );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to list roles for user `%s`: %s", username, e.getMessage() );
throw e;
}
}
@Description( "List all users currently assigned the specified role." )
@Procedure( name = "dbms.security.listUsersForRole", mode = DBMS )
public Stream<StringResult> listUsersForRole( @Name( "roleName" ) String roleName )
throws InvalidArgumentsException, IOException
{
try
{
StandardEnterpriseAuthSubject adminSubject = ensureAdminAuthSubject();
return adminSubject.getUserManager().getUsernamesForRole( roleName ).stream().map( StringResult::new );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to list users for role `%s`: %s", roleName, e.getMessage() );
throw e;
}
}
@Description( "Create a new role." )
@Procedure( name = "dbms.security.createRole", mode = DBMS)
public void createRole( @Name( "roleName" ) String roleName )
throws InvalidArgumentsException, IOException
{
try
{
ensureAdminAuthSubject().getUserManager().newRole( roleName );
securityLog.info( authSubject, "created role `%s`", roleName );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to create role `%s`: %s", roleName, e.getMessage() );
throw e;
}
}
@Description( "Delete the specified role. Any role assignments will be removed." )
@Procedure( name = "dbms.security.deleteRole", mode = DBMS)
public void deleteRole( @Name( "roleName" ) String roleName )
throws InvalidArgumentsException, IOException
{
try
{
ensureAdminAuthSubject().getUserManager().deleteRole( roleName );
securityLog.info( authSubject, "deleted role `%s`", roleName );
}
catch ( Exception e )
{
securityLog.error( authSubject, "tried to delete role `%s`: %s", roleName, e.getMessage() );
throw e;
}
}
// TODO: add security logging once listQueries PR is in
// ====================================================== FROM HERE
@Procedure( name = "dbms.security.listTransactions", mode = DBMS )
public Stream<TransactionResult> listTransactions()
throws InvalidArgumentsException, IOException
{
ensureAdminAuthSubject();
return countTransactionByUsername(
getActiveTransactions().stream()
.filter( tx -> !tx.terminationReason().isPresent() )
.map( tx -> getUsernameFromAccessMode( tx.mode() ) )
);
}
@Procedure( name = "dbms.security.terminateTransactionsForUser", mode = DBMS )
public Stream<TransactionTerminationResult> terminateTransactionsForUser( @Name( "username" ) String username )
throws InvalidArgumentsException, IOException
{
ensureSelfOrAdminAuthSubject( username );
return terminateTransactionsForValidUser( username );
}
@Procedure( name = "dbms.security.listConnections", mode = DBMS )
public Stream<ConnectionResult> listConnections()
{
ensureAdminAuthSubject();
BoltConnectionTracker boltConnectionTracker = getBoltConnectionTracker();
return countConnectionsByUsername(
boltConnectionTracker.getActiveConnections().stream()
.filter( session -> !session.hasTerminated() )
.map( ManagedBoltStateMachine::owner )
);
}
@Procedure( name = "dbms.security.terminateConnectionsForUser", mode = DBMS )
public Stream<ConnectionResult> terminateConnectionsForUser( @Name( "username" ) String username )
throws InvalidArgumentsException
{
StandardEnterpriseAuthSubject subject = StandardEnterpriseAuthSubject.castOrFail( authSubject );
if ( !subject.isAdmin() && !subject.doesUsernameMatch( username ) )
{
throw new AuthorizationViolationException( PERMISSION_DENIED );
}
subject.getUserManager().getUser( username );
return terminateConnectionsForValidUser( username );
}
// ====================================================== TO HERE
// ----------------- helpers ---------------------
Stream<TransactionTerminationResult> terminateTransactionsForValidUser( String username )
{
long terminatedCount = 0;
for ( KernelTransactionHandle tx : getActiveTransactions() )
{
if ( getUsernameFromAccessMode( tx.mode() ).equals( username ) && !tx.isUnderlyingTransaction( this.tx ) )
{
boolean marked = tx.markForTermination( Status.Transaction.Terminated );
if ( marked )
{
terminatedCount++;
}
}
}
return Stream.of( new TransactionTerminationResult( username, terminatedCount ) );
}
Stream<ConnectionResult> terminateConnectionsForValidUser( String username )
{
Long killCount = 0L;
for ( ManagedBoltStateMachine connection : getBoltConnectionTracker().getActiveConnections( username ) )
{
connection.terminate();
killCount += 1;
}
return Stream.of( new ConnectionResult( username, killCount ) );
}
private Set<KernelTransactionHandle> getActiveTransactions()
{
return graph.getDependencyResolver().resolveDependency( KernelTransactions.class ).activeTransactions();
}
private BoltConnectionTracker getBoltConnectionTracker()
{
return graph.getDependencyResolver().resolveDependency( BoltConnectionTracker.class );
}
private Stream<TransactionResult> countTransactionByUsername( Stream<String> usernames )
{
return usernames.collect(
Collectors.groupingBy( Function.identity(), Collectors.counting() )
).entrySet().stream().map(
entry -> new TransactionResult( entry.getKey(), entry.getValue() )
);
}
private Stream<ConnectionResult> countConnectionsByUsername( Stream<String> usernames )
{
return usernames.collect(
Collectors.groupingBy( Function.identity(), Collectors.counting() )
).entrySet().stream().map(
entry -> new ConnectionResult( entry.getKey(), entry.getValue() )
);
}
private StandardEnterpriseAuthSubject ensureAdminAuthSubject()
{
StandardEnterpriseAuthSubject enterpriseAuthSubject = StandardEnterpriseAuthSubject.castOrFail( authSubject );
if ( !enterpriseAuthSubject.isAdmin() )
{
throw new AuthorizationViolationException( PERMISSION_DENIED );
}
return enterpriseAuthSubject;
}
private StandardEnterpriseAuthSubject ensureSelfOrAdminAuthSubject( String username ) throws InvalidArgumentsException
{
StandardEnterpriseAuthSubject subject = StandardEnterpriseAuthSubject.castOrFail( authSubject );
if ( subject.isAdmin() || subject.doesUsernameMatch( username ) )
{
subject.getUserManager().getUser( username );
return subject;
}
throw new AuthorizationViolationException( PERMISSION_DENIED );
}
public static class StringResult
{
public final String value;
StringResult( String value )
{
this.value = value;
}
}
public static class UserResult
{
public final String username;
public final List<String> roles;
public final List<String> flags;
UserResult( String username, Set<String> roles, Iterable<String> flags )
{
this.username = username;
this.roles = new ArrayList<>();
this.roles.addAll( roles );
this.flags = new ArrayList<>();
for ( String f : flags ) {this.flags.add( f );}
}
}
public static class RoleResult
{
public final String role;
public final List<String> users;
RoleResult( String role, Set<String> users )
{
this.role = role;
this.users = new ArrayList<>();
this.users.addAll( users );
}
}
public static class TransactionResult
{
public final String username;
public final Long activeTransactions;
TransactionResult( String username, Long activeTransactions )
{
this.username = username;
this.activeTransactions = activeTransactions;
}
}
public static class QueryStatusResult
{
public final long queryId;
public final String username;
public final String query;
public final Map<String, Object> parameters;
public final long startTime;
public QueryStatusResult(
long queryId,
String username,
String query,
Map<String,Object> parameters,
long startTime )
{
this.queryId = queryId;
this.username = username;
this.query = query;
this.parameters = parameters;
this.startTime = startTime;
}
}
public static class TransactionTerminationResult
{
public final String username;
public final Long transactionsTerminated;
TransactionTerminationResult( String username, Long transactionsTerminated )
{
this.username = username;
this.transactionsTerminated = transactionsTerminated;
}
}
public static class ConnectionResult
{
public final String username;
public final Long connectionCount;
ConnectionResult( String username, Long connectionCount )
{
this.username = username;
this.connectionCount = connectionCount;
}
}
}