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

* Fix test db setup and add lombok annotations

* Formatting
This commit is contained in:
Torge Hamann 2024-11-17 21:30:50 +01:00 committed by GitHub
parent c3da0eff5c
commit a2aea580f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 26 additions and 41 deletions

View file

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

View file

@ -1,7 +1,5 @@
package de.mlessmann.certassist;
import static org.slf4j.LoggerFactory.getLogger;
import de.mlessmann.certassist.except.UnresolvableCLIDependency;
import java.io.File;
import java.nio.file.Files;
@ -9,22 +7,21 @@ import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class ExecutableResolver {
private static final Logger LOGGER = getLogger(ExecutableResolver.class);
@Value("${openssl.path:#{null}}")
private String opensslPath;
public String getOpenSSLPath() throws UnresolvableCLIDependency {
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");
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.",
executableName
);

View file

@ -1,7 +1,5 @@
package de.mlessmann.certassist.openssl;
import static org.slf4j.LoggerFactory.getLogger;
import de.mlessmann.certassist.ExecutableResolver;
import de.mlessmann.certassist.except.CommandLineOperationException;
import de.mlessmann.certassist.except.UnresolvableCLIDependency;
@ -13,9 +11,9 @@ import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.stereotype.Service;
import org.zeroturnaround.exec.ProcessExecutor;
@ -23,11 +21,12 @@ import org.zeroturnaround.exec.StartedProcess;
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;
@Service
@RequiredArgsConstructor
@Slf4j
public class OpenSSLCertificateCreator {
public static final String OPENSSL_CERT_SUBJECT_TEMPLATE =
"/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 =
"""
authorityKeyIdentifier=keyid,issuer
@ -40,11 +39,6 @@ public class OpenSSLCertificateCreator {
private final ExecutableResolver executableResolver;
@Autowired
public OpenSSLCertificateCreator(ExecutableResolver executableResolver) {
this.executableResolver = executableResolver;
}
private static String buildSubjectArg(CertificateRequest request) {
String certSubject = OPENSSL_CERT_SUBJECT_TEMPLATE
.replace("ISO-COUNTRY", request.getSubject().getCountry())
@ -85,7 +79,7 @@ public class OpenSSLCertificateCreator {
private Path createKeyfile(CertificateRequest request, Path outFile)
throws CommandLineOperationException, InterruptedException {
Path keyFile = outFile.toAbsolutePath();
LOGGER.atDebug().log("Writing new certificate key to {}", keyFile);
log.atDebug().log("Writing new certificate key to {}", keyFile);
try {
StartedProcess keygenProc = new ProcessExecutor()
@ -113,7 +107,7 @@ public class OpenSSLCertificateCreator {
private Path createCertificate(CertificateRequest request, Path keyFile, Path outFile)
throws CommandLineOperationException, InterruptedException {
LOGGER.atDebug().log("Writing new certificate file {}", outFile);
log.atDebug().log("Writing new certificate file {}", outFile);
String certSubject = buildSubjectArg(request);
try {
@ -153,7 +147,7 @@ public class OpenSSLCertificateCreator {
private Path signCertificate(CertificateRequest request, Path caCert, Path caKey, Path csrFile)
throws CommandLineOperationException, InterruptedException {
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"));
try {
@ -170,7 +164,7 @@ public class OpenSSLCertificateCreator {
extContent = extContent.replaceAll("\\[alt_names]\n?, ", "");
}
LOGGER.debug("Writing extension file content: \n {}", extContent);
log.debug("Writing extension file content: \n {}", extContent);
Files.writeString(
extFile,
extContent,

View file

@ -1,27 +1,23 @@
package de.mlessmann.certassist.openssl;
import static org.slf4j.LoggerFactory.getLogger;
import de.mlessmann.certassist.DeleteRecursiveFileVisitor;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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 {
private static final Logger LOGGER = getLogger(OpenSSLCertificateResult.class);
private final Path tmpDir;
OpenSSLCertificateResult(Path tmpDir) {
this.tmpDir = tmpDir;
}
@Override
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.deleteIfExists(tmpDir);
}

View file

@ -1,2 +1,3 @@
#TODO: Use flyway for db setup
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.Test;
public class TestOpenSSLCertificateCreator {
class TestOpenSSLCertificateCreator {
private OpenSSLCertificateCreator openSSLCertificateCreator;