diff --git a/CHANGELOG.md b/CHANGELOG.md
index b98cd94a05..eda5f53bd6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
### Fixed
+## 0.1.2
+### Added
+- Use credentials from Docker config if none can be found otherwise ([]())
+
## 0.1.1
### Added
- Simple example `helloworld` project under `examples/` ([#62](https://github.com/google/jib/pull/62))
diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/DockerCredentialRetrieverIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialRetrieverIntegrationTest.java
similarity index 98%
rename from jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/DockerCredentialRetrieverIntegrationTest.java
rename to jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialRetrieverIntegrationTest.java
index 6fb6311c85..108fc17b08 100644
--- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/DockerCredentialRetrieverIntegrationTest.java
+++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialRetrieverIntegrationTest.java
@@ -14,7 +14,7 @@
* the License.
*/
-package com.google.cloud.tools.jib.registry;
+package com.google.cloud.tools.jib.registry.credentials;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.common.io.Resources;
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/RetrieveRegistryCredentialsStep.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/RetrieveRegistryCredentialsStep.java
index 436b9fb550..46a6d77666 100644
--- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/RetrieveRegistryCredentialsStep.java
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/RetrieveRegistryCredentialsStep.java
@@ -18,9 +18,9 @@
import com.google.cloud.tools.jib.Timer;
import com.google.cloud.tools.jib.http.Authorization;
-import com.google.cloud.tools.jib.registry.DockerCredentialRetriever;
-import com.google.cloud.tools.jib.registry.NonexistentDockerCredentialHelperException;
-import com.google.cloud.tools.jib.registry.NonexistentServerUrlDockerCredentialHelperException;
+import com.google.cloud.tools.jib.registry.credentials.DockerCredentialRetriever;
+import com.google.cloud.tools.jib.registry.credentials.NonexistentDockerCredentialHelperException;
+import com.google.cloud.tools.jib.registry.credentials.NonexistentServerUrlDockerCredentialHelperException;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.concurrent.Callable;
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/http/Authorizations.java b/jib-core/src/main/java/com/google/cloud/tools/jib/http/Authorizations.java
index 29514ccc0c..6d6c2dbb0c 100644
--- a/jib-core/src/main/java/com/google/cloud/tools/jib/http/Authorizations.java
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/http/Authorizations.java
@@ -22,10 +22,12 @@
/** Static initializers for {@link Authorization}. */
public class Authorizations {
+ /** Creates an {@link Authorization} with a {@code Bearer} token. */
public static Authorization withBearerToken(String token) {
return new Authorization("Bearer", token);
}
+ /** Creates an {@link Authorization} with a {@code Basic} credentials. */
public static Authorization withBasicCredentials(String username, String secret) {
String credentials = username + ":" + secret;
String token =
@@ -35,5 +37,10 @@ public static Authorization withBasicCredentials(String username, String secret)
return new Authorization("Basic", token);
}
+ /** Creates an {@link Authorization} with a base64-encoded {@code username:password} string. */
+ public static Authorization withBasicToken(String token) {
+ return new Authorization("Basic", token);
+ }
+
private Authorizations() {}
}
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetriever.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetriever.java
new file mode 100644
index 0000000000..7111c454d1
--- /dev/null
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetriever.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.cloud.tools.jib.registry.credentials;
+
+import com.google.cloud.tools.jib.http.Authorization;
+import com.google.cloud.tools.jib.http.Authorizations;
+import com.google.cloud.tools.jib.json.JsonTemplateMapper;
+import com.google.cloud.tools.jib.registry.credentials.json.DockerConfigTemplate;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import javax.annotation.Nullable;
+
+/**
+ * Retrieves registry credentials from the Docker config.
+ *
+ *
The credentials are searched in the following order (stopping when credentials are found):
+ *
+ *
+ * - If there is an {@code auth} defined for a registry.
+ *
- Using the {@code credsStore} credential helper, if available.
+ *
- Using the credential helper from {@code credHelpers}, if available.
+ *
+ *
+ * @see https://docs.docker.com/engine/reference/commandline/login/
+ */
+public class DockerConfigCredentialRetriever {
+
+ /**
+ * @see https://docs.docker.com/engine/reference/commandline/login/#privileged-user-requirement
+ */
+ private static final Path DOCKER_CONFIG_FILE =
+ Paths.get(System.getProperty("user.home")).resolve(".docker").resolve("config.json");
+
+ /** Factory class for constructing {@link DockerCredentialRetriever}. */
+ @VisibleForTesting
+ static class DockerCredentialRetrieverFactory {
+
+ private final String registry;
+
+ private DockerCredentialRetrieverFactory(String registry) {
+ this.registry = registry;
+ }
+
+ @VisibleForTesting
+ DockerCredentialRetriever withSuffix(String credentialHelperSuffix) {
+ return new DockerCredentialRetriever(registry, credentialHelperSuffix);
+ }
+ }
+
+ private final String registry;
+ private final Path dockerConfigFile;
+ private final DockerCredentialRetrieverFactory dockerCredentialRetrieverFactory;
+
+ public DockerConfigCredentialRetriever(String registry) {
+ this(registry, DOCKER_CONFIG_FILE);
+ }
+
+ @VisibleForTesting
+ DockerConfigCredentialRetriever(String registry, Path dockerConfigFile) {
+ this.registry = registry;
+ this.dockerConfigFile = dockerConfigFile;
+ this.dockerCredentialRetrieverFactory = new DockerCredentialRetrieverFactory(registry);
+ }
+
+ @VisibleForTesting
+ DockerConfigCredentialRetriever(
+ String registry,
+ Path dockerConfigFile,
+ DockerCredentialRetrieverFactory dockerCredentialRetrieverFactory) {
+ this.registry = registry;
+ this.dockerConfigFile = dockerConfigFile;
+ this.dockerCredentialRetrieverFactory = dockerCredentialRetrieverFactory;
+ }
+
+ /** @return {@link Authorization} found for {@code registry}, or {@code null} if not found */
+ @Nullable
+ public Authorization retrieve() {
+ DockerConfigTemplate dockerConfigTemplate = loadDockerConfigTemplate();
+ if (dockerConfigTemplate == null) {
+ return null;
+ }
+
+ String auth = dockerConfigTemplate.getAuthFor(registry);
+ if (auth != null) {
+ return Authorizations.withBasicToken(auth);
+ }
+
+ String credentialHelperSuffix = dockerConfigTemplate.getCredentialHelperFor(registry);
+ if (credentialHelperSuffix != null) {
+ try {
+ return dockerCredentialRetrieverFactory.withSuffix(credentialHelperSuffix).retrieve();
+
+ } catch (IOException
+ | NonexistentServerUrlDockerCredentialHelperException
+ | NonexistentDockerCredentialHelperException ex) {
+ // Ignores credential helper retrieval exceptions.
+ }
+ }
+
+ return null;
+ }
+
+ /** Loads the Docker config JSON and caches it. */
+ @Nullable
+ private DockerConfigTemplate loadDockerConfigTemplate() {
+ // Loads the Docker config.
+ if (!Files.exists(dockerConfigFile)) {
+ return null;
+ }
+ try {
+ return JsonTemplateMapper.readJsonFromFile(dockerConfigFile, DockerConfigTemplate.class);
+
+ } catch (IOException ex) {
+ // TODO: Throw some exception about not being able to parse Docker config.
+ return null;
+ }
+ }
+}
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/DockerCredentialRetriever.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialRetriever.java
similarity index 98%
rename from jib-core/src/main/java/com/google/cloud/tools/jib/registry/DockerCredentialRetriever.java
rename to jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialRetriever.java
index d5e385c822..ef5672f10c 100644
--- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/DockerCredentialRetriever.java
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialRetriever.java
@@ -14,7 +14,7 @@
* the License.
*/
-package com.google.cloud.tools.jib.registry;
+package com.google.cloud.tools.jib.registry.credentials;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -27,7 +27,6 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
-// TODO: Replace with non-CLI method.
/**
* Retrieves Docker credentials with a Docker credential helper.
*
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/NonexistentDockerCredentialHelperException.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/NonexistentDockerCredentialHelperException.java
similarity index 94%
rename from jib-core/src/main/java/com/google/cloud/tools/jib/registry/NonexistentDockerCredentialHelperException.java
rename to jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/NonexistentDockerCredentialHelperException.java
index b67a6f06de..cb11e50262 100644
--- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/NonexistentDockerCredentialHelperException.java
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/NonexistentDockerCredentialHelperException.java
@@ -14,7 +14,7 @@
* the License.
*/
-package com.google.cloud.tools.jib.registry;
+package com.google.cloud.tools.jib.registry.credentials;
/** Thrown because the requested credential helper CLI does not exist. */
public class NonexistentDockerCredentialHelperException extends Exception {
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/NonexistentServerUrlDockerCredentialHelperException.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/NonexistentServerUrlDockerCredentialHelperException.java
similarity index 95%
rename from jib-core/src/main/java/com/google/cloud/tools/jib/registry/NonexistentServerUrlDockerCredentialHelperException.java
rename to jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/NonexistentServerUrlDockerCredentialHelperException.java
index a30f7cca6e..f80f66ccd9 100644
--- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/NonexistentServerUrlDockerCredentialHelperException.java
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/NonexistentServerUrlDockerCredentialHelperException.java
@@ -14,7 +14,7 @@
* the License.
*/
-package com.google.cloud.tools.jib.registry;
+package com.google.cloud.tools.jib.registry.credentials;
/** Thrown because the credential helper does not have credentials for the specified server URL. */
public class NonexistentServerUrlDockerCredentialHelperException extends Exception {
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/RegistryCredentials.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/RegistryCredentials.java
index d98dd55957..084a9c2e42 100644
--- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/RegistryCredentials.java
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/RegistryCredentials.java
@@ -17,9 +17,6 @@
package com.google.cloud.tools.jib.registry.credentials;
import com.google.cloud.tools.jib.http.Authorization;
-import com.google.cloud.tools.jib.registry.DockerCredentialRetriever;
-import com.google.cloud.tools.jib.registry.NonexistentDockerCredentialHelperException;
-import com.google.cloud.tools.jib.registry.NonexistentServerUrlDockerCredentialHelperException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/json/DockerConfigTemplate.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/json/DockerConfigTemplate.java
new file mode 100644
index 0000000000..026f867891
--- /dev/null
+++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/credentials/json/DockerConfigTemplate.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.cloud.tools.jib.registry.credentials.json;
+
+import com.google.cloud.tools.jib.json.JsonTemplate;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.Nullable;
+
+/**
+ * Template for a Docker config file.
+ *
+ * Example:
+ *
+ *
{@code
+ * {
+ * "auths": {
+ * "registry": {
+ * "auth": "username:password in base64"
+ * },
+ * "anotherregistry": {},
+ * ...
+ * },
+ * "credsStore": "credential helper name",
+ * "credHelpers": {
+ * "registry": "credential helper name",
+ * "anotherregistry": "another credential helper name",
+ * ...
+ * }
+ * }
+ * }
+ *
+ * If an {@code auth} is defined for a registry, that is a valid {@code Basic} authorization to use
+ * for that registry.
+ *
+ * If {@code credsStore} is defined, is a credential helper that stores authorizations for all
+ * registries listed under {@code auths}.
+ *
+ *
Each entry in {@code credHelpers} is a mapping from a registry to a credential helper that
+ * stores the authorization for that registry.
+ *
+ * @see https://www.projectatomic.io/blog/2016/03/docker-credentials-store/
+ */
+public class DockerConfigTemplate extends JsonTemplate {
+
+ /** Template for an {@code auth} defined for a registry under {@code auths}. */
+ private static class AuthTemplate extends JsonTemplate {
+
+ private String auth;
+ }
+
+ /** Maps from registry to its {@link AuthTemplate}. */
+ private final Map auths = new HashMap<>();
+
+ private String credsStore;
+
+ /** Maps from registry to credential helper name. */
+ private final Map credHelpers = new HashMap<>();
+
+ /**
+ * @return the base64-encoded {@code Basic} authorization for {@code registry}, or {@code null} if
+ * none exists
+ */
+ @Nullable
+ public String getAuthFor(String registry) {
+ if (!auths.containsKey(registry)) {
+ return null;
+ }
+ return auths.get(registry).auth;
+ }
+
+ /**
+ * @return {@code credsStore} if {@code registry} is present in {@code auths}; otherwise, searches
+ * {@code credHelpers}; otherwise, {@code null} if not found
+ */
+ @Nullable
+ public String getCredentialHelperFor(String registry) {
+ if (credsStore != null && auths.containsKey(registry)) {
+ return credsStore;
+ }
+ if (credHelpers.containsKey(registry)) {
+ return credHelpers.get(registry);
+ }
+ return null;
+ }
+
+ @VisibleForTesting
+ DockerConfigTemplate addAuth(String registry, @Nullable String auth) {
+ AuthTemplate authTemplate = new AuthTemplate();
+ authTemplate.auth = auth;
+ auths.put(registry, authTemplate);
+ return this;
+ }
+
+ @VisibleForTesting
+ DockerConfigTemplate setCredsStore(String credsStore) {
+ this.credsStore = credsStore;
+ return this;
+ }
+
+ @VisibleForTesting
+ DockerConfigTemplate addCredHelper(String registry, String credHelper) {
+ credHelpers.put(registry, credHelper);
+ return this;
+ }
+}
diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetrieverTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetrieverTest.java
new file mode 100644
index 0000000000..70cbb2a0c8
--- /dev/null
+++ b/jib-core/src/test/java/com/google/cloud/tools/jib/registry/credentials/DockerConfigCredentialRetrieverTest.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.cloud.tools.jib.registry.credentials;
+
+import com.google.cloud.tools.jib.http.Authorization;
+import com.google.common.io.Resources;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+/** Tests for {@link DockerConfigCredentialRetriever}. */
+@RunWith(MockitoJUnitRunner.class)
+public class DockerConfigCredentialRetrieverTest {
+
+ @Mock private Authorization mockAuthorization;
+ @Mock private DockerCredentialRetriever mockDockerCredentialRetriever;
+
+ @Mock
+ private DockerConfigCredentialRetriever.DockerCredentialRetrieverFactory
+ mockDockerCredentialRetrieverFactory;
+
+ private Path dockerConfigFile;
+
+ @Before
+ public void setUp()
+ throws URISyntaxException, NonexistentServerUrlDockerCredentialHelperException,
+ NonexistentDockerCredentialHelperException, IOException {
+ dockerConfigFile = Paths.get(Resources.getResource("json/dockerconfig.json").toURI());
+
+ Mockito.when(mockDockerCredentialRetriever.retrieve()).thenReturn(mockAuthorization);
+ }
+
+ @Test
+ public void testRetrieve_nonexistentDockerConfigFile() throws URISyntaxException {
+ DockerConfigCredentialRetriever dockerConfigCredentialRetriever =
+ new DockerConfigCredentialRetriever("some registry", Paths.get("fake/path"));
+
+ Assert.assertNull(dockerConfigCredentialRetriever.retrieve());
+ }
+
+ @Test
+ public void testRetrieve_hasAuth() {
+ DockerConfigCredentialRetriever dockerConfigCredentialRetriever =
+ new DockerConfigCredentialRetriever("some registry", dockerConfigFile, null);
+
+ Authorization authorization = dockerConfigCredentialRetriever.retrieve();
+ Assert.assertNotNull(authorization);
+ Assert.assertEquals("some auth", authorization.getToken());
+ }
+
+ @Test
+ public void testRetrieve_useCredsStore() {
+ Mockito.when(mockDockerCredentialRetrieverFactory.withSuffix("some credential store"))
+ .thenReturn(mockDockerCredentialRetriever);
+
+ DockerConfigCredentialRetriever dockerConfigCredentialRetriever =
+ new DockerConfigCredentialRetriever(
+ "just registry", dockerConfigFile, mockDockerCredentialRetrieverFactory);
+
+ Authorization authorization = dockerConfigCredentialRetriever.retrieve();
+ Assert.assertNotNull(authorization);
+ Assert.assertEquals(mockAuthorization, authorization);
+ }
+
+ @Test
+ public void testRetrieve_useCredHelper() {
+ Mockito.when(mockDockerCredentialRetrieverFactory.withSuffix("another credential helper"))
+ .thenReturn(mockDockerCredentialRetriever);
+
+ DockerConfigCredentialRetriever dockerConfigCredentialRetriever =
+ new DockerConfigCredentialRetriever(
+ "another registry", dockerConfigFile, mockDockerCredentialRetrieverFactory);
+
+ Authorization authorization = dockerConfigCredentialRetriever.retrieve();
+ Assert.assertNotNull(authorization);
+ Assert.assertEquals(mockAuthorization, authorization);
+ }
+
+ @Test
+ public void testRetrieve_none() {
+ DockerConfigCredentialRetriever dockerConfigCredentialRetriever =
+ new DockerConfigCredentialRetriever("unknown registry", dockerConfigFile);
+
+ Assert.assertNull(dockerConfigCredentialRetriever.retrieve());
+ }
+}
diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/registry/credentials/json/DockerConfigTemplateTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/registry/credentials/json/DockerConfigTemplateTest.java
new file mode 100644
index 0000000000..06b68dbda9
--- /dev/null
+++ b/jib-core/src/test/java/com/google/cloud/tools/jib/registry/credentials/json/DockerConfigTemplateTest.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.cloud.tools.jib.registry.credentials.json;
+
+import com.google.cloud.tools.jib.json.JsonTemplateMapper;
+import com.google.common.io.Resources;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.junit.Assert;
+import org.junit.Test;
+
+/** Tests for {@link DockerConfigTemplate}. */
+public class DockerConfigTemplateTest {
+
+ @Test
+ public void test_toJson() throws URISyntaxException, IOException {
+ // Loads the expected JSON string.
+ Path jsonFile = Paths.get(Resources.getResource("json/dockerconfig.json").toURI());
+ String expectedJson = new String(Files.readAllBytes(jsonFile), StandardCharsets.UTF_8);
+
+ // Creates the JSON object to serialize.
+ DockerConfigTemplate dockerConfigTemplate =
+ new DockerConfigTemplate()
+ .addAuth("some registry", "some auth")
+ .addAuth("some other registry", "some other auth")
+ .addAuth("just registry", null)
+ .setCredsStore("some credential store")
+ .addCredHelper("some registry", "some credential helper")
+ .addCredHelper("another registry", "another credential helper");
+
+ // Serializes the JSON object.
+ ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
+ JsonTemplateMapper.toBlob(dockerConfigTemplate).writeTo(jsonStream);
+
+ Assert.assertEquals(expectedJson, jsonStream.toString());
+ }
+
+ @Test
+ public void test_fromJson() throws URISyntaxException, IOException {
+ // Loads the JSON string.
+ Path jsonFile = Paths.get(Resources.getResource("json/dockerconfig.json").toURI());
+
+ // Deserializes into a docker config JSON object.
+ DockerConfigTemplate dockerConfigTemplate =
+ JsonTemplateMapper.readJsonFromFile(jsonFile, DockerConfigTemplate.class);
+
+ Assert.assertEquals("some auth", dockerConfigTemplate.getAuthFor("some registry"));
+ Assert.assertEquals("some other auth", dockerConfigTemplate.getAuthFor("some other registry"));
+ Assert.assertEquals(null, dockerConfigTemplate.getAuthFor("just registry"));
+
+ Assert.assertEquals(
+ "some credential store", dockerConfigTemplate.getCredentialHelperFor("some registry"));
+ Assert.assertEquals(
+ "some credential store",
+ dockerConfigTemplate.getCredentialHelperFor("some other registry"));
+ Assert.assertEquals(
+ "some credential store", dockerConfigTemplate.getCredentialHelperFor("just registry"));
+ Assert.assertEquals(
+ "another credential helper",
+ dockerConfigTemplate.getCredentialHelperFor("another registry"));
+ Assert.assertEquals(null, dockerConfigTemplate.getCredentialHelperFor("unknonwn registry"));
+ }
+}
diff --git a/jib-core/src/test/resources/json/dockerconfig.json b/jib-core/src/test/resources/json/dockerconfig.json
new file mode 100644
index 0000000000..3e8af36afc
--- /dev/null
+++ b/jib-core/src/test/resources/json/dockerconfig.json
@@ -0,0 +1 @@
+{"auths":{"some other registry":{"auth":"some other auth"},"some registry":{"auth":"some auth"},"just registry":{}},"credsStore":"some credential store","credHelpers":{"another registry":"another credential helper","some registry":"some credential helper"}}
\ No newline at end of file