Search in sources :

Example 1 with BlobHttpContent

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

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

the class ManifestPusherTest method testGetContent.

@Test
public void testGetContent() throws IOException {
    BlobHttpContent body = testManifestPusher.getContent();
    Assert.assertNotNull(body);
    Assert.assertEquals(V22ManifestTemplate.MANIFEST_MEDIA_TYPE, body.getType());
    ByteArrayOutputStream bodyCaptureStream = new ByteArrayOutputStream();
    body.writeTo(bodyCaptureStream);
    String v22manifestJson = new String(Files.readAllBytes(v22manifestJsonFile), StandardCharsets.UTF_8);
    Assert.assertEquals(v22manifestJson, new String(bodyCaptureStream.toByteArray(), StandardCharsets.UTF_8));
}
Also used : BlobHttpContent(com.google.cloud.tools.jib.http.BlobHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with BlobHttpContent

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

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

the class BlobPusherTest method testWriter_getContent.

@Test
public void testWriter_getContent() throws IOException {
    LongAdder byteCount = new LongAdder();
    BlobHttpContent body = testBlobPusher.writer(mockUrl, byteCount::add).getContent();
    Assert.assertNotNull(body);
    Assert.assertEquals("application/octet-stream", body.getType());
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    body.writeTo(byteArrayOutputStream);
    Assert.assertEquals(TEST_BLOB_CONTENT, new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8));
    Assert.assertEquals(TEST_BLOB_CONTENT.length(), byteCount.sum());
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) BlobHttpContent(com.google.cloud.tools.jib.http.BlobHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with BlobHttpContent

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

the class ManifestPusherTest method testGetContent.

@Test
public void testGetContent() throws IOException {
    BlobHttpContent body = testManifestPusher.getContent();
    Assert.assertNotNull(body);
    Assert.assertEquals(V22ManifestTemplate.MANIFEST_MEDIA_TYPE, body.getType());
    ByteArrayOutputStream bodyCaptureStream = new ByteArrayOutputStream();
    body.writeTo(bodyCaptureStream);
    String v22manifestJson = new String(Files.readAllBytes(v22manifestJsonFile), StandardCharsets.UTF_8);
    Assert.assertEquals(v22manifestJson, new String(bodyCaptureStream.toByteArray(), StandardCharsets.UTF_8));
}
Also used : BlobHttpContent(com.google.cloud.tools.jib.http.BlobHttpContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

BlobHttpContent (com.google.cloud.tools.jib.http.BlobHttpContent)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Test (org.junit.Test)4 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)2 Request (com.google.cloud.tools.jib.http.Request)2 Response (com.google.cloud.tools.jib.http.Response)2 ResponseException (com.google.cloud.tools.jib.http.ResponseException)2 IOException (java.io.IOException)2 URL (java.net.URL)2 LongAdder (java.util.concurrent.atomic.LongAdder)2