From 38ea51b521fad5ff69ff041fd46f5e0d9cf1a851 Mon Sep 17 00:00:00 2001 From: Chanseok Oh Date: Mon, 22 Jun 2020 15:50:23 -0400 Subject: [PATCH] Fix NPE when reading "auths" section in ~/.docker/config.json (#2536) --- jib-core/CHANGELOG.md | 2 ++ .../credentials/DockerConfigCredentialRetriever.java | 2 +- .../DockerConfigCredentialRetrieverTest.java | 11 +++++++++++ .../src/test/resources/core/json/dockerconfig.json | 3 ++- jib-gradle-plugin/CHANGELOG.md | 1 + jib-maven-plugin/CHANGELOG.md | 1 + 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/jib-core/CHANGELOG.md b/jib-core/CHANGELOG.md index 564d1a1aa6..3104ccc909 100644 --- a/jib-core/CHANGELOG.md +++ b/jib-core/CHANGELOG.md @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file. ### Fixed +- Fixed `NullPointerException` when the `"auths":` section in `~/.docker/config.json` has an entry with no `"auth":` field. ([#2535](https://github.com/GoogleContainerTools/jib/issues/2535)) + ## 0.15.0 ### Added 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 index 89318bef0a..50c97929a0 100644 --- 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 @@ -140,7 +140,7 @@ Optional retrieve(DockerConfig dockerConfig, Consumer logg // Lastly, find defined auth. AuthTemplate auth = dockerConfig.getAuthFor(registryAlias); - if (auth != null) { + if (auth != null && auth.getAuth() != null) { // 'auth' is a basic authentication token that should be parsed back into credentials String usernameColonPassword = new String(Base64.decodeBase64(auth.getAuth()), StandardCharsets.UTF_8); 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 index 6fe02af2b8..47803f2de5 100644 --- 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 @@ -194,4 +194,15 @@ public void testRetrieve_azureIdentityToken() throws IOException, URISyntaxExcep Assert.assertEquals("", credentials.get().getUsername()); Assert.assertEquals("cool identity token", credentials.get().getPassword()); } + + @Test + public void testRetrieve_noErrorWhenMissingAuthField() throws IOException, URISyntaxException { + Path dockerConfigFile = Paths.get(Resources.getResource("core/json/dockerconfig.json").toURI()); + + DockerConfigCredentialRetriever dockerConfigCredentialRetriever = + DockerConfigCredentialRetriever.create("no auth field", dockerConfigFile); + + Optional credentials = dockerConfigCredentialRetriever.retrieve(mockLogger); + Assert.assertFalse(credentials.isPresent()); + } } diff --git a/jib-core/src/test/resources/core/json/dockerconfig.json b/jib-core/src/test/resources/core/json/dockerconfig.json index 00d7673bd1..8df57f4153 100644 --- a/jib-core/src/test/resources/core/json/dockerconfig.json +++ b/jib-core/src/test/resources/core/json/dockerconfig.json @@ -4,7 +4,8 @@ "some registry":{"auth":"c29tZTphdXRo","password":"ignored"}, "https://registry":{"auth":"dG9rZW4="}, - "example.com":{"auth":"should not match example"} + "example.com":{"auth":"should not match example"}, + "no auth field":{} }, "credsStore":"some credential store", "credHelpers":{ diff --git a/jib-gradle-plugin/CHANGELOG.md b/jib-gradle-plugin/CHANGELOG.md index d38ea696b9..1e45ed28c7 100644 --- a/jib-gradle-plugin/CHANGELOG.md +++ b/jib-gradle-plugin/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. ### Fixed - Fixed reporting a wrong credential helper name when the helper does not exist on Windows. ([#2527](https://github.com/GoogleContainerTools/jib/issues/2527)) +- Fixed `NullPointerException` when the `"auths":` section in `~/.docker/config.json` has an entry with no `"auth":` field. ([#2535](https://github.com/GoogleContainerTools/jib/issues/2535)) ## 2.4.0 diff --git a/jib-maven-plugin/CHANGELOG.md b/jib-maven-plugin/CHANGELOG.md index f112cb78a9..b9a9a690d2 100644 --- a/jib-maven-plugin/CHANGELOG.md +++ b/jib-maven-plugin/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. ### Fixed - Fixed reporting a wrong credential helper name when the helper does not exist on Windows. ([#2527](https://github.com/GoogleContainerTools/jib/issues/2527)) +- Fixed `NullPointerException` when the `"auths":` section in `~/.docker/config.json` has an entry with no `"auth":` field. ([#2535](https://github.com/GoogleContainerTools/jib/issues/2535)) ## 2.4.0