Skip to content

Commit

Permalink
TLS - Prevent Duplicate Entries in .env File
Browse files Browse the repository at this point in the history
Previously, when generating local certificates, running the command multiple times could result in duplicate lines being written to the `.env` file. This commit resolves the issue by implementing the same mechanism used in the Let's Encrypt commands, ensuring that duplicate entries are avoided.
  • Loading branch information
cescoffier committed Aug 26, 2024
1 parent 2105d46 commit 1aa9508
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

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;

import java.io.BufferedReader;
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;
Expand Down Expand Up @@ -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<String> 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, """
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String> dotEnvContent = readDotEnvFile();
Expand All @@ -97,29 +96,4 @@ public Integer call() throws Exception {
return 0;
}

List<String> readDotEnvFile() throws IOException {
if (!DOT_ENV_FILE.exists()) {
return new ArrayList<>();
}
return new ArrayList<>(Files.readAllLines(DOT_ENV_FILE.toPath()));
}

void addOrReplaceProperty(List<String> 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<String> 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;
}

}

0 comments on commit 1aa9508

Please sign in to comment.