Search in sources :

Example 1 with InsecureRegistryException

use of com.google.cloud.tools.jib.api.InsecureRegistryException in project jib by GoogleContainerTools.

the class RegistryEndpointCaller method call.

/**
 * Calls the registry endpoint with a certain {@link URL}.
 *
 * @param url the endpoint URL to call
 * @return an object representing the response
 * @throws IOException for most I/O exceptions when making the request
 * @throws RegistryException for known exceptions when interacting with the registry
 */
private T call(URL url) throws IOException, RegistryException {
    String serverUrl = registryEndpointRequestProperties.getServerUrl();
    String imageName = registryEndpointRequestProperties.getImageName();
    Request.Builder requestBuilder = Request.builder().setUserAgent(userAgent).setHttpTimeout(JibSystemProperties.getHttpTimeout()).setAccept(registryEndpointProvider.getAccept()).setBody(registryEndpointProvider.getContent()).setAuthorization(authorization);
    try (Response response = httpClient.call(registryEndpointProvider.getHttpMethod(), url, requestBuilder.build())) {
        return registryEndpointProvider.handleResponse(response);
    } catch (ResponseException ex) {
        // First, see if the endpoint provider handles an exception as an expected response.
        try {
            return registryEndpointProvider.handleHttpResponseException(ex);
        } catch (ResponseException responseException) {
            if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_METHOD_NOT_ALLOWED) {
                // The name or reference was invalid.
                throw newRegistryErrorException(responseException);
            } else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
                throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
            } else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
                if (responseException.requestAuthorizationCleared()) {
                    throw new RegistryCredentialsNotSentException(serverUrl, imageName);
                } else {
                    // Credentials are either missing or wrong.
                    throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
                }
            } else {
                // Unknown
                throw responseException;
            }
        }
    } catch (IOException ex) {
        logError("I/O error for image [" + serverUrl + "/" + imageName + "]:");
        logError("    " + ex.getClass().getName());
        logError("    " + (ex.getMessage() == null ? "(null exception message)" : ex.getMessage()));
        logErrorIfBrokenPipe(ex);
        if (ex instanceof SSLException) {
            throw new InsecureRegistryException(url, ex);
        }
        throw ex;
    }
}
Also used : Response(com.google.cloud.tools.jib.http.Response) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) Request(com.google.cloud.tools.jib.http.Request) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 2 with InsecureRegistryException

use of com.google.cloud.tools.jib.api.InsecureRegistryException in project jib by GoogleContainerTools.

the class JibBuildRunner method runBuild.

/**
 * Runs the Jib build.
 *
 * @return the built {@link JibContainer}
 * @throws BuildStepsExecutionException if another exception is thrown during the build
 * @throws IOException if an I/O exception occurs
 * @throws CacheDirectoryCreationException if the cache directory could not be created
 */
public JibContainer runBuild() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException {
    try {
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(startupMessage));
        JibContainer jibContainer = jibContainerBuilder.containerize(containerizer);
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(successMessage));
        // when an image is built, write out the digest and id
        if (imageDigestOutputPath != null) {
            String imageDigest = jibContainer.getDigest().toString();
            Files.write(imageDigestOutputPath, imageDigest.getBytes(StandardCharsets.UTF_8));
        }
        if (imageIdOutputPath != null) {
            String imageId = jibContainer.getImageId().toString();
            Files.write(imageIdOutputPath, imageId.getBytes(StandardCharsets.UTF_8));
        }
        if (imageJsonOutputPath != null) {
            ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJibContainer(jibContainer);
            String imageJson = metadataOutput.toJson();
            Files.write(imageJsonOutputPath, imageJson.getBytes(StandardCharsets.UTF_8));
        }
        return jibContainer;
    } catch (HttpHostConnectException ex) {
        // Failed to connect to registry.
        throw new BuildStepsExecutionException(helpfulSuggestions.forHttpHostConnect(), ex);
    } catch (RegistryUnauthorizedException ex) {
        handleRegistryUnauthorizedException(ex, helpfulSuggestions);
    } catch (RegistryCredentialsNotSentException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forCredentialsNotSent(), ex);
    } catch (RegistryAuthenticationFailedException ex) {
        if (ex.getCause() instanceof ResponseException) {
            handleRegistryUnauthorizedException(new RegistryUnauthorizedException(ex.getServerUrl(), ex.getImageName(), (ResponseException) ex.getCause()), helpfulSuggestions);
        } else {
            // Unknown cause
            throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
        }
    } catch (UnknownHostException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forUnknownHost(), ex);
    } catch (InsecureRegistryException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forInsecureRegistry(), ex);
    } catch (RegistryException ex) {
        // keep null-away happy
        String message = Verify.verifyNotNull(ex.getMessage());
        throw new BuildStepsExecutionException(message, ex);
    } catch (ExecutionException ex) {
        String message = ex.getCause().getMessage();
        throw new BuildStepsExecutionException(message == null ? "(null exception message)" : message, ex.getCause());
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
    }
    throw new IllegalStateException("unreachable");
}
Also used : RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) UnknownHostException(java.net.UnknownHostException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) JibContainer(com.google.cloud.tools.jib.api.JibContainer) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) RegistryException(com.google.cloud.tools.jib.api.RegistryException) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) RegistryCredentialsNotSentException(com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with InsecureRegistryException

use of com.google.cloud.tools.jib.api.InsecureRegistryException in project jib by google.

the class JibBuildRunnerTest method testBuildImage_insecureRegistryException.

@Test
public void testBuildImage_insecureRegistryException() throws InterruptedException, IOException, CacheDirectoryCreationException, RegistryException, ExecutionException {
    InsecureRegistryException mockInsecureRegistryException = Mockito.mock(InsecureRegistryException.class);
    Mockito.doThrow(mockInsecureRegistryException).when(mockJibContainerBuilder).containerize(mockContainerizer);
    try {
        testJibBuildRunner.runBuild();
        Assert.fail();
    } catch (BuildStepsExecutionException ex) {
        Assert.assertEquals(TEST_HELPFUL_SUGGESTIONS.forInsecureRegistry(), ex.getMessage());
    }
}
Also used : InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) Test(org.junit.Test)

Example 4 with InsecureRegistryException

use of com.google.cloud.tools.jib.api.InsecureRegistryException in project jib by google.

the class RegistryEndpointCaller method call.

/**
 * Calls the registry endpoint with a certain {@link URL}.
 *
 * @param url the endpoint URL to call
 * @return an object representing the response
 * @throws IOException for most I/O exceptions when making the request
 * @throws RegistryException for known exceptions when interacting with the registry
 */
private T call(URL url) throws IOException, RegistryException {
    String serverUrl = registryEndpointRequestProperties.getServerUrl();
    String imageName = registryEndpointRequestProperties.getImageName();
    Request.Builder requestBuilder = Request.builder().setUserAgent(userAgent).setHttpTimeout(JibSystemProperties.getHttpTimeout()).setAccept(registryEndpointProvider.getAccept()).setBody(registryEndpointProvider.getContent()).setAuthorization(authorization);
    try (Response response = httpClient.call(registryEndpointProvider.getHttpMethod(), url, requestBuilder.build())) {
        return registryEndpointProvider.handleResponse(response);
    } catch (ResponseException ex) {
        // First, see if the endpoint provider handles an exception as an expected response.
        try {
            return registryEndpointProvider.handleHttpResponseException(ex);
        } catch (ResponseException responseException) {
            if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_METHOD_NOT_ALLOWED) {
                // The name or reference was invalid.
                throw newRegistryErrorException(responseException);
            } else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
                throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
            } else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
                if (responseException.requestAuthorizationCleared()) {
                    throw new RegistryCredentialsNotSentException(serverUrl, imageName);
                } else {
                    // Credentials are either missing or wrong.
                    throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
                }
            } else {
                // Unknown
                throw responseException;
            }
        }
    } catch (IOException ex) {
        logError("I/O error for image [" + serverUrl + "/" + imageName + "]:");
        logError("    " + ex.getClass().getName());
        logError("    " + (ex.getMessage() == null ? "(null exception message)" : ex.getMessage()));
        logErrorIfBrokenPipe(ex);
        if (ex instanceof SSLException) {
            throw new InsecureRegistryException(url, ex);
        }
        throw ex;
    }
}
Also used : Response(com.google.cloud.tools.jib.http.Response) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) Request(com.google.cloud.tools.jib.http.Request) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 5 with InsecureRegistryException

use of com.google.cloud.tools.jib.api.InsecureRegistryException in project jib by google.

the class JibBuildRunner method runBuild.

/**
 * Runs the Jib build.
 *
 * @return the built {@link JibContainer}
 * @throws BuildStepsExecutionException if another exception is thrown during the build
 * @throws IOException if an I/O exception occurs
 * @throws CacheDirectoryCreationException if the cache directory could not be created
 */
public JibContainer runBuild() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException {
    try {
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(startupMessage));
        JibContainer jibContainer = jibContainerBuilder.containerize(containerizer);
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(successMessage));
        // when an image is built, write out the digest and id
        if (imageDigestOutputPath != null) {
            String imageDigest = jibContainer.getDigest().toString();
            Files.write(imageDigestOutputPath, imageDigest.getBytes(StandardCharsets.UTF_8));
        }
        if (imageIdOutputPath != null) {
            String imageId = jibContainer.getImageId().toString();
            Files.write(imageIdOutputPath, imageId.getBytes(StandardCharsets.UTF_8));
        }
        if (imageJsonOutputPath != null) {
            ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJibContainer(jibContainer);
            String imageJson = metadataOutput.toJson();
            Files.write(imageJsonOutputPath, imageJson.getBytes(StandardCharsets.UTF_8));
        }
        return jibContainer;
    } catch (HttpHostConnectException ex) {
        // Failed to connect to registry.
        throw new BuildStepsExecutionException(helpfulSuggestions.forHttpHostConnect(), ex);
    } catch (RegistryUnauthorizedException ex) {
        handleRegistryUnauthorizedException(ex, helpfulSuggestions);
    } catch (RegistryCredentialsNotSentException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forCredentialsNotSent(), ex);
    } catch (RegistryAuthenticationFailedException ex) {
        if (ex.getCause() instanceof ResponseException) {
            handleRegistryUnauthorizedException(new RegistryUnauthorizedException(ex.getServerUrl(), ex.getImageName(), (ResponseException) ex.getCause()), helpfulSuggestions);
        } else {
            // Unknown cause
            throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
        }
    } catch (UnknownHostException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forUnknownHost(), ex);
    } catch (InsecureRegistryException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forInsecureRegistry(), ex);
    } catch (RegistryException ex) {
        // keep null-away happy
        String message = Verify.verifyNotNull(ex.getMessage());
        throw new BuildStepsExecutionException(message, ex);
    } catch (ExecutionException ex) {
        String message = ex.getCause().getMessage();
        throw new BuildStepsExecutionException(message == null ? "(null exception message)" : message, ex.getCause());
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
    }
    throw new IllegalStateException("unreachable");
}
Also used : RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) UnknownHostException(java.net.UnknownHostException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) JibContainer(com.google.cloud.tools.jib.api.JibContainer) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) RegistryException(com.google.cloud.tools.jib.api.RegistryException) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) RegistryCredentialsNotSentException(com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

InsecureRegistryException (com.google.cloud.tools.jib.api.InsecureRegistryException)6 RegistryUnauthorizedException (com.google.cloud.tools.jib.api.RegistryUnauthorizedException)4 ResponseException (com.google.cloud.tools.jib.http.ResponseException)4 JibContainer (com.google.cloud.tools.jib.api.JibContainer)2 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)2 RegistryException (com.google.cloud.tools.jib.api.RegistryException)2 Request (com.google.cloud.tools.jib.http.Request)2 Response (com.google.cloud.tools.jib.http.Response)2 RegistryCredentialsNotSentException (com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 ExecutionException (java.util.concurrent.ExecutionException)2 SSLException (javax.net.ssl.SSLException)2 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)2 Test (org.junit.Test)2