diff --git a/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCACommand.java b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCACommand.java index eb612b66ca029..cc62bcabb27d2 100644 --- a/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCACommand.java +++ b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCACommand.java @@ -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; } diff --git a/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCertificateCommand.java b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCertificateCommand.java index 437b2ab07f69b..8ce6dec5fce79 100644 --- a/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCertificateCommand.java +++ b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/GenerateCertificateCommand.java @@ -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; @@ -50,7 +51,7 @@ public class GenerateCertificateCommand implements Callable { @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") @@ -83,35 +84,35 @@ 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 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) { @@ -119,10 +120,10 @@ private void printConfig(String path, String password) { } 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 { @@ -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) diff --git a/extensions/tls-registry/cli/src/test/java/io/quarkus/tls/cli/SelfSignedGenerationTest.java b/extensions/tls-registry/cli/src/test/java/io/quarkus/tls/cli/SelfSignedGenerationTest.java index 6db2867cc3924..50563f61848a3 100644 --- a/extensions/tls-registry/cli/src/test/java/io/quarkus/tls/cli/SelfSignedGenerationTest.java +++ b/extensions/tls-registry/cli/src/test/java/io/quarkus/tls/cli/SelfSignedGenerationTest.java @@ -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; @@ -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();