mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-12-01 17:33:04 -05:00
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:
7 files changed
+87
-15
No files matched your search
@@ -72,6 +72,11 @@ public class RestartCommand implements Callable<Integer> {
|
||||
|
||||
// The real invocation's globals win: they already carry the profile plus any flag the
|
||||
// user passed, resolved once by the outer parse.
|
||||
//
|
||||
// Known residual: those globals come from the outer parse's read of the profile while the
|
||||
// fields above come from resolvedFor's, so a profile edited between the two would mix
|
||||
// snapshots. Closing it means making the outer provider reachable from commands, which is
|
||||
// a wider change than this fix; tracked as batman LAY-1.
|
||||
start.global = global;
|
||||
start.pull = "missing";
|
||||
start.detach = false;
|
||||
|
||||
@@ -65,11 +65,22 @@ public class StartCommand implements Callable<Integer> {
|
||||
* disagrees with {@code start} on any interpolated value.
|
||||
*/
|
||||
public static StartCommand resolvedFor(ProductProfile product, ProfileStore store, String profileName) {
|
||||
return resolvedFor(product, new ProfileDefaultValueProvider(store), profileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, but with the provider supplied so a caller can read back the one profile snapshot
|
||||
* it resolved ({@link ProfileDefaultValueProvider#resolved()}) instead of reading the file a
|
||||
* second time and risking a mix of two versions.
|
||||
*/
|
||||
public static StartCommand resolvedFor(ProductProfile product,
|
||||
ProfileDefaultValueProvider provider,
|
||||
String profileName) {
|
||||
StartCommand start = new StartCommand(product);
|
||||
if (profileName != null) {
|
||||
new CommandLine(start)
|
||||
.setCaseInsensitiveEnumValuesAllowed(true)
|
||||
.setDefaultValueProvider(new ProfileDefaultValueProvider(store))
|
||||
.setDefaultValueProvider(provider)
|
||||
.parseArgs("--profile", profileName);
|
||||
}
|
||||
return start;
|
||||
|
||||
@@ -4,13 +4,13 @@ import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.StartCommand;
|
||||
import io.floci.cli.config.Profile;
|
||||
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.output.Printer;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -74,21 +74,22 @@ public class ConfigShowCommand implements Callable<Integer> {
|
||||
}
|
||||
|
||||
// Values come from a profile-resolved StartCommand rather than the Profile bean, so an
|
||||
// interpolated persistDir reads here exactly as 'start' would use it. The bean is still
|
||||
// consulted for presence, so a product default never shows up looking like a profile value.
|
||||
// interpolated persistDir reads here exactly as 'start' would use it. The bean is consulted
|
||||
// for presence, so a product default never shows up looking like a profile value.
|
||||
//
|
||||
// Both come from ONE snapshot: the provider memoizes the profile it read, so presence and
|
||||
// values cannot disagree if another process edits the file mid-command. If the profile has
|
||||
// gone since the outer parse resolved it, resolvedFor throws and the command fails loudly,
|
||||
// which beats printing half of an old profile next to half of a new one.
|
||||
private void addStartSettings(Map<String, Object> data) {
|
||||
if (global.profile == null) return;
|
||||
Optional<Profile> declared;
|
||||
try {
|
||||
declared = store.get(global.profile);
|
||||
} catch (IOException | IllegalArgumentException e) {
|
||||
// Parsing already resolved this name; nothing useful to add here.
|
||||
return;
|
||||
}
|
||||
|
||||
ProfileDefaultValueProvider provider = new ProfileDefaultValueProvider(store);
|
||||
StartCommand resolved = StartCommand.resolvedFor(profile, provider, global.profile);
|
||||
Optional<Profile> declared = provider.resolved();
|
||||
if (declared.isEmpty()) return;
|
||||
|
||||
Profile p = declared.get();
|
||||
StartCommand resolved = StartCommand.resolvedFor(profile, store, global.profile);
|
||||
if (p.image != null) data.put("image", resolved.image());
|
||||
if (p.port != null) data.put("port", resolved.port());
|
||||
if (p.persistDir != null) data.put("persistDir", resolved.persistDir());
|
||||
|
||||
@@ -41,6 +41,15 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
/**
|
||||
* The profile this provider resolved during the last parse, if any. Reading it costs nothing
|
||||
* and, crucially, costs no second read of the file: a caller that needs both which keys the
|
||||
* profile declares and what they expand to gets both from one snapshot.
|
||||
*/
|
||||
public Optional<Profile> resolved() {
|
||||
return Optional.ofNullable(resolved);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(ArgSpec arg) {
|
||||
// Positional parameters reach the provider too; only options map to profile fields.
|
||||
|
||||
@@ -24,7 +24,12 @@ public class ProfileStore {
|
||||
// 'config profile list' and unreachable by show, --profile and delete, with no way to remove
|
||||
// them through the CLI. The guarantee is carried by resolveInProfilesDir below, not by the
|
||||
// character rules.
|
||||
private static final Pattern PATH_SEPARATOR = Pattern.compile("[/\\\\]");
|
||||
//
|
||||
// Backslash is deliberately NOT here. Java NIO treats it as a separator on Windows, where
|
||||
// resolveInProfilesDir already rejects '..\\..\\escape' on the parent check, and as an
|
||||
// ordinary file-name character on Unix, where 0.2.1 could create 'team\\alpha.yaml' and
|
||||
// list() still returns it. Denying it would orphan that profile on Unix for no gain.
|
||||
private static final Pattern PATH_SEPARATOR = Pattern.compile("/");
|
||||
|
||||
private final Path profilesDir;
|
||||
|
||||
@@ -48,7 +53,7 @@ public class ProfileStore {
|
||||
}
|
||||
if (".".equals(name) || "..".equals(name) || PATH_SEPARATOR.matcher(name).find()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid profile name '" + name + "'. It must not be '.', '..', or contain a path separator.\n"
|
||||
"Invalid profile name '" + name + "'. It must not be '.', '..', or contain '/'.\n"
|
||||
+ "Run 'floci config profile list' to see available profiles.");
|
||||
}
|
||||
return name;
|
||||
|
||||
@@ -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;
|
||||
@@ -115,11 +117,30 @@ class ProfileStoreTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", " ", ".", "..", "../x", "../../etc/passwd", "a/b", "a\\b", "/abs"})
|
||||
@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));
|
||||
|
||||
Reference in new issue
Block a user