diff --git a/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/DotEnvHelper.java b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/DotEnvHelper.java new file mode 100644 index 0000000000000..5c6b06c385af9 --- /dev/null +++ b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/DotEnvHelper.java @@ -0,0 +1,48 @@ +package io.quarkus.tls.cli; + +import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.DOT_ENV_FILE; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; + +public class DotEnvHelper { + + private DotEnvHelper() { + // Avoid direct instantiation + } + + public static List readDotEnvFile() throws IOException { + if (!DOT_ENV_FILE.exists()) { + return new ArrayList<>(); + } + return new ArrayList<>(Files.readAllLines(DOT_ENV_FILE.toPath())); + } + + public static void addOrReplaceProperty(List content, String key, String value) { + var line = hasLine(content, key); + if (line != -1) { + content.set(line, key + "=" + value); + } else { + content.add(key + "=" + value); + } + } + + private static int hasLine(List content, String key) { + for (int i = 0; i < content.size(); i++) { + if (content.get(i).startsWith(key + "=") || content.get(i).startsWith(key + " =")) { + return i; + } + } + return -1; + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + public static void deleteQuietly(File file) { + if (file.isFile()) { + file.delete(); + } + } +} 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 f570fd8345939..b45fb7e4dbb8b 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 @@ -2,6 +2,9 @@ import static io.quarkus.tls.cli.Constants.CA_FILE; import static io.quarkus.tls.cli.Constants.PK_FILE; +import static io.quarkus.tls.cli.DotEnvHelper.addOrReplaceProperty; +import static io.quarkus.tls.cli.DotEnvHelper.readDotEnvFile; +import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.DOT_ENV_FILE; import static java.lang.System.Logger.Level.ERROR; import static java.lang.System.Logger.Level.INFO; @@ -9,14 +12,15 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; import java.security.KeyPair; import java.security.PrivateKey; import java.security.Security; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.time.Duration; +import java.util.List; import java.util.concurrent.Callable; import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; @@ -105,17 +109,13 @@ private void printConfig(String path, String password) { path = path.replace("\\", "\\\\"); } - // .env format - String env = String.format(""" - _DEV_QUARKUS_TLS_KEY_STORE_P12_PATH=%s - _DEV_QUARKUS_TLS_KEY_STORE_P12_PASSWORD=%s - """, path, password); - - var dotEnvFile = new File(".env"); - try (var writer = new FileWriter(dotEnvFile, dotEnvFile.isFile())) { - writer.write(env); + try { + List dotEnvContent = readDotEnvFile(); + addOrReplaceProperty(dotEnvContent, "%dev.quarkus.tls.key-store.p12.path", path); + 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 write to .env file", e); + LOGGER.log(ERROR, "Failed to read .env file", e); } LOGGER.log(INFO, """ diff --git a/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/letsencrypt/LetsEncryptPrepareCommand.java b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/letsencrypt/LetsEncryptPrepareCommand.java index b6fa4a34050dd..cea61fdd90396 100644 --- a/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/letsencrypt/LetsEncryptPrepareCommand.java +++ b/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/letsencrypt/LetsEncryptPrepareCommand.java @@ -1,15 +1,16 @@ package io.quarkus.tls.cli.letsencrypt; -import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.*; +import static io.quarkus.tls.cli.DotEnvHelper.addOrReplaceProperty; +import static io.quarkus.tls.cli.DotEnvHelper.deleteQuietly; +import static io.quarkus.tls.cli.DotEnvHelper.readDotEnvFile; +import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.CA_FILE; import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.CERT_FILE; import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.DOT_ENV_FILE; import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.KEY_FILE; import static io.quarkus.tls.cli.letsencrypt.LetsEncryptConstants.LETS_ENCRYPT_DIR; -import java.io.IOException; import java.nio.file.Files; import java.time.Duration; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; @@ -72,9 +73,7 @@ public Integer call() throws Exception { } // Delete the CA file, we do not use it. - if (CA_FILE.isFile()) { - CA_FILE.delete(); - } + deleteQuietly(CA_FILE); // Step 3 - Create .env file or append if exists List dotEnvContent = readDotEnvFile(); @@ -97,29 +96,4 @@ public Integer call() throws Exception { return 0; } - List readDotEnvFile() throws IOException { - if (!DOT_ENV_FILE.exists()) { - return new ArrayList<>(); - } - return new ArrayList<>(Files.readAllLines(DOT_ENV_FILE.toPath())); - } - - void addOrReplaceProperty(List content, String key, String value) { - var line = hasLine(content, key); - if (line != -1) { - content.set(line, key + "=" + value); - } else { - content.add(key + "=" + value); - } - } - - private int hasLine(List content, String key) { - for (int i = 0; i < content.size(); i++) { - if (content.get(i).startsWith(key + "=") || content.get(i).startsWith(key + " =")) { - return i; - } - } - return -1; - } - }