Search in sources :

Example 6 with CredentialRetriever

use of com.google.cloud.tools.jib.api.CredentialRetriever in project jib by google.

the class BuildContextTest method testBuilder.

@Test
public void testBuilder() throws Exception {
    String expectedBaseImageServerUrl = "someserver";
    String expectedBaseImageName = "baseimage";
    String expectedBaseImageTag = "baseimagetag";
    String expectedTargetServerUrl = "someotherserver";
    String expectedTargetImageName = "targetimage";
    String expectedTargetTag = "targettag";
    Set<String> additionalTargetImageTags = ImmutableSet.of("tag1", "tag2", "tag3");
    Set<String> expectedTargetImageTags = ImmutableSet.of("targettag", "tag1", "tag2", "tag3");
    List<CredentialRetriever> credentialRetrievers = Collections.singletonList(() -> Optional.of(Credential.from("username", "password")));
    Instant expectedCreationTime = Instant.ofEpochSecond(10000);
    List<String> expectedEntrypoint = Arrays.asList("some", "entrypoint");
    List<String> expectedProgramArguments = Arrays.asList("arg1", "arg2");
    Map<String, String> expectedEnvironment = ImmutableMap.of("key", "value");
    Set<Port> expectedExposedPorts = ImmutableSet.of(Port.tcp(1000), Port.tcp(2000));
    Map<String, String> expectedLabels = ImmutableMap.of("key1", "value1", "key2", "value2");
    Class<? extends BuildableManifestTemplate> expectedTargetFormat = OciManifestTemplate.class;
    Path expectedApplicationLayersCacheDirectory = Paths.get("application/layers");
    Path expectedBaseImageLayersCacheDirectory = Paths.get("base/image/layers");
    List<FileEntriesLayer> expectedLayerConfigurations = Collections.singletonList(FileEntriesLayer.builder().addEntry(Paths.get("sourceFile"), AbsoluteUnixPath.get("/path/in/container")).build());
    String expectedCreatedBy = "createdBy";
    ListMultimap<String, String> expectedRegistryMirrors = ImmutableListMultimap.of("some.registry", "mirror1", "some.registry", "mirror2");
    ImageConfiguration baseImageConfiguration = ImageConfiguration.builder(ImageReference.of(expectedBaseImageServerUrl, expectedBaseImageName, expectedBaseImageTag)).build();
    ImageConfiguration targetImageConfiguration = ImageConfiguration.builder(ImageReference.of(expectedTargetServerUrl, expectedTargetImageName, expectedTargetTag)).setCredentialRetrievers(credentialRetrievers).build();
    ContainerConfiguration containerConfiguration = ContainerConfiguration.builder().setCreationTime(expectedCreationTime).setEntrypoint(expectedEntrypoint).setProgramArguments(expectedProgramArguments).setEnvironment(expectedEnvironment).setExposedPorts(expectedExposedPorts).setLabels(expectedLabels).build();
    BuildContext.Builder buildContextBuilder = BuildContext.builder().setBaseImageConfiguration(baseImageConfiguration).setTargetImageConfiguration(targetImageConfiguration).setAdditionalTargetImageTags(additionalTargetImageTags).setContainerConfiguration(containerConfiguration).setApplicationLayersCacheDirectory(expectedApplicationLayersCacheDirectory).setBaseImageLayersCacheDirectory(expectedBaseImageLayersCacheDirectory).setTargetFormat(ImageFormat.OCI).setEnablePlatformTags(true).setAllowInsecureRegistries(true).setLayerConfigurations(expectedLayerConfigurations).setToolName(expectedCreatedBy).setRegistryMirrors(expectedRegistryMirrors);
    BuildContext buildContext = buildContextBuilder.build();
    Assert.assertEquals(expectedCreationTime, buildContext.getContainerConfiguration().getCreationTime());
    Assert.assertEquals(expectedBaseImageServerUrl, buildContext.getBaseImageConfiguration().getImageRegistry());
    Assert.assertEquals(expectedBaseImageName, buildContext.getBaseImageConfiguration().getImageRepository());
    Assert.assertEquals(expectedBaseImageTag, buildContext.getBaseImageConfiguration().getImageQualifier());
    Assert.assertEquals(expectedTargetServerUrl, buildContext.getTargetImageConfiguration().getImageRegistry());
    Assert.assertEquals(expectedTargetImageName, buildContext.getTargetImageConfiguration().getImageRepository());
    Assert.assertEquals(expectedTargetTag, buildContext.getTargetImageConfiguration().getImageQualifier());
    Assert.assertEquals(expectedTargetImageTags, buildContext.getAllTargetImageTags());
    Assert.assertEquals(Credential.from("username", "password"), buildContext.getTargetImageConfiguration().getCredentialRetrievers().get(0).retrieve().orElseThrow(AssertionError::new));
    Assert.assertEquals(expectedProgramArguments, buildContext.getContainerConfiguration().getProgramArguments());
    Assert.assertEquals(expectedEnvironment, buildContext.getContainerConfiguration().getEnvironmentMap());
    Assert.assertEquals(expectedExposedPorts, buildContext.getContainerConfiguration().getExposedPorts());
    Assert.assertEquals(expectedLabels, buildContext.getContainerConfiguration().getLabels());
    Assert.assertEquals(expectedTargetFormat, buildContext.getTargetFormat());
    Assert.assertEquals(expectedApplicationLayersCacheDirectory, buildContextBuilder.getApplicationLayersCacheDirectory());
    Assert.assertEquals(expectedBaseImageLayersCacheDirectory, buildContextBuilder.getBaseImageLayersCacheDirectory());
    Assert.assertEquals(expectedLayerConfigurations, buildContext.getLayerConfigurations());
    Assert.assertEquals(expectedEntrypoint, buildContext.getContainerConfiguration().getEntrypoint());
    Assert.assertEquals(expectedCreatedBy, buildContext.getToolName());
    Assert.assertEquals(expectedRegistryMirrors, buildContext.getRegistryMirrors());
    Assert.assertNotNull(buildContext.getExecutorService());
    Assert.assertTrue(buildContext.getEnablePlatformTags());
}
Also used : CredentialRetriever(com.google.cloud.tools.jib.api.CredentialRetriever) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) Port(com.google.cloud.tools.jib.api.buildplan.Port) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) Test(org.junit.Test)

Example 7 with CredentialRetriever

use of com.google.cloud.tools.jib.api.CredentialRetriever in project jib by google.

the class RegistryCredentialRetriever method retrieve.

private static Optional<Credential> retrieve(ImageConfiguration imageConfiguration, EventHandlers eventHandlers) throws CredentialRetrievalException {
    for (CredentialRetriever retriever : imageConfiguration.getCredentialRetrievers()) {
        Optional<Credential> credential = retriever.retrieve();
        if (credential.isPresent()) {
            return credential;
        }
    }
    String registry = imageConfiguration.getImageRegistry();
    String repository = imageConfiguration.getImageRepository();
    eventHandlers.dispatch(LogEvent.info("No credentials could be retrieved for " + registry + "/" + repository));
    return Optional.empty();
}
Also used : CredentialRetriever(com.google.cloud.tools.jib.api.CredentialRetriever) Credential(com.google.cloud.tools.jib.api.Credential)

Example 8 with CredentialRetriever

use of com.google.cloud.tools.jib.api.CredentialRetriever in project jib by google.

the class DefaultCredentialRetrieversTest method testCredentialHelper_cmdExtension.

@Test
public void testCredentialHelper_cmdExtension() throws IOException {
    Path credHelper = temporaryFolder.newFile("foo.cmd").toPath();
    Path pathWithoutCmd = credHelper.getParent().resolve("foo");
    assertThat(credHelper).isEqualTo(pathWithoutCmd.getParent().resolve("foo.cmd"));
    DefaultCredentialRetrievers credentialRetrievers = new DefaultCredentialRetrievers(mockCredentialRetrieverFactory, properties, environment).setCredentialHelper(pathWithoutCmd.toString());
    Exception ex = assertThrows(FileNotFoundException.class, credentialRetrievers::asList);
    assertThat(ex).hasMessageThat().startsWith("Specified credential helper was not found:");
    assertThat(ex).hasMessageThat().endsWith("foo");
    properties.setProperty("os.name", "winDOWs");
    List<CredentialRetriever> retrievers = new DefaultCredentialRetrievers(mockCredentialRetrieverFactory, properties, environment).setCredentialHelper(pathWithoutCmd.toString()).asList();
    assertThat(retrievers).containsExactly(mockDockerCredentialHelperCredentialRetriever, mockXdgPrimaryCredentialRetriever, mockEnvHomeXdgCredentialRetriever, mockSystemHomeXdgCredentialRetriever, mockDockerConfigEnvDockerConfigCredentialRetriever, mockDockerConfigEnvKubernetesDockerConfigCredentialRetriever, mockDockerConfigEnvLegacyDockerConfigCredentialRetriever, mockSystemHomeDockerConfigCredentialRetriever, mockSystemHomeKubernetesDockerConfigCredentialRetriever, mockSystemHomeLegacyDockerConfigCredentialRetriever, mockEnvHomeDockerConfigCredentialRetriever, mockEnvHomeKubernetesDockerConfigCredentialRetriever, mockEnvHomeLegacyDockerConfigCredentialRetriever, mockWellKnownCredentialHelpersCredentialRetriever, mockApplicationDefaultCredentialRetriever).inOrder();
}
Also used : Path(java.nio.file.Path) CredentialRetriever(com.google.cloud.tools.jib.api.CredentialRetriever) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 9 with CredentialRetriever

use of com.google.cloud.tools.jib.api.CredentialRetriever in project jib by GoogleContainerTools.

the class DefaultCredentialRetrieversTest method testCredentialHelper_cmdExtension.

@Test
public void testCredentialHelper_cmdExtension() throws IOException {
    Path credHelper = temporaryFolder.newFile("foo.cmd").toPath();
    Path pathWithoutCmd = credHelper.getParent().resolve("foo");
    assertThat(credHelper).isEqualTo(pathWithoutCmd.getParent().resolve("foo.cmd"));
    DefaultCredentialRetrievers credentialRetrievers = new DefaultCredentialRetrievers(mockCredentialRetrieverFactory, properties, environment).setCredentialHelper(pathWithoutCmd.toString());
    Exception ex = assertThrows(FileNotFoundException.class, credentialRetrievers::asList);
    assertThat(ex).hasMessageThat().startsWith("Specified credential helper was not found:");
    assertThat(ex).hasMessageThat().endsWith("foo");
    properties.setProperty("os.name", "winDOWs");
    List<CredentialRetriever> retrievers = new DefaultCredentialRetrievers(mockCredentialRetrieverFactory, properties, environment).setCredentialHelper(pathWithoutCmd.toString()).asList();
    assertThat(retrievers).containsExactly(mockDockerCredentialHelperCredentialRetriever, mockXdgPrimaryCredentialRetriever, mockEnvHomeXdgCredentialRetriever, mockSystemHomeXdgCredentialRetriever, mockDockerConfigEnvDockerConfigCredentialRetriever, mockDockerConfigEnvKubernetesDockerConfigCredentialRetriever, mockDockerConfigEnvLegacyDockerConfigCredentialRetriever, mockSystemHomeDockerConfigCredentialRetriever, mockSystemHomeKubernetesDockerConfigCredentialRetriever, mockSystemHomeLegacyDockerConfigCredentialRetriever, mockEnvHomeDockerConfigCredentialRetriever, mockEnvHomeKubernetesDockerConfigCredentialRetriever, mockEnvHomeLegacyDockerConfigCredentialRetriever, mockWellKnownCredentialHelpersCredentialRetriever, mockApplicationDefaultCredentialRetriever).inOrder();
}
Also used : Path(java.nio.file.Path) CredentialRetriever(com.google.cloud.tools.jib.api.CredentialRetriever) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 10 with CredentialRetriever

use of com.google.cloud.tools.jib.api.CredentialRetriever in project jib by GoogleContainerTools.

the class DefaultCredentialRetrieversTest method testCredentialHelper_exeExtension.

@Test
public void testCredentialHelper_exeExtension() throws IOException {
    Path credHelper = temporaryFolder.newFile("foo.exe").toPath();
    Path pathWithoutExe = credHelper.getParent().resolve("foo");
    assertThat(credHelper).isEqualTo(pathWithoutExe.getParent().resolve("foo.exe"));
    DefaultCredentialRetrievers credentialRetrievers = new DefaultCredentialRetrievers(mockCredentialRetrieverFactory, properties, environment).setCredentialHelper(pathWithoutExe.toString());
    Exception ex = assertThrows(FileNotFoundException.class, credentialRetrievers::asList);
    assertThat(ex).hasMessageThat().startsWith("Specified credential helper was not found:");
    assertThat(ex).hasMessageThat().endsWith("foo");
    properties.setProperty("os.name", "winDOWs");
    List<CredentialRetriever> retrievers = new DefaultCredentialRetrievers(mockCredentialRetrieverFactory, properties, environment).setCredentialHelper(pathWithoutExe.toString()).asList();
    assertThat(retrievers).containsExactly(mockDockerCredentialHelperCredentialRetriever, mockXdgPrimaryCredentialRetriever, mockEnvHomeXdgCredentialRetriever, mockSystemHomeXdgCredentialRetriever, mockDockerConfigEnvDockerConfigCredentialRetriever, mockDockerConfigEnvKubernetesDockerConfigCredentialRetriever, mockDockerConfigEnvLegacyDockerConfigCredentialRetriever, mockSystemHomeDockerConfigCredentialRetriever, mockSystemHomeKubernetesDockerConfigCredentialRetriever, mockSystemHomeLegacyDockerConfigCredentialRetriever, mockEnvHomeDockerConfigCredentialRetriever, mockEnvHomeKubernetesDockerConfigCredentialRetriever, mockEnvHomeLegacyDockerConfigCredentialRetriever, mockWellKnownCredentialHelpersCredentialRetriever, mockApplicationDefaultCredentialRetriever).inOrder();
}
Also used : Path(java.nio.file.Path) CredentialRetriever(com.google.cloud.tools.jib.api.CredentialRetriever) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Aggregations

CredentialRetriever (com.google.cloud.tools.jib.api.CredentialRetriever)10 Path (java.nio.file.Path)8 Test (org.junit.Test)8 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 Credential (com.google.cloud.tools.jib.api.Credential)2 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)2 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)2 Port (com.google.cloud.tools.jib.api.buildplan.Port)2 OciManifestTemplate (com.google.cloud.tools.jib.image.json.OciManifestTemplate)2 Instant (java.time.Instant)2