Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup test certificate temp files #9707

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ interface Cmd {
public void testGenerateRootCaCertWithDays() throws Exception {

File key = Files.createTempFile("key-", ".key").toFile();
key.deleteOnExit();
File cert = Files.createTempFile("crt-", ".crt").toFile();
cert.deleteOnExit();
File store = Files.createTempFile("crt-", ".p12").toFile();
store.deleteOnExit();
Subject sbj = new Subject.Builder().withCommonName("MyCommonName").withOrganizationName("MyOrganization").build();

((Cmd) () -> ssl.generateSelfSignedCert(key, cert, sbj, 365)).exec();
Expand Down Expand Up @@ -263,15 +266,21 @@ public void testGenerateClientCert() throws Exception {
path.toFile().deleteOnExit();
long fileCount = Files.list(path).count();
File caKey = Files.createTempFile("ca-key-", ".key").toFile();
caKey.deleteOnExit();
File caCert = Files.createTempFile("ca-crt-", ".crt").toFile();
caCert.deleteOnExit();
File store = Files.createTempFile("store-", ".p12").toFile();
store.deleteOnExit();

Subject caSbj = new Subject.Builder().withCommonName("CACommonName").withOrganizationName("CAOrganizationName").build();

File key = Files.createTempFile("key-", ".key").toFile();
key.deleteOnExit();
File csr = Files.createTempFile("csr-", ".csr").toFile();
csr.deleteOnExit();
Subject sbj = new Subject.Builder().withCommonName("MyCommonName").withOrganizationName("MyOrganization").build();
File cert = Files.createTempFile("crt-", ".crt").toFile();
cert.deleteOnExit();

ssl.generateSelfSignedCert(caKey, caCert, caSbj, 365);
doGenerateSignedCert(caKey, caCert, caSbj, key, csr, cert, store, "123456", sbj);
Expand All @@ -290,13 +299,18 @@ public void testGenerateClientCert() throws Exception {
public void testGenerateClientCertWithSubjectAndAltNames() throws Exception {

File caKey = Files.createTempFile("ca-key-", ".key").toFile();
caKey.deleteOnExit();
File caCert = Files.createTempFile("ca-crt-", ".crt").toFile();
caCert.deleteOnExit();
File store = Files.createTempFile("store-", ".p12").toFile();
store.deleteOnExit();

Subject caSbj = new Subject.Builder().withCommonName("CACommonName").withOrganizationName("CAOrganizationName").build();

File key = Files.createTempFile("key-", ".key").toFile();
key.deleteOnExit();
File csr = Files.createTempFile("csr-", ".csr").toFile();
csr.deleteOnExit();
Subject subject = new Subject.Builder()
.withCommonName("MyCommonName")
.withOrganizationName("MyOrganization")
Expand Down Expand Up @@ -375,8 +389,11 @@ public void testRenewSelfSignedCertWithSubject() throws Exception {
public void doRenewSelfSignedCertWithSubject(Subject caSubject) throws Exception {
// First generate a self-signed cert
File caKey = Files.createTempFile("key-", ".key").toFile();
caKey.deleteOnExit();
File originalCert = Files.createTempFile("crt-", ".crt").toFile();
originalCert.deleteOnExit();
File originalStore = Files.createTempFile("crt-", ".p12").toFile();
originalStore.deleteOnExit();

((Cmd) () -> ssl.generateSelfSignedCert(caKey, originalCert, caSubject, 365)).exec();
ssl.addCertToTrustStore(originalCert, "ca", originalStore, "123456");
Expand Down Expand Up @@ -442,8 +459,11 @@ public void testIPv6SANs() throws Exception {

// First generate the CA
File caKey = Files.createTempFile("ca-", ".key").toFile();
caKey.deleteOnExit();
File caCert = Files.createTempFile("ca-", ".crt").toFile();
caCert.deleteOnExit();
File caStore = Files.createTempFile("ca-", ".p12").toFile();
caStore.deleteOnExit();

ssl.generateSelfSignedCert(caKey, caCert, caSubject, 365);
ssl.addCertToTrustStore(caCert, "ca", caStore, "123456");
Expand Down
fvaleri marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,25 @@ private CertAndKey generateCa(OpenSslCertManager certManager, CertificateAuthori
String clusterCaStorePassword = "123456";

Path clusterCaKeyFile = Files.createTempFile("tls", "cluster-ca-key");
clusterCaKeyFile.toFile().deleteOnExit();
Path clusterCaCertFile = Files.createTempFile("tls", "cluster-ca-cert");
clusterCaCertFile.toFile().deleteOnExit();
Path clusterCaStoreFile = Files.createTempFile("tls", "cluster-ca-store");

try {
Subject sbj = new Subject.Builder()
.withOrganizationName("io.strimzi")
.withCommonName(commonName).build();

certManager.generateSelfSignedCert(clusterCaKeyFile.toFile(), clusterCaCertFile.toFile(), sbj, ModelUtils.getCertificateValidity(certificateAuthority));

certManager.addCertToTrustStore(clusterCaCertFile.toFile(), CA_CRT, clusterCaStoreFile.toFile(), clusterCaStorePassword);
return new CertAndKey(
Files.readAllBytes(clusterCaKeyFile),
Files.readAllBytes(clusterCaCertFile),
Files.readAllBytes(clusterCaStoreFile),
null,
clusterCaStorePassword);
} finally {
Files.delete(clusterCaKeyFile);
Files.delete(clusterCaCertFile);
Files.delete(clusterCaStoreFile);
}
clusterCaStoreFile.toFile().deleteOnExit();

Subject sbj = new Subject.Builder()
.withOrganizationName("io.strimzi")
.withCommonName(commonName).build();

certManager.generateSelfSignedCert(clusterCaKeyFile.toFile(), clusterCaCertFile.toFile(), sbj, ModelUtils.getCertificateValidity(certificateAuthority));

certManager.addCertToTrustStore(clusterCaCertFile.toFile(), CA_CRT, clusterCaStoreFile.toFile(), clusterCaStorePassword);
return new CertAndKey(
Files.readAllBytes(clusterCaKeyFile),
Files.readAllBytes(clusterCaCertFile),
Files.readAllBytes(clusterCaStoreFile),
null,
clusterCaStorePassword);
}

private List<Secret> initialClusterCaSecrets(CertificateAuthority certificateAuthority)
Expand Down Expand Up @@ -1108,7 +1105,9 @@ public void testExpiredCertsGetRemovedAuto(Vertx vertx, VertxTestContext context
assertThat(initialClusterCaKeySecret.getData().get(CA_KEY), is(notNullValue()));
// ... and to the related truststore
Path certFile = Files.createTempFile("tls", "-cert");
certFile.toFile().deleteOnExit();
Path trustStoreFile = Files.createTempFile("tls", "-truststore");
trustStoreFile.toFile().deleteOnExit();
Files.write(certFile, Base64.getDecoder().decode(initialClusterCaCertSecret.getData().get("ca-2018-07-01T09-00-00.crt")));
Files.write(trustStoreFile, Base64.getDecoder().decode(initialClusterCaCertSecret.getData().get(CA_STORE)));
String trustStorePassword = new String(Base64.getDecoder().decode(initialClusterCaCertSecret.getData().get(CA_STORE_PASSWORD)), StandardCharsets.US_ASCII);
Expand All @@ -1135,8 +1134,10 @@ public void testExpiredCertsGetRemovedAuto(Vertx vertx, VertxTestContext context

// ... and to the related truststore
certFile = Files.createTempFile("tls", "-cert");
certFile.toFile().deleteOnExit();
Files.write(certFile, Base64.getDecoder().decode(initialClientsCaCertSecret.getData().get("ca-2018-07-01T09-00-00.crt")));
trustStoreFile = Files.createTempFile("tls", "-truststore");
trustStoreFile.toFile().deleteOnExit();
Files.write(trustStoreFile, Base64.getDecoder().decode(initialClientsCaCertSecret.getData().get(CA_STORE)));
trustStorePassword = new String(Base64.getDecoder().decode(initialClientsCaCertSecret.getData().get(CA_STORE_PASSWORD)), StandardCharsets.US_ASCII);
certManager.addCertToTrustStore(certFile.toFile(), "ca-2018-07-01T09-00-00.crt", trustStoreFile.toFile(), trustStorePassword);
Expand Down
Loading
Loading