1010
1111public 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