chore: Add version logging for OpenSSL

This commit is contained in:
Magnus Leßmann (@MarkL4YG) 2024-11-22 20:27:50 +01:00
parent f2ed523285
commit d271be988f
4 changed files with 58 additions and 33 deletions

View file

@ -3,7 +3,6 @@ package de.mlessmann.certassist.openssl;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import lombok.Builder;
import lombok.Getter;
@ -17,11 +16,13 @@ public class CertificateRequestExtension {
public CertificateRequestExtensionBuilder alternativeNames(String... altNames) {
Objects.requireNonNull(altNames, "Alternative names must not be null (but can be empty)");
this.alternativeNames = Stream.of(altNames)
.filter(Objects::nonNull)
.map(String::trim)
.map(name -> name.replaceAll("^DNS:", ""))
.toList();
this.alternativeNames =
Stream
.of(altNames)
.filter(Objects::nonNull)
.map(String::trim)
.map(name -> name.replaceAll("^DNS:", ""))
.toList();
return this;
}
}

View file

@ -16,6 +16,7 @@ import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -49,6 +50,7 @@ public class OpenSSLCertificateCreator {
private static final Pattern FINGERPRINT_EXTRACTOR = Pattern.compile(
"^(?<algo>[0-9a-zA-Z]+) (?i)Fingerprint(?-i)=(?<finger>[a-z:A-Z0-9]+)"
);
private final AtomicBoolean versionLogged = new AtomicBoolean(false);
private final ExecutableResolver executableResolver;
private final CertificatePasswordProvider passwordProvider;
@ -426,7 +428,7 @@ public class OpenSSLCertificateCreator {
if (infoResult.getExitValue() != 0) {
log.debug("Certificate info command output:\n{}", output);
throw new CommandLineOperationException(
"Failed to get info of path. Exit code: %d".formatted(infoResult.getExitValue())
"Failed to get info of path. Exit code: %d".formatted(infoResult.getExitValue())
);
}
return getCertificateInfo(output.lines().toArray(String[]::new));
@ -437,7 +439,28 @@ public class OpenSSLCertificateCreator {
private String resolveOpenSSL() throws CommandLineOperationException {
try {
return executableResolver.getOpenSSLPath();
String path = executableResolver.getOpenSSLPath();
if (!versionLogged.get()) {
try {
StartedProcess versionProc = new ProcessExecutor()
.command(path, "version")
.readOutput(true)
.redirectError(Slf4jStream.ofCaller().asError())
.start();
var versionResult = versionProc.getFuture().get();
if (versionResult.getExitValue() != 0) {
throw new CommandLineOperationException(
"Failed to get OpenSSL version. Exit code: " + versionResult.getExitValue()
);
}
log.info("Using OpenSSL version: {}", versionResult.getOutput().getUTF8());
versionLogged.set(true);
} catch (IOException | InterruptedException | ExecutionException e) {
throw new CommandLineOperationException("Failed to get OpenSSL version", e);
}
}
return path;
} catch (UnresolvableCLIDependency e) {
throw new CommandLineOperationException(e);
}