You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
4.7 KiB
130 lines
4.7 KiB
package de.superx.spring; |
|
|
|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.IOException; |
|
import java.nio.charset.StandardCharsets; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.security.InvalidKeyException; |
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.HashSet; |
|
import java.util.Map; |
|
import java.util.List; |
|
import java.util.Properties; |
|
import org.apache.log4j.Logger; |
|
import de.superx.util.PathAndFileUtils; |
|
import java.util.regex.Matcher; |
|
import java.util.regex.Pattern; |
|
import java.util.stream.Collectors; |
|
import javax.crypto.Cipher; |
|
|
|
import javax.crypto.BadPaddingException; |
|
import javax.crypto.IllegalBlockSizeException; |
|
import javax.crypto.NoSuchPaddingException; |
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import java.util.Set; |
|
|
|
public class CustomDatabases{ |
|
|
|
private static Logger logger = Logger.getLogger(CustomDatabases.class); |
|
private static SecretKeySpec KEY; |
|
private static Cipher desCipher; |
|
|
|
public static Map<String, Map<String,String>> getCustomConnections() throws |
|
InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{ |
|
Map<String,Map<String,String>> connections = new HashMap<>(); |
|
//loop over all properties and map them to connections |
|
//automatically compute dbconnections from the name property |
|
Properties props = fetchProperties(); |
|
List<String> dbconnections = getDBConnectionsFromNameProperty(props); |
|
final Set<String> propertyNames = props.stringPropertyNames(); |
|
for (String s : dbconnections) { |
|
Map<String,String> properties = new HashMap<>(); |
|
logger.info(String.format("%s connection retrieved from HISinOne", s)); |
|
List<String> keys = propertyNames.stream().filter(it -> it.startsWith("db." + s + ".")).collect(Collectors.toList()); |
|
for (String key : keys) { |
|
String value = props.getProperty(key); |
|
if (key.endsWith("password")) { |
|
value = decryptDbPassword(value.trim()); |
|
} |
|
properties.put(key.substring(key.lastIndexOf('.') + 1), value); |
|
} |
|
connections.put(s, properties); |
|
} |
|
|
|
return connections; |
|
} |
|
|
|
private static List<String> getPropertyValuesByRegex(Properties properties, String regex) { |
|
List<String> values = new ArrayList<>(); |
|
Pattern pattern = Pattern.compile(regex); |
|
|
|
for (String key : properties.stringPropertyNames()) { |
|
Matcher matcher = pattern.matcher(key); |
|
if (matcher.matches()) { |
|
values.add(properties.getProperty(key)); |
|
} |
|
} |
|
return values; |
|
} |
|
|
|
public static List<String> getDBConnectionsFromNameProperty(Properties props){ |
|
ArrayList<String> connection_names = new ArrayList<String>(); |
|
String regex = "db.*.name"; |
|
connection_names = (ArrayList<String>) getPropertyValuesByRegex(props, regex); |
|
|
|
//check for duplicate names |
|
Set<String> seen = new HashSet<>(); |
|
for (String db_name : connection_names) { |
|
if(!seen.add(db_name)) { |
|
logger.warn("Found atleast two databases with the name" + db_name); |
|
} |
|
} |
|
|
|
return connection_names; |
|
} |
|
|
|
private static String decryptDbPassword(String password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { |
|
if(password == null || password.equals("")) { |
|
return null; |
|
}else if(!password.startsWith("sx_des")) { |
|
return password; |
|
} |
|
|
|
if(desCipher == null) { |
|
desCipher = getDesCipher(); |
|
} |
|
|
|
String[] stext = password.substring(7).split("#"); |
|
byte[] bstext = new byte[stext.length]; |
|
for(int i = 0; i<stext.length;i++) { |
|
bstext[i] = Byte.parseByte(stext[i]); |
|
} |
|
desCipher.init(Cipher.DECRYPT_MODE, KEY); |
|
byte[] decrypted = desCipher.doFinal(bstext); |
|
return new String(decrypted, StandardCharsets.UTF_8); |
|
} |
|
|
|
public static void copy_encryption_key(SecretKeySpec key){ |
|
KEY = key; |
|
} |
|
|
|
private static Cipher getDesCipher() throws NoSuchAlgorithmException, NoSuchPaddingException { |
|
return Cipher.getInstance("DES"); |
|
} |
|
|
|
private static Properties fetchProperties(){ |
|
Properties props = new Properties(); |
|
String FileName = "extra_connections.properties"; |
|
File extra_connections_file_path = new File(PathAndFileUtils.getWebinfPath() + File.separator + "conf" + File.separator + FileName); |
|
try (FileInputStream s = new FileInputStream(extra_connections_file_path)){ |
|
props.load(s); |
|
} catch (IOException e1) { |
|
logger.info("Unable to access properties file where extra connections as described: " + e1.getMessage()); |
|
} |
|
|
|
return props; |
|
} |
|
}
|
|
|