Skip to content

Commit

Permalink
Merge pull request #45299 from gsmet/tls-minor-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cescoffier authored Dec 29, 2024
2 parents 6ce7dcf + b4c8a3d commit c3ccc13
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Integer call() throws Exception {
LOGGER.log(INFO, "✅ Truststore generated successfully.");
}

LOGGER.log(INFO, "✅ Quarkus Development CA generated and installed");
LOGGER.log(INFO, "✅ Quarkus Dev CA certificate generated and installed");

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.Security;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class GenerateCertificateCommand implements Callable<Integer> {

@CommandLine.Option(names = { "-d",
"--directory" }, description = "The directory in which the certificates will be created. Default is `.certs`", defaultValue = ".certs")
String directory;
Path directory;

@CommandLine.Option(names = { "-r",
"--renew" }, description = "Whether existing certificates will need to be replaced", defaultValue = "false")
Expand Down Expand Up @@ -83,46 +84,46 @@ public Integer call() throws Exception {
createSignedCertificate(caCert, caPrivateKey);

LOGGER.log(INFO, "✅ Signed Certificate generated successfully and exported into `{0}-keystore.p12`", name);
printConfig(new File(directory, name + "-keystore.p12").getAbsolutePath(), password);
printConfig(directory.resolve(name + "-keystore.p12"), password);

return 0;
}

private void generateSelfSignedCertificate() throws Exception {
File out = new File(directory);
if (!out.exists()) {
out.mkdirs();
if (!Files.exists(directory)) {
Files.createDirectories(directory);
}
new CertificateGenerator(out.toPath(), renew).generate(new CertificateRequest()
new CertificateGenerator(directory, renew).generate(new CertificateRequest()
.withName(name)
.withCN(cn)
.withPassword(password)
.withDuration(Duration.ofDays(365))
.withFormat(Format.PKCS12));
LOGGER.log(INFO, "✅ Self-signed certificate generated successfully and exported into `{0}-keystore.p12`", name);
printConfig(new File(directory, name + "-keystore.p12").getAbsolutePath(), password);
printConfig(directory.resolve(name + "-keystore.p12"), password);

}

private void printConfig(String path, String password) {
private void printConfig(Path certificatePath, String password) {
String certificatePathProperty = certificatePath.toString();
if (OS.WINDOWS.isCurrent()) {
path = path.replace("\\", "\\\\");
certificatePathProperty = certificatePathProperty.replace("\\", "\\\\");
}

try {
List<String> dotEnvContent = readDotEnvFile();
addOrReplaceProperty(dotEnvContent, "%dev.quarkus.tls.key-store.p12.path", path);
addOrReplaceProperty(dotEnvContent, "%dev.quarkus.tls.key-store.p12.path", certificatePathProperty);
addOrReplaceProperty(dotEnvContent, "%dev.quarkus.tls.key-store.p12.password", password);
Files.write(DOT_ENV_FILE.toPath(), dotEnvContent);
} catch (IOException e) {
LOGGER.log(ERROR, "Failed to read .env file", e);
}

LOGGER.log(INFO, """
✅ Required configuration added to the `.env` file:
%dev.quarkus.tls.key-store.p12.path={0}
%dev.quarkus.tls.key-store.p12.password={1}
""", path, password);
✅ Required configuration added to the `.env` file:
%dev.quarkus.tls.key-store.p12.path={0}
%dev.quarkus.tls.key-store.p12.password={1}
""", certificatePathProperty, password);
}

private X509Certificate loadRootCertificate(File ca) throws Exception {
Expand Down Expand Up @@ -151,11 +152,10 @@ private PrivateKey loadPrivateKey() throws Exception {

private void createSignedCertificate(X509Certificate issuerCert,
PrivateKey issuerPrivateKey) throws Exception {
File out = new File(directory);
if (!out.exists()) {
out.mkdirs();
if (!Files.exists(directory)) {
Files.createDirectories(directory);
}
new CertificateGenerator(out.toPath(), renew).generate(new CertificateRequest()
new CertificateGenerator(directory, renew).generate(new CertificateRequest()
.withName(name)
.withCN(cn)
.withPassword(password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Path;
import java.security.KeyStore;

import org.junit.jupiter.api.AfterAll;
Expand All @@ -25,7 +26,7 @@ public void testSelfSignedGeneration() throws Exception {
command.name = "test";
command.renew = true;
command.selfSigned = true;
command.directory = "target";
command.directory = Path.of("target");
command.password = "password";
command.call();

Expand Down

0 comments on commit c3ccc13

Please sign in to comment.