mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-26 15:21:06 -05:00
fix(profile): keep legacy profile names usable and expand values consistently (#25)
* fix(profile): keep legacy profile names usable and expand values consistently Two edges of the 0.2.2 profile work, both found by an oversight pass over what landed since 0.2.1. Profile names that earlier versions accepted became unreachable. The path traversal guard added in #23 was an allow-list over the whole character set ([A-Za-z0-9._-]+), which also rejects names 0.2.1 created happily: a space, a plus, an at sign. Such a profile stayed visible in 'config profile list' while show, --profile and delete all refused it, so it could not even be removed through the CLI. The guard is now a deny-list on what actually traverses ('.', '..', and path separators), and the guarantee is carried by resolving the file and asserting its parent is the profiles directory. A name that is illegal on the running platform but legal elsewhere, such as a colon on Windows, now reports a clean message instead of InvalidPathException. Profile values were expanded on one path and not the others. Interpolation of ${env:HOME} and friends is picocli's, applied when it fills an option default, so 'start --profile p' expanded it while 'restart --profile p' and 'config show --profile p' read the Profile bean directly and got the literal text. Restart therefore asked docker for a directory named '${env:HOME}/...', which is the silent state loss #23 set out to fix. Both now resolve through StartCommand.resolvedFor, the same provider and the same precedence a real start invocation uses, so one profile cannot mean two different directories. That also removes the duplicated field mapping in RestartCommand: ProfileDefaults is the only place a profile field maps to a flag again. Restart now resolves the profile before stopping the container, so a profile that fails to resolve no longer leaves the container stopped. * fix(profile): stop denying backslash, and read the profile once in config show Two P2s from the review on this PR, both correct. Backslash is a separator on Windows and an ordinary file-name character on Unix. Denying it outright orphaned a 'team\alpha' profile that 0.2.1 could create and that list() still returns, which is the same bug class this PR exists to fix, left half fixed. The character check is now forward slash only; resolveInProfilesDir already rejects a real Windows traversal on the parent assertion, so the guarantee is unchanged. config show read the profile twice: key presence from one read, values from a second through resolvedFor. A concurrent edit could print half of one version next to half of another. ProfileDefaultValueProvider already memoizes the profile it resolved, so exposing that memo gives both from one snapshot, and resolvedFor gained a provider overload to pass it through. If the profile has gone since the outer parse, the command now fails loudly instead of printing a mixed row. RestartCommand has the same shape between the outer parse and resolvedFor. Closing it means making the outer provider reachable from commands, which is wider than this fix; documented in place rather than left silent.
This commit is contained in:
1 parent
3159a653b4
commit
7343a539c2
9 files changed
+231
-49
No files matched your search
@@ -78,6 +78,16 @@ class ConfigCommandsTest {
|
||||
assertTrue(out.contains("\"container\" : \"floci-probe\""), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsTheInterpolatedPersistDirThatStartWouldActuallyUse() throws Exception {
|
||||
writeProfile("interp", "persistDir: ${env:HOME}/floci-data\n");
|
||||
|
||||
String out = runShow(ProductProfile.AWS, "--profile", "interp", "-o", "json");
|
||||
|
||||
assertTrue(out.contains("\"persistDir\" : \"" + System.getenv("HOME") + "/floci-data\""), out);
|
||||
assertFalse(out.contains("${env:"), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAProfileItReportsTheProductDefaults() {
|
||||
String out = runShow(ProductProfile.GCP, "-o", "json");
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.FlociCli;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.StartCommand;
|
||||
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@@ -139,6 +142,23 @@ class ProfilePrecedenceTest {
|
||||
leafSpec("update", "--check").findOption("--check").getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* config show reads presence and values from one snapshot by asking the provider what it
|
||||
* resolved, rather than reading the file a second time. If this memo stops being populated,
|
||||
* that command silently drops every start-only row.
|
||||
*/
|
||||
@Test
|
||||
void theProviderRemembersTheProfileItResolved() throws Exception {
|
||||
writeProfile("probe", FULL_PROFILE);
|
||||
ProfileDefaultValueProvider provider = new ProfileDefaultValueProvider(new ProfileStore(tempDir));
|
||||
|
||||
StartCommand resolved = StartCommand.resolvedFor(ProductProfile.AWS, provider, "probe");
|
||||
|
||||
assertEquals("floci/floci:enforced", resolved.image());
|
||||
assertEquals(4599, resolved.port());
|
||||
assertEquals("floci-probe", provider.resolved().orElseThrow().container);
|
||||
}
|
||||
|
||||
@Test
|
||||
void picocliInterpolatesProfileValues() throws Exception {
|
||||
// Documented consequence of routing values through picocli's default-value machinery.
|
||||
|
||||
@@ -4,6 +4,8 @@ import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.config.Profile;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledOnOs;
|
||||
import org.junit.jupiter.api.condition.OS;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
@@ -93,12 +95,52 @@ class ProfileStoreTest {
|
||||
assertEquals(name, ProfileStore.validateName(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 0.2.1 resolved the raw name, so every one of these was creatable. An allow-list on the
|
||||
* character set orphaned them: listed by 'config profile list', rejected by show, --profile
|
||||
* and delete, with no way to remove them through the CLI.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", " ", ".", "..", "../x", "../../etc/passwd", "a/b", "a\\b", "/abs", "a:b"})
|
||||
@ValueSource(strings = {"team alpha", "prod+eu", "dev@local", "staging(1)", "a b", "sam's"})
|
||||
void keepsAcceptingNamesEarlierVersionsAllowed(String name) {
|
||||
assertEquals(name, ProfileStore.validateName(name));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyNameRoundTripsAndCanBeDeleted() throws Exception {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve("team alpha.yaml"), "container: floci-team\n");
|
||||
|
||||
assertEquals("floci-team", store().get("team alpha").orElseThrow().container);
|
||||
assertTrue(store().delete("team alpha"));
|
||||
assertTrue(store().get("team alpha").isEmpty());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", " ", ".", "..", "../x", "../../etc/passwd", "a/b", "/abs"})
|
||||
void rejectsNamesThatCouldEscapeTheProfilesDirectory(String name) {
|
||||
assertThrows(IllegalArgumentException.class, () -> ProfileStore.validateName(name), name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Backslash is a separator on Windows and an ordinary character on Unix, so 0.2.1 could
|
||||
* create 'team\\alpha.yaml' here and list() still returns it. Denying it outright would
|
||||
* orphan that profile exactly the way the character allow-list did. The escape check is
|
||||
* resolveInProfilesDir, which rejects a real Windows traversal on the parent assertion.
|
||||
*/
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void backslashIsAnOrdinaryCharacterOnUnix() throws Exception {
|
||||
assertEquals("team\\alpha", ProfileStore.validateName("team\\alpha"));
|
||||
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve("team\\alpha.yaml"), "container: floci-team\n");
|
||||
assertEquals("floci-team", store().get("team\\alpha").orElseThrow().container);
|
||||
assertEquals(tempDir.toAbsolutePath().normalize(),
|
||||
store().profileFile("team\\alpha").getParent());
|
||||
assertTrue(store().delete("team\\alpha"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsNullName() {
|
||||
assertThrows(IllegalArgumentException.class, () -> ProfileStore.validateName(null));
|
||||
@@ -109,4 +151,13 @@ class ProfileStoreTest {
|
||||
assertThrows(IllegalArgumentException.class, () -> store().profileFile("../../escaped"));
|
||||
assertThrows(IllegalArgumentException.class, () -> store().delete("../../escaped"));
|
||||
}
|
||||
|
||||
/** The guarantee is the resolved parent, not the character rules. */
|
||||
@Test
|
||||
void everyResolvedFileSitsDirectlyInTheProfilesDirectory() {
|
||||
for (String name : new String[]{"ok", "team alpha", "prod+eu", "dev@local", "..leading"}) {
|
||||
assertEquals(tempDir.toAbsolutePath().normalize(),
|
||||
store().profileFile(name).getParent(), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,23 @@ class RestartCommandTest {
|
||||
"floci/floci:enforced"), args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Values reach a real 'start' through picocli, which interpolates ${env:...}. Restart applies
|
||||
* the profile itself, so it has to go through the same machinery or one profile means two
|
||||
* different host directories depending on which command you ran.
|
||||
*/
|
||||
@Test
|
||||
void interpolatesProfileValuesExactlyAsStartDoes() throws Exception {
|
||||
writeProfile("interp", "persistDir: ${env:HOME}/floci-data\n");
|
||||
|
||||
List<String> args = parse(ProductProfile.AWS, "--profile", "interp")
|
||||
.buildStartCommand().dockerRunArgs(SOCKET);
|
||||
|
||||
assertTrue(args.contains(System.getenv("HOME") + "/floci-data:/app/data"),
|
||||
"restart must expand the profile the way start does, but got: " + args);
|
||||
assertFalse(args.stream().anyMatch(a -> a.contains("${env:")), args.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAProfileItStillUsesTheProductDefaults() {
|
||||
List<String> args = parse(ProductProfile.GCP).buildStartCommand().dockerRunArgs(SOCKET);
|
||||
|
||||
Reference in new issue
Block a user