-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
QueryLoggerIT.java
624 lines (537 loc) · 27.4 KB
/
QueryLoggerIT.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
/*
* Copyright (c) 2002-2017 "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.kernel.impl.query;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.security.AuthToken;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.enterprise.api.security.EnterpriseAuthManager;
import org.neo4j.kernel.enterprise.api.security.EnterpriseSecurityContext;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.server.security.enterprise.auth.EmbeddedInteraction;
import org.neo4j.server.security.enterprise.auth.EnterpriseAuthAndUserManager;
import org.neo4j.test.TestEnterpriseGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.kernel.api.security.AuthSubject.AUTH_DISABLED;
public class QueryLoggerIT
{
// It is imperitive that this test executes using a real filesystem; otherwise rotation failures will not be
// detected on Windows.
@Rule
public final DefaultFileSystemRule fileSystem = new DefaultFileSystemRule();
@Rule
public final TestDirectory testDirectory = TestDirectory.testDirectory();
@Rule
public final ExpectedException expectedException = ExpectedException.none();
private GraphDatabaseBuilder databaseBuilder;
private static final String QUERY = "CREATE (n:Foo {bar: 'baz'})";
private File logsDirectory;
private File logFilename;
@Before
public void setUp()
{
logsDirectory = new File( testDirectory.graphDbDir(), "logs" );
logFilename = new File( logsDirectory, "query.log" );
AssertableLogProvider inMemoryLog = new AssertableLogProvider();
databaseBuilder = new TestEnterpriseGraphDatabaseFactory().setFileSystem( fileSystem.get() )
.setInternalLogProvider( inMemoryLog )
.newImpermanentDatabaseBuilder( testDirectory.graphDbDir() );
}
@Test
public void shouldLogCustomUserName() throws Throwable
{
// turn on query logging
final Map<String, String> config = stringMap(
GraphDatabaseSettings.logs_directory.name(), logsDirectory.getPath(),
GraphDatabaseSettings.log_queries.name(), Settings.TRUE );
EmbeddedInteraction db = new EmbeddedInteraction( databaseBuilder, config );
// create users
db.getLocalUserManager().newUser( "mats", "neo4j", false );
db.getLocalUserManager().newUser( "andres", "neo4j", false );
db.getLocalUserManager().addRoleToUser( "architect", "mats" );
db.getLocalUserManager().addRoleToUser( "reader", "andres" );
EnterpriseSecurityContext mats = db.login( "mats", "neo4j" );
// run query
db.executeQuery( mats, "UNWIND range(0, 10) AS i CREATE (:Foo {p: i})", Collections.emptyMap(), ResourceIterator::close );
db.executeQuery( mats, "CREATE (:Label)", Collections.emptyMap(), ResourceIterator::close );
// switch user, run query
EnterpriseSecurityContext andres = db.login( "andres", "neo4j" );
db.executeQuery( andres, "MATCH (n:Label) RETURN n", Collections.emptyMap(), ResourceIterator::close );
db.tearDown();
// THEN
List<String> logLines = readAllLines( logFilename );
assertThat( logLines, hasSize( 3 ) );
assertThat( logLines.get( 0 ), containsString( "mats" ) );
assertThat( logLines.get( 1 ), containsString( "mats" ) );
assertThat( logLines.get( 2 ), containsString( "andres" ) );
}
@Test
public void shouldLogTXMetaDataInQueryLog() throws Throwable
{
// turn on query logging
databaseBuilder.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() );
databaseBuilder.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE );
EmbeddedInteraction db = new EmbeddedInteraction( databaseBuilder, Collections.emptyMap() );
GraphDatabaseFacade graph = db.getLocalGraph();
db.getLocalUserManager().setUserPassword( "neo4j", "123", false );
EnterpriseSecurityContext subject = db.login( "neo4j", "123" );
db.executeQuery( subject, "UNWIND range(0, 10) AS i CREATE (:Foo {p: i})", Collections.emptyMap(),
ResourceIterator::close );
// Set meta data and execute query in transaction
try (InternalTransaction tx = db.beginLocalTransactionAsUser( subject, KernelTransaction.Type.explicit ))
{
graph.execute( "CALL dbms.setTXMetaData( { User: 'Johan' } )", Collections.emptyMap() );
graph.execute( "CALL dbms.procedures() YIELD name RETURN name", Collections.emptyMap() ).close();
graph.execute( "MATCH (n) RETURN n", Collections.emptyMap() ).close();
graph.execute( QUERY, Collections.emptyMap() );
tx.success();
}
// Ensure that old meta data is not retained
try (InternalTransaction tx = db.beginLocalTransactionAsUser( subject, KernelTransaction.Type.explicit ))
{
graph.execute( "CALL dbms.setTXMetaData( { Location: 'Sweden' } )", Collections.emptyMap() );
graph.execute( "MATCH ()-[r]-() RETURN count(r)", Collections.emptyMap() ).close();
tx.success();
}
db.tearDown();
// THEN
List<String> logLines = readAllLines( logFilename );
assertThat( logLines, hasSize( 7 ) );
assertThat( logLines.get( 0 ), not( containsString( "User: 'Johan'" ) ) );
// we don't care if setTXMetaData contains the meta data
//assertThat( logLines.get( 1 ), containsString( "User: Johan" ) );
assertThat( logLines.get( 2 ), containsString( "User: 'Johan'" ) );
assertThat( logLines.get( 3 ), containsString( "User: 'Johan'" ) );
assertThat( logLines.get( 4 ), containsString( "User: 'Johan'" ) );
// we want to make sure that the new transaction does not carry old meta data
assertThat( logLines.get( 5 ), not( containsString( "User: 'Johan'" ) ) );
assertThat( logLines.get( 6 ), containsString( "Location: 'Sweden'" ) );
}
@Test
public void shouldLogQuerySlowerThanThreshold() throws Exception
{
GraphDatabaseService database = databaseBuilder.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.log_queries_parameter_logging_enabled, Settings.FALSE )
.newGraphDatabase();
executeQueryAndShutdown( database );
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ), endsWith( String.format( " ms: %s - %s - {}", querySource(
AUTH_DISABLED.username() ), QUERY ) ) );
assertThat( logLines.get( 0 ), containsString( AUTH_DISABLED.username() ) );
}
@Test
public void shouldLogParametersWhenNestedMap() throws Exception
{
GraphDatabaseService database = databaseBuilder.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.log_queries_parameter_logging_enabled, Settings.TRUE )
.newGraphDatabase();
Map<String,Object> props = new LinkedHashMap<>(); // to be sure about ordering in the last assertion
props.put( "name", "Roland" );
props.put( "position", "Gunslinger" );
props.put( "followers", Arrays.asList( "Jake", "Eddie", "Susannah" ) );
Map<String,Object> params = new HashMap<>();
params.put( "props", props );
String query = "CREATE ({props})";
executeQueryAndShutdown( database, query, params );
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
QuerySource querySource = querySource( AUTH_DISABLED.username() );
assertThat( logLines.get( 0 ), endsWith( String.format(
" ms: %s - %s - {props: {name: 'Roland', position: 'Gunslinger', followers: [Jake, Eddie, Susannah]}} - {}",
querySource, query) ) );
assertThat( logLines.get( 0 ), containsString( AUTH_DISABLED.username() ) );
}
private QuerySource querySource( String username )
{
return QueryEngineProvider.describe().append( username );
}
@Test
public void shouldLogParametersWhenList() throws Exception
{
GraphDatabaseService database = databaseBuilder.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.newGraphDatabase();
Map<String,Object> params = new HashMap<>();
params.put( "ids", Arrays.asList( 0, 1, 2 ) );
String query = "MATCH (n) WHERE id(n) in {ids} RETURN n.name";
executeQueryAndShutdown( database, query, params );
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
endsWith( String.format(
" ms: %s - %s - {ids: [0, 1, 2]} - {}",
querySource( AUTH_DISABLED.username() ), query) ) );
assertThat( logLines.get( 0 ), containsString( AUTH_DISABLED.username() ) );
}
@Test
public void disabledQueryLogging() throws IOException
{
GraphDatabaseService database = databaseBuilder.setConfig( GraphDatabaseSettings.log_queries, Settings.FALSE )
.setConfig( GraphDatabaseSettings.log_queries_filename, logFilename.getPath() )
.newGraphDatabase();
executeQueryAndShutdown( database );
expectedException.expect( FileNotFoundException.class );
readAllLines( logFilename );
}
@Test
public void disabledQueryLogRotation() throws Exception
{
final File logsDirectory = new File( testDirectory.graphDbDir(), "logs" );
final File logFilename = new File( logsDirectory, "query.log" );
final File shiftedLogFilename1 = new File( logsDirectory, "query.log.1" );
GraphDatabaseService database = databaseBuilder.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.log_queries_rotation_threshold, "0" )
.newGraphDatabase();
// Logging is done asynchronously, so write many times to make sure we would have rotated something
for ( int i = 0; i < 100; i++ )
{
database.execute( QUERY );
}
database.shutdown();
assertFalse( "There should not exist a shifted log file because rotation is disabled",
shiftedLogFilename1.exists() );
List<String> lines = readAllLines( logFilename );
assertEquals( 100, lines.size() );
}
@Test
public void queryLogRotation() throws Exception
{
final File logsDirectory = new File( testDirectory.graphDbDir(), "logs" );
GraphDatabaseService database = databaseBuilder.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.log_queries_max_archives, "100" )
.setConfig( GraphDatabaseSettings.log_queries_rotation_threshold, "1" )
.newGraphDatabase();
// Logging is done asynchronously, and it turns out it's really hard to make it all work the same on Linux
// and on Windows, so just write many times to make sure we rotate several times.
for ( int i = 0; i < 100; i++ )
{
database.execute( QUERY );
}
database.shutdown();
File[] queryLogs = fileSystem.get().listFiles( logsDirectory, ( dir, name ) -> name.startsWith( "query.log" ) );
assertThat( "Expect to have more then one query log file.", queryLogs.length, greaterThanOrEqualTo( 2 ) );
List<String> loggedQueries = Arrays.stream( queryLogs )
.map( this::readAllLinesSilent )
.flatMap( Collection::stream )
.collect( Collectors.toList() );
assertThat( "Expected log file to have at least one log entry", loggedQueries, hasSize( 100 ) );
}
@Test
public void shouldNotLogPassword() throws Exception
{
GraphDatabaseFacade database = (GraphDatabaseFacade) databaseBuilder
.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.auth_enabled, Settings.TRUE )
.newGraphDatabase();
EnterpriseAuthManager authManager = database.getDependencyResolver().resolveDependency( EnterpriseAuthManager.class );
EnterpriseSecurityContext neo = authManager.login( AuthToken.newBasicAuthToken( "neo4j", "neo4j" ) );
String query = "CALL dbms.security.changePassword('abc123')";
try ( InternalTransaction tx = database
.beginTransaction( KernelTransaction.Type.explicit, neo ) )
{
Result res = database.execute( tx, query, Collections.emptyMap() );
res.close();
tx.success();
}
finally
{
database.shutdown();
}
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
containsString( "CALL dbms.security.changePassword(******)") ) ;
assertThat( logLines.get( 0 ),not( containsString( "abc123" ) ) );
assertThat( logLines.get( 0 ), containsString( neo.subject().username() ) );
}
@Test
public void shouldNotLogChangeUserPassword() throws Exception
{
GraphDatabaseFacade database = (GraphDatabaseFacade) databaseBuilder
.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.auth_enabled, Settings.TRUE )
.newGraphDatabase();
EnterpriseAuthManager authManager = database.getDependencyResolver().resolveDependency( EnterpriseAuthManager.class );
EnterpriseSecurityContext neo = authManager.login( AuthToken.newBasicAuthToken( "neo4j", "neo4j" ) );
String query = "CALL dbms.security.changeUserPassword('neo4j', 'abc123')";
try ( InternalTransaction tx = database
.beginTransaction( KernelTransaction.Type.explicit, neo ) )
{
Result res = database.execute( tx, query, Collections.emptyMap() );
res.close();
tx.success();
}
finally
{
database.shutdown();
}
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
containsString( "CALL dbms.security.changeUserPassword('neo4j', ******)") ) ;
assertThat( logLines.get( 0 ),not( containsString( "abc123" ) ) );
assertThat( logLines.get( 0 ), containsString( neo.subject().username() ) );
}
@Test
public void shouldNotLogPasswordEvenIfPasswordIsSilly() throws Exception
{
GraphDatabaseFacade database = (GraphDatabaseFacade) databaseBuilder
.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.auth_enabled, Settings.TRUE )
.newGraphDatabase();
EnterpriseAuthManager authManager = database.getDependencyResolver().resolveDependency( EnterpriseAuthManager.class );
EnterpriseSecurityContext neo = authManager.login( AuthToken.newBasicAuthToken( "neo4j", "neo4j" ) );
String query = "CALL dbms.security.changePassword('.changePassword(\\'si\"lly\\')')";
try ( InternalTransaction tx = database
.beginTransaction( KernelTransaction.Type.explicit, neo ) )
{
Result res = database.execute( tx, query, Collections.emptyMap() );
res.close();
tx.success();
}
finally
{
database.shutdown();
}
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
containsString( "CALL dbms.security.changePassword(******)") ) ;
assertThat( logLines.get( 0 ),not( containsString( ".changePassword(\\'si\"lly\\" ) ) );
assertThat( logLines.get( 0 ), containsString( neo.subject().username() ) );
}
@Test
public void shouldNotLogPasswordEvenIfYouDoTwoThingsAtTheSameTime() throws Exception
{
GraphDatabaseFacade database = (GraphDatabaseFacade) databaseBuilder
.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.auth_enabled, Settings.TRUE )
.newGraphDatabase();
EnterpriseAuthManager authManager = database.getDependencyResolver().resolveDependency( EnterpriseAuthManager.class );
EnterpriseAuthAndUserManager userManager= database.getDependencyResolver().resolveDependency(
EnterpriseAuthAndUserManager.class );
userManager.getUserManager().newUser( "smith", "himitsu", false );
EnterpriseSecurityContext neo = authManager.login( AuthToken.newBasicAuthToken( "neo4j", "neo4j" ) );
String query = "CALL dbms.security.changeUserPassword('neo4j','.changePassword(silly)') "+
"CALL dbms.security.changeUserPassword('smith','other$silly') RETURN 1";
try ( InternalTransaction tx = database
.beginTransaction( KernelTransaction.Type.explicit, neo ) )
{
Result res = database.execute( tx, query, Collections.emptyMap() );
res.close();
tx.success();
}
finally
{
database.shutdown();
}
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
containsString( "CALL dbms.security.changeUserPassword('neo4j',******) CALL dbms.security.changeUserPassword('smith',******)") ) ;
assertThat( logLines.get( 0 ),not( containsString( "other$silly" ) ) );
assertThat( logLines.get( 0 ),not( containsString( ".changePassword(silly)" ) ) );
assertThat( logLines.get( 0 ), containsString( neo.subject().username() ) );
}
@Test
public void shouldNotLogPasswordEvenIfYouDoTwoThingsAtTheSameTimeWithSeveralParms() throws Exception
{
GraphDatabaseFacade database = (GraphDatabaseFacade) databaseBuilder
.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.auth_enabled, Settings.TRUE )
.newGraphDatabase();
EnterpriseAuthManager authManager = database.getDependencyResolver().resolveDependency( EnterpriseAuthManager.class );
EnterpriseAuthAndUserManager userManager= database.getDependencyResolver().resolveDependency(
EnterpriseAuthAndUserManager.class );
userManager.getUserManager().newUser( "smith", "himitsu", false );
EnterpriseSecurityContext neo = authManager.login( AuthToken.newBasicAuthToken( "neo4j", "neo4j" ) );
String query = "CALL dbms.security.changeUserPassword('neo4j',$first) "+
"CALL dbms.security.changeUserPassword('smith',$second) RETURN 1";
try ( InternalTransaction tx = database
.beginTransaction( KernelTransaction.Type.explicit, neo ) )
{
Map<String,Object> params = new HashMap( );
params.put("first",".changePassword(silly)");
params.put("second",".other$silly");
Result res = database.execute( tx, query, params );
res.close();
tx.success();
}
finally
{
database.shutdown();
}
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
containsString( "{first: ******, second: ******}"));
assertThat( logLines.get( 0 ),not( containsString( "other$silly" ) ) );
assertThat( logLines.get( 0 ),not( containsString( ".changePassword(silly)" ) ) );
assertThat( logLines.get( 0 ), containsString( neo.subject().username() ) );
}
@Test
public void shouldNotLogPasswordInParams() throws Exception
{
GraphDatabaseFacade database = (GraphDatabaseFacade) databaseBuilder
.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.auth_enabled, Settings.TRUE )
.newGraphDatabase();
EnterpriseAuthManager authManager = database.getDependencyResolver().resolveDependency( EnterpriseAuthManager.class );
EnterpriseSecurityContext neo = authManager.login( AuthToken.newBasicAuthToken( "neo4j", "neo4j" ) );
String query = "CALL dbms.changePassword($password)";
try ( InternalTransaction tx = database
.beginTransaction( KernelTransaction.Type.explicit, neo ) )
{
Result res = database.execute( tx, query, Collections.singletonMap( "password", "abc123" ) );
res.close();
tx.success();
}
finally
{
database.shutdown();
}
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
containsString( "{password: ******}"));
assertThat( logLines.get( 0 ),not( containsString( "abc123" ) ) );
assertThat( logLines.get( 0 ), containsString( neo.subject().username() ) );
}
@Test
public void shouldNotLogPasswordInDeprecatedParams() throws Exception
{
GraphDatabaseFacade database = (GraphDatabaseFacade) databaseBuilder
.setConfig( GraphDatabaseSettings.log_queries, Settings.TRUE )
.setConfig( GraphDatabaseSettings.logs_directory, logsDirectory.getPath() )
.setConfig( GraphDatabaseSettings.auth_enabled, Settings.TRUE )
.newGraphDatabase();
EnterpriseAuthManager authManager = database.getDependencyResolver().resolveDependency( EnterpriseAuthManager.class );
EnterpriseSecurityContext neo = authManager.login( AuthToken.newBasicAuthToken( "neo4j", "neo4j" ) );
String query = "CALL dbms.changePassword({password})";
try ( InternalTransaction tx = database
.beginTransaction( KernelTransaction.Type.explicit, neo ) )
{
Result res = database.execute( tx, query, Collections.singletonMap( "password", "abc123" ) );
res.close();
tx.success();
}
finally
{
database.shutdown();
}
List<String> logLines = readAllLines( logFilename );
assertEquals( 1, logLines.size() );
assertThat( logLines.get( 0 ),
containsString( "{password: ******}"));
assertThat( logLines.get( 0 ),not( containsString( "abc123" ) ) );
assertThat( logLines.get( 0 ), containsString( neo.subject().username() ) );
}
private void executeQueryAndShutdown( GraphDatabaseService database )
{
executeQueryAndShutdown( database, QUERY, Collections.emptyMap() );
}
private void executeQueryAndShutdown( GraphDatabaseService database, String query, Map<String,Object> params )
{
Result execute = database.execute( query, params );
execute.close();
database.shutdown();
}
private List<String> readAllLinesSilent( File logFilename )
{
try
{
return readAllLines( fileSystem.get(), logFilename );
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}
private List<String> readAllLines( File logFilename ) throws IOException
{
return readAllLines( fileSystem.get(), logFilename );
}
public static List<String> readAllLines( FileSystemAbstraction fs, File logFilename ) throws IOException
{
List<String> logLines = new ArrayList<>();
// this is needed as the EphemeralFSA is broken, and creates a new file when reading a non-existent file from
// a valid directory
if ( !fs.fileExists( logFilename ) )
{
throw new FileNotFoundException( "File does not exist." );
}
try ( BufferedReader reader = new BufferedReader(
fs.openAsReader( logFilename, StandardCharsets.UTF_8 ) ) )
{
for ( String line; ( line = reader.readLine() ) != null; )
{
logLines.add( line );
}
}
return logLines;
}
}