Skip to content

Commit

Permalink
Moving to database setting
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBergstrand committed Jan 8, 2025
1 parent 34f83a4 commit 04a3239
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ apply from: "licenses-source-header.gradle"

ext {
publicDir = "${project.rootDir}"
neo4jVersionEffective = project.hasProperty("neo4jVersionOverride") ? project.getProperty("neo4jVersionOverride") : "2025.01.0"
neo4jVersionEffective = project.hasProperty("neo4jVersionOverride") ? project.getProperty("neo4jVersionOverride") : "2025.01.0-SNAPSHOT"
testContainersVersion = '1.20.2'
apacheArrowVersion = '15.0.0'
}
51 changes: 27 additions & 24 deletions common/src/main/java/apoc/ApocConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -188,30 +189,32 @@ 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).resolve(Config.DEFAULT_CONFIG_DIR_NAME).toString();
// String defaultPath =neo4jConfig.get(configuration_directory).toString(); //resolve(Config.DEFAULT_CONFIG_DIR_NAME).toString();
// 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;
// }
}

/**
Expand Down
34 changes: 21 additions & 13 deletions common/src/test/java/apoc/ApocConfigCommandExpansionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ 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"));
// when(neo4jConfig.get(GraphDatabaseSettings.neo4j_home)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));

apocConfigCommandExpansionFile = new File(getClass()
.getClassLoader()
.getResource("apoc-config-command-expansion/apoc.conf")
.getResource("apoc.conf")
.toURI());
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 =
Expand All @@ -93,6 +95,8 @@ public void setup() throws Exception {
@After
public void cleanup() {
System.clearProperty(SUN_JAVA_COMMAND);
System.clearProperty(APOC_MAX_DECOMPRESSION_RATIO);
System.clearProperty("command.expansion");
}

private void setApocConfigFilePermissions(Set<PosixFilePermission> forbidden) throws Exception {
Expand All @@ -101,16 +105,16 @@ private void setApocConfigFilePermissions(Set<PosixFilePermission> forbidden) th
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());
}
// 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();
// setApocConfigSystemProperty();
apocConfig.init();

assertEquals("expanded value", apocConfig.getConfig().getString("command.expansion"));
Expand All @@ -120,7 +124,7 @@ public void testApocConfWithExpandCommands() {
public void testApocConfWithInvalidExpandCommands() throws Exception {
String invalidExpandLine = "command.expansion.3=$(fakeCommand 3 + 3)";
addLineToApocConfig(invalidExpandLine);
setApocConfigSystemProperty();
// setApocConfigSystemProperty();

RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init);
String expectedMessage =
Expand All @@ -134,7 +138,7 @@ public void testApocConfWithInvalidExpandCommands() throws Exception {
public void testApocConfWithWrongFilePermissions() throws Exception {
for (PosixFilePermission filePermission : forbiddenFilePermissionsForCommandExpansion) {
setApocConfigFilePermissions(Set.of(filePermission));
setApocConfigSystemProperty();
// setApocConfigSystemProperty();

RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init);
String expectedMessage = "does not have the correct file permissions to evaluate commands.";
Expand All @@ -152,14 +156,16 @@ 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.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"));
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();
// setApocConfigSystemProperty();

RuntimeException e = assertThrows(RuntimeException.class, apocConfig::init);
String expectedMessage =
Expand All @@ -177,6 +183,8 @@ public void testMaxDecompressionRatioValidation() {
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"));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of(apocConfigCommandExpansionFile.getParent()));

GlobalProceduresRegistry registry = mock(GlobalProceduresRegistry.class);
DatabaseManagementService databaseManagementService = mock(DatabaseManagementService.class);
Expand Down
1 change: 1 addition & 0 deletions common/src/test/java/apoc/ApocConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void setup() throws Exception {
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"));
when(neo4jConfig.get(GraphDatabaseSettings.configuration_directory)).thenReturn(Path.of("C:/neo4j/neo4j-enterprise-5.x.0"));

apocConfigFile =
new File(getClass().getClassLoader().getResource("apoc.conf").toURI());
Expand Down
File renamed without changes.

0 comments on commit 04a3239

Please sign in to comment.