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