private long sumIndexFiles( File file, FilenameFilter filter ) { long total = 0; if ( outsideWorld.fileSystem().isDirectory( file ) ) { File[] children = outsideWorld.fileSystem().listFiles( file, filter ); if ( children != null ) { for ( File child : children ) { total += sumIndexFiles( child, filter ); } } } else { total += outsideWorld.fileSystem().getFileSize( file ); } return total; }
private FilenameFilter getNativeIndexFileFilter( File storeDir, boolean inverse ) { FileFilter nativeIndexFilter = new NativeIndexFileFilter( storeDir ); return ( dir, name ) -> { File file = new File( dir, name ); if ( outsideWorld.fileSystem().isDirectory( file ) ) { // Always go down directories return true; } if ( name.equals( FailureStorage.DEFAULT_FAILURE_FILE_NAME ) ) { // Never include failure-storage files return false; } return inverse != nativeIndexFilter.accept( file ); }; }
DiagnosticsReportCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld ) { this.homeDir = homeDir; this.configDir = configDir; this.fs = outsideWorld.fileSystem(); this.out = outsideWorld.outStream(); err = outsideWorld.errorStream(); }
private void resetOutsideWorldMock() { reset(out); when( out.fileSystem() ).thenReturn( fileSystem ); } }
private void resetOutsideWorldMock() { reset( out ); when( out.fileSystem() ).thenReturn( fileSystem ); } }
private long sumStoreFiles( DatabaseLayout databaseLayout ) { long total = 0; for ( StoreType type : StoreType.values() ) { if ( type.isRecordStore() ) { FileSystemAbstraction fileSystem = outsideWorld.fileSystem(); total += databaseLayout.file( type.getDatabaseFile() ).filter( fileSystem::fileExists ).mapToLong( fileSystem::getFileSize ).sum(); } } return total; }
private Config getConfig( File configFile, String databaseName ) throws CommandFailed { if ( !outsideWorld.fileSystem().fileExists( configFile ) ) { throw new CommandFailed( "Unable to find config file, tried: " + configFile.getAbsolutePath() ); } try { return Config.fromFile( configFile ).withHome( homeDir ).withSetting( active_database, databaseName ).withConnectorsDisabled().build(); } catch ( Exception e ) { throw new CommandFailed( "Failed to read config file: " + configFile.getAbsolutePath(), e ); } }
private boolean realUsersExist( Config config ) { boolean result = false; File authFile = CommunitySecurityModule.getUserRepositoryFile( config ); if ( outsideWorld.fileSystem().fileExists( authFile ) ) { result = true; // Check if it only contains the default neo4j user FileUserRepository userRepository = new FileUserRepository( outsideWorld.fileSystem(), authFile, NullLogProvider.getInstance() ); try ( Lifespan life = new Lifespan( userRepository ) ) { ListSnapshot<User> users = userRepository.getPersistedSnapshot(); if ( users.values().size() == 1 ) { User user = users.values().get( 0 ); if ( INITIAL_USER_NAME.equals( user.name() ) && user.credentials().matchesPassword( INITIAL_PASSWORD ) ) { // We allow overwriting an unmodified default neo4j user result = false; } } } catch ( IOException e ) { // Do not allow overwriting if we had a problem reading the file } } return result; }
@Test void listShouldDisplayAllClassifiers() throws Exception { try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { PrintStream ps = new PrintStream( baos ); String[] args = {"--list"}; OutsideWorld outsideWorld = mock( OutsideWorld.class ); when( outsideWorld.fileSystem() ).thenReturn( fs ); when( outsideWorld.outStream() ).thenReturn( ps ); DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); diagnosticsReportCommand.execute( args ); assertThat( baos.toString(), is(String.format( "Finding running instance of neo4j%n" + "No running instance of neo4j was found. Online reports will be omitted.%n" + "If neo4j is running but not detected, you can supply the process id of the running instance with --pid%n" + "All available classifiers:%n" + " config include configuration file%n" + " logs include log files%n" + " plugins include a view of the plugin directory%n" + " ps include a list of running processes%n" + " tree include a view of the tree structure of the data directory%n" + " tx include transaction logs%n" ) ) ); } }
@Before public void setup() { fileSystem = fileSystemRule.get(); OutsideWorld mock = mock( OutsideWorld.class ); when( mock.fileSystem() ).thenReturn( fileSystem ); setPasswordCommand = new SetInitialPasswordCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(), mock ); authInitFile = CommunitySecurityModule.getInitialUserRepositoryFile( setPasswordCommand.loadNeo4jConfig() ); CommunitySecurityModule.getUserRepositoryFile( setPasswordCommand.loadNeo4jConfig() ); }
@Before public void setup() throws IOException, InvalidArgumentsException { OutsideWorld mock = mock( OutsideWorld.class ); when( mock.fileSystem() ).thenReturn( fileSystem ); setDefaultAdmin = new SetDefaultAdminCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(), mock ); config = setDefaultAdmin.loadNeo4jConfig(); UserRepository users = CommunitySecurityModule.getUserRepository( config, NullLogProvider.getInstance(), fileSystem ); users.create( new User.Builder( "jake", LegacyCredential.forPassword( "123" ) ) .withRequiredPasswordChange( false ) .build() ); adminIniFile = new File( CommunitySecurityModule.getUserRepositoryFile( config ).getParentFile(), "admin.ini" ); }
private void setDefaultAdmin( String username ) throws Throwable FileSystemAbstraction fileSystem = outsideWorld.fileSystem(); Config config = loadNeo4jConfig();
@Override public void doImport() throws IOException { FileSystemAbstraction fs = outsideWorld.fileSystem(); File storeDir = databaseConfig.get( GraphDatabaseSettings.database_path ); File logsDir = databaseConfig.get( GraphDatabaseSettings.logs_directory ); File reportFile = new File( reportFileName ); OutputStream badOutput = new BufferedOutputStream( fs.openAsOutputStream( reportFile, false ) ); Collector badCollector = badCollector( badOutput, isIgnoringSomething() ? BadCollector.UNLIMITED_TOLERANCE : 0, collect( ignoreBadRelationships, ignoreDuplicateNodes, ignoreExtraColumns ) ); Configuration configuration = new WrappedBatchImporterConfigurationForNeo4jAdmin( importConfiguration( null, false, databaseConfig, storeDir, highIO ) ); // Extract the default time zone from the database configuration ZoneId dbTimeZone = databaseConfig.get( GraphDatabaseSettings.db_temporal_timezone ); Supplier<ZoneId> defaultTimeZone = () -> dbTimeZone; CsvInput input = new CsvInput( nodeData( inputEncoding, nodesFiles ), defaultFormatNodeFileHeader( defaultTimeZone ), relationshipData( inputEncoding, relationshipsFiles ), defaultFormatRelationshipFileHeader( defaultTimeZone ), idType, new WrappedCsvInputConfigurationForNeo4jAdmin( csvConfiguration( args, false ) ), badCollector ); ImportTool.doImport( outsideWorld.errorStream(), outsideWorld.errorStream(), outsideWorld.inStream(), DatabaseLayout.of( storeDir ), logsDir, reportFile, fs, nodesFiles, relationshipsFiles, false, input, this.databaseConfig, badOutput, configuration, false ); }
private void setPassword( String password ) throws Throwable { Config config = loadNeo4jConfig(); FileSystemAbstraction fileSystem = outsideWorld.fileSystem(); if ( realUsersExist( config ) ) { File authFile = CommunitySecurityModule.getUserRepositoryFile( config ); throw new CommandFailed( realUsersExistErrorMsg( fileSystem, authFile ) ); } else { File file = CommunitySecurityModule.getInitialUserRepositoryFile( config ); if ( fileSystem.fileExists( file ) ) { fileSystem.deleteFile( file ); } FileUserRepository userRepository = new FileUserRepository( fileSystem, file, NullLogProvider.getInstance() ); userRepository.start(); userRepository.create( new User.Builder( INITIAL_USER_NAME, LegacyCredential.forPassword( UTF8.encode( password ) ) ) .withRequiredPasswordChange( false ) .build() ); userRepository.shutdown(); outsideWorld.stdOutLine( "Changed password for user '" + INITIAL_USER_NAME + "'." ); } }
private void checkDestination( Path path ) throws CommandFailed { if ( !outsideWorld.fileSystem().isDirectory( path.toFile() ) ) { throw new CommandFailed( String.format( "Directory '%s' does not exist.", path ) ); } } }
private long sumIndexFiles( File file, FilenameFilter filter ) { long total = 0; if ( outsideWorld.fileSystem().isDirectory( file ) ) { File[] children = outsideWorld.fileSystem().listFiles( file, filter ); if ( children != null ) { for ( File child : children ) { total += sumIndexFiles( child, filter ); } } } else { total += outsideWorld.fileSystem().getFileSize( file ); } return total; }
DiagnosticsReportCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld ) { this.homeDir = homeDir; this.configDir = configDir; this.fs = outsideWorld.fileSystem(); this.out = outsideWorld.outStream(); err = outsideWorld.errorStream(); }
private long sumStoreFiles( DatabaseLayout databaseLayout ) { long total = 0; for ( StoreType type : StoreType.values() ) { if ( type.isRecordStore() ) { FileSystemAbstraction fileSystem = outsideWorld.fileSystem(); total += databaseLayout.file( type.getDatabaseFile() ).filter( fileSystem::fileExists ).mapToLong( fileSystem::getFileSize ).sum(); } } return total; }
/** * Dependencies that can be resolved immediately after launching the backup tool * * @param outsideWorld filesystem and output streams that the tool interacts with * @param logProvider made available to subsequent dependency resolution classes * @param monitors will become shared across all resolved dependencies */ BackupModule( OutsideWorld outsideWorld, LogProvider logProvider, Monitors monitors ) { this.outsideWorld = outsideWorld; this.logProvider = logProvider; this.monitors = monitors; this.clock = Clock.systemDefaultZone(); this.transactionLogCatchUpFactory = new TransactionLogCatchUpFactory(); this.fileSystemAbstraction = outsideWorld.fileSystem(); }
private Config getConfig( File configFile, String databaseName ) throws CommandFailed { if ( !outsideWorld.fileSystem().fileExists( configFile ) ) { throw new CommandFailed( "Unable to find config file, tried: " + configFile.getAbsolutePath() ); } try { return Config.fromFile( configFile ).withHome( homeDir ).withSetting( active_database, databaseName ).withConnectorsDisabled().build(); } catch ( Exception e ) { throw new CommandFailed( "Failed to read config file: " + configFile.getAbsolutePath(), e ); } }