diff --git a/src/main/java/io/floci/cli/commands/ResetCommand.java b/src/main/java/io/floci/cli/commands/ResetCommand.java index d6f6839..232f091 100644 --- a/src/main/java/io/floci/cli/commands/ResetCommand.java +++ b/src/main/java/io/floci/cli/commands/ResetCommand.java @@ -190,14 +190,18 @@ public class ResetCommand implements Callable { } /** - * Whether the daemon runs on another machine. Docker reached over TCP or SSH usually does, - * but {@code tcp://127.0.0.1:2375} is this machine's daemon on a socket of another kind, and - * its published ports are local like any other's. + * Whether the daemon runs on another machine. Docker reached over TCP usually does, but + * {@code tcp://127.0.0.1:2375} is this machine's daemon on a socket of another kind, and its + * published ports are local like any other's. SSH is always remote: a loopback address there + * ({@code ssh://user@127.0.0.1:2222}) is a forwarded port to a VM or another machine, not a + * daemon on this one. */ public static boolean isRemote(DockerClient.DockerHost daemon) { if (daemon.kind() != DockerClient.Kind.TCP) return false; try { - String host = URI.create(daemon.raw()).getHost(); + URI address = URI.create(daemon.raw()); + if ("ssh".equalsIgnoreCase(address.getScheme())) return true; + String host = address.getHost(); return host == null || !thisMachine(host); } catch (IllegalArgumentException | NullPointerException e) { return true; // cannot tell: the careful answer diff --git a/src/test/java/io/floci/cli/unit/ResetCommandTest.java b/src/test/java/io/floci/cli/unit/ResetCommandTest.java index 77bec5f..2a33485 100644 --- a/src/test/java/io/floci/cli/unit/ResetCommandTest.java +++ b/src/test/java/io/floci/cli/unit/ResetCommandTest.java @@ -279,6 +279,8 @@ class ResetCommandTest { "unix:///var/run/docker.sock, false", "tcp://remote-host:2375, true", "ssh://deploy@remote-host, true", + "ssh://user@127.0.0.1:2222, true", + "ssh://localhost, true", "tcp://192.168.1.20:2375, true"}) void onlyADaemonOnAnotherMachineIsRemote(String dockerHost, boolean remote) { assertEquals(remote, ResetCommand.isRemote(DockerClient.parseDockerHost(dockerHost, null, "Linux"))); @@ -298,6 +300,17 @@ class ResetCommandTest { assertTrue(ResetCommand.sameServer("http://localhost:14566", "http://localhost:14566", localTcp)); } + /** SSH to a loopback address is a forwarded port to somewhere else: never reset through localhost. */ + @Test + void anSshDaemonOnALoopbackAddressIsStillRemote() { + DockerHost forwarded = DockerClient.parseDockerHost("ssh://user@127.0.0.1:2222", null, "Linux"); + + ResetCommand.Target target = targetOf(dockerWith("floci", "running", "4566->4566/tcp", forwarded)); + + assertNull(target.endpoint()); + assertTrue(target.error().contains("runs on the remote Docker host ssh://user@127.0.0.1:2222"), target.error()); + } + /** A profile for an instance on a remote Docker host: its endpoint and its container are one server. */ @Test void aRemoteInstanceWhoseEndpointMatchesIsReset() throws Exception {