diff --git a/common/src/main/java/apoc/ApocConfig.java b/common/src/main/java/apoc/ApocConfig.java index 3f738e89a..b876328d0 100644 --- a/common/src/main/java/apoc/ApocConfig.java +++ b/common/src/main/java/apoc/ApocConfig.java @@ -23,6 +23,7 @@ import static org.neo4j.configuration.BootloaderSettings.lib_directory; import static org.neo4j.configuration.BootloaderSettings.run_directory; import static org.neo4j.configuration.GraphDatabaseSettings.SYSTEM_DATABASE_NAME; +import static org.neo4j.configuration.GraphDatabaseSettings.configuration_directory; import static org.neo4j.configuration.GraphDatabaseSettings.data_directory; import static org.neo4j.configuration.GraphDatabaseSettings.load_csv_file_url_root; import static org.neo4j.configuration.GraphDatabaseSettings.logs_directory; @@ -43,7 +44,6 @@ import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.stream.Stream; import org.apache.commons.configuration2.CombinedConfiguration; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.EnvironmentConfiguration; @@ -173,8 +173,8 @@ private String evaluateIfCommand(String settingName, String entry) { @Override public void init() { log.debug("called init"); - // grab NEO4J_CONF from environment. If not set, calculate it from sun.java.command system property or Neo4j - // default + // Grab NEO4J_CONF from environment. + // If not set get it from database internal setting: "server.directories.configuration" String neo4jConfFolder = System.getenv().getOrDefault("NEO4J_CONF", determineNeo4jConfFolder()); System.setProperty("NEO4J_CONF", neo4jConfFolder); log.info("system property NEO4J_CONF set to %s", neo4jConfFolder); @@ -188,30 +188,7 @@ public void init() { } protected String determineNeo4jConfFolder() { - String defaultPath = neo4jConfig - .get(neo4j_home) - .resolve(Config.DEFAULT_CONFIG_DIR_NAME) - .toString(); - String command = System.getProperty(SUN_JAVA_COMMAND); - if (command == null) { - log.warn( - "system property %s is not set, assuming %s as conf dir. This might cause `apoc.conf` not getting loaded.", - SUN_JAVA_COMMAND, defaultPath); - return defaultPath; - } else { - final String neo4jConfFolder = Stream.of(command.split("--")) - .map(String::trim) - .filter(s -> s.startsWith(CONFIG_DIR)) - .map(s -> s.substring(CONFIG_DIR.length())) - .findFirst() - .orElse(defaultPath); - if (defaultPath.equals(neo4jConfFolder)) { - log.info("cannot determine conf folder from sys property %s, assuming %s", command, defaultPath); - } else { - log.info("from system properties: NEO4J_CONF=%s", neo4jConfFolder); - } - return neo4jConfFolder; - } + return neo4jConfig.get(configuration_directory).toString(); } /** diff --git a/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java b/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java index 7d56d80f4..885aac6b2 100644 --- a/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java +++ b/common/src/test/java/apoc/ApocConfigCommandExpansionTest.java @@ -19,7 +19,6 @@ package apoc; import static apoc.ApocConfig.APOC_MAX_DECOMPRESSION_RATIO; -import static apoc.ApocConfig.SUN_JAVA_COMMAND; import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE; import static java.nio.file.attribute.PosixFilePermission.GROUP_READ; import static java.nio.file.attribute.PosixFilePermission.GROUP_WRITE; @@ -46,7 +45,6 @@ import java.util.Collections; import java.util.Set; import org.assertj.core.api.Assertions; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.neo4j.configuration.Config; @@ -75,7 +73,6 @@ public void setup() throws Exception { when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false); when(neo4jConfig.expandCommands()).thenReturn(true); - when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); apocConfigCommandExpansionFile = new File(getClass() .getClassLoader() @@ -84,33 +81,23 @@ public void setup() throws Exception { Files.setPosixFilePermissions( apocConfigCommandExpansionFile.toPath(), permittedFilePermissionsForCommandExpansion); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of(apocConfigCommandExpansionFile.getParent())); + GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); apocConfig = new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService); } - @After - public void cleanup() { - System.clearProperty(SUN_JAVA_COMMAND); - } - private void setApocConfigFilePermissions(Set forbidden) throws Exception { Files.setPosixFilePermissions( apocConfigCommandExpansionFile.toPath(), Sets.union(permittedFilePermissionsForCommandExpansion, forbidden)); } - private void setApocConfigSystemProperty() { - System.setProperty( - SUN_JAVA_COMMAND, - "com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir=" - + apocConfigCommandExpansionFile.getParent()); - } - @Test public void testApocConfWithExpandCommands() { - setApocConfigSystemProperty(); apocConfig.init(); assertEquals("expanded value", apocConfig.getConfig().getString("command.expansion")); @@ -120,7 +107,6 @@ public void testApocConfWithExpandCommands() { public void testApocConfWithInvalidExpandCommands() throws Exception { String invalidExpandLine = "command.expansion.3=$(fakeCommand 3 + 3)"; addLineToApocConfig(invalidExpandLine); - setApocConfigSystemProperty(); RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init); String expectedMessage = @@ -134,7 +120,6 @@ public void testApocConfWithInvalidExpandCommands() throws Exception { public void testApocConfWithWrongFilePermissions() throws Exception { for (PosixFilePermission filePermission : forbiddenFilePermissionsForCommandExpansion) { setApocConfigFilePermissions(Set.of(filePermission)); - setApocConfigSystemProperty(); RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init); String expectedMessage = "does not have the correct file permissions to evaluate commands."; @@ -152,15 +137,14 @@ public void testApocConfWithoutExpandCommands() { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.expandCommands()).thenReturn(false); - when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of(apocConfigCommandExpansionFile.getParent())); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); ApocConfig apocConfig = new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService); - setApocConfigSystemProperty(); - RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init); String expectedMessage = "$(echo \"expanded value\") is a command, but config is not explicitly told to expand it. (Missing --expand-commands argument?)"; @@ -176,7 +160,8 @@ public void testMaxDecompressionRatioValidation() { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.expandCommands()).thenReturn(false); - when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0/conf")); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); diff --git a/common/src/test/java/apoc/ApocConfigTest.java b/common/src/test/java/apoc/ApocConfigTest.java index 1f0eb7820..bfd9c0028 100644 --- a/common/src/test/java/apoc/ApocConfigTest.java +++ b/common/src/test/java/apoc/ApocConfigTest.java @@ -18,7 +18,6 @@ */ package apoc; -import static apoc.ApocConfig.SUN_JAVA_COMMAND; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -50,10 +49,11 @@ public void setup() throws Exception { when(neo4jConfig.getDeclaredSettings()).thenReturn(Collections.emptyMap()); when(neo4jConfig.get(any())).thenReturn(null); when(neo4jConfig.get(GraphDatabaseSettings.allow_file_urls)).thenReturn(false); - when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0")); apocConfigFile = new File(getClass().getClassLoader().getResource("apoc.conf").toURI()); + when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)) + .thenReturn(Path.of(apocConfigFile.getParent())); GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class); DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class); @@ -61,42 +61,15 @@ public void setup() throws Exception { new ApocConfig(neo4jConfig, new SimpleLogService(logProvider), registry, databaseManagementService); } - private void setApocConfigSystemProperty() { - System.setProperty( - SUN_JAVA_COMMAND, - "com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir=" - + apocConfigFile.getParent()); - } - @Test public void testDetermineNeo4jConfFolderDefault() { - System.setProperty(SUN_JAVA_COMMAND, ""); - assertEquals("C:/neo4j/neo4j-enterprise-5.x.0/conf", apocConfig.determineNeo4jConfFolder()); - } - - @Test - public void testDetermineNeo4jConfFolder() { - System.setProperty( - SUN_JAVA_COMMAND, - "com.neo4j.server.enterprise.CommercialEntryPoint --home-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02 --config-dir=/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02/conf"); - - assertEquals("/home/stefan/neo4j-enterprise-4.0.0-alpha09mr02/conf", apocConfig.determineNeo4jConfFolder()); + assertEquals(apocConfigFile.getParent(), apocConfig.determineNeo4jConfFolder()); } @Test public void testApocConfFileBeingLoaded() { - setApocConfigSystemProperty(); apocConfig.init(); assertEquals("bar", apocConfig.getConfig().getString("foo")); } - - @Test - public void testDetermineNeo4jConfFolderWithWhitespaces() { - System.setProperty( - SUN_JAVA_COMMAND, - "com.neo4j.server.enterprise.CommercialEntryPoint --config-dir=/home/stefan/neo4j enterprise-4.0.0-alpha09mr02/conf --home-dir=/home/stefan/neo4j enterprise-4.0.0-alpha09mr02"); - - assertEquals("/home/stefan/neo4j enterprise-4.0.0-alpha09mr02/conf", apocConfig.determineNeo4jConfFolder()); - } }