Search in sources :

Example 11 with Authorization

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;
    }
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Timer(com.google.cloud.tools.jib.Timer) IOException(java.io.IOException) NonexistentDockerCredentialHelperException(com.google.cloud.tools.jib.registry.credentials.NonexistentDockerCredentialHelperException) Nullable(javax.annotation.Nullable)

Example 12 with Authorization

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();
    }
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Timer(com.google.cloud.tools.jib.Timer) RegistryAuthenticator(com.google.cloud.tools.jib.registry.RegistryAuthenticator)

Example 13 with Authorization

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());
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Test(org.junit.Test)

Aggregations

Authorization (com.google.cloud.tools.jib.http.Authorization)13 Test (org.junit.Test)7 Timer (com.google.cloud.tools.jib.Timer)4 Nullable (javax.annotation.Nullable)3 Caches (com.google.cloud.tools.jib.cache.Caches)2 RegistryAuthenticator (com.google.cloud.tools.jib.registry.RegistryAuthenticator)2 IOException (java.io.IOException)2 Command (com.google.cloud.tools.jib.Command)1 BuildConfiguration (com.google.cloud.tools.jib.builder.BuildConfiguration)1 BuildImageSteps (com.google.cloud.tools.jib.builder.BuildImageSteps)1 SourceFilesConfiguration (com.google.cloud.tools.jib.builder.SourceFilesConfiguration)1 Cache (com.google.cloud.tools.jib.cache.Cache)1 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)1 Image (com.google.cloud.tools.jib.image.Image)1 ImageReference (com.google.cloud.tools.jib.image.ImageReference)1 NonexistentDockerCredentialHelperException (com.google.cloud.tools.jib.registry.credentials.NonexistentDockerCredentialHelperException)1 NonexistentServerUrlDockerCredentialHelperException (com.google.cloud.tools.jib.registry.credentials.NonexistentServerUrlDockerCredentialHelperException)1 RegistryCredentials (com.google.cloud.tools.jib.registry.credentials.RegistryCredentials)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1