Skip to content

Commit a7241fc

Browse files
committed
Refactor encryption key resolution to rely solely on symmetric_encryption_key system property
1 parent a75c1cd commit a7241fc

3 files changed

Lines changed: 12 additions & 47 deletions

File tree

tomcat-password-encryption/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>it.eng.knowage</groupId>
55
<artifactId>tomcat-password-encryption</artifactId>
66
<version>9.0.0-SNAPSHOT</version>
7-
<name>Archetype - tomcat-password-encryption</name>
7+
<name>tomcat-password-encryption</name>
88
<url>http://maven.apache.org</url>
99

1010
<properties>

tomcat-password-encryption/src/main/java/it/eng/knowage/tomcatpasswordencryption/helper/EncryptOnce.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
33
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
44

5+
import static it.eng.knowage.tomcatpasswordencryption.helper.EncryptedPasswordUtils.ENCRYPTION_KEY_SYSTEM_PROPERTY_NAME;
6+
57
public class EncryptOnce {
68

79
public static void main(String[] args) {
@@ -11,14 +13,14 @@ public static void main(String[] args) {
1113
System.exit(1);
1214
}
1315
String clear = args[0];
14-
String key = EncryptedPasswordUtils.resolveKey();
15-
if (key == null || key.isEmpty()) {
16-
System.err.println("Missing -Dknowage.enc.password.file");
16+
String encryptionKey =System.getProperty(ENCRYPTION_KEY_SYSTEM_PROPERTY_NAME);
17+
if (encryptionKey == null || encryptionKey.isEmpty()) {
18+
System.err.println("Missing decryption key. Provide it via system property symmetric_encryption_key.");
1719
System.exit(2);
1820
}
1921

2022
SimpleStringPBEConfig cfg = new SimpleStringPBEConfig();
21-
cfg.setPassword(key);
23+
cfg.setPassword(encryptionKey);
2224
cfg.setPoolSize("1");
2325
cfg.setStringOutputType("base64");
2426

tomcat-password-encryption/src/main/java/it/eng/knowage/tomcatpasswordencryption/helper/EncryptedPasswordUtils.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
public final class EncryptedPasswordUtils {
1212
private static final String ENCRYPTED_PREFIX = "#encr#";
13+
public static final String ENCRYPTION_KEY_SYSTEM_PROPERTY_NAME = "symmetric_encryption_key";
1314

1415
private EncryptedPasswordUtils() {}
1516

@@ -19,56 +20,18 @@ public static String decrypt(String value) {
1920
return value;
2021
}
2122
String cipherText = value.substring(ENCRYPTED_PREFIX.length());
22-
String password = resolveKey();
23-
if (password == null || password.isEmpty()) {
24-
throw new IllegalStateException("""
25-
Missing decryption key. Provide it via system property knowage.enc.password, " +
26-
"environment variable KNOWAGE_ENC_PASSWORD, or a file at ${catalina.base}/conf/knowageTomcatEncryptedPasswordDatasource " +
27-
"or -Dknowage.enc.password.file=/secure/path
28-
""");
23+
String decryptionKey = System.getProperty(ENCRYPTION_KEY_SYSTEM_PROPERTY_NAME);
24+
if (decryptionKey == null || decryptionKey.isEmpty()) {
25+
throw new IllegalStateException("Missing decryption key. Provide it via system property symmetric_encryption_key");
2926
}
3027

3128
SimpleStringPBEConfig cfg = new SimpleStringPBEConfig();
32-
cfg.setPassword(password);
29+
cfg.setPassword(decryptionKey);
3330
cfg.setPoolSize("1");
3431
cfg.setStringOutputType("base64");
3532

3633
StandardPBEStringEncryptor enc = new StandardPBEStringEncryptor();
3734
enc.setConfig(cfg);
3835
return enc.decrypt(cipherText);
3936
}
40-
41-
public static String resolveKey() {
42-
// Prefer explicit file path via system property
43-
String fileProp = System.getProperty("knowage.enc.password.file");
44-
if (fileProp != null && !fileProp.isEmpty()) {
45-
String fromFile = readFirstLineTrimmed(Path.of(fileProp));
46-
if (fromFile != null && !fromFile.isEmpty()) return fromFile;
47-
}
48-
49-
// Default file under Tomcat conf: ${catalina.base}/conf/passwordEncryptionSecret
50-
String catalinaBase = System.getProperty("catalina.base");
51-
if (catalinaBase != null && !catalinaBase.isEmpty()) {
52-
Path defaultPath = Path.of(catalinaBase, "conf", "knowageTomcatEncryptedPasswordDatasource");
53-
String fromFile = readFirstLineTrimmed(defaultPath);
54-
if (fromFile != null && !fromFile.isEmpty()) return fromFile;
55-
}
56-
57-
return null;
58-
}
59-
60-
private static String readFirstLineTrimmed(Path path) {
61-
try {
62-
if (Files.isRegularFile(path)) {
63-
for (String line : Files.readAllLines(path, StandardCharsets.UTF_8)) {
64-
String trimmed = line.trim();
65-
if (!trimmed.isEmpty()) return trimmed;
66-
}
67-
}
68-
} catch (IOException ignored) {
69-
}
70-
return null;
71-
}
72-
73-
7437
}

0 commit comments

Comments
 (0)