public ReflectiveProcedureCompiler withoutNamingRestrictions() { return new ReflectiveProcedureCompiler( inputSignatureDeterminer, outputMappers, safeFieldInjections, allFieldInjections, log, typeMappers, config, name -> { // all ok } ); }
public Procedures( EmbeddedProxySPI proxySPI, ThrowingConsumer<Procedures,ProcedureException> builtin, File pluginDir, Log log, ProcedureConfig config ) { this.builtin = builtin; this.pluginDir = pluginDir; this.log = log; this.typeMappers = new TypeMappers( proxySPI ); this.compiler = new ReflectiveProcedureCompiler( typeMappers, safeComponents, allComponents, log, config ); }
@Test public void shouldNotLoadNoneWhiteListedFunction() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "WrongName" ) ) ); List<CallableUserAggregationFunction> method = compile( SingleAggregationFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.collectCool' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Test public void shouldNotLoadAnyProcedureIfConfigIsEmpty() throws Throwable { // Given ProcedureConfig config = new ProcedureConfig( Config.defaults( procedure_whitelist, "" ) ); Log log = mock(Log.class); ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, log, config ); // When List<CallableProcedure> proc = procedureCompiler.compileProcedure( SingleReadOnlyProcedure.class, null, false ); // Then verify( log ) .warn( "The procedure 'org.neo4j.kernel.impl.proc.listCoolPeople' is not on the whitelist and won't be loaded." ); assertThat( proc.isEmpty(), is(true) ); }
@Test public void shouldNotLoadNoneWhiteListedProcedure() throws Throwable { // Given ProcedureConfig config = new ProcedureConfig( Config.defaults( procedure_whitelist, "org.neo4j.kernel.impl.proc.NOTlistCoolPeople" ) ); Log log = mock(Log.class); ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, log, config ); // When List<CallableProcedure> proc = procedureCompiler.compileProcedure( SingleReadOnlyProcedure.class, null, false ); // Then verify( log ) .warn( "The procedure 'org.neo4j.kernel.impl.proc.listCoolPeople' is not on the whitelist and won't be loaded." ); assertThat( proc.isEmpty(), is(true) ); }
@Test public void shouldNotLoadNoneWhiteListedFunction() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "WrongName" ) ) ); List<CallableUserFunction> method = compile( SingleReadOnlyFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.listCoolPeople' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Test public void shouldNotLoadAnyFunctionIfConfigIsEmpty() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "" ) ) ); List<CallableUserAggregationFunction> method = compile( SingleAggregationFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.collectCool' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Test public void shouldNotLoadAnyFunctionIfConfigIsEmpty() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "" ) ) ); List<CallableUserFunction> method = compile( SingleReadOnlyFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.listCoolPeople' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Before public void setUp() { components = new ComponentRegistry(); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, NullLog.getInstance(), ProcedureConfig.DEFAULT ); }
@Before public void setUp() { components = new ComponentRegistry(); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, NullLog.getInstance(), ProcedureConfig.DEFAULT ); }
@Before public void setUp() { components = new ComponentRegistry(); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, NullLog.getInstance(), ProcedureConfig.DEFAULT ); }
@Test public void shouldReturnEmptySetOnNullArgument() throws Exception { // given ProcedureJarLoader jarloader = new ProcedureJarLoader( new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), registryWithUnsafeAPI(), log, procedureConfig() ), NullLog.getInstance() ); // when ProcedureJarLoader.Callables callables = jarloader.loadProceduresFromDir( null ); // then assertEquals( 0, callables.procedures().size() + callables.functions().size() ); }
@Test public void shouldIgnoreWhiteListingIfFullAccess() throws Throwable { // Given ProcedureConfig config = new ProcedureConfig( Config.defaults( procedure_whitelist, "empty" ) ); Log log = mock(Log.class); ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, log, config ); // When CallableProcedure proc = procedureCompiler.compileProcedure( SingleReadOnlyProcedure.class, null, true ).get( 0 ); // Then RawIterator<Object[],ProcedureException> result = proc.apply( new BasicContext(), new Object[0], resourceTracker ); assertEquals( result.next()[0], "Bonnie" ); }
@Test public void shouldLoadWhiteListedFunction() throws Throwable { // Given procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), NullLog.getInstance(), new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "org.neo4j.kernel.impl.proc.listCoolPeople" ) ) ); CallableUserFunction method = compile( SingleReadOnlyFunction.class ).get( 0 ); // Expect Object out = method.apply( new BasicContext(), new AnyValue[0] ); assertThat(out, equalTo( ValueUtils.of( Arrays.asList("Bonnie", "Clyde") ) ) ); }
@Test public void shouldLoadWhiteListedProcedure() throws Throwable { // Given ProcedureConfig config = new ProcedureConfig( Config.defaults( procedure_whitelist, "org.neo4j.kernel.impl.proc.listCoolPeople" ) ); Log log = mock(Log.class); ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, log, config ); // When CallableProcedure proc = procedureCompiler.compileProcedure( SingleReadOnlyProcedure.class, null, false ).get( 0 ); // When RawIterator<Object[],ProcedureException> result = proc.apply( new BasicContext(), new Object[0], resourceTracker ); // Then assertEquals( result.next()[0], "Bonnie" ); }
@Test public void shouldLogHelpfullyWhenPluginJarIsCorrupt() throws Exception { // given URL theJar = createJarFor( ClassWithOneProcedure.class, ClassWithAnotherProcedure.class, ClassWithNoProcedureAtAll.class ); corruptJar( theJar ); AssertableLogProvider logProvider = new AssertableLogProvider( true ); ProcedureJarLoader jarloader = new ProcedureJarLoader( new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), registryWithUnsafeAPI(), log, procedureConfig() ), logProvider.getLog( ProcedureJarLoader.class ) ); // when try { jarloader.loadProceduresFromDir( parentDir( theJar ) ); fail( "Should have logged and thrown exception." ); } catch ( ZipException expected ) { // then logProvider.assertContainsLogCallContaining( escapeJava( String.format( "Plugin jar file: %s corrupted.", new File( theJar.toURI() ).toPath() ) ) ); } }
@Test public void shouldLoadWhiteListedFunction() throws Throwable { // Given procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), NullLog.getInstance(), new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "org.neo4j.kernel.impl.proc.collectCool" ) ) ); CallableUserAggregationFunction method = compile( SingleAggregationFunction.class ).get( 0 ); // Expect UserAggregator created = method.create( new BasicContext() ); created.update( new Object[]{"Bonnie"} ); assertThat(created.result(), equalTo( Collections.singletonList( "Bonnie" ) ) ); }
private List<CallableProcedure> compile( Class<?> clazz ) throws KernelException { return new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), new ComponentRegistry(), NullLog.getInstance(), ProcedureConfig.DEFAULT ).compileProcedure( clazz, null, true ); } }
@Test public void shouldWorkOnPathsWithSpaces() throws Exception { // given File fileWithSpacesInName = tmpdir.newFile( new Random().nextInt() + " some spaces in the filename" + ".jar" ); URL theJar = new JarBuilder().createJarFor( fileWithSpacesInName, ClassWithOneProcedure.class ); corruptJar( theJar ); AssertableLogProvider logProvider = new AssertableLogProvider( true ); ProcedureJarLoader jarloader = new ProcedureJarLoader( new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), registryWithUnsafeAPI(), log, procedureConfig() ), logProvider.getLog( ProcedureJarLoader.class ) ); // when try { jarloader.loadProceduresFromDir( parentDir( theJar ) ); fail( "Should have logged and thrown exception." ); } catch ( ZipException expected ) { // then logProvider.assertContainsLogCallContaining( escapeJava( String.format( "Plugin jar file: %s corrupted.", fileWithSpacesInName.toPath() ) ) ); } }
@Before public void setUp() { ComponentRegistry safeComponents = new ComponentRegistry(); ComponentRegistry allComponents = new ComponentRegistry(); safeComponents.register( MyAwesomeAPI.class, ctx -> new MyAwesomeAPI() ); allComponents.register( MyAwesomeAPI.class, ctx -> new MyAwesomeAPI() ); allComponents.register( MyUnsafeAPI.class, ctx -> new MyUnsafeAPI() ); compiler = new ReflectiveProcedureCompiler( new TypeMappers(), safeComponents, allComponents, log, ProcedureConfig.DEFAULT ); }