From 3c708764486790ccfd4ce17993d276304bff74ea Mon Sep 17 00:00:00 2001 From: soreavis <263610811+soreavis@users.noreply.github.com> Date: Thu, 5 Nov 2026 21:25:35 +0100 Subject: [PATCH] feat(start): add --security-opt so Floci can reach the socket on SELinux hosts (#70) * feat(start): add --security-opt so Floci can reach the socket on SELinux hosts With SELinux enforcing, as in the podman machine VM, the emulator cannot connect to the mounted Podman socket: the kernel denies connectto from container_t to the daemon's container_runtime_t, and the web console fails with java.net.BindException: Permission denied. Relabeling the mount with :z fixes access to the socket file but not that check. --security-opt label=disable gets past it, and start had no way to pass it. The option is repeatable and goes to docker run unchanged. A restart without --profile reads it back from the running container, like the port, image and services; with --profile the start is rebuilt from the profile, which has no key for it. docker inspect reports a seccomp profile file inline, which docker run rejects, so such a value fails start's validation, and with it the restart, before the stop removes the container. Signed-off-by: soreavis <263610811+soreavis@users.noreply.github.com> * fix(start): keep --security-opt in retry and recovery commands The failed-pull hint and the restart recovery command rebuild the start from its flags, so they dropped --security-opt and a retry lost label=disable. Carry it into both, shell-quoted. The README's profile workaround stopped the default instance: name the same profile and product on stop as on start. Signed-off-by: soreavis <263610811+soreavis@users.noreply.github.com> --------- Signed-off-by: soreavis <263610811+soreavis@users.noreply.github.com> --- README.md | 19 +++++++- docs/help/floci-aws-start.txt | 44 ++++++++++--------- docs/help/floci-az-start.txt | 44 ++++++++++--------- docs/help/floci-gcp-start.txt | 44 ++++++++++--------- docs/help/floci-oci-start.txt | 44 ++++++++++--------- docs/help/floci-start.txt | 44 ++++++++++--------- plugin.json | 2 +- skills/floci/references/troubleshooting.md | 8 +++- .../io/floci/cli/commands/RestartCommand.java | 1 + .../io/floci/cli/commands/StartCommand.java | 14 ++++++ .../io/floci/cli/docker/DockerClient.java | 10 +++-- .../cli/unit/DockerClientProcessTest.java | 4 +- .../io/floci/cli/unit/InstanceHintsTest.java | 6 ++- .../io/floci/cli/unit/RestartCommandTest.java | 36 +++++++++++++-- .../floci/cli/unit/RestartRecoveryTest.java | 4 +- .../floci/cli/unit/StartCommandArgsTest.java | 12 +++++ 16 files changed, 216 insertions(+), 120 deletions(-) diff --git a/README.md b/README.md index ecbc8c0..2a8b888 100644 --- a/README.md +++ b/README.md @@ -474,6 +474,23 @@ For a `unix://` socket the resolved path is bind-mounted into the container; for remote `tcp://` daemon the `DOCKER_HOST` value is passed through to the container instead. Run `floci doctor` to see which endpoint was resolved. +With SELinux enforcing (for example the Fedora VM behind `podman machine`), the container's label +keeps Floci from connecting to the socket, and the web console fails to start with +`java.net.BindException: Permission denied`. Relabeling the mount with `:z` is not enough: it fixes +access to the socket file, but SELinux also checks the daemon on the other end of the socket. Turn +labeling off for the Floci container instead: + +```sh +floci start --security-opt label=disable +``` + +`--security-opt` is passed to `docker run` unchanged and can be repeated. Without `--profile`, +`floci restart` passes back the options `docker inspect` reports for the running container. With +`--profile` it starts from the profile, which has no key for them: run +`floci stop --profile --remove`, then +`floci start --profile --security-opt ...`, where `` is the profile's +`aws`, `gcp`, `az` or `oci`. + ### `floci stop` / `floci gcp stop` / `floci az stop` / `floci oci stop` ```sh @@ -979,7 +996,7 @@ Where a command's codes carry a specific meaning a script can rely on: | `doctor` | every check passed (warnings allowed) | at least one check failed | `--check` names no known check | | `update --check` | up to date | an update is available | the check itself failed | | `update` | updated, or already up to date | the update failed; the binary is unchanged | `--version` is not a release version | -| `start`, `restart` | started (and ready, unless `--detach`) | Docker, the image, the port or readiness failed, or the `--persist` directory cannot be created | `--port`, `--pull`, the namespace or the `--persist` path is not valid | +| `start`, `restart` | started (and ready, unless `--detach`) | Docker, the image, the port or readiness failed, or the `--persist` directory cannot be created | `--port`, `--pull`, the namespace, the `--persist` path or a `--security-opt seccomp=` value is not valid | | `stop` | stopped, or was not running | no such container, or Docker failed | bad flag or profile | | `reset` | state reset | declined, not available on this server, no running instance to reset, or the configuration names two different servers | no terminal to ask on and no `--yes` | | `config validate` | no errors (warnings allowed) | errors found, or the file cannot be read | bad flag | diff --git a/docs/help/floci-aws-start.txt b/docs/help/floci-aws-start.txt index 0355605..df865c9 100644 --- a/docs/help/floci-aws-start.txt +++ b/docs/help/floci-aws-start.txt @@ -1,27 +1,29 @@ Usage: floci aws start [-hqvV] [--detach] [--json] [--no-color] [--no-emoji] [--container=] [--endpoint=] [--image=] [--namespace=] [-o=text|json|yaml] [--persist=] [--port=] [--profile=] - [--pull=always|missing|never] [--services=] + [--pull=always|missing|never] [--services=] [--security-opt=]... Start the Floci AWS emulator container - --container= Floci container name (default: floci) - --detach Return immediately without waiting for readiness - --endpoint= Floci server endpoint URL (default: http://localhost:4566) - -h, --help Show this help message and exit. - --image= Image reference to use (default: floci/floci:latest) - --json Shorthand for --output json - --namespace= Docker resource namespace for the containers this instance launches - (default: derived from --container; none for the default container) - --no-color Disable ANSI colors - --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is - piped) + --container= Floci container name (default: floci) + --detach Return immediately without waiting for readiness + --endpoint= Floci server endpoint URL (default: http://localhost:4566) + -h, --help Show this help message and exit. + --image= Image reference to use (default: floci/floci:latest) + --json Shorthand for --output json + --namespace= Docker resource namespace for the containers this instance launches + (default: derived from --container; none for the default container) + --no-color Disable ANSI colors + --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is + piped) -o, --output=text|json|yaml - Output format: text, json, yaml - --persist= Host directory for persistent state - --port= Host port to bind (default: 4566) - --profile= Config profile from ~/.floci/profiles/ + Output format: text, json, yaml + --persist= Host directory for persistent state + --port= Host port to bind (default: 4566) + --profile= Config profile from ~/.floci/profiles/ --pull=always|missing|never - Image pull policy: always, missing, never - -q, --quiet Suppress non-error output - --services= Comma-separated list of services to enable - -v, --verbose Debug logging to stderr - -V, --version Print version information and exit. + Image pull policy: always, missing, never + -q, --quiet Suppress non-error output + --security-opt= Docker security option for the emulator container, repeatable + (label=disable lets it reach the Docker socket on SELinux hosts) + --services= Comma-separated list of services to enable + -v, --verbose Debug logging to stderr + -V, --version Print version information and exit. diff --git a/docs/help/floci-az-start.txt b/docs/help/floci-az-start.txt index ce12c02..7863a05 100644 --- a/docs/help/floci-az-start.txt +++ b/docs/help/floci-az-start.txt @@ -1,27 +1,29 @@ Usage: floci az start [-hqvV] [--detach] [--json] [--no-color] [--no-emoji] [--container=] [--endpoint=] [--image=] [--namespace=] [-o=text|json|yaml] [--persist=] [--port=] [--profile=] - [--pull=always|missing|never] [--services=] + [--pull=always|missing|never] [--services=] [--security-opt=]... Start the Floci Azure emulator container - --container= Floci container name (default: floci-az) - --detach Return immediately without waiting for readiness - --endpoint= Floci server endpoint URL (default: http://localhost:4577) - -h, --help Show this help message and exit. - --image= Image reference to use (default: floci/floci-az:latest) - --json Shorthand for --output json - --namespace= Docker resource namespace for the containers this instance launches - (default: derived from --container; none for the default container) - --no-color Disable ANSI colors - --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is - piped) + --container= Floci container name (default: floci-az) + --detach Return immediately without waiting for readiness + --endpoint= Floci server endpoint URL (default: http://localhost:4577) + -h, --help Show this help message and exit. + --image= Image reference to use (default: floci/floci-az:latest) + --json Shorthand for --output json + --namespace= Docker resource namespace for the containers this instance launches + (default: derived from --container; none for the default container) + --no-color Disable ANSI colors + --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is + piped) -o, --output=text|json|yaml - Output format: text, json, yaml - --persist= Host directory for persistent state - --port= Host port to bind (default: 4577) - --profile= Config profile from ~/.floci/profiles/ + Output format: text, json, yaml + --persist= Host directory for persistent state + --port= Host port to bind (default: 4577) + --profile= Config profile from ~/.floci/profiles/ --pull=always|missing|never - Image pull policy: always, missing, never - -q, --quiet Suppress non-error output - --services= Comma-separated list of services to enable - -v, --verbose Debug logging to stderr - -V, --version Print version information and exit. + Image pull policy: always, missing, never + -q, --quiet Suppress non-error output + --security-opt= Docker security option for the emulator container, repeatable + (label=disable lets it reach the Docker socket on SELinux hosts) + --services= Comma-separated list of services to enable + -v, --verbose Debug logging to stderr + -V, --version Print version information and exit. diff --git a/docs/help/floci-gcp-start.txt b/docs/help/floci-gcp-start.txt index a10aa40..41c0466 100644 --- a/docs/help/floci-gcp-start.txt +++ b/docs/help/floci-gcp-start.txt @@ -1,27 +1,29 @@ Usage: floci gcp start [-hqvV] [--detach] [--json] [--no-color] [--no-emoji] [--container=] [--endpoint=] [--image=] [--namespace=] [-o=text|json|yaml] [--persist=] [--port=] [--profile=] - [--pull=always|missing|never] [--services=] + [--pull=always|missing|never] [--services=] [--security-opt=]... Start the Floci GCP emulator container - --container= Floci container name (default: floci-gcp) - --detach Return immediately without waiting for readiness - --endpoint= Floci server endpoint URL (default: http://localhost:4588) - -h, --help Show this help message and exit. - --image= Image reference to use (default: floci/floci-gcp:latest) - --json Shorthand for --output json - --namespace= Docker resource namespace for the containers this instance launches - (default: derived from --container; none for the default container) - --no-color Disable ANSI colors - --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is - piped) + --container= Floci container name (default: floci-gcp) + --detach Return immediately without waiting for readiness + --endpoint= Floci server endpoint URL (default: http://localhost:4588) + -h, --help Show this help message and exit. + --image= Image reference to use (default: floci/floci-gcp:latest) + --json Shorthand for --output json + --namespace= Docker resource namespace for the containers this instance launches + (default: derived from --container; none for the default container) + --no-color Disable ANSI colors + --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is + piped) -o, --output=text|json|yaml - Output format: text, json, yaml - --persist= Host directory for persistent state - --port= Host port to bind (default: 4588) - --profile= Config profile from ~/.floci/profiles/ + Output format: text, json, yaml + --persist= Host directory for persistent state + --port= Host port to bind (default: 4588) + --profile= Config profile from ~/.floci/profiles/ --pull=always|missing|never - Image pull policy: always, missing, never - -q, --quiet Suppress non-error output - --services= Comma-separated list of services to enable - -v, --verbose Debug logging to stderr - -V, --version Print version information and exit. + Image pull policy: always, missing, never + -q, --quiet Suppress non-error output + --security-opt= Docker security option for the emulator container, repeatable + (label=disable lets it reach the Docker socket on SELinux hosts) + --services= Comma-separated list of services to enable + -v, --verbose Debug logging to stderr + -V, --version Print version information and exit. diff --git a/docs/help/floci-oci-start.txt b/docs/help/floci-oci-start.txt index 45ce9c5..1f62145 100644 --- a/docs/help/floci-oci-start.txt +++ b/docs/help/floci-oci-start.txt @@ -1,27 +1,29 @@ Usage: floci oci start [-hqvV] [--detach] [--json] [--no-color] [--no-emoji] [--container=] [--endpoint=] [--image=] [--namespace=] [-o=text|json|yaml] [--persist=] [--port=] [--profile=] - [--pull=always|missing|never] [--services=] + [--pull=always|missing|never] [--services=] [--security-opt=]... Start the Floci OCI emulator container - --container= Floci container name (default: floci-oci) - --detach Return immediately without waiting for readiness - --endpoint= Floci server endpoint URL (default: http://localhost:4599) - -h, --help Show this help message and exit. - --image= Image reference to use (default: floci/floci-oci:latest) - --json Shorthand for --output json - --namespace= Docker resource namespace for the containers this instance launches - (default: derived from --container; none for the default container) - --no-color Disable ANSI colors - --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is - piped) + --container= Floci container name (default: floci-oci) + --detach Return immediately without waiting for readiness + --endpoint= Floci server endpoint URL (default: http://localhost:4599) + -h, --help Show this help message and exit. + --image= Image reference to use (default: floci/floci-oci:latest) + --json Shorthand for --output json + --namespace= Docker resource namespace for the containers this instance launches + (default: derived from --container; none for the default container) + --no-color Disable ANSI colors + --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is + piped) -o, --output=text|json|yaml - Output format: text, json, yaml - --persist= Host directory for persistent state - --port= Host port to bind (default: 4599) - --profile= Config profile from ~/.floci/profiles/ + Output format: text, json, yaml + --persist= Host directory for persistent state + --port= Host port to bind (default: 4599) + --profile= Config profile from ~/.floci/profiles/ --pull=always|missing|never - Image pull policy: always, missing, never - -q, --quiet Suppress non-error output - --services= Comma-separated list of services to enable - -v, --verbose Debug logging to stderr - -V, --version Print version information and exit. + Image pull policy: always, missing, never + -q, --quiet Suppress non-error output + --security-opt= Docker security option for the emulator container, repeatable + (label=disable lets it reach the Docker socket on SELinux hosts) + --services= Comma-separated list of services to enable + -v, --verbose Debug logging to stderr + -V, --version Print version information and exit. diff --git a/docs/help/floci-start.txt b/docs/help/floci-start.txt index dbcb69d..801c566 100644 --- a/docs/help/floci-start.txt +++ b/docs/help/floci-start.txt @@ -1,27 +1,29 @@ Usage: floci start [-hqvV] [--detach] [--json] [--no-color] [--no-emoji] [--container=] [--endpoint=] [--image=] [--namespace=] [-o=text|json|yaml] [--persist=] [--port=] [--profile=] - [--pull=always|missing|never] [--services=] + [--pull=always|missing|never] [--services=] [--security-opt=]... Start the Floci AWS emulator container - --container= Floci container name (default: floci) - --detach Return immediately without waiting for readiness - --endpoint= Floci server endpoint URL (default: http://localhost:4566) - -h, --help Show this help message and exit. - --image= Image reference to use (default: floci/floci:latest) - --json Shorthand for --output json - --namespace= Docker resource namespace for the containers this instance launches - (default: derived from --container; none for the default container) - --no-color Disable ANSI colors - --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is - piped) + --container= Floci container name (default: floci) + --detach Return immediately without waiting for readiness + --endpoint= Floci server endpoint URL (default: http://localhost:4566) + -h, --help Show this help message and exit. + --image= Image reference to use (default: floci/floci:latest) + --json Shorthand for --output json + --namespace= Docker resource namespace for the containers this instance launches + (default: derived from --container; none for the default container) + --no-color Disable ANSI colors + --no-emoji Plain ASCII marks instead of Unicode ones (the default when output is + piped) -o, --output=text|json|yaml - Output format: text, json, yaml - --persist= Host directory for persistent state - --port= Host port to bind (default: 4566) - --profile= Config profile from ~/.floci/profiles/ + Output format: text, json, yaml + --persist= Host directory for persistent state + --port= Host port to bind (default: 4566) + --profile= Config profile from ~/.floci/profiles/ --pull=always|missing|never - Image pull policy: always, missing, never - -q, --quiet Suppress non-error output - --services= Comma-separated list of services to enable - -v, --verbose Debug logging to stderr - -V, --version Print version information and exit. + Image pull policy: always, missing, never + -q, --quiet Suppress non-error output + --security-opt= Docker security option for the emulator container, repeatable + (label=disable lets it reach the Docker socket on SELinux hosts) + --services= Comma-separated list of services to enable + -v, --verbose Debug logging to stderr + -V, --version Print version information and exit. diff --git a/plugin.json b/plugin.json index 1ae46b4..1347c73 100644 --- a/plugin.json +++ b/plugin.json @@ -1,7 +1,7 @@ { "$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", "name": "floci", - "version": "1.0.4", + "version": "1.0.5", "description": "Run, wire up and troubleshoot Floci, the free local emulators for AWS, GCP, Azure and OCI: floci-cli lifecycle, safe wiring of aws, gcloud, az, oci and the SDKs, docker run and Docker Compose files, several instances side by side, and lookups in the published docs.", "author": { "name": "Floci", diff --git a/skills/floci/references/troubleshooting.md b/skills/floci/references/troubleshooting.md index 9930474..e107cb3 100644 --- a/skills/floci/references/troubleshooting.md +++ b/skills/floci/references/troubleshooting.md @@ -41,7 +41,10 @@ pass `-o text`. Then check the cloud-specific leaks: SQS endpoint bypass (AWS), **Docker-backed services fail (Lambda, ECS, RDS, Cloud Run, AKS, Functions, OKE)** The emulator cannot reach a Docker daemon. floci-cli resolves `DOCKER_HOST`, then `DOCKER_SOCK`, then the OS default socket. For Podman: `export DOCKER_HOST=unix:///run/user/$(id -u)/podman/podman.sock`. -`doctor` shows the resolved endpoint. In Compose, mount the socket explicitly. +`doctor` shows the resolved endpoint. In Compose, mount the socket explicitly. With SELinux +enforcing (for example the `podman machine` VM), `java.net.BindException: Permission denied` means +the container's label blocks the socket: run `floci start --security-opt label=disable` +(`docker run` takes the same flag). Relabeling the mount with `:z` is not enough. **Child containers vanish or another instance's tasks get killed** Two instances share a Docker host without distinct namespaces. See namespaces.md. @@ -81,7 +84,8 @@ lists supported operations and gaps. Report it instead of retrying. ## Exit codes worth knowing - `0` success; non-zero failure for every command. -- `2` usage or validation error: bad profile, duration, namespace, port, `--pull` or `--shell`. +- `2` usage or validation error: bad profile, duration, namespace, port, `--pull`, `--shell` or an + inline `--security-opt seccomp=` profile. - `1` from every snapshot subcommand (`save`, `load`, `list`, `delete`, `export`, `import`) in every tree: not available server-side yet. A name that is a path (`.`, `..`, or with `/`) is `2`, and a bare `floci snapshot` just prints its usage and exits `0`. diff --git a/src/main/java/io/floci/cli/commands/RestartCommand.java b/src/main/java/io/floci/cli/commands/RestartCommand.java index 9d7550b..3a6ec89 100644 --- a/src/main/java/io/floci/cli/commands/RestartCommand.java +++ b/src/main/java/io/floci/cli/commands/RestartCommand.java @@ -157,6 +157,7 @@ public class RestartCommand implements Callable { if (services != null) start.services = services; String namespace = run.env().get(profile.envVar("DOCKER_RESOURCE_NAMESPACE")); if (namespace != null) start.namespace = namespace; + start.securityOpts = run.securityOpts(); }); } diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index 713f330..8209eda 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -81,6 +81,9 @@ public class StartCommand implements Callable { // Docker's own name rule: the emulators put the namespace inside child container names. private static final Pattern NAMESPACE = Pattern.compile("[A-Za-z0-9][A-Za-z0-9_.-]*"); + @Option(names = {"--security-opt"}, description = "Docker security option for the emulator container, repeatable (label=disable lets it reach the Docker socket on SELinux hosts)", paramLabel = "") + List securityOpts = List.of(); + @Option(names = {"--pull"}, description = "Image pull policy: always, missing, never", defaultValue = "missing", paramLabel = "always|missing|never") String pull; @@ -125,6 +128,7 @@ public class StartCommand implements Callable { if (global.typed("--persist")) flags.append(" --persist ").append(GlobalOptions.shellWord(persistDir)); if (global.typed("--services")) flags.append(" --services ").append(GlobalOptions.shellWord(services)); if (global.typed("--namespace")) flags.append(" --namespace ").append(GlobalOptions.shellWord(namespace)); + if (global.typed("--security-opt")) securityOpts.forEach(opt -> flags.append(" --security-opt ").append(GlobalOptions.shellWord(opt))); return flags.toString(); } @@ -144,6 +148,7 @@ public class StartCommand implements Callable { } if (services != null) command.append(" --services ").append(GlobalOptions.shellWord(services)); if (namespace != null) command.append(" --namespace ").append(GlobalOptions.shellWord(namespace)); + securityOpts.forEach(opt -> command.append(" --security-opt ").append(GlobalOptions.shellWord(opt))); return command.toString(); } @@ -180,6 +185,14 @@ public class StartCommand implements Callable { // Anything else used to fall through to a pull, so a typo silently meant "always". return "Invalid --pull '" + pull + "'.\nUse always, missing or never."; } + // docker inspect reports a seccomp profile file inline, so restart would carry it over in a + // form docker run rejects, and only after the stop has removed the container. + if (securityOpts.stream().anyMatch(opt -> opt.startsWith("seccomp={"))) { + String prefix = profile.commandPrefix(); + return "--security-opt seccomp= takes the path of a profile file, not the profile itself.\n" + + "Run '" + prefix + " start" + global.instanceSelector() + " --security-opt seccomp=', after '" + + prefix + " stop" + global.instanceSelector() + " --remove' if the container exists."; + } return null; } @@ -303,6 +316,7 @@ public class StartCommand implements Callable { args.addAll(List.of("-d", "--name", global.container)); args.addAll(List.of("-p", port + ":" + profile.defaultPort())); args.addAll(socketArgs); + securityOpts.forEach(opt -> args.addAll(List.of("--security-opt", opt))); if (persistDir != null && !persistDir.isBlank()) { args.addAll(List.of("-v", bindSource(daemon) + ":/app/data")); // The server defaults to in-memory storage; enable persistent mode so diff --git a/src/main/java/io/floci/cli/docker/DockerClient.java b/src/main/java/io/floci/cli/docker/DockerClient.java index 0d0e264..77e30a6 100644 --- a/src/main/java/io/floci/cli/docker/DockerClient.java +++ b/src/main/java/io/floci/cli/docker/DockerClient.java @@ -61,9 +61,11 @@ public class DockerClient { /** * What a container was started with, as {@code restart} needs it back: the image, the host * port bound to {@code containerPort}, the host directory bind-mounted at {@code /app/data} - * (null for a docker volume), and the environment. Any of them may be absent (null, or an empty map). + * (null for a docker volume), the environment and the {@code --security-opt} values. Any of them + * may be absent (null, or an empty map or list). */ - public record RunSettings(String image, Integer hostPort, String persistSource, Map env) {} + public record RunSettings(String image, Integer hostPort, String persistSource, Map env, + List securityOpts) {} private static final ObjectMapper JSON = new ObjectMapper(); @@ -95,7 +97,9 @@ public class DockerClient { int eq = kv.indexOf('='); if (eq > 0) env.put(kv.substring(0, eq), kv.substring(eq + 1)); } - return Optional.of(new RunSettings(image, hostPort, persist, Map.copyOf(env))); + List securityOpts = new ArrayList<>(); + for (JsonNode opt : c.path("HostConfig").path("SecurityOpt")) securityOpts.add(opt.asText()); + return Optional.of(new RunSettings(image, hostPort, persist, Map.copyOf(env), List.copyOf(securityOpts))); } catch (Exception e) { throw new DockerException("Could not read the settings of container '" + name + "': " + e.getMessage()); } diff --git a/src/test/java/io/floci/cli/unit/DockerClientProcessTest.java b/src/test/java/io/floci/cli/unit/DockerClientProcessTest.java index 2dea2c7..6eeb73f 100644 --- a/src/test/java/io/floci/cli/unit/DockerClientProcessTest.java +++ b/src/test/java/io/floci/cli/unit/DockerClientProcessTest.java @@ -216,7 +216,7 @@ class DockerClientProcessTest { Path json = tempDir.resolve("inspect.json"); Files.writeString(json, """ {"Config":{"Image":"floci/floci-gcp:0.9","Env":["FLOCI_GCP_SERVICES=storage","PATH=/usr/bin"]}, - "HostConfig":{"PortBindings":{"4588/tcp":[{"HostIp":"","HostPort":"14588"}]}}, + "HostConfig":{"PortBindings":{"4588/tcp":[{"HostIp":"","HostPort":"14588"}]},"SecurityOpt":["label=disable"]}, "Mounts":[{"Type":"bind","Source":"/data/gcp","Destination":"/app/data"}]} """); Path fakeDocker = tempDir.resolve("docker"); @@ -230,6 +230,7 @@ class DockerClientProcessTest { assertEquals(14588, run.hostPort()); assertEquals("/data/gcp", run.persistSource()); assertEquals("storage", run.env().get("FLOCI_GCP_SERVICES")); + assertEquals(List.of("label=disable"), run.securityOpts()); } /** Without --persist the image's /app/data volume is mounted; that is not a persist directory. */ @@ -250,6 +251,7 @@ class DockerClientProcessTest { .inspectRunSettings("floci-oci", 4599).orElseThrow(); assertNull(run.persistSource()); + assertEquals(List.of(), run.securityOpts()); } /** BL-041: with --verbose set, each docker command is echoed to stderr before it runs. */ diff --git a/src/test/java/io/floci/cli/unit/InstanceHintsTest.java b/src/test/java/io/floci/cli/unit/InstanceHintsTest.java index b1a3bb9..58c77f8 100644 --- a/src/test/java/io/floci/cli/unit/InstanceHintsTest.java +++ b/src/test/java/io/floci/cli/unit/InstanceHintsTest.java @@ -84,9 +84,11 @@ class InstanceHintsTest { @Override public void pull(String image, String policy, PrintStream stream) throws DockerException { throw new DockerException("no such image"); } }; Run start = run(new StartCommand(ProductProfile.AWS, docker), "--container", "other", "--port", "14566", - "--image", "my image:1", "--pull", "always"); + "--image", "my image:1", "--pull", "always", "--security-opt", "label=disable", + "--security-opt", "seccomp=/etc/my profile.json"); assertEquals(1, start.exit()); - assertTrue(start.err().contains("floci start --pull never --container other --port 14566 --image 'my image:1'"), start.err()); + assertTrue(start.err().contains("floci start --pull never --container other --port 14566 --image 'my image:1'" + + " --security-opt label=disable --security-opt 'seccomp=/etc/my profile.json'"), start.err()); } @Test void envLoadHintsPreserveTheProfileInAllFourTrees() throws Exception { diff --git a/src/test/java/io/floci/cli/unit/RestartCommandTest.java b/src/test/java/io/floci/cli/unit/RestartCommandTest.java index 28a4f21..211badb 100644 --- a/src/test/java/io/floci/cli/unit/RestartCommandTest.java +++ b/src/test/java/io/floci/cli/unit/RestartCommandTest.java @@ -219,7 +219,8 @@ class RestartCommandTest { @Test void withoutAProfileTheRunningContainersSettingsCarryOver() throws Exception { var running = new DockerClient.RunSettings("floci/floci-gcp:0.9", 14588, "/data/gcp", - Map.of("FLOCI_GCP_SERVICES", "storage", "FLOCI_GCP_DOCKER_RESOURCE_NAMESPACE", "team")); + Map.of("FLOCI_GCP_SERVICES", "storage", "FLOCI_GCP_DOCKER_RESOURCE_NAMESPACE", "team"), + List.of("label=disable")); List args = restartWith(ProductProfile.GCP, running).buildStartCommand().dockerRunArgs(SOCKET); @@ -227,6 +228,7 @@ class RestartCommandTest { assertTrue(args.contains("/data/gcp:/app/data"), args.toString()); assertTrue(args.contains("FLOCI_GCP_SERVICES=storage"), args.toString()); assertTrue(args.contains("FLOCI_GCP_DOCKER_RESOURCE_NAMESPACE=team"), args.toString()); + assertEquals("label=disable", args.get(args.indexOf("--security-opt") + 1), args.toString()); assertEquals("floci/floci-gcp:0.9", args.get(args.size() - 1)); } @@ -234,13 +236,15 @@ class RestartCommandTest { @Test void aProfileWinsOverTheRunningContainersSettings() throws Exception { writeProfile("pinned", "port: 15000\n"); - var running = new DockerClient.RunSettings("floci/floci:0.9", 14566, "/data/x", Map.of()); + var running = new DockerClient.RunSettings("floci/floci:0.9", 14566, "/data/x", Map.of(), + List.of("label=disable")); List args = restartWith(ProductProfile.AWS, running, "--profile", "pinned") .buildStartCommand().dockerRunArgs(SOCKET); assertTrue(args.contains("15000:4566"), args.toString()); assertFalse(args.contains("/data/x:/app/data"), args.toString()); + assertFalse(args.contains("--security-opt"), args.toString()); } /** What a restart did, in order: "stop" and "start" as it ran them. */ @@ -332,7 +336,7 @@ class RestartCommandTest { DockerClient present = new DockerClient() { @Override public Optional inspectRunSettings(String name, int containerPort) { - return Optional.of(new RunSettings("floci/floci:0.9", 14566, null, Map.of())); + return Optional.of(new RunSettings("floci/floci:0.9", 14566, null, Map.of(), List.of())); } @Override @@ -375,6 +379,30 @@ class RestartCommandTest { assertTrue(r.err().contains("floci restart"), r.err()); } + /** docker inspect reports a seccomp profile file inline, which docker run does not take back. */ + @Test + void anInlineSeccompProfileFailsTheRestartBeforeTheStop() { + DockerClient seccomp = new DockerClient() { + @Override + public Optional inspectRunSettings(String name, int containerPort) { + return Optional.of(new RunSettings("floci/floci:0.9", 14566, null, Map.of(), + List.of("label=disable", "seccomp={\"defaultAction\":\"SCMP_ACT_ALLOW\"}"))); + } + + @Override + public Optional inspectContainer(String name) { + return Optional.of(new ContainerInfo("abc", name, "floci/floci:0.9", "running", "")); + } + }; + List ran = new ArrayList<>(); + + Ran r = run(recordingRestart(seccomp, ran)); + + assertEquals(2, r.exit()); + assertEquals(List.of(), ran); + assertTrue(r.err().contains("floci stop --remove"), r.err()); + } + /** * A --persist value that cannot be used is a usage error (exit 2), and restart finds out before * it stops anything: here a relative path, which a remote daemon cannot resolve. @@ -386,7 +414,7 @@ class RestartCommandTest { @Override public Optional inspectRunSettings(String name, int containerPort) { // What the running container was started with: a relative data directory. - return Optional.of(new RunSettings("floci/floci:0.9", 14566, "data", Map.of())); + return Optional.of(new RunSettings("floci/floci:0.9", 14566, "data", Map.of(), List.of())); } @Override diff --git a/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java b/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java index 99506e3..1dc263c 100644 --- a/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java +++ b/src/test/java/io/floci/cli/unit/RestartRecoveryTest.java @@ -34,7 +34,8 @@ class RestartRecoveryTest { } @Override public Optional inspectRunSettings(String name, int port) { return Optional.of(new RunSettings("floci/floci-gcp:0.9", 14588, "/data/team's state", - Map.of("FLOCI_GCP_SERVICES", "storage,pubsub", "FLOCI_GCP_DOCKER_RESOURCE_NAMESPACE", "team"))); + Map.of("FLOCI_GCP_SERVICES", "storage,pubsub", "FLOCI_GCP_DOCKER_RESOURCE_NAMESPACE", "team"), + List.of("label=disable", "seccomp=/etc/my profile.json"))); } @Override public void stopContainer(String name, int timeout) throws DockerException { if (failStop) throw new DockerException("stop failed"); @@ -67,6 +68,7 @@ class RestartRecoveryTest { assertTrue(line.contains("--image floci/floci-gcp:0.9"), line); assertTrue(line.contains("--persist '/data/team'\\''s state'"), line); assertTrue(line.contains("--services storage,pubsub --namespace team"), line); + assertTrue(line.contains("--security-opt label=disable --security-opt 'seccomp=/etc/my profile.json'"), line); assertTrue(line.contains("--container floci-gcp-team"), line); assertFalse(line.contains("--endpoint"), line); if (format.equals("json")) assertEquals("", stdout.toString()); diff --git a/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java b/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java index fbdfe19..e1e8c36 100644 --- a/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java +++ b/src/test/java/io/floci/cli/unit/StartCommandArgsTest.java @@ -96,6 +96,18 @@ class StartCommandArgsTest { assertFalse(args.stream().anyMatch(a -> a.contains("STORAGE_MODE"))); } + @Test + void eachSecurityOptReachesDockerRunInEveryTree() { + assertEquals(List.of("-d", "--name", "floci", "-p", "4566:4566", "-v", "/sock:/sock", + "--security-opt", "label=disable", "--security-opt", "no-new-privileges", "floci/floci:latest"), + parse("start", "--security-opt", "label=disable", "--security-opt", "no-new-privileges") + .dockerRunArgs(SOCKET)); + for (String tree : new String[]{"gcp", "az", "oci"}) { + List args = parse(tree, "start", "--security-opt", "label=disable").dockerRunArgs(SOCKET); + assertEquals("label=disable", args.get(args.indexOf("--security-opt") + 1), tree); + } + } + @Test void explicitPersistFlagStillWinsOverTheProfile() throws Exception { writeProfile("probe", "persistDir: /from-profile\n");