From 67fa40bc2c484da0546333914ea07a89fe44eaaf Mon Sep 17 00:00:00 2001 From: Mridula <66699525+mpeddada1@users.noreply.github.com> Date: Thu, 25 Aug 2022 16:23:29 -0400 Subject: [PATCH] Check if file exists when executable path is passed in through jib dockerClient.executable (#3744) * Check if file exists when executable path is passed in through jib.dockerClient.executable --- .../tools/jib/docker/CliDockerClient.java | 22 +++++++++---------- .../tools/jib/docker/CliDockerClientTest.java | 9 ++++++++ .../src/test/resources/core/docker/emptyFile | 0 jib-gradle-plugin/README.md | 2 +- jib-maven-plugin/README.md | 2 +- 5 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 jib-core/src/test/resources/core/docker/emptyFile diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/CliDockerClient.java b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/CliDockerClient.java index c62ff819a1..7c9c23cd9f 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/docker/CliDockerClient.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/docker/CliDockerClient.java @@ -103,30 +103,28 @@ public List getDiffIds() throws DigestException { public static final Path DEFAULT_DOCKER_CLIENT = Paths.get("docker"); /** - * Checks if Docker is installed on the user's system and accessible by running the default {@code - * docker} command. + * Checks if Docker is installed on the user's system by running the `docker` command. * * @return {@code true} if Docker is installed on the user's system and accessible */ public static boolean isDefaultDockerInstalled() { - return isDockerInstalled(DEFAULT_DOCKER_CLIENT); + try { + new ProcessBuilder(DEFAULT_DOCKER_CLIENT.toString()).start(); + return true; + } catch (IOException ex) { + return false; + } } /** - * Checks if Docker is installed on the user's system and accessible by running the given {@code - * docker} executable. + * Checks if Docker is installed on the user's system and by verifying if the executable path + * provided has the appropriate permissions. * * @param dockerExecutable path to the executable to test running * @return {@code true} if Docker is installed on the user's system and accessible */ public static boolean isDockerInstalled(Path dockerExecutable) { - try { - new ProcessBuilder(dockerExecutable.toString()).start(); - return true; - - } catch (IOException ex) { - return false; - } + return Files.exists(dockerExecutable); } /** diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/CliDockerClientTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/CliDockerClientTest.java index 4d1f37f6ce..3e7cca5b4a 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/docker/CliDockerClientTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/docker/CliDockerClientTest.java @@ -24,11 +24,13 @@ import com.google.cloud.tools.jib.json.JsonTemplateMapper; import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteStreams; +import com.google.common.io.Resources; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.security.DigestException; @@ -74,6 +76,13 @@ public void testIsDockerInstalled_fail() { Assert.assertFalse(CliDockerClient.isDockerInstalled(Paths.get("path/to/nonexistent/file"))); } + @Test + public void testIsDockerInstalled_pass() throws URISyntaxException { + Assert.assertTrue( + CliDockerClient.isDockerInstalled( + Paths.get(Resources.getResource("core/docker/emptyFile").toURI()))); + } + @Test public void testLoad() throws IOException, InterruptedException { DockerClient testDockerClient = diff --git a/jib-core/src/test/resources/core/docker/emptyFile b/jib-core/src/test/resources/core/docker/emptyFile new file mode 100644 index 0000000000..e69de29bb2 diff --git a/jib-gradle-plugin/README.md b/jib-gradle-plugin/README.md index 452d5ea0d2..5a130b72d7 100644 --- a/jib-gradle-plugin/README.md +++ b/jib-gradle-plugin/README.md @@ -290,7 +290,7 @@ Property | Type | Default | Description Property | Type | Default | Description --- | --- | --- | --- -`executable` | `File` | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon. +`executable` | `File` | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon. **Please note**: Users are responsible for ensuring that the Docker path passed in is valid and has the right permissions to be executed. `environment` | `Map` | *None* | Sets environment variables used by the Docker executable. #### System Properties diff --git a/jib-maven-plugin/README.md b/jib-maven-plugin/README.md index 37dc8acaee..b62c1b325d 100644 --- a/jib-maven-plugin/README.md +++ b/jib-maven-plugin/README.md @@ -341,7 +341,7 @@ Property | Type | Default | Description Property | Type | Default | Description --- | --- | --- | --- -`executable` | string | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon. +`executable` | string | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon. **Please note**: Users are responsible for ensuring that the Docker path passed in is valid and has the right permissions to be executed. `environment` | map | *None* | Sets environment variables used by the Docker executable. #### System Properties