diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/SystemPropertyValidator.java b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/SystemPropertyValidator.java new file mode 100644 index 0000000000..e83cb628cd --- /dev/null +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/frontend/SystemPropertyValidator.java @@ -0,0 +1,45 @@ +/* + * Copyright 2018 Google LLC. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.tools.jib.frontend; + +import java.util.function.Function; + +/** Validator for system properties. */ +public class SystemPropertyValidator { + + /** + * Checks the {@code jib.httpTimeout} system property for invalid (non-integer or negative) + * values. + * + * @param exceptionFactory factory to create an exception with the given description + * @param the exception type to throw if invalid values + * @throws T if invalid values + */ + public static void checkHttpTimeoutProperty( + Function exceptionFactory) throws T { + String value = System.getProperty("jib.httpTimeout"); + try { + if (value != null && Integer.parseInt(value) < 0) { + throw exceptionFactory.apply("jib.httpTimeout cannot be negative: " + value); + } + } catch (NumberFormatException ex) { + throw exceptionFactory.apply("jib.httpTimeout must be an integer: " + value); + } + } + + private SystemPropertyValidator() {} +} diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/SystemPropertyValidatorTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/SystemPropertyValidatorTest.java new file mode 100644 index 0000000000..ef7feb0c30 --- /dev/null +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/frontend/SystemPropertyValidatorTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2018 Google LLC. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.tools.jib.frontend; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + +/** Tests for {@link SystemPropertyValidator}. */ +public class SystemPropertyValidatorTest { + + @After + public void tearDown() { + System.clearProperty("jib.httpTimeout"); + } + + @Test + public void testCheckHttpTimeoutSystemProperty_ok() throws Exception { + Assert.assertNull(System.getProperty("jib.httpTimeout")); + SystemPropertyValidator.checkHttpTimeoutProperty(Exception::new); + } + + @Test + public void testCheckHttpTimeoutSystemProperty_stringValue() { + System.setProperty("jib.httpTimeout", "random string"); + try { + SystemPropertyValidator.checkHttpTimeoutProperty(Exception::new); + Assert.fail("Should error with a non-integer timeout"); + } catch (Exception ex) { + Assert.assertEquals("jib.httpTimeout must be an integer: random string", ex.getMessage()); + } + } + + @Test + public void testCheckHttpTimeoutSystemProperty_negativeValue() { + System.setProperty("jib.httpTimeout", "-80"); + try { + SystemPropertyValidator.checkHttpTimeoutProperty(Exception::new); + Assert.fail("Should error with a negative timeout"); + } catch (Exception ex) { + Assert.assertEquals("jib.httpTimeout cannot be negative: -80", ex.getMessage()); + } + } +} diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java index 7415aed902..0b6241d29a 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java @@ -25,6 +25,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.image.InvalidImageReferenceException; @@ -87,6 +88,7 @@ public void buildDocker() throws InvalidImageReferenceException, IOException { Preconditions.checkNotNull(jibExtension); GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger()); jibExtension.handleDeprecatedParameters(gradleBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(GradleException::new); if (Boolean.getBoolean("sendCredentialsOverHttp")) { gradleBuildLogger.warn( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java index 497f2da9de..4e77100196 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java @@ -24,6 +24,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.image.InvalidImageReferenceException; @@ -83,6 +84,7 @@ public void buildImage() throws InvalidImageReferenceException, IOException { Preconditions.checkNotNull(jibExtension); GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger()); jibExtension.handleDeprecatedParameters(gradleBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(GradleException::new); if (Strings.isNullOrEmpty(jibExtension.getTargetImage())) { throw new GradleException( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java index 35fab736eb..64086da31a 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildTarTask.java @@ -24,6 +24,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.http.Authorization; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.image.InvalidImageReferenceException; @@ -115,6 +116,7 @@ public void buildTar() throws InvalidImageReferenceException, IOException { Preconditions.checkNotNull(jibExtension); GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger()); jibExtension.handleDeprecatedParameters(gradleBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(GradleException::new); if (Boolean.getBoolean("sendCredentialsOverHttp")) { gradleBuildLogger.warn( diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java index 97b14522b8..2e10cb4521 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/DockerContextTask.java @@ -18,6 +18,7 @@ import com.google.cloud.tools.jib.docker.DockerContextGenerator; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.common.base.Preconditions; import com.google.common.io.InsecureRecursiveDeleteException; import java.io.IOException; @@ -100,6 +101,7 @@ public void generateDockerContext() { GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger()); jibExtension.handleDeprecatedParameters(gradleBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(GradleException::new); GradleProjectProperties gradleProjectProperties = GradleProjectProperties.getForProject(getProject(), gradleBuildLogger); diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java index e41d7dca77..f34f8c9528 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildDockerMojo.java @@ -25,6 +25,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; @@ -58,6 +59,7 @@ public class BuildDockerMojo extends JibPluginConfiguration { public void execute() throws MojoExecutionException { MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog()); handleDeprecatedParameters(mavenBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(MojoExecutionException::new); if (!new DockerClient().isDockerInstalled()) { throw new MojoExecutionException(HELPFUL_SUGGESTIONS.forDockerNotInstalled()); diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java index 5c028bc191..62a840f291 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java @@ -24,6 +24,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageFormat; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.RegistryClient; @@ -61,6 +62,7 @@ public class BuildImageMojo extends JibPluginConfiguration { public void execute() throws MojoExecutionException, MojoFailureException { MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog()); handleDeprecatedParameters(mavenBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(MojoExecutionException::new); // Validates 'format'. if (Arrays.stream(ImageFormat.values()).noneMatch(value -> value.name().equals(getFormat()))) { diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java index ebe232ce01..4787261e18 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildTarMojo.java @@ -24,6 +24,7 @@ import com.google.cloud.tools.jib.frontend.BuildStepsRunner; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; import com.google.cloud.tools.jib.frontend.HelpfulSuggestions; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.cloud.tools.jib.image.ImageReference; import com.google.cloud.tools.jib.registry.RegistryClient; import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials; @@ -60,6 +61,7 @@ public class BuildTarMojo extends JibPluginConfiguration { public void execute() throws MojoExecutionException { MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog()); handleDeprecatedParameters(mavenBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(MojoExecutionException::new); // Parses 'from' and 'to' into image reference. MavenProjectProperties mavenProjectProperties = diff --git a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java index 5735839029..9911661fee 100644 --- a/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java +++ b/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/DockerContextMojo.java @@ -18,6 +18,7 @@ import com.google.cloud.tools.jib.docker.DockerContextGenerator; import com.google.cloud.tools.jib.frontend.ExposedPortsParser; +import com.google.cloud.tools.jib.frontend.SystemPropertyValidator; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.io.InsecureRecursiveDeleteException; @@ -49,6 +50,7 @@ public class DockerContextMojo extends JibPluginConfiguration { public void execute() throws MojoExecutionException, MojoFailureException { MavenBuildLogger mavenBuildLogger = new MavenBuildLogger(getLog()); handleDeprecatedParameters(mavenBuildLogger); + SystemPropertyValidator.checkHttpTimeoutProperty(MojoExecutionException::new); Preconditions.checkNotNull(targetDir);