Skip to content

Commit 5f96086

Browse files
committed
Polish
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent c6045c3 commit 5f96086

File tree

6 files changed

+46
-27
lines changed

6 files changed

+46
-27
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Credential extends MappedObject {
4040

4141
private final String secret;
4242

43-
private String serverUrl;
43+
private final String serverUrl;
4444

4545
Credential(JsonNode node) {
4646
super(node, MethodHandles.lookup());

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CredentialHelper {
3939

4040
private static final String USR_LOCAL_BIN = "/usr/local/bin/";
4141

42-
Set<String> CREDENTIAL_NOT_FOUND_MESSAGES = Set.of("credentials not found in native keychain",
42+
private static final Set<String> CREDENTIAL_NOT_FOUND_MESSAGES = Set.of("credentials not found in native keychain",
4343
"no credentials server URL", "no credentials username");
4444

4545
private final String executable;
@@ -73,12 +73,12 @@ Credential get(String serverUrl) throws IOException {
7373
}
7474
}
7575

76-
private ProcessBuilder processBuilder(String string) {
76+
private ProcessBuilder processBuilder(String action) {
7777
ProcessBuilder processBuilder = new ProcessBuilder().redirectErrorStream(true);
7878
if (Platform.isWindows()) {
7979
processBuilder.command("cmd", "/c");
8080
}
81-
processBuilder.command(this.executable, string);
81+
processBuilder.command(this.executable, action);
8282
return processBuilder;
8383
}
8484

@@ -90,14 +90,21 @@ private Process start(ProcessBuilder processBuilder) throws IOException {
9090
if (!Platform.isMac()) {
9191
throw ex;
9292
}
93-
List<String> command = new ArrayList<>(processBuilder.command());
94-
command.set(0, USR_LOCAL_BIN + command.get(0));
95-
return processBuilder.command(command).start();
93+
try {
94+
List<String> command = new ArrayList<>(processBuilder.command());
95+
command.set(0, USR_LOCAL_BIN + command.get(0));
96+
return processBuilder.command(command).start();
97+
}
98+
catch (Exception suppressed) {
99+
// Suppresses the exception and rethrows the original exception
100+
ex.addSuppressed(suppressed);
101+
throw ex;
102+
}
96103
}
97104
}
98105

99-
private boolean isCredentialsNotFoundError(String message) {
100-
return this.CREDENTIAL_NOT_FOUND_MESSAGES.contains(message.trim());
106+
private static boolean isCredentialsNotFoundError(String message) {
107+
return CREDENTIAL_NOT_FOUND_MESSAGES.contains(message.trim());
101108
}
102109

103110
}

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Base64;
2727
import java.util.HexFormat;
2828
import java.util.Map;
29+
import java.util.function.Supplier;
2930

3031
import com.fasterxml.jackson.core.JsonProcessingException;
3132
import com.fasterxml.jackson.databind.JsonNode;
@@ -36,6 +37,7 @@
3637
import org.springframework.boot.buildpack.platform.system.Environment;
3738
import org.springframework.util.Assert;
3839
import org.springframework.util.StringUtils;
40+
import org.springframework.util.function.SingletonSupplier;
3941

4042
/**
4143
* Docker configuration stored in metadata files managed by the Docker CLI.
@@ -63,7 +65,8 @@ final class DockerConfigurationMetadata {
6365

6466
private static final String CONTEXT_FILE_NAME = "meta.json";
6567

66-
private static volatile DockerConfigurationMetadata systemEnvironmentConfigurationMetadata;
68+
private static final Supplier<DockerConfigurationMetadata> systemEnvironmentConfigurationMetadata = SingletonSupplier
69+
.of(() -> DockerConfigurationMetadata.create(Environment.SYSTEM));
6770

6871
private final String configLocation;
6972

@@ -90,20 +93,18 @@ DockerContext forContext(String context) {
9093
}
9194

9295
static DockerConfigurationMetadata from(Environment environment) {
93-
DockerConfigurationMetadata dockerConfigurationMetadata = (environment == Environment.SYSTEM)
94-
? DockerConfigurationMetadata.systemEnvironmentConfigurationMetadata : null;
95-
if (dockerConfigurationMetadata != null) {
96-
return dockerConfigurationMetadata;
96+
if (environment == Environment.SYSTEM) {
97+
return systemEnvironmentConfigurationMetadata.get();
9798
}
99+
return create(environment);
100+
}
101+
102+
private static DockerConfigurationMetadata create(Environment environment) {
98103
String configLocation = environment.get(DOCKER_CONFIG);
99104
configLocation = (configLocation != null) ? configLocation : getUserHomeConfigLocation();
100105
DockerConfig dockerConfig = createDockerConfig(configLocation);
101106
DockerContext dockerContext = createDockerContext(configLocation, dockerConfig.getCurrentContext());
102-
dockerConfigurationMetadata = new DockerConfigurationMetadata(configLocation, dockerConfig, dockerContext);
103-
if (environment == Environment.SYSTEM) {
104-
DockerConfigurationMetadata.systemEnvironmentConfigurationMetadata = dockerConfigurationMetadata;
105-
}
106-
return dockerConfigurationMetadata;
107+
return new DockerConfigurationMetadata(configLocation, dockerConfig, dockerContext);
107108
}
108109

109110
private static String getUserHomeConfigLocation() {

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.function.BiConsumer;
2020

2121
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
22+
import org.springframework.util.Assert;
2223

2324
/**
2425
* Docker registry authentication configuration.
@@ -83,7 +84,8 @@ static DockerRegistryAuthentication user(String username, String password, Strin
8384
* Factory method that returns a new {@link DockerRegistryAuthentication} instance
8485
* that uses the standard docker JSON config (including support for credential
8586
* helpers) to generate auth headers.
86-
* @param fallback the fallback authentication to use if no suitable config is found
87+
* @param fallback the fallback authentication to use if no suitable config is found,
88+
* may be null {@code}
8789
* @return a new {@link DockerRegistryAuthentication} instance
8890
* @since 3.5.0
8991
* @see #configuration(DockerRegistryAuthentication, BiConsumer)
@@ -96,15 +98,17 @@ static DockerRegistryAuthentication configuration(DockerRegistryAuthentication f
9698
* Factory method that returns a new {@link DockerRegistryAuthentication} instance
9799
* that uses the standard docker JSON config (including support for credential
98100
* helpers) to generate auth headers.
99-
* @param fallback the fallback authentication to use if no suitable config is found
101+
* @param fallback the fallback authentication to use if no suitable config is found,
102+
* may be {@code null}
100103
* @param credentialHelperExceptionHandler callback that should handle credential
101-
* helper exceptions
104+
* helper exceptions, never {@code null}
102105
* @return a new {@link DockerRegistryAuthentication} instance
103106
* @since 3.5.0
104107
* @see #configuration(DockerRegistryAuthentication, BiConsumer)
105108
*/
106109
static DockerRegistryAuthentication configuration(DockerRegistryAuthentication fallback,
107110
BiConsumer<String, Exception> credentialHelperExceptionHandler) {
111+
Assert.notNull(credentialHelperExceptionHandler, () -> "'credentialHelperExceptionHandler' must not be null");
108112
return new DockerRegistryConfigAuthentication(fallback, credentialHelperExceptionHandler);
109113
}
110114

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,17 @@ void getWhenUnknownErrorThrowsException() {
8989
}
9090

9191
@Test
92-
void getWhenCommandDoesNotExistErrorThrowsException() {
93-
String name = "docker-credential-%s".formatted(UUID.randomUUID().toString());
94-
assertThatIOException().isThrownBy(() -> new CredentialHelper(name).get("invalid.example.com"))
95-
.withMessageContaining(name);
92+
void getWhenExecutableDoesNotExistErrorThrowsException() {
93+
String executable = "docker-credential-%s".formatted(UUID.randomUUID().toString());
94+
assertThatIOException().isThrownBy(() -> new CredentialHelper(executable).get("invalid.example.com"))
95+
.withMessageContaining(executable)
96+
.satisfies((ex) -> {
97+
if (Platform.isMac()) {
98+
assertThat(ex.getMessage()).doesNotContain("/usr/local/bin/");
99+
assertThat(ex.getSuppressed()).allSatisfy((suppressed) -> assertThat(suppressed)
100+
.hasMessageContaining("/usr/local/bin/" + executable));
101+
}
102+
});
96103
}
97104

98105
}
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author Dmytro Nosan
3434
*/
35-
class CredentialsTests {
35+
class CredentialTests {
3636

3737
@Test
3838
@WithResource(name = "credentials.json", content = """

0 commit comments

Comments
 (0)