From df61041682a96d8676b526dd22d8651a580cc58a Mon Sep 17 00:00:00 2001 From: iroqueta Date: Wed, 31 Jul 2024 15:23:20 -0300 Subject: [PATCH 1/2] Allow use user and password encrypted in Datasource definition To use this feature DataSource definition must include factory property with value "com.genexus.db.EncryptedDataSourceFactory" and gxcfg property with value ".GXcfg" SAC #60026 --- .../main/java/com/genexus/util/IniFile.java | 4 ++ java/pom.xml | 5 ++ .../db/EncryptedDataSourceFactory.java | 53 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 java/src/main/java/com/genexus/db/EncryptedDataSourceFactory.java diff --git a/common/src/main/java/com/genexus/util/IniFile.java b/common/src/main/java/com/genexus/util/IniFile.java index 9c4e38a00..ea30da1da 100644 --- a/common/src/main/java/com/genexus/util/IniFile.java +++ b/common/src/main/java/com/genexus/util/IniFile.java @@ -245,6 +245,10 @@ public void setPropertyEncrypted(String section, String key, String value) { public String getPropertyEncrypted(String section, String key) { String val = getProperty(section, key); + return decryptValue(val, key); + } + + public String decryptValue(String val, String key) { if (val != null) { int checkSumLength = Encryption.getCheckSumLength(); diff --git a/java/pom.xml b/java/pom.xml index 50b250f30..a9f43b333 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -169,6 +169,11 @@ opentelemetry-instrumentation-annotations ${io.opentelemetry.version} + + org.apache.commons + commons-dbcp2 + 2.9.0 + diff --git a/java/src/main/java/com/genexus/db/EncryptedDataSourceFactory.java b/java/src/main/java/com/genexus/db/EncryptedDataSourceFactory.java new file mode 100644 index 000000000..29ea3d130 --- /dev/null +++ b/java/src/main/java/com/genexus/db/EncryptedDataSourceFactory.java @@ -0,0 +1,53 @@ +package com.genexus.db; + +import com.genexus.specific.java.Connect; +import com.genexus.util.IniFile; +import org.apache.commons.dbcp2.BasicDataSourceFactory; + +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.Reference; +import javax.naming.spi.ObjectFactory; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Properties; + +public class EncryptedDataSourceFactory implements ObjectFactory { + + private final static String USERNAME = "username"; + private final static String PASSWORD = "password"; + + @Override + public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { + if (obj instanceof Reference) { + Reference ref = (Reference) obj; + Properties properties = new Properties(); + + Enumeration addresses = ref.getAll(); + while (addresses.hasMoreElements()) { + javax.naming.RefAddr addr = addresses.nextElement(); + properties.setProperty(addr.getType(), (String) addr.getContent()); + } + + IniFile config; + try { + Class gxCfg = Class.forName(properties.getProperty("gxcfg")); + Connect.init(); + config = com.genexus.ConfigFileFinder.getConfigFile(null, "client.cfg", gxCfg); + } + catch (Exception e) { + System.out.println("ERROR com.genexus.db.EncryptedDataSourceFactory - Could not found gxcfg Class"); + return null; + } + + String encryptedUsername = properties.getProperty(USERNAME); + properties.setProperty(USERNAME, config.decryptValue(encryptedUsername, "USER_ID")); + + String encryptedPassword = properties.getProperty(PASSWORD); + properties.setProperty(PASSWORD, config.decryptValue(encryptedPassword, "USER_PASSWORD")); + + return BasicDataSourceFactory.createDataSource(properties); + } + return null; + } +} \ No newline at end of file From 20283a610412d44cd9feb2b45e0be1547fbe54ac Mon Sep 17 00:00:00 2001 From: iroqueta Date: Wed, 31 Jul 2024 16:12:35 -0300 Subject: [PATCH 2/2] Remove comment --- java/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/pom.xml b/java/pom.xml index a9f43b333..82af66eff 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -172,7 +172,7 @@ org.apache.commons commons-dbcp2 - 2.9.0 + 2.9.0