Search in sources :

Example 1 with Authorization

use of com.google.cloud.tools.jib.http.Authorization in project jib by google.

the class BuildImageMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    validateParameters();
    ProjectProperties projectProperties = new ProjectProperties(project, getLog());
    if (mainClass == null) {
        mainClass = projectProperties.getMainClassFromMavenJarPlugin();
        if (mainClass == null) {
            throwMojoExecutionExceptionWithHelpMessage(new MojoFailureException("Could not find main class specified in maven-jar-plugin"), "add a `mainClass` configuration to jib-maven-plugin");
        }
    }
    SourceFilesConfiguration sourceFilesConfiguration = projectProperties.getSourceFilesConfiguration();
    // Parses 'from' into image reference.
    ImageReference baseImage = getBaseImageReference();
    // Checks Maven settings for registry credentials.
    session.getSettings().getServer(baseImage.getRegistry());
    Map<String, Authorization> registryCredentials = new HashMap<>(2);
    // Retrieves credentials for the base image registry.
    Authorization baseImageRegistryCredentials = getRegistryCredentialsFromSettings(baseImage.getRegistry());
    if (baseImageRegistryCredentials != null) {
        registryCredentials.put(baseImage.getRegistry(), baseImageRegistryCredentials);
    }
    // Retrieves credentials for the target registry.
    Authorization targetRegistryCredentials = getRegistryCredentialsFromSettings(registry);
    if (targetRegistryCredentials != null) {
        registryCredentials.put(registry, targetRegistryCredentials);
    }
    RegistryCredentials mavenSettingsCredentials = RegistryCredentials.from("Maven settings", registryCredentials);
    ImageReference targetImageReference = ImageReference.of(registry, repository, tag);
    ImageFormat imageFormatToEnum = ImageFormat.valueOf(imageFormat);
    BuildConfiguration buildConfiguration = BuildConfiguration.builder(new MavenBuildLogger(getLog())).setBaseImage(baseImage).setTargetImage(targetImageReference).setCredentialHelperNames(credHelpers).setKnownRegistryCredentials(mavenSettingsCredentials).setMainClass(mainClass).setEnableReproducibleBuilds(enableReproducibleBuilds).setJvmFlags(jvmFlags).setEnvironment(environment).setTargetFormat(imageFormatToEnum.getManifestTemplateClass()).build();
    // Uses a directory in the Maven build cache as the Jib cache.
    Path cacheDirectory = Paths.get(project.getBuild().getDirectory(), CACHE_DIRECTORY_NAME);
    if (!Files.exists(cacheDirectory)) {
        try {
            Files.createDirectory(cacheDirectory);
        } catch (IOException ex) {
            throw new MojoExecutionException("Could not create cache directory: " + cacheDirectory, ex);
        }
    }
    Caches.Initializer cachesInitializer = Caches.newInitializer(cacheDirectory);
    if (useOnlyProjectCache) {
        cachesInitializer.setBaseCacheDirectory(cacheDirectory);
    }
    getLog().info("");
    getLog().info("Pushing image as " + targetImageReference);
    getLog().info("");
    // TODO: Instead of disabling logging, have authentication credentials be provided
    // Disables annoying Apache HTTP client logging.
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "error");
    RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX);
    buildImage(new BuildImageSteps(buildConfiguration, sourceFilesConfiguration, cachesInitializer));
    getLog().info("");
    getLog().info("Built and pushed image as " + targetImageReference);
    getLog().info("");
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) BuildImageSteps(com.google.cloud.tools.jib.builder.BuildImageSteps) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) Caches(com.google.cloud.tools.jib.cache.Caches) RegistryCredentials(com.google.cloud.tools.jib.registry.credentials.RegistryCredentials) Authorization(com.google.cloud.tools.jib.http.Authorization) BuildConfiguration(com.google.cloud.tools.jib.builder.BuildConfiguration) SourceFilesConfiguration(com.google.cloud.tools.jib.builder.SourceFilesConfiguration) ImageReference(com.google.cloud.tools.jib.image.ImageReference)

Example 2 with Authorization

use of com.google.cloud.tools.jib.http.Authorization in project jib by google.

the class DockerConfigCredentialRetrieverTest method testRetrieve_hasAuth.

@Test
public void testRetrieve_hasAuth() throws IOException {
    DockerConfigCredentialRetriever dockerConfigCredentialRetriever = new DockerConfigCredentialRetriever("some registry", dockerConfigFile, null);
    Authorization authorization = dockerConfigCredentialRetriever.retrieve();
    Assert.assertNotNull(authorization);
    Assert.assertEquals("some auth", authorization.getToken());
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Test(org.junit.Test)

Example 3 with Authorization

use of com.google.cloud.tools.jib.http.Authorization in project jib by google.

the class DockerConfigCredentialRetrieverTest method testRetrieve_useCredsStore_withProtocol.

@Test
public void testRetrieve_useCredsStore_withProtocol() throws IOException {
    Mockito.when(mockDockerCredentialHelperFactory.withCredentialHelperSuffix("some credential store")).thenReturn(mockDockerCredentialHelper);
    DockerConfigCredentialRetriever dockerConfigCredentialRetriever = new DockerConfigCredentialRetriever("with.protocol", dockerConfigFile, mockDockerCredentialHelperFactory);
    Authorization authorization = dockerConfigCredentialRetriever.retrieve();
    Assert.assertEquals(mockAuthorization, authorization);
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Test(org.junit.Test)

Example 4 with Authorization

use of com.google.cloud.tools.jib.http.Authorization in project jib by google.

the class DockerConfigCredentialRetrieverTest method testRetrieve_useCredHelper.

@Test
public void testRetrieve_useCredHelper() throws IOException {
    Mockito.when(mockDockerCredentialHelperFactory.withCredentialHelperSuffix("another credential helper")).thenReturn(mockDockerCredentialHelper);
    DockerConfigCredentialRetriever dockerConfigCredentialRetriever = new DockerConfigCredentialRetriever("another registry", dockerConfigFile, mockDockerCredentialHelperFactory);
    Authorization authorization = dockerConfigCredentialRetriever.retrieve();
    Assert.assertEquals(mockAuthorization, authorization);
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Test(org.junit.Test)

Example 5 with Authorization

use of com.google.cloud.tools.jib.http.Authorization in project jib by google.

the class DockerCredentialHelperIntegrationTest method testRetrieveGCR.

/**
 * Tests retrieval via {@code docker-credential-gcr} CLI.
 */
@Test
public void testRetrieveGCR() throws IOException, NonexistentServerUrlDockerCredentialHelperException, NonexistentDockerCredentialHelperException, URISyntaxException, InterruptedException {
    new Command("docker-credential-gcr", "store").run(Files.readAllBytes(Paths.get(Resources.getResource("credentials.json").toURI())));
    DockerCredentialHelper dockerCredentialHelper = new DockerCredentialHelperFactory("myregistry").withCredentialHelperSuffix("gcr");
    Authorization authorization = dockerCredentialHelper.retrieve();
    // Checks that token received was base64 encoding of "myusername:mysecret".
    Assert.assertEquals("bXl1c2VybmFtZTpteXNlY3JldA==", authorization.getToken());
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Command(com.google.cloud.tools.jib.Command) 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