test: Fix test db setup and add lombok annotations #6

Merged
torge-hmn merged 3 commits from test-setup into main 2024-11-17 20:30:50 +00:00
6 changed files with 26 additions and 41 deletions

View file

@ -1,20 +1,17 @@
package de.mlessmann.certassist; package de.mlessmann.certassist;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor; import java.nio.file.FileVisitor;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import org.slf4j.Logger; import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@Slf4j
public class DeleteRecursiveFileVisitor implements FileVisitor<Path> { public class DeleteRecursiveFileVisitor implements FileVisitor<Path> {
private static final Logger LOGGER = getLogger(DeleteRecursiveFileVisitor.class);
@NonNull @NonNull
@Override @Override
public FileVisitResult preVisitDirectory(Path dir, @NonNull BasicFileAttributes attrs) throws IOException { public FileVisitResult preVisitDirectory(Path dir, @NonNull BasicFileAttributes attrs) throws IOException {
@ -24,7 +21,7 @@ public class DeleteRecursiveFileVisitor implements FileVisitor<Path> {
@NonNull @NonNull
@Override @Override
public FileVisitResult visitFile(Path file, @NonNull BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, @NonNull BasicFileAttributes attrs) throws IOException {
LOGGER.trace("Deleting file {}", file); log.trace("Deleting file {}", file);
Files.delete(file); Files.delete(file);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
@ -32,14 +29,14 @@ public class DeleteRecursiveFileVisitor implements FileVisitor<Path> {
@NonNull @NonNull
@Override @Override
public FileVisitResult visitFileFailed(Path file, @NonNull IOException exc) throws IOException { public FileVisitResult visitFileFailed(Path file, @NonNull IOException exc) throws IOException {
LOGGER.error("Could not delete file {}", file, exc); log.error("Could not delete file {}", file, exc);
throw exc; throw exc;
} }
@NonNull @NonNull
@Override @Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
LOGGER.trace("Deleting directory {}", dir); log.trace("Deleting directory {}", dir);
Files.delete(dir); Files.delete(dir);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }

View file

@ -1,7 +1,5 @@
package de.mlessmann.certassist; package de.mlessmann.certassist;
import static org.slf4j.LoggerFactory.getLogger;
import de.mlessmann.certassist.except.UnresolvableCLIDependency; import de.mlessmann.certassist.except.UnresolvableCLIDependency;
import java.io.File; import java.io.File;
import java.nio.file.Files; import java.nio.file.Files;
@ -9,22 +7,21 @@ import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@Slf4j
public class ExecutableResolver { public class ExecutableResolver {
private static final Logger LOGGER = getLogger(ExecutableResolver.class);
@Value("${openssl.path:#{null}}") @Value("${openssl.path:#{null}}")
private String opensslPath; private String opensslPath;
public String getOpenSSLPath() throws UnresolvableCLIDependency { public String getOpenSSLPath() throws UnresolvableCLIDependency {
if (opensslPath == null) { if (opensslPath == null) {
LOGGER.atDebug().log("No openssl path configured, falling back to resolving by shell."); log.atDebug().log("No openssl path configured, falling back to resolving by shell.");
var optSSLPath = searchCommandFromPath("openssl"); var optSSLPath = searchCommandFromPath("openssl");
opensslPath = optSSLPath.orElseThrow(() -> new UnresolvableCLIDependency("openssl", "openssl.path")); opensslPath = optSSLPath.orElseThrow(() -> new UnresolvableCLIDependency("openssl", "openssl.path"));
} }
@ -51,7 +48,7 @@ public class ExecutableResolver {
} }
} }
LOGGER.error( log.error(
"Could not find executable '{}' in PATH. Make sure that it exists on the of the directory and is executable.", "Could not find executable '{}' in PATH. Make sure that it exists on the of the directory and is executable.",
executableName executableName
); );

View file

@ -1,7 +1,5 @@
package de.mlessmann.certassist.openssl; package de.mlessmann.certassist.openssl;
import static org.slf4j.LoggerFactory.getLogger;
import de.mlessmann.certassist.ExecutableResolver; import de.mlessmann.certassist.ExecutableResolver;
import de.mlessmann.certassist.except.CommandLineOperationException; import de.mlessmann.certassist.except.CommandLineOperationException;
import de.mlessmann.certassist.except.UnresolvableCLIDependency; import de.mlessmann.certassist.except.UnresolvableCLIDependency;
@ -13,9 +11,9 @@ import java.nio.file.StandardOpenOption;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.zeroturnaround.exec.ProcessExecutor; import org.zeroturnaround.exec.ProcessExecutor;
@ -23,11 +21,12 @@ import org.zeroturnaround.exec.StartedProcess;
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream; import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;
@Service @Service
@RequiredArgsConstructor
@Slf4j
public class OpenSSLCertificateCreator { public class OpenSSLCertificateCreator {
public static final String OPENSSL_CERT_SUBJECT_TEMPLATE = public static final String OPENSSL_CERT_SUBJECT_TEMPLATE =
"/C=ISO-COUNTRY/ST=STATE/L=LOCALITY/O=ORGANIZATION/CN=COMMON-NAME"; "/C=ISO-COUNTRY/ST=STATE/L=LOCALITY/O=ORGANIZATION/CN=COMMON-NAME";
private static final Logger LOGGER = getLogger(OpenSSLCertificateCreator.class);
private static final String CSR_EXT_TEMPLATE = private static final String CSR_EXT_TEMPLATE =
""" """
authorityKeyIdentifier=keyid,issuer authorityKeyIdentifier=keyid,issuer
@ -40,11 +39,6 @@ public class OpenSSLCertificateCreator {
private final ExecutableResolver executableResolver; private final ExecutableResolver executableResolver;
@Autowired
public OpenSSLCertificateCreator(ExecutableResolver executableResolver) {
this.executableResolver = executableResolver;
}
private static String buildSubjectArg(CertificateRequest request) { private static String buildSubjectArg(CertificateRequest request) {
String certSubject = OPENSSL_CERT_SUBJECT_TEMPLATE String certSubject = OPENSSL_CERT_SUBJECT_TEMPLATE
.replace("ISO-COUNTRY", request.getSubject().getCountry()) .replace("ISO-COUNTRY", request.getSubject().getCountry())
@ -85,7 +79,7 @@ public class OpenSSLCertificateCreator {
private Path createKeyfile(CertificateRequest request, Path outFile) private Path createKeyfile(CertificateRequest request, Path outFile)
throws CommandLineOperationException, InterruptedException { throws CommandLineOperationException, InterruptedException {
Path keyFile = outFile.toAbsolutePath(); Path keyFile = outFile.toAbsolutePath();
LOGGER.atDebug().log("Writing new certificate key to {}", keyFile); log.atDebug().log("Writing new certificate key to {}", keyFile);
try { try {
StartedProcess keygenProc = new ProcessExecutor() StartedProcess keygenProc = new ProcessExecutor()
@ -113,7 +107,7 @@ public class OpenSSLCertificateCreator {
private Path createCertificate(CertificateRequest request, Path keyFile, Path outFile) private Path createCertificate(CertificateRequest request, Path keyFile, Path outFile)
throws CommandLineOperationException, InterruptedException { throws CommandLineOperationException, InterruptedException {
LOGGER.atDebug().log("Writing new certificate file {}", outFile); log.atDebug().log("Writing new certificate file {}", outFile);
String certSubject = buildSubjectArg(request); String certSubject = buildSubjectArg(request);
try { try {
@ -153,7 +147,7 @@ public class OpenSSLCertificateCreator {
private Path signCertificate(CertificateRequest request, Path caCert, Path caKey, Path csrFile) private Path signCertificate(CertificateRequest request, Path caCert, Path caKey, Path csrFile)
throws CommandLineOperationException, InterruptedException { throws CommandLineOperationException, InterruptedException {
Path outFile = csrFile.resolveSibling(csrFile.getFileName().toString().replace(".csr", ".crt")); Path outFile = csrFile.resolveSibling(csrFile.getFileName().toString().replace(".csr", ".crt"));
LOGGER.atDebug().log("Writing new signed certificate file {}", outFile); log.atDebug().log("Writing new signed certificate file {}", outFile);
Path extFile = csrFile.resolveSibling(csrFile.getFileName().toString().replace(".csr", ".ext")); Path extFile = csrFile.resolveSibling(csrFile.getFileName().toString().replace(".csr", ".ext"));
try { try {
@ -170,7 +164,7 @@ public class OpenSSLCertificateCreator {
extContent = extContent.replaceAll("\\[alt_names]\n?, ", ""); extContent = extContent.replaceAll("\\[alt_names]\n?, ", "");
} }
LOGGER.debug("Writing extension file content: \n {}", extContent); log.debug("Writing extension file content: \n {}", extContent);
Files.writeString( Files.writeString(
extFile, extFile,
extContent, extContent,

View file

@ -1,27 +1,23 @@
package de.mlessmann.certassist.openssl; package de.mlessmann.certassist.openssl;
import static org.slf4j.LoggerFactory.getLogger;
import de.mlessmann.certassist.DeleteRecursiveFileVisitor; import de.mlessmann.certassist.DeleteRecursiveFileVisitor;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger; import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class OpenSSLCertificateResult implements AutoCloseable { public class OpenSSLCertificateResult implements AutoCloseable {
private static final Logger LOGGER = getLogger(OpenSSLCertificateResult.class);
private final Path tmpDir; private final Path tmpDir;
OpenSSLCertificateResult(Path tmpDir) {
this.tmpDir = tmpDir;
}
@Override @Override
public void close() throws IOException { public void close() throws IOException {
LOGGER.info("Cleaning up temporary output directory {}", tmpDir); log.info("Cleaning up temporary output directory {}", tmpDir);
Files.walkFileTree(tmpDir, Set.of(), Integer.MAX_VALUE, new DeleteRecursiveFileVisitor()); Files.walkFileTree(tmpDir, Set.of(), Integer.MAX_VALUE, new DeleteRecursiveFileVisitor());
Files.deleteIfExists(tmpDir); Files.deleteIfExists(tmpDir);
} }

View file

@ -1,2 +1,3 @@
#TODO: Use flyway for db setup #TODO: Use flyway for db setup
hibernate.hbm2ddl.auto=create-drop hibernate.hbm2ddl.auto=create-drop
url=jdbc:sqlite:build/sqLiteTestDb.db

View file

@ -8,7 +8,7 @@ import de.mlessmann.certassist.openssl.OpenSSLCertificateCreator;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class TestOpenSSLCertificateCreator { class TestOpenSSLCertificateCreator {
private OpenSSLCertificateCreator openSSLCertificateCreator; private OpenSSLCertificateCreator openSSLCertificateCreator;