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);
}
}
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));
}
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);
}
}
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());
}
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));
}
Aggregations