chore: Make TestOpenSSLCertificateCreator a spring boot test
This commit is contained in:
parent
dd2e11c8e8
commit
97eea3a20f
2 changed files with 19 additions and 10 deletions
|
@ -88,28 +88,28 @@ public class OpenSSLCertificateCreator {
|
||||||
throw new CommandLineOperationException("Could not create temporary directory for certificate creation", e);
|
throw new CommandLineOperationException("Could not create temporary directory for certificate creation", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
String certPassword = passwordProvider.generateNewPassword();
|
String keypassphrase = passwordProvider.generateNewPassword();
|
||||||
Path keyFile = createKeyfile(request, tmpDir.resolve("certificate.key"), certPassword);
|
Path keyFile = createKeyfile(request, tmpDir.resolve("certificate.key"), keypassphrase);
|
||||||
if (
|
if (
|
||||||
request.getType() == RequestType.ROOT_AUTHORITY || request.getType() == RequestType.STANDALONE_CERTIFICATE
|
request.getType() == RequestType.ROOT_AUTHORITY || request.getType() == RequestType.STANDALONE_CERTIFICATE
|
||||||
) {
|
) {
|
||||||
Path certificate = createCertificate(request, keyFile, tmpDir.resolve("certificate.crt"), certPassword);
|
Path certificate = createCertificate(request, keyFile, tmpDir.resolve("certificate.crt"), keypassphrase);
|
||||||
String fingerprint = getCertificateFingerprint(certificate);
|
String fingerprint = getCertificateFingerprint(certificate);
|
||||||
passwordProvider.setPasswordFor(fingerprint, certPassword);
|
passwordProvider.setPasswordFor(fingerprint, keypassphrase);
|
||||||
return new OpenSSLCertificateResult(tmpDir, certificate, keyFile, certificate, fingerprint);
|
return new OpenSSLCertificateResult(tmpDir, certificate, keyFile, certificate, fingerprint);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (var certAuthority = certificateProvider.requestCertificateUsage(request.getTrustingAuthority())) {
|
try (var certAuthority = certificateProvider.requestCertificateUsage(request.getTrustingAuthority())) {
|
||||||
Path unsignedCert = createSigningRequest(request, keyFile, tmpDir.resolve("child.csr"), certPassword);
|
Path unsignedCert = createSigningRequest(request, keyFile, tmpDir.resolve("child.csr"), keypassphrase);
|
||||||
Path signedCert = signCertificate(
|
Path signedCert = signCertificate(
|
||||||
request,
|
request,
|
||||||
certAuthority.certificatePath(),
|
certAuthority.certificatePath(),
|
||||||
certAuthority.certificateKeyPath(),
|
certAuthority.certificateKeyPath(),
|
||||||
unsignedCert,
|
unsignedCert,
|
||||||
certPassword
|
keypassphrase
|
||||||
);
|
);
|
||||||
String fingerprint = getCertificateFingerprint(signedCert);
|
String fingerprint = getCertificateFingerprint(signedCert);
|
||||||
passwordProvider.setPasswordFor(fingerprint, certPassword);
|
passwordProvider.setPasswordFor(fingerprint, keypassphrase);
|
||||||
|
|
||||||
Path fullchain = tmpDir.resolve("fullchain.pem");
|
Path fullchain = tmpDir.resolve("fullchain.pem");
|
||||||
try {
|
try {
|
||||||
|
@ -129,7 +129,7 @@ public class OpenSSLCertificateCreator {
|
||||||
private Path createKeyfile(CertificateRequest request, Path outFile, String filePassword)
|
private Path createKeyfile(CertificateRequest request, Path outFile, String filePassword)
|
||||||
throws CommandLineOperationException, InterruptedException {
|
throws CommandLineOperationException, InterruptedException {
|
||||||
Path keyFile = outFile.toAbsolutePath();
|
Path keyFile = outFile.toAbsolutePath();
|
||||||
log.atDebug().log("Writing new certificate key to {}", keyFile);
|
log.debug("Writing new certificate key to {}", keyFile);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
StartedProcess keygenProc = new ProcessExecutor()
|
StartedProcess keygenProc = new ProcessExecutor()
|
||||||
|
@ -158,7 +158,7 @@ public class OpenSSLCertificateCreator {
|
||||||
|
|
||||||
private Path createCertificate(CertificateRequest request, Path keyFile, Path outFile, String certPassword)
|
private Path createCertificate(CertificateRequest request, Path keyFile, Path outFile, String certPassword)
|
||||||
throws CommandLineOperationException, InterruptedException {
|
throws CommandLineOperationException, InterruptedException {
|
||||||
log.atDebug().log("Writing new certificate file {}", outFile);
|
log.debug("Writing new certificate file {}", outFile);
|
||||||
|
|
||||||
String certSubject = buildSubjectArg(request);
|
String certSubject = buildSubjectArg(request);
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -9,14 +9,23 @@ import java.nio.file.Path;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
class TestOpenSSLCertificateCreator {
|
class TestOpenSSLCertificateCreator {
|
||||||
|
|
||||||
public static final String TEST_CERT_PASSPHRASE = "ABC-123";
|
public static final String TEST_CERT_PASSPHRASE = "ABC-123";
|
||||||
public static final Path TEST_CERT_PATH = Path.of("src/test/resources/openssl");
|
public static final Path TEST_CERT_PATH = Path.of("src/test/resources/openssl");
|
||||||
public static final String ERR_NOT_ENCRYPTED = "Private key not encrypted";
|
public static final String ERR_NOT_ENCRYPTED = "Private key not encrypted";
|
||||||
public static final String ERR_VERIFY_FAILED = "Certificate verification failed";
|
public static final String ERR_VERIFY_FAILED = "Certificate verification failed";
|
||||||
private CertificatePasswordProvider passwordProvider;
|
|
||||||
|
@Autowired
|
||||||
|
OpenSSLCertificateCreator openSSLCertificateCreator;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
CertificatePasswordProvider passwordProvider;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue