Home / Class/ CredentialHelper Class — spring-boot Architecture

CredentialHelper Class — spring-boot Architecture

Architecture documentation for the CredentialHelper class in CredentialHelper.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/CredentialHelper.java lines 40–112

class CredentialHelper {

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

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

	private final String executable;

	CredentialHelper(String executable) {
		this.executable = executable;
	}

	@Nullable Credential get(String serverUrl) throws IOException {
		ProcessBuilder processBuilder = processBuilder("get");
		Process process = start(processBuilder);
		try (OutputStream request = process.getOutputStream()) {
			request.write(serverUrl.getBytes(StandardCharsets.UTF_8));
		}
		try {
			int exitCode = process.waitFor();
			try (InputStream response = process.getInputStream()) {
				if (exitCode == 0) {
					return new Credential(SharedJsonMapper.get().readTree(response));
				}
				String errorMessage = new String(response.readAllBytes(), StandardCharsets.UTF_8);
				if (!isCredentialsNotFoundError(errorMessage)) {
					throw new IOException("%s' exited with code %d: %s".formatted(process, exitCode, errorMessage));
				}
				return null;
			}
		}
		catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
			return null;
		}
	}

	private ProcessBuilder processBuilder(String action) {
		ProcessBuilder processBuilder = new ProcessBuilder().redirectErrorStream(true);
		if (Platform.isWindows()) {
			processBuilder.command("cmd", "/c");
		}
		processBuilder.command().addAll(Arrays.asList(this.executable, action));
		return processBuilder;
	}

	private Process start(ProcessBuilder processBuilder) throws IOException {
		try {
			return processBuilder.start();
		}
		catch (IOException ex) {
			if (!Platform.isMac()) {
				throw ex;
			}
			try {
				List<String> command = new ArrayList<>(processBuilder.command());
				command.set(0, USR_LOCAL_BIN + command.get(0));
				return processBuilder.command(command).start();
			}
			catch (Exception suppressed) {
				// Suppresses the exception and rethrows the original exception
				ex.addSuppressed(suppressed);
				throw ex;
			}
		}
	}

	private static boolean isCredentialsNotFoundError(String message) {
		return CREDENTIAL_NOT_FOUND_MESSAGES.contains(message.trim());
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free