mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
feat(core): keep a host native binary's state in the user data directory (#5769)
The bundled persistent path is ./data, relative to the working directory. That suits the image (which sets /app/data) and a run from the checkout, but a native binary on the PATH would write its state, and its TLS material, into whatever directory it was started from. A native run outside a container now defaults to the platform's per-user data directory: $XDG_DATA_HOME/floci-aws (or ~/.local/share/floci-aws) on Linux, ~/Library/Application Support/floci-aws on macOS, %LOCALAPPDATA%\floci-aws on Windows. HostDefaultsConfigSource sits at ordinal 260, between the bundled application.yml (255) and config/application.yml (265), so any path the user configures still wins. The JVM, dev mode and the image keep ./data. Refs #5767 Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
6e01216f88
commit
0a5809a559
6 files changed
+207
-1
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.quarkus.runtime.ImageMode;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Where a standalone Floci binary keeps its state when nothing says otherwise.
|
||||
*
|
||||
* <p>The bundled default, {@code ./data}, is right for the image (which sets
|
||||
* {@code /app/data}) and for a developer running from the checkout, but a native binary on the
|
||||
* user's PATH would write into whatever directory it happens to be started from. A native run
|
||||
* outside a container therefore defaults to the platform's per-user data directory instead.
|
||||
* The JVM, dev mode and the image keep {@code ./data}.
|
||||
*/
|
||||
public final class HostDataDir {
|
||||
|
||||
static final String BUNDLED_DEFAULT = "./data";
|
||||
static final String DIR_NAME = "floci-aws";
|
||||
|
||||
private HostDataDir() {
|
||||
}
|
||||
|
||||
/** The persistent path to use when none is configured. */
|
||||
public static String persistentPathDefault() {
|
||||
if (!applies()) {
|
||||
return BUNDLED_DEFAULT;
|
||||
}
|
||||
return forPlatform(System.getProperty("os.name", ""), System.getenv(),
|
||||
System.getProperty("user.home", ".")).toString();
|
||||
}
|
||||
|
||||
static boolean applies() {
|
||||
return ImageMode.current() == ImageMode.NATIVE_RUN && !new ContainerDetector().isRunningInContainer();
|
||||
}
|
||||
|
||||
static Path forPlatform(String osName, Map<String, String> env, String userHome) {
|
||||
String os = osName.toLowerCase(Locale.ROOT);
|
||||
if (os.startsWith("windows")) {
|
||||
String localAppData = env.get("LOCALAPPDATA");
|
||||
Path base = isSet(localAppData) ? Path.of(localAppData) : Path.of(userHome, "AppData", "Local");
|
||||
return base.resolve(DIR_NAME);
|
||||
}
|
||||
if (os.startsWith("mac")) {
|
||||
return Path.of(userHome, "Library", "Application Support", DIR_NAME);
|
||||
}
|
||||
String xdgDataHome = env.get("XDG_DATA_HOME");
|
||||
Path base = isSet(xdgDataHome) ? Path.of(xdgDataHome) : Path.of(userHome, ".local", "share");
|
||||
return base.resolve(DIR_NAME);
|
||||
}
|
||||
|
||||
private static boolean isSet(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import org.eclipse.microprofile.config.spi.ConfigSource;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Supplies {@link HostDataDir}'s per-user data directory as the storage path of a native binary
|
||||
* running outside a container.
|
||||
*
|
||||
* <p>The ordinal sits just above the bundled {@code application.yml} (255) and below every source
|
||||
* a user sets explicitly: {@code config/application.yml} in the working directory (265),
|
||||
* {@code .env} (295), environment variables (300) and system properties (400). So it only
|
||||
* replaces the bundled {@code ./data}; any path the user configures still wins. It is empty for
|
||||
* the JVM, dev mode and the image.
|
||||
*/
|
||||
public class HostDefaultsConfigSource implements ConfigSource {
|
||||
|
||||
static final int ORDINAL = 260;
|
||||
static final String PERSISTENT_PATH = "floci.storage.persistent-path";
|
||||
static final String HOST_PERSISTENT_PATH = "floci.storage.host-persistent-path";
|
||||
|
||||
private final Map<String, String> properties;
|
||||
|
||||
public HostDefaultsConfigSource() {
|
||||
this(HostDataDir.applies());
|
||||
}
|
||||
|
||||
HostDefaultsConfigSource(boolean nativeOnHost) {
|
||||
if (nativeOnHost) {
|
||||
String dir = HostDataDir.persistentPathDefault();
|
||||
// The binary and Docker share the host filesystem, so child containers bind the same path.
|
||||
this.properties = Map.of(PERSISTENT_PATH, dir, HOST_PERSISTENT_PATH, dir);
|
||||
} else {
|
||||
this.properties = Map.of();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getPropertyNames() {
|
||||
return properties.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue(String propertyName) {
|
||||
return properties.get(propertyName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "floci-host-defaults";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrdinal() {
|
||||
return ORDINAL;
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,7 @@ public class TlsConfigSource implements ConfigSource {
|
||||
String certPath = resolveProperty("floci.tls.cert-path", "");
|
||||
String keyPath = resolveProperty("floci.tls.key-path", "");
|
||||
String selfSigned = resolveProperty("floci.tls.self-signed", "true");
|
||||
String persistentPath = resolveProperty("floci.storage.persistent-path", "./data");
|
||||
String persistentPath = resolveProperty("floci.storage.persistent-path", HostDataDir.persistentPathDefault());
|
||||
Path tlsDir = Path.of(persistentPath, TLS_DIR);
|
||||
resolvedTlsDir = tlsDir;
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
io.github.hectorvent.floci.config.TlsConfigSource
|
||||
io.github.hectorvent.floci.config.HostDefaultsConfigSource
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
class HostDataDirTest {
|
||||
|
||||
@Test
|
||||
void linuxUsesXdgDataHomeWhenSet() {
|
||||
assertThat(HostDataDir.forPlatform("Linux", Map.of("XDG_DATA_HOME", "/srv/xdg"), "/home/u"),
|
||||
equalTo(Path.of("/srv/xdg", "floci-aws")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void linuxFallsBackToLocalShare() {
|
||||
assertThat(HostDataDir.forPlatform("Linux", Map.of("XDG_DATA_HOME", " "), "/home/u"),
|
||||
equalTo(Path.of("/home/u", ".local", "share", "floci-aws")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void macUsesApplicationSupport() {
|
||||
assertThat(HostDataDir.forPlatform("Mac OS X", Map.of(), "/Users/u"),
|
||||
equalTo(Path.of("/Users/u", "Library", "Application Support", "floci-aws")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void windowsUsesLocalAppData() {
|
||||
assertThat(HostDataDir.forPlatform("Windows 11", Map.of("LOCALAPPDATA", "C:/Users/u/AppData/Local"), "C:/Users/u"),
|
||||
equalTo(Path.of("C:/Users/u/AppData/Local", "floci-aws")));
|
||||
assertThat(HostDataDir.forPlatform("Windows 11", Map.of(), "C:/Users/u"),
|
||||
equalTo(Path.of("C:/Users/u", "AppData", "Local", "floci-aws")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jvmRunsKeepTheBundledDefault() {
|
||||
// The test JVM is not a native image, so nothing moves.
|
||||
assertThat(HostDataDir.persistentPathDefault(), equalTo("./data"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.aMapWithSize;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.Matchers.lessThan;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
class HostDefaultsConfigSourceTest {
|
||||
|
||||
@Test
|
||||
void emptyOutsideANativeHostRun() {
|
||||
HostDefaultsConfigSource source = new HostDefaultsConfigSource(false);
|
||||
assertThat(source.getProperties(), aMapWithSize(0));
|
||||
assertThat(source.getValue(HostDefaultsConfigSource.PERSISTENT_PATH), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void jvmConstructorIsEmpty() {
|
||||
assertThat(new HostDefaultsConfigSource().getProperties(), aMapWithSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nativeHostRunSetsBothPathsToTheSameDirectory() {
|
||||
HostDefaultsConfigSource source = new HostDefaultsConfigSource(true);
|
||||
String dir = source.getValue(HostDefaultsConfigSource.PERSISTENT_PATH);
|
||||
assertThat(source.getValue(HostDefaultsConfigSource.HOST_PERSISTENT_PATH), equalTo(dir));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ordinalOverridesOnlyTheBundledYaml() {
|
||||
// classpath application.yml is 255, config/application.yml in the working directory 265.
|
||||
assertThat(new HostDefaultsConfigSource(false).getOrdinal(), allOf(greaterThan(255), lessThan(265)));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user