Search in sources :

Example 1 with Request

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

the class RegistryClient method callRegistryEndpoint.

/**
 * Calls the registry endpoint with an override URL.
 *
 * @param url the endpoint URL to call, or {@code null} to use default from {@code
 *     registryEndpointProvider}
 * @param registryEndpointProvider the {@link RegistryEndpointProvider} to the endpoint
 */
@Nullable
private <T> T callRegistryEndpoint(@Nullable URL url, RegistryEndpointProvider<T> registryEndpointProvider) throws IOException, RegistryException {
    if (url == null) {
        url = registryEndpointProvider.getApiRoute(getApiRouteBase());
    }
    try (Connection connection = new Connection(url)) {
        Request request = Request.builder().setAuthorization(authorization).setUserAgent(getUserAgent()).setAccept(registryEndpointProvider.getAccept()).setBody(registryEndpointProvider.getContent()).build();
        Response response = connection.send(registryEndpointProvider.getHttpMethod(), request);
        return registryEndpointProvider.handleResponse(response);
    } catch (HttpResponseException ex) {
        // First, see if the endpoint provider handles an exception as an expected response.
        try {
            return registryEndpointProvider.handleHttpResponseException(ex);
        } catch (HttpResponseException httpResponseException) {
            if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_METHOD_NOT_ALLOWED) {
                // The name or reference was invalid.
                ErrorResponseTemplate errorResponse = JsonTemplateMapper.readJson(httpResponseException.getContent(), ErrorResponseTemplate.class);
                RegistryErrorExceptionBuilder registryErrorExceptionBuilder = new RegistryErrorExceptionBuilder(registryEndpointProvider.getActionDescription(), httpResponseException);
                for (ErrorEntryTemplate errorEntry : errorResponse.getErrors()) {
                    registryErrorExceptionBuilder.addReason(errorEntry);
                }
                throw registryErrorExceptionBuilder.build();
            } else if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
                throw new RegistryUnauthorizedException(registryEndpointProperties.getServerUrl(), registryEndpointProperties.getImageName(), httpResponseException);
            } else if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_TEMPORARY_REDIRECT) {
                return callRegistryEndpoint(new URL(httpResponseException.getHeaders().getLocation()), registryEndpointProvider);
            } else {
                // Unknown
                throw httpResponseException;
            }
        }
    } catch (NoHttpResponseException ex) {
        throw new RegistryNoResponseException(ex);
    } catch (SSLPeerUnverifiedException ex) {
        // Fall-back to HTTP
        GenericUrl httpUrl = new GenericUrl(url);
        httpUrl.setScheme("http");
        return callRegistryEndpoint(httpUrl.toURL(), registryEndpointProvider);
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Connection(com.google.cloud.tools.jib.http.Connection) Request(com.google.cloud.tools.jib.http.Request) NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) URL(java.net.URL) Response(com.google.cloud.tools.jib.http.Response) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Nullable(javax.annotation.Nullable)

Example 2 with Request

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

the class RegistryAuthenticator method authenticate.

/**
 * Sends the authentication request and retrieves the Bearer authorization token.
 *
 * @param scope the scope of permissions to authenticate for
 * @see <a
 *     href="https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate">https://docs.docker.com/registry/spec/auth/token/#how-to-authenticate</a>
 */
private Authorization authenticate(String scope) throws RegistryAuthenticationFailedException {
    try {
        URL authenticationUrl = getAuthenticationUrl(scope);
        try (Connection connection = new Connection(authenticationUrl)) {
            Request.Builder requestBuilder = Request.builder();
            if (authorization != null) {
                requestBuilder.setAuthorization(authorization);
            }
            Response response = connection.get(requestBuilder.build());
            String responseString = Blobs.writeToString(response.getBody());
            AuthenticationResponseTemplate responseJson = JsonTemplateMapper.readJson(responseString, AuthenticationResponseTemplate.class);
            if (responseJson.token == null) {
                throw new RegistryAuthenticationFailedException("Did not get token in authentication response from " + authenticationUrl);
            }
            return Authorizations.withBearerToken(responseJson.token);
        }
    } catch (IOException ex) {
        throw new RegistryAuthenticationFailedException(ex);
    }
}
Also used : Response(com.google.cloud.tools.jib.http.Response) Connection(com.google.cloud.tools.jib.http.Connection) Request(com.google.cloud.tools.jib.http.Request) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

Connection (com.google.cloud.tools.jib.http.Connection)2 Request (com.google.cloud.tools.jib.http.Request)2 Response (com.google.cloud.tools.jib.http.Response)2 URL (java.net.URL)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)1 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 NoHttpResponseException (org.apache.http.NoHttpResponseException)1