Search in sources :

Example 16 with ResponseException

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

the class ManifestPusherIntegrationTest method testPush_missingBlobs.

@Test
public void testPush_missingBlobs() throws IOException, RegistryException {
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "gcr.io", "distroless/java", httpClient).newRegistryClient();
    ManifestTemplate manifestTemplate = registryClient.pullManifest("latest").getManifest();
    registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "ignored", httpClient).newRegistryClient();
    try {
        registryClient.pushManifest(manifestTemplate, "latest");
        Assert.fail("Pushing manifest without its BLOBs should fail");
    } catch (RegistryErrorException ex) {
        ResponseException responseException = (ResponseException) ex.getCause();
        Assert.assertEquals(HttpStatusCodes.STATUS_CODE_BAD_REQUEST, responseException.getStatusCode());
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 17 with ResponseException

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

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 18 with ResponseException

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

the class ManifestPusherTest method testHandleHttpResponseException_otherError.

@Test
public void testHandleHttpResponseException_otherError() throws RegistryErrorException {
    ResponseException exception = Mockito.mock(ResponseException.class);
    Mockito.when(exception.getStatusCode()).thenReturn(HttpStatus.SC_UNAUTHORIZED);
    try {
        testManifestPusher.handleHttpResponseException(exception);
        Assert.fail();
    } catch (ResponseException ex) {
        Assert.assertSame(exception, ex);
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 19 with ResponseException

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

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)

Example 20 with ResponseException

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

the class RegistryEndpointCallerTest method testNewRegistryErrorException_noOutputFromRegistry.

@Test
public void testNewRegistryErrorException_noOutputFromRegistry() {
    ResponseException httpException = Mockito.mock(ResponseException.class);
    // Registry returning null error output
    Mockito.when(httpException.getContent()).thenReturn(null);
    Mockito.when(httpException.getStatusCode()).thenReturn(404);
    RegistryErrorException registryException = endpointCaller.newRegistryErrorException(httpException);
    Assert.assertSame(httpException, registryException.getCause());
    Assert.assertEquals("Tried to actionDescription but failed because: registry returned error code 404 " + "but did not return any details; possible causes include invalid or wrong reference, or proxy/firewall/VPN interfering \n", registryException.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