🚧 Fix issue where cert cleanup fails
- Delete temp directory using FileTree visitor recursively - Update CertificateRequestBuilder to accept subject info directly from builder
This commit is contained in:
parent
98a6556bf9
commit
b5571aa2e5
6 changed files with 124 additions and 87 deletions
|
@ -0,0 +1,47 @@
|
|||
package de.mlessmann.certassist;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
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 static org.slf4j.LoggerFactory.getLogger;
|
||||
|
||||
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 {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, @NonNull BasicFileAttributes attrs) throws IOException {
|
||||
LOGGER.trace("Deleting file {}", file);
|
||||
Files.delete(file);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public FileVisitResult visitFileFailed(Path file, @NonNull IOException exc) throws IOException {
|
||||
LOGGER.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);
|
||||
Files.delete(dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
}
|
|
@ -2,11 +2,10 @@ package de.mlessmann.certassist.except;
|
|||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class UnresolvableCLIDependency extends Exception {
|
||||
|
||||
@Getter
|
||||
private final String executableName;
|
||||
@Getter
|
||||
private final String propertyName;
|
||||
|
||||
public UnresolvableCLIDependency(String executableName, String propertyName) {
|
||||
|
|
|
@ -1,50 +1,36 @@
|
|||
package de.mlessmann.certassist.openssl;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class CertificateRequest {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder.Default
|
||||
private String oid = UUID.randomUUID().toString();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private RequestType type;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String commonName;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String trustingAuthority;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder.Default
|
||||
private int requestedKeyLength = 4096;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder.Default
|
||||
private int requestedValidityDays = 365;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder.Default
|
||||
private CertificateSubject subject = CertificateSubject.builder().build();
|
||||
private CertificateSubject subject;
|
||||
|
||||
public enum RequestType {
|
||||
ROOT_AUTHORITY,
|
||||
STANDALONE_CERTIFICATE,
|
||||
NORMAL_CERTIFICATE
|
||||
}
|
||||
|
||||
public static class CertificateRequestBuilder {
|
||||
public CertificateRequestBuilder subject(CertificateSubject.CertificateSubjectBuilder builder) {
|
||||
this.subject = builder.build();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ public class OpenSSLCertificateCreator {
|
|||
|
||||
String certSubject = buildSubjectArg(request);
|
||||
try {
|
||||
StartedProcess keygenProc = new ProcessExecutor().command(resolveOpenSSL(), "req", "x509", "-new", "-nodes",
|
||||
StartedProcess keygenProc = new ProcessExecutor().command(resolveOpenSSL(), "req", "-new", "-nodes",
|
||||
"-key", keyFile.toString(), "-sha256", "-days",
|
||||
Integer.toString(
|
||||
request.getRequestedValidityDays()),
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package de.mlessmann.certassist.openssl;
|
||||
|
||||
import de.mlessmann.certassist.DeleteRecursiveFileVisitor;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.slf4j.LoggerFactory.getLogger;
|
||||
|
||||
|
@ -21,6 +23,6 @@ public class OpenSSLCertificateResult implements AutoCloseable {
|
|||
@Override
|
||||
public void close() throws IOException {
|
||||
LOGGER.info("Cleaning up temporary output directory {}", tmpDir);
|
||||
Files.deleteIfExists(tmpDir);
|
||||
Files.walkFileTree(tmpDir, Set.of(), Integer.MAX_VALUE, new DeleteRecursiveFileVisitor());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package de.mlessmann.certassist;
|
|||
|
||||
import de.mlessmann.certassist.openssl.CertificateRequest;
|
||||
import de.mlessmann.certassist.openssl.CertificateRequest.RequestType;
|
||||
import de.mlessmann.certassist.openssl.CertificateSubject;
|
||||
import de.mlessmann.certassist.openssl.OpenSSLCertificateCreator;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -21,6 +22,8 @@ public class TestOpenSSLCertificateCreator {
|
|||
CertificateRequest certRequest = CertificateRequest.builder()
|
||||
.commonName("test.home")
|
||||
.type(RequestType.STANDALONE_CERTIFICATE)
|
||||
.subject(CertificateSubject.builder().country("DE").state("SH")
|
||||
.locality("").organization("Crazy-Cats"))
|
||||
.build();
|
||||
|
||||
try (var cert = openSSLCertificateCreator.createCertificate(certRequest)) {
|
||||
|
|
Loading…
Add table
Reference in a new issue