Skip to content

Commit 7892951

Browse files
Revert "attempt to create keystore file via java"
This reverts commit c4e3b08.
1 parent 1c217f6 commit 7892951

File tree

4 files changed

+8
-151
lines changed

4 files changed

+8
-151
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ docker-test:
420420

421421
docker-stop:
422422
docker compose --env-file src/test/resources/docker-env/.env -f src/test/resources/docker-env/docker-compose.yml down; \
423-
rm -rf /tmp/redis-env-work
424423

425424
prepare: stop
426425

pom.xml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -589,25 +589,6 @@
589589
<scope>test</scope>
590590
</dependency>
591591

592-
593-
<!-- Bouncy Castle Provider (Crypto API) -->
594-
<dependency>
595-
<groupId>org.bouncycastle</groupId>
596-
<artifactId>bcprov-jdk18on</artifactId>
597-
<version>1.80</version>
598-
</dependency>
599-
600-
<!-- Bouncy Castle PKIX (X.509 Certificates, PKCS, CSR) -->
601-
<dependency>
602-
<groupId>org.bouncycastle</groupId>
603-
<artifactId>bcpkix-jdk18on</artifactId>
604-
<version>1.80</version>
605-
</dependency>
606-
607-
608-
609-
610-
611592
</dependencies>
612593

613594
<build>

src/test/java/io/lettuce/core/SslIntegrationTests.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ class SslIntegrationTests extends TestSupport {
109109

110110
private final RedisClient redisClient;
111111

112-
private static File keystore;
113-
114112
@Inject
115113
SslIntegrationTests(RedisClient redisClient) {
116114
this.redisClient = redisClient;
@@ -131,13 +129,6 @@ static void beforeClass() {
131129
truststoreFile2 = path2.toFile();
132130
cacertFile = envCa(Paths.get("redis-standalone-sentinel-controlled/work/tls")).toFile();
133131

134-
try {
135-
generateCertificates(testGenCertPath("redis-standalone-0/work/tls").toString(), "redis-standalone-0/work/tls");
136-
} catch (Exception e) {
137-
throw new RuntimeException(e);
138-
}
139-
140-
keystore = Paths.get("redis-standalone-0/work/tls/keystore.jks").toFile();
141132
assumeTrue(CanConnect.to(TestSettings.host(), sslPort()), "Assume that stunnel runs on port 6443");
142133
// Maybe we should do a list.
143134
assertThat(truststoreFile0).exists();
@@ -221,10 +212,10 @@ void standaloneWithJdkSslUsingTruststoreUrl() throws Exception {
221212

222213
@Test
223214
void standaloneWithClientCertificates() {
224-
// 6444
215+
// 6445
225216
SslOptions sslOptions = SslOptions.builder() //
226217
.jdkSslProvider() //
227-
.keystore(keystore, "changeit".toCharArray()) //
218+
.keystore(new File(KEYSTORE), "changeit".toCharArray()) //
228219
.truststore(truststoreFile0, "changeit") //
229220
.build();
230221
setOptions(sslOptions);
Lines changed: 6 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
11
package io.lettuce.test.settings;
22

3-
import io.lettuce.core.internal.LettuceStrings;
4-
import org.testcontainers.shaded.org.bouncycastle.cert.X509v3CertificateBuilder;
5-
import org.testcontainers.shaded.org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
6-
import org.testcontainers.shaded.org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
7-
import org.testcontainers.shaded.org.bouncycastle.operator.ContentSigner;
8-
import org.testcontainers.shaded.org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
9-
import org.testcontainers.shaded.org.bouncycastle.pkcs.PKCS10CertificationRequest;
10-
import org.testcontainers.shaded.org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
11-
import org.testcontainers.shaded.org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest;
12-
import org.testcontainers.shaded.org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
13-
import org.testcontainers.shaded.org.bouncycastle.util.io.pem.PemObject;
14-
import org.testcontainers.shaded.org.bouncycastle.util.io.pem.PemWriter;
15-
import sun.security.x509.X500Name;
16-
17-
import javax.security.auth.x500.X500Principal;
18-
import java.io.*;
19-
import java.math.BigInteger;
20-
import java.nio.file.Files;
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
216
import java.nio.file.Path;
227
import java.nio.file.Paths;
23-
import java.security.*;
8+
import java.security.KeyStore;
9+
import java.security.KeyStoreException;
10+
import java.security.NoSuchAlgorithmException;
2411
import java.security.cert.CertificateException;
2512
import java.security.cert.CertificateFactory;
2613
import java.security.cert.X509Certificate;
27-
import java.time.Duration;
28-
import java.time.Instant;
2914
import java.util.ArrayList;
30-
import java.util.Date;
3115
import java.util.List;
3216
import java.util.UUID;
3317

@@ -43,10 +27,6 @@ public class TlsSettings {
4327

4428
private static final String TEST_TRUSTSTORE = "truststore.jks";
4529

46-
private static final String TEST_KEYSTORE = "keystore.jks";
47-
48-
private static final String PASSWORD = "changeit";
49-
5030
public static Path envServerCert(Path certLocation) {
5131
return Paths.get(TEST_WORK_FOLDER, certLocation.toString(), TEST_SERVER_CERT);
5232
}
@@ -59,14 +39,6 @@ public static Path testTruststorePath(String name) {
5939
return Paths.get(TEST_WORK_FOLDER, name + '-' + TEST_TRUSTSTORE);
6040
}
6141

62-
public static Path testGenCertPath(String keystoreLocation) {
63-
return Paths.get(TEST_WORK_FOLDER, keystoreLocation);
64-
}
65-
66-
public static Path testKeyStorePath(String keystoreLocation) {
67-
return Paths.get(TEST_WORK_FOLDER, keystoreLocation, TEST_KEYSTORE);
68-
}
69-
7042
/**
7143
* Creates an empty truststore.
7244
*
@@ -149,90 +121,4 @@ public static Path createAndSaveTestTruststore(String trustStoreName, Path certi
149121
return createAndSaveTruststore(trustedCertPaths, trustStorePath, truststorePassword);
150122
}
151123

152-
public static void generateCertificates(String caDir, String keystoreFile) throws Exception {
153-
createDirectories(caDir);
154-
155-
KeyPair keyPair = generateKeyPair();
156-
157-
savePrivateKey(keyPair.getPrivate(), caDir);
158-
159-
PKCS10CertificationRequest csr = generateCSR(keyPair);
160-
161-
X509Certificate certificate = signCertificate(csr, keyPair);
162-
163-
saveCertificate(certificate, caDir);
164-
165-
createPKCS12(keyPair.getPrivate(), certificate, keystoreFile);
166-
}
167-
168-
private static void createDirectories(String caDir) throws IOException {
169-
Files.createDirectories(Paths.get(caDir, "private"));
170-
Files.createDirectories(Paths.get(caDir, "certs"));
171-
}
172-
173-
private static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
174-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
175-
keyGen.initialize(2048);
176-
return keyGen.generateKeyPair();
177-
}
178-
179-
private static void savePrivateKey(PrivateKey privateKey, String caDir) throws Exception {
180-
String keyPath = Paths.get(caDir, "private", "client.key.pem").toString();
181-
try (PemWriter pemWriter = new PemWriter(new FileWriter(keyPath))) {
182-
pemWriter.writeObject(new PemObject("PRIVATE KEY", privateKey.getEncoded()));
183-
}
184-
185-
File keyFile = new File(keyPath);
186-
keyFile.setReadable(false, false);
187-
keyFile.setReadable(true, true);
188-
keyFile.setWritable(false, false);
189-
keyFile.setExecutable(false, false);
190-
}
191-
192-
private static PKCS10CertificationRequest generateCSR(KeyPair keyPair) throws Exception {
193-
X500Principal subject = new X500Principal("CN=client,O=lettuce,C=NN,ST=Unknown,L=Unknown");
194-
PKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(subject, keyPair.getPublic());
195-
196-
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA").build(keyPair.getPrivate());
197-
198-
return csrBuilder.build(signer);
199-
}
200-
201-
private static X509Certificate signCertificate(PKCS10CertificationRequest csr, KeyPair keyPair) throws Exception {
202-
org.bouncycastle.asn1.x500.X500Name issuerName = new org.bouncycastle.asn1.x500.X500Name(
203-
"CN=client,O=lettuce,C=NN,ST=Unknown,L=Unknown");
204-
205-
BigInteger serialNumber = BigInteger.valueOf(System.currentTimeMillis());
206-
Instant now = Instant.now();
207-
Date startDate = Date.from(now);
208-
Date endDate = Date.from(now.plus(Duration.ofDays(375)));
209-
210-
JcaPKCS10CertificationRequest jcaCsr = new JcaPKCS10CertificationRequest(csr);
211-
212-
X509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(org.testcontainers.shaded.org.bouncycastle.asn1.x500.X500Name.getInstance(issuerName), serialNumber, startDate, endDate,
213-
jcaCsr.getSubject(), jcaCsr.getPublicKey());
214-
215-
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA").build(keyPair.getPrivate());
216-
217-
return new JcaX509CertificateConverter().getCertificate(certBuilder.build(signer));
218-
}
219-
220-
private static void saveCertificate(X509Certificate certificate, String caDir) throws Exception {
221-
String certPath = Paths.get(caDir, "certs", "client.cert.pem").toString();
222-
try (PemWriter pemWriter = new PemWriter(new FileWriter(certPath))) {
223-
pemWriter.writeObject(new PemObject("CERTIFICATE", certificate.getEncoded()));
224-
}
225-
}
226-
227-
private static void createPKCS12(PrivateKey privateKey, X509Certificate certificate, String keystoreFile) throws Exception {
228-
KeyStore keyStore = KeyStore.getInstance("PKCS12");
229-
keyStore.load(null, null);
230-
231-
keyStore.setKeyEntry("client", privateKey, PASSWORD.toCharArray(), new X509Certificate[] { certificate });
232-
233-
try (OutputStream output = Files.newOutputStream(testKeyStorePath(keystoreFile))) {
234-
keyStore.store(output, PASSWORD.toCharArray());
235-
}
236-
}
237-
238124
}

0 commit comments

Comments
 (0)