Remove hard coded default locations for admin config

This commit is contained in:
Armin Schrenk
2026-02-09 16:46:07 +01:00
parent cad7b45808
commit 5e52811c74
2 changed files with 27 additions and 33 deletions

View File

@@ -1,6 +1,5 @@
package org.cryptomator.launcher;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import java.io.IOException;
@@ -14,14 +13,7 @@ import java.util.Set;
/**
* Class to overwrite system properties with an external properties file
* <p>
* To overwrite system properties, the method {@link #adjustSystemProperties()} loads the properties file {@value CONFIG_NAME} from the file defined in the property {@value #ADMIN_PROP_FILE_KEY} and writes all supported properties to the {@link System} properties.
* If {@value #ADMIN_PROP_FILE_KEY} is {@code null} OS-specific, predefined paths are used:
* <ul>
* <li>Linux: {@value LINUX_DIR }</li>
* <li>macOS: {@value MAC_DIR }</li>
* <li>Windows: {@value WIN_DIR }</li>
* </ul>
* </p>
* To overwrite system properties, the method {@link #adjustSystemProperties()} reads the properties file defined in the property {@value #ADMIN_PROP_FILE_KEY} and writes all supported properties to the {@link System} properties.
* <p>
* The overridable properties are:
* <ul>
@@ -38,10 +30,6 @@ class AdminPropertiesSetter {
private static final Logger LOG = EventualLogger.INSTANCE;
private static final long MAX_CONFIG_SIZE_BYTES = 8192;
private static final String LINUX_DIR = "/etc/cryptomator";
private static final String MAC_DIR = "/Library/Application Support/Cryptomator";
private static final String WIN_DIR = "C:\\ProgramData\\Cryptomator";
private static final String CONFIG_NAME = "cryptomator.config";
private static final String ADMIN_PROP_FILE_KEY = "cryptomator.adminConfig";
private static final Set<String> ALLOWED_OVERRIDES = Set.of( //
"cryptomator.logDir", //
@@ -50,25 +38,6 @@ class AdminPropertiesSetter {
"cryptomator.mountPointsDir", //
"cryptomator.disableUpdateCheck");
private static final Path ADMIN_PROPERTIES_FILE;
static {
final String systemPropertyDefinedAdminFile = System.getProperty(ADMIN_PROP_FILE_KEY);
if (systemPropertyDefinedAdminFile != null) {
ADMIN_PROPERTIES_FILE = Path.of(systemPropertyDefinedAdminFile);
} else {
final Path defaultAdminDir;
if (SystemUtils.IS_OS_WINDOWS) {
defaultAdminDir = Path.of(WIN_DIR);
} else if (SystemUtils.IS_OS_MAC) {
defaultAdminDir = Path.of(MAC_DIR);
} else {
defaultAdminDir = Path.of(LINUX_DIR);
}
ADMIN_PROPERTIES_FILE = defaultAdminDir.resolve(CONFIG_NAME);
}
}
/**
* Adjusts the system properties by loading administrative properties from a predefined file location.
@@ -80,7 +49,13 @@ class AdminPropertiesSetter {
*/
static Properties adjustSystemProperties() {
var systemProps = System.getProperties();
var adminProps = loadAdminProperties(ADMIN_PROPERTIES_FILE);
final String systemPropertyDefinedAdminFile = System.getProperty(ADMIN_PROP_FILE_KEY);
if (systemPropertyDefinedAdminFile == null) {
LOG.debug("Path to admin properties file is not defined.");
return systemProps;
}
var adminProps = loadAdminProperties(Path.of(systemPropertyDefinedAdminFile));
for (var key : adminProps.stringPropertyNames()) {
if (ALLOWED_OVERRIDES.contains(key)) {

View File

@@ -13,9 +13,13 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Properties;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.hasEntry;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
public class AdminPropertiesSetterTest {
@@ -74,4 +78,19 @@ public class AdminPropertiesSetterTest {
MatcherAssert.assertThat(properties, anEmptyMap());
}
@Test
@DisplayName("If system property for config path is null, skip do not load anything")
void skipAdjustSystemPropertiesOnUndefinedProperty() {
Assertions.assertNull(System.getProperty("cryptomator.adminConfig"));
try (var adminPropSetterMock = mockStatic(AdminPropertiesSetter.class)) {
adminPropSetterMock.when(AdminPropertiesSetter::adjustSystemProperties).thenCallRealMethod();
adminPropSetterMock.when(() -> AdminPropertiesSetter.loadAdminProperties(any())).thenReturn(new Properties());
AdminPropertiesSetter.adjustSystemProperties();
adminPropSetterMock.verify(() -> AdminPropertiesSetter.loadAdminProperties(any()), never());
}
}
}