Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on invalid jib.httpTimeout values at plugin front-ends #667

Merged
merged 23 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2f9bafd
Enable jib.httpTimeout system property
chanseokoh Jul 18, 2018
ba9b44f
Merge branch 'master' into i582-http-timeout
chanseokoh Jul 18, 2018
4654fac
Format code
chanseokoh Jul 18, 2018
0af2594
Set timeout only when property is defined
chanseokoh Jul 18, 2018
b561e61
Warn invalid jib.httpTimeout values
chanseokoh Jul 18, 2018
d4251d0
Warn invalid jib.httpTimeout values - gradle side
chanseokoh Jul 18, 2018
6856f29
Format code
chanseokoh Jul 18, 2018
93c7cb8
Add Javadocs
chanseokoh Jul 18, 2018
b780031
Merge remote-tracking branch 'origin/master' into i582-http-timeout
chanseokoh Jul 19, 2018
5643bf3
Opt for simple code
chanseokoh Jul 19, 2018
d7f1db7
Merge branch 'master' into i582-http-timeout-frontends
chanseokoh Jul 19, 2018
f7e1741
Merge branch 'i582-http-timeout' into i582-http-timeout-frontends
chanseokoh Jul 19, 2018
95eb9bd
Error instead of warn
chanseokoh Jul 19, 2018
28a96ea
Error on invalid timeouts - gradle plugin
chanseokoh Jul 19, 2018
609ac5e
Fix Javadoc
chanseokoh Jul 19, 2018
8ffcb97
Merge remote-tracking branch 'origin/master' into i582-http-timeout
chanseokoh Jul 19, 2018
4c98316
Update CHANGELOG files
chanseokoh Jul 19, 2018
66c029c
Merge branch 'i582-http-timeout' into i582-http-timeout-frontends
chanseokoh Jul 19, 2018
ffecc88
Delete .gitignore files
chanseokoh Jul 19, 2018
539d3cb
Refactor code and throw Maven/Gradle exception
chanseokoh Jul 19, 2018
502e523
Merge remote-tracking branch 'origin/master' into i582-http-timeout-f…
chanseokoh Jul 19, 2018
e452c03
Change error wordings / add private constructor
chanseokoh Jul 19, 2018
a4098b0
Merge branch 'master' into i582-http-timeout-frontends
chanseokoh Jul 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <T> the exception type to throw if invalid values
* @throws T if invalid values
*/
public static <T extends Throwable> void checkHttpTimeoutProperty(
Function<String, T> 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() {}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down