use of com.google.cloud.tools.jib.http.Authorization in project jib by google.
the class RetrieveRegistryCredentialsStep method call.
@Override
@Nullable
public Authorization call() throws IOException, NonexistentDockerCredentialHelperException {
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, buildConfiguration.getTargetRegistry()))) {
// Tries to get registry credentials from Docker credential helpers.
for (String credentialHelperSuffix : buildConfiguration.getCredentialHelperNames()) {
Authorization authorization = retrieveFromCredentialHelper(credentialHelperSuffix);
if (authorization != null) {
return authorization;
}
}
// Tries to get registry credentials from known registry credentials.
String credentialSource = buildConfiguration.getKnownRegistryCredentials().getCredentialSource(registry);
if (credentialSource != null) {
logGotCredentialsFrom(credentialSource);
return buildConfiguration.getKnownRegistryCredentials().getAuthorization(registry);
}
// Tries to get registry credentials from the Docker config.
try {
Authorization dockerConfigAuthorization = dockerConfigCredentialRetriever.retrieve();
if (dockerConfigAuthorization != null) {
buildConfiguration.getBuildLogger().info("Using credentials from Docker config for " + registry);
return dockerConfigAuthorization;
}
} catch (IOException ex) {
buildConfiguration.getBuildLogger().info("Unable to parse Docker config");
}
// Tries to infer common credential helpers for known registries.
for (String registrySuffix : COMMON_CREDENTIAL_HELPERS.keySet()) {
if (registry.endsWith(registrySuffix)) {
try {
String commonCredentialHelper = COMMON_CREDENTIAL_HELPERS.get(registrySuffix);
if (commonCredentialHelper == null) {
throw new IllegalStateException("No COMMON_CREDENTIAL_HELPERS should be null");
}
Authorization authorization = retrieveFromCredentialHelper(commonCredentialHelper);
if (authorization != null) {
return authorization;
}
} catch (NonexistentDockerCredentialHelperException ex) {
if (ex.getMessage() != null) {
// Warns the user that the specified (or inferred) credential helper is not on the
// system.
buildConfiguration.getBuildLogger().warn(ex.getMessage());
}
}
}
}
/*
* If no credentials found, give an info (not warning because in most cases, the base image is
* public and does not need extra credentials) and return null.
*/
buildConfiguration.getBuildLogger().info("No credentials could be retrieved for registry " + registry);
return null;
}
}
use of com.google.cloud.tools.jib.http.Authorization in project jib by google.
the class AuthenticatePullStep method call.
/**
* Depends on {@link RetrieveRegistryCredentialsStep}.
*/
@Override
public Authorization call() throws RegistryAuthenticationFailedException, IOException, RegistryException, ExecutionException, InterruptedException {
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, buildConfiguration.getBaseImageRegistry()))) {
Authorization registryCredentials = NonBlockingFutures.get(registryCredentialsFuture);
RegistryAuthenticator registryAuthenticator = RegistryAuthenticators.forOther(buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
if (registryAuthenticator == null) {
return registryCredentials;
}
return registryAuthenticator.setAuthorization(registryCredentials).authenticatePull();
}
}
use of com.google.cloud.tools.jib.http.Authorization in project jib by google.
the class RegistryAuthenticatorIntegrationTest method testAuthenticate.
@Test
public void testAuthenticate() throws RegistryAuthenticationFailedException {
RegistryAuthenticator registryAuthenticator = RegistryAuthenticators.forDockerHub("library/busybox");
Authorization authorization = registryAuthenticator.authenticatePull();
// Checks that some token was received.
Assert.assertTrue(0 < authorization.getToken().length());
}
Aggregations