Search in sources :

Example 1 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException 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 ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.

the class RegistryAuthenticator method authenticate.

private Authorization authenticate(@Nullable Credential credential, Map<String, String> repositoryScopes) throws RegistryAuthenticationFailedException, RegistryCredentialsNotSentException {
    String registryUrl = registryEndpointRequestProperties.getServerUrl();
    String imageName = registryEndpointRequestProperties.getImageName();
    try {
        URL url = getAuthenticationUrl(credential, repositoryScopes);
        Request.Builder requestBuilder = Request.builder().setHttpTimeout(JibSystemProperties.getHttpTimeout()).setUserAgent(userAgent);
        if (isOAuth2Auth(credential)) {
            String parameters = getAuthRequestParameters(credential, repositoryScopes);
            requestBuilder.setBody(new BlobHttpContent(Blobs.from(parameters), MediaType.FORM_DATA.toString()));
        } else if (credential != null) {
            requestBuilder.setAuthorization(Authorization.fromBasicCredentials(credential.getUsername(), credential.getPassword()));
        }
        String httpMethod = isOAuth2Auth(credential) ? HttpMethods.POST : HttpMethods.GET;
        try (Response response = httpClient.call(httpMethod, url, requestBuilder.build())) {
            AuthenticationResponseTemplate responseJson = JsonTemplateMapper.readJson(response.getBody(), AuthenticationResponseTemplate.class);
            if (responseJson.getToken() == null) {
                throw new RegistryAuthenticationFailedException(registryUrl, imageName, "Did not get token in authentication response from " + getAuthenticationUrl(credential, repositoryScopes) + "; parameters: " + getAuthRequestParameters(credential, repositoryScopes));
            }
            return Authorization.fromBearerToken(responseJson.getToken());
        }
    } catch (ResponseException ex) {
        if (ex.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED && ex.requestAuthorizationCleared()) {
            throw new RegistryCredentialsNotSentException(registryUrl, imageName);
        }
        throw new RegistryAuthenticationFailedException(registryUrl, imageName, ex);
    } catch (IOException ex) {
        throw new RegistryAuthenticationFailedException(registryUrl, imageName, ex);
    }
}
Also used : Response(com.google.cloud.tools.jib.http.Response) RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) BlobHttpContent(com.google.cloud.tools.jib.http.BlobHttpContent) Request(com.google.cloud.tools.jib.http.Request) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.

the class RegistryEndpointCallerTest method testCall_unknown.

@Test
public void testCall_unknown() throws IOException, RegistryException {
    ResponseException responseException = mockResponseException(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
    setUpRegistryResponse(responseException);
    try {
        endpointCaller.call();
        Assert.fail("Call should have failed");
    } catch (ResponseException ex) {
        Assert.assertSame(responseException, ex);
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 4 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.

the class RegistryEndpointCallerTest method testNewRegistryErrorException_jsonErrorOutput.

@Test
public void testNewRegistryErrorException_jsonErrorOutput() {
    ResponseException httpException = Mockito.mock(ResponseException.class);
    Mockito.when(httpException.getContent()).thenReturn("{\"errors\": [{\"code\": \"MANIFEST_UNKNOWN\", \"message\": \"manifest unknown\"}]}");
    RegistryErrorException registryException = endpointCaller.newRegistryErrorException(httpException);
    Assert.assertSame(httpException, registryException.getCause());
    Assert.assertEquals("Tried to actionDescription but failed because: manifest unknown", registryException.getMessage());
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 5 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.

the class RegistryEndpointCallerTest method testCall_credentialsNotSentOverHttp.

@Test
public void testCall_credentialsNotSentOverHttp() throws IOException, RegistryException {
    ResponseException unauthorizedException = mockResponseException(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
    Mockito.when(unauthorizedException.requestAuthorizationCleared()).thenReturn(true);
    setUpRegistryResponse(unauthorizedException);
    try {
        endpointCaller.call();
        Assert.fail("Call should have failed");
    } catch (RegistryCredentialsNotSentException ex) {
        Assert.assertEquals("Required credentials for serverUrl/imageName were not sent because the connection was over HTTP", ex.getMessage());
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Aggregations

ResponseException (com.google.cloud.tools.jib.http.ResponseException)44 Test (org.junit.Test)32 NoHttpResponseException (org.apache.http.NoHttpResponseException)18 RegistryUnauthorizedException (com.google.cloud.tools.jib.api.RegistryUnauthorizedException)8 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)6 InsecureRegistryException (com.google.cloud.tools.jib.api.InsecureRegistryException)4 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)4 Request (com.google.cloud.tools.jib.http.Request)4 Response (com.google.cloud.tools.jib.http.Response)4 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)4 IOException (java.io.IOException)4 JibContainer (com.google.cloud.tools.jib.api.JibContainer)2 RegistryException (com.google.cloud.tools.jib.api.RegistryException)2 BlobHttpContent (com.google.cloud.tools.jib.http.BlobHttpContent)2 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 RegistryCredentialsNotSentException (com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 ExecutionException (java.util.concurrent.ExecutionException)2