use of com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException 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.api.RegistryAuthenticationFailedException in project jib by GoogleContainerTools.
the class RegistryAuthenticationFailedExceptionTest method testRegistryAuthenticationFailedException_message.
@Test
public void testRegistryAuthenticationFailedException_message() {
RegistryAuthenticationFailedException exception = new RegistryAuthenticationFailedException("serverUrl", "imageName", "message");
Assert.assertEquals("serverUrl", exception.getServerUrl());
Assert.assertEquals("imageName", exception.getImageName());
Assert.assertEquals("Failed to authenticate with registry serverUrl/imageName because: message", exception.getMessage());
}
use of com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException in project jib by GoogleContainerTools.
the class RegistryAuthenticatorTest method testUserAgent.
@Test
public void testUserAgent() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryCredentialsNotSentException {
try (TestWebServer server = new TestWebServer(false)) {
try {
RegistryAuthenticator authenticator = RegistryAuthenticator.fromAuthenticationMethod("Bearer realm=\"" + server.getEndpoint() + "\"", registryEndpointRequestProperties, "Competent-Agent", new FailoverHttpClient(true, false, ignored -> {
})).get();
authenticator.authenticatePush(null);
} catch (RegistryAuthenticationFailedException ex) {
// Doesn't matter if auth fails. We only examine what we sent.
}
MatcherAssert.assertThat(server.getInputRead(), CoreMatchers.containsString("User-Agent: Competent-Agent"));
}
}
use of com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException in project jib by GoogleContainerTools.
the class JibBuildRunner method runBuild.
/**
* Runs the Jib build.
*
* @return the built {@link JibContainer}
* @throws BuildStepsExecutionException if another exception is thrown during the build
* @throws IOException if an I/O exception occurs
* @throws CacheDirectoryCreationException if the cache directory could not be created
*/
public JibContainer runBuild() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException {
try {
logger.accept(LogEvent.lifecycle(""));
logger.accept(LogEvent.lifecycle(startupMessage));
JibContainer jibContainer = jibContainerBuilder.containerize(containerizer);
logger.accept(LogEvent.lifecycle(""));
logger.accept(LogEvent.lifecycle(successMessage));
// when an image is built, write out the digest and id
if (imageDigestOutputPath != null) {
String imageDigest = jibContainer.getDigest().toString();
Files.write(imageDigestOutputPath, imageDigest.getBytes(StandardCharsets.UTF_8));
}
if (imageIdOutputPath != null) {
String imageId = jibContainer.getImageId().toString();
Files.write(imageIdOutputPath, imageId.getBytes(StandardCharsets.UTF_8));
}
if (imageJsonOutputPath != null) {
ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJibContainer(jibContainer);
String imageJson = metadataOutput.toJson();
Files.write(imageJsonOutputPath, imageJson.getBytes(StandardCharsets.UTF_8));
}
return jibContainer;
} catch (HttpHostConnectException ex) {
// Failed to connect to registry.
throw new BuildStepsExecutionException(helpfulSuggestions.forHttpHostConnect(), ex);
} catch (RegistryUnauthorizedException ex) {
handleRegistryUnauthorizedException(ex, helpfulSuggestions);
} catch (RegistryCredentialsNotSentException ex) {
throw new BuildStepsExecutionException(helpfulSuggestions.forCredentialsNotSent(), ex);
} catch (RegistryAuthenticationFailedException ex) {
if (ex.getCause() instanceof ResponseException) {
handleRegistryUnauthorizedException(new RegistryUnauthorizedException(ex.getServerUrl(), ex.getImageName(), (ResponseException) ex.getCause()), helpfulSuggestions);
} else {
// Unknown cause
throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
}
} catch (UnknownHostException ex) {
throw new BuildStepsExecutionException(helpfulSuggestions.forUnknownHost(), ex);
} catch (InsecureRegistryException ex) {
throw new BuildStepsExecutionException(helpfulSuggestions.forInsecureRegistry(), ex);
} catch (RegistryException ex) {
// keep null-away happy
String message = Verify.verifyNotNull(ex.getMessage());
throw new BuildStepsExecutionException(message, ex);
} catch (ExecutionException ex) {
String message = ex.getCause().getMessage();
throw new BuildStepsExecutionException(message == null ? "(null exception message)" : message, ex.getCause());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
}
throw new IllegalStateException("unreachable");
}
use of com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException 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);
}
}
Aggregations