use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
the class RegistryEndpointCaller method call.
/**
* Calls the registry endpoint with a certain {@link URL}.
*
* @param url the endpoint URL to call
* @return an object representing the response
* @throws IOException for most I/O exceptions when making the request
* @throws RegistryException for known exceptions when interacting with the registry
*/
private T call(URL url) throws IOException, RegistryException {
String serverUrl = registryEndpointRequestProperties.getServerUrl();
String imageName = registryEndpointRequestProperties.getImageName();
Request.Builder requestBuilder = Request.builder().setUserAgent(userAgent).setHttpTimeout(JibSystemProperties.getHttpTimeout()).setAccept(registryEndpointProvider.getAccept()).setBody(registryEndpointProvider.getContent()).setAuthorization(authorization);
try (Response response = httpClient.call(registryEndpointProvider.getHttpMethod(), url, requestBuilder.build())) {
return registryEndpointProvider.handleResponse(response);
} catch (ResponseException ex) {
// First, see if the endpoint provider handles an exception as an expected response.
try {
return registryEndpointProvider.handleHttpResponseException(ex);
} catch (ResponseException responseException) {
if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_METHOD_NOT_ALLOWED) {
// The name or reference was invalid.
throw newRegistryErrorException(responseException);
} else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
} else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
if (responseException.requestAuthorizationCleared()) {
throw new RegistryCredentialsNotSentException(serverUrl, imageName);
} else {
// Credentials are either missing or wrong.
throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
}
} else {
// Unknown
throw responseException;
}
}
} catch (IOException ex) {
logError("I/O error for image [" + serverUrl + "/" + imageName + "]:");
logError(" " + ex.getClass().getName());
logError(" " + (ex.getMessage() == null ? "(null exception message)" : ex.getMessage()));
logErrorIfBrokenPipe(ex);
if (ex instanceof SSLException) {
throw new InsecureRegistryException(url, ex);
}
throw ex;
}
}
use of com.google.cloud.tools.jib.http.ResponseException 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.ResponseException in project jib by GoogleContainerTools.
the class RegistryEndpointCallerTest method testCall_unknown.
@Test
public void testCall_unknown() throws IOException, RegistryException {
ResponseException responseException = mockResponseException(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
setUpRegistryResponse(responseException);
try {
endpointCaller.call();
Assert.fail("Call should have failed");
} catch (ResponseException ex) {
Assert.assertSame(responseException, ex);
}
}
use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
the class RegistryEndpointCallerTest method testNewRegistryErrorException_jsonErrorOutput.
@Test
public void testNewRegistryErrorException_jsonErrorOutput() {
ResponseException httpException = Mockito.mock(ResponseException.class);
Mockito.when(httpException.getContent()).thenReturn("{\"errors\": [{\"code\": \"MANIFEST_UNKNOWN\", \"message\": \"manifest unknown\"}]}");
RegistryErrorException registryException = endpointCaller.newRegistryErrorException(httpException);
Assert.assertSame(httpException, registryException.getCause());
Assert.assertEquals("Tried to actionDescription but failed because: manifest unknown", registryException.getMessage());
}
use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
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());
}
}
Aggregations