From 3ad2d6ab6a12c8b8904ce810dfd82838a466d5d0 Mon Sep 17 00:00:00 2001 From: Stephen Moon Date: Mon, 21 Aug 2017 23:16:08 +0900 Subject: [PATCH] Avoid 'JSONException: null object' when no key 'auths' exist in .docker/config.json --- .../docker/commons/credentials/DockerRegistryToken.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerRegistryToken.java b/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerRegistryToken.java index b16187ee..c2267df9 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerRegistryToken.java +++ b/src/main/java/org/jenkinsci/plugins/docker/commons/credentials/DockerRegistryToken.java @@ -76,12 +76,12 @@ public KeyMaterialFactory newKeyMaterialFactory(final URL endpoint, @Nonnull Vir * Makes the credentials available locally and returns {@link KeyMaterialFactory} that gives you the parameters * needed to access it. * - * This is done by inserting the token into {@code ~/.dockercfg} + * This is done by inserting the token into {@code ~/.dockercfg} or {@code ~/.docker/config.json} */ public KeyMaterialFactory newKeyMaterialFactory(final @Nonnull URL endpoint, @Nonnull VirtualChannel target, @CheckForNull Launcher launcher, final @Nonnull TaskListener listener) throws InterruptedException, IOException { target.call(new MasterToSlaveCallable() { /** - * Insert the token into {@code ~/.dockercfg} + * Insert the token into {@code ~/.dockercfg} or {@code ~/.docker/config.json} */ @Override public Void call() throws IOException { @@ -96,6 +96,11 @@ public Void call() throws IOException { if (config.exists()) { json = JSONObject.fromObject(FileUtils.readFileToString(config, "UTF-8")); auths = json.getJSONObject("auths"); + if (auths.isNullObject()) { + auths = new JSONObject(); + json.put("auths", auths); + FileUtils.writeStringToFile(config, json.toString(2), "UTF-8"); + } } else { config = new File(System.getProperty("user.home"), ".dockercfg"); if (config.exists()) {