|
24 | 24 | import java.security.MessageDigest; |
25 | 25 | import java.security.NoSuchAlgorithmException; |
26 | 26 | import java.util.Base64; |
27 | | -import java.util.Collections; |
28 | 27 | import java.util.HexFormat; |
29 | | -import java.util.LinkedHashMap; |
30 | 28 | import java.util.Map; |
31 | 29 |
|
32 | 30 | import com.fasterxml.jackson.core.JsonProcessingException; |
|
36 | 34 | import org.springframework.boot.buildpack.platform.json.MappedObject; |
37 | 35 | import org.springframework.boot.buildpack.platform.json.SharedObjectMapper; |
38 | 36 | import org.springframework.boot.buildpack.platform.system.Environment; |
| 37 | +import org.springframework.util.Assert; |
| 38 | +import org.springframework.util.StringUtils; |
39 | 39 |
|
40 | 40 | /** |
41 | 41 | * Docker configuration stored in metadata files managed by the Docker CLI. |
42 | 42 | * |
43 | 43 | * @author Scott Frederick |
| 44 | + * @author Dmytro Nosan |
44 | 45 | */ |
45 | 46 | final class DockerConfigurationMetadata { |
46 | 47 |
|
@@ -162,22 +163,8 @@ private DockerConfig(JsonNode node) { |
162 | 163 | super(node, MethodHandles.lookup()); |
163 | 164 | this.currentContext = valueAt("/currentContext", String.class); |
164 | 165 | this.credsStore = valueAt("/credsStore", String.class); |
165 | | - this.credHelpers = extractCredHelpers(); |
166 | | - this.auths = extractAuths(); |
167 | | - } |
168 | | - |
169 | | - private Map<String, Auth> extractAuths() { |
170 | | - Map<String, Auth> auths = new LinkedHashMap<>(); |
171 | | - getNode().at("/auths") |
172 | | - .fields() |
173 | | - .forEachRemaining((entry) -> auths.put(entry.getKey(), new Auth(entry.getValue()))); |
174 | | - return Map.copyOf(auths); |
175 | | - } |
176 | | - |
177 | | - @SuppressWarnings("unchecked") |
178 | | - private Map<String, String> extractCredHelpers() { |
179 | | - Map<String, String> credHelpers = valueAt("/credHelpers", Map.class); |
180 | | - return (credHelpers != null) ? Map.copyOf(credHelpers) : Collections.emptyMap(); |
| 166 | + this.credHelpers = mapAt("/credHelpers", JsonNode::textValue); |
| 167 | + this.auths = mapAt("/auths", Auth::new); |
181 | 168 | } |
182 | 169 |
|
183 | 170 | String getCurrentContext() { |
@@ -216,18 +203,17 @@ static final class Auth extends MappedObject { |
216 | 203 |
|
217 | 204 | Auth(JsonNode node) { |
218 | 205 | super(node, MethodHandles.lookup()); |
219 | | - String username = valueAt("/username", String.class); |
220 | | - String password = valueAt("/password", String.class); |
221 | 206 | String auth = valueAt("/auth", String.class); |
222 | | - if (auth != null) { |
| 207 | + if (StringUtils.hasText(auth)) { |
223 | 208 | String[] parts = new String(Base64.getDecoder().decode(auth)).split(":", 2); |
224 | | - if (parts.length == 2) { |
225 | | - username = parts[0]; |
226 | | - password = parts[1]; |
227 | | - } |
| 209 | + Assert.state(parts.length == 2, "Malformed auth in docker configuration metadata"); |
| 210 | + this.username = parts[0]; |
| 211 | + this.password = parts[1]; |
| 212 | + } |
| 213 | + else { |
| 214 | + this.username = valueAt("/username", String.class); |
| 215 | + this.password = valueAt("/password", String.class); |
228 | 216 | } |
229 | | - this.username = username; |
230 | | - this.password = password; |
231 | 217 | this.email = valueAt("/email", String.class); |
232 | 218 | } |
233 | 219 |
|
|
0 commit comments