@@ -110,24 +110,29 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
110110 }
111111
112112 Map <String , AuthConfig > configMap = null ;
113- /*
114- Registry v2 expects config expects config.json while v2 expects .dockercfg
115- The only difference between them is that config.json wraps "auths" around the AuthConfig
116- */
117- try {
118- // try registry version 2
119- final ObjectNode node = filterNonAuthsFromJSON (confFile );
120- Map <String , Map <String , AuthConfig >> configJson = MAPPER .convertValue (node , CONFIG_JSON_MAP_TYPE );
121- if (configJson != null && !configJson .isEmpty ()) {
122- configMap = configJson .get (AUTHS_PROPERTY );
123- }
124-
125- } catch (IOException e1 ) {
113+ if (isJSONFile (confFile )) {
114+ /*
115+ Registry v2 expects config expects config.json while v2 expects .dockercfg
116+ The only difference between them is that config.json wraps "auths" around the AuthConfig
117+ */
126118 try {
127- // try registry version 1
128- configMap = MAPPER .readValue (confFile , CONFIG_CFG_MAP_TYPE );
129- } catch (IOException e2 ) {
130- // pass
119+ // try registry version 2
120+ final ObjectNode node = filterNonAuthsFromJSON (confFile );
121+ Map <String , Map <String , AuthConfig >> configJson = MAPPER .convertValue (node , CONFIG_JSON_MAP_TYPE );
122+ if (configJson != null && !configJson .isEmpty ()) {
123+ configMap = configJson .get (AUTHS_PROPERTY );
124+ }
125+
126+ } catch (IOException e1 ) {
127+ try {
128+ // try registry version 1
129+ configMap = MAPPER .readValue (confFile , CONFIG_CFG_MAP_TYPE );
130+ } catch (IOException e2 ) {
131+ // we know it is JSON so it does not make sense to check for the old format
132+ // probably Docker writes some unstructured contents here, see #806
133+ // at least it is not worse than the totally absent file
134+ return new AuthConfigFile ();
135+ }
131136 }
132137 }
133138
@@ -165,6 +170,14 @@ public static AuthConfigFile loadConfig(File confFile) throws IOException {
165170
166171 }
167172
173+ private static boolean isJSONFile (final File confFile ) {
174+ try {
175+ return MAPPER .readValue (confFile , ObjectNode .class ) != null ;
176+ } catch (IOException e ) {
177+ return false ;
178+ }
179+ }
180+
168181 private static ObjectNode filterNonAuthsFromJSON (final File confFile ) throws IOException {
169182 final ObjectNode node = MAPPER .readValue (confFile , ObjectNode .class );
170183 if (!node .has (AUTHS_PROPERTY )) {
0 commit comments