Skip to content

Commit 2a9e30a

Browse files
committed
Polish 'Update DockerConfigurationMetadata to support credentials'
See gh-45269
1 parent 958e28d commit 2a9e30a

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerConfigurationMetadata.java

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import java.security.MessageDigest;
2525
import java.security.NoSuchAlgorithmException;
2626
import java.util.Base64;
27-
import java.util.Collections;
2827
import java.util.HexFormat;
29-
import java.util.LinkedHashMap;
3028
import java.util.Map;
3129

3230
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -36,11 +34,14 @@
3634
import org.springframework.boot.buildpack.platform.json.MappedObject;
3735
import org.springframework.boot.buildpack.platform.json.SharedObjectMapper;
3836
import org.springframework.boot.buildpack.platform.system.Environment;
37+
import org.springframework.util.Assert;
38+
import org.springframework.util.StringUtils;
3939

4040
/**
4141
* Docker configuration stored in metadata files managed by the Docker CLI.
4242
*
4343
* @author Scott Frederick
44+
* @author Dmytro Nosan
4445
*/
4546
final class DockerConfigurationMetadata {
4647

@@ -162,22 +163,8 @@ private DockerConfig(JsonNode node) {
162163
super(node, MethodHandles.lookup());
163164
this.currentContext = valueAt("/currentContext", String.class);
164165
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);
181168
}
182169

183170
String getCurrentContext() {
@@ -216,18 +203,17 @@ static final class Auth extends MappedObject {
216203

217204
Auth(JsonNode node) {
218205
super(node, MethodHandles.lookup());
219-
String username = valueAt("/username", String.class);
220-
String password = valueAt("/password", String.class);
221206
String auth = valueAt("/auth", String.class);
222-
if (auth != null) {
207+
if (StringUtils.hasText(auth)) {
223208
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);
228216
}
229-
this.username = username;
230-
this.password = password;
231217
this.email = valueAt("/email", String.class);
232218
}
233219

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/json/MappedObject.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,9 @@
2525
import java.lang.reflect.Proxy;
2626
import java.util.ArrayList;
2727
import java.util.Collections;
28+
import java.util.LinkedHashMap;
2829
import java.util.List;
30+
import java.util.Map;
2931
import java.util.function.Function;
3032

3133
import com.fasterxml.jackson.databind.JsonNode;
@@ -38,6 +40,7 @@
3840
* Base class for mapped JSON objects.
3941
*
4042
* @author Phillip Webb
43+
* @author Dmytro Nosan
4144
* @since 2.3.0
4245
*/
4346
public class MappedObject {
@@ -75,6 +78,23 @@ protected <T> T valueAt(String expression, Class<T> type) {
7578
return valueAt(this, this.node, this.lookup, expression, type);
7679
}
7780

81+
/**
82+
* Get a {@link Map} at the given JSON path expression with a value mapped from a
83+
* related {@link JsonNode}.
84+
* @param <V> the value type
85+
* @param expression the JSON path expression
86+
* @param valueMapper function to map the value from the {@link JsonNode}
87+
* @return the map
88+
* @since 3.5.0
89+
*/
90+
protected <V> Map<String, V> mapAt(String expression, Function<JsonNode, V> valueMapper) {
91+
Map<String, V> map = new LinkedHashMap<>();
92+
getNode().at(expression)
93+
.fields()
94+
.forEachRemaining((entry) -> map.put(entry.getKey(), valueMapper.apply(entry.getValue())));
95+
return Collections.unmodifiableMap(map);
96+
}
97+
7898
/**
7999
* Get children at the given JSON path expression by constructing them using the given
80100
* factory.

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerConfigurationMetadataTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* Tests for {@link DockerConfigurationMetadata}.
3838
*
3939
* @author Scott Frederick
40+
* @author Dmytro Nosan
4041
*/
4142
class DockerConfigurationMetadataTests extends AbstractJsonTests {
4243

0 commit comments

Comments
 (0)