Skip to content

Commit 2ef733a

Browse files
committed
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 "<java_package_name>.GXcfg" SAC #60026 (cherry picked from commit df61041)
1 parent 7972276 commit 2ef733a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

common/src/main/java/com/genexus/util/IniFile.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ public void setPropertyEncrypted(String section, String key, String value) {
245245

246246
public String getPropertyEncrypted(String section, String key) {
247247
String val = getProperty(section, key);
248+
return decryptValue(val, key);
249+
}
250+
251+
public String decryptValue(String val, String key) {
248252
if (val != null) {
249253
int checkSumLength = Encryption.getCheckSumLength();
250254

java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@
165165
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
166166
<version>${io.opentelemetry.version}</version>
167167
</dependency>
168+
<dependency>
169+
<groupId>org.apache.commons</groupId>
170+
<artifactId>commons-dbcp2</artifactId>
171+
<version>2.9.0</version> <!-- Verifica la última versión disponible -->
172+
</dependency>
168173
</dependencies>
169174

170175
<build>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.genexus.db;
2+
3+
import com.genexus.specific.java.Connect;
4+
import com.genexus.util.IniFile;
5+
import org.apache.commons.dbcp2.BasicDataSourceFactory;
6+
7+
import javax.naming.Context;
8+
import javax.naming.Name;
9+
import javax.naming.Reference;
10+
import javax.naming.spi.ObjectFactory;
11+
import java.util.Enumeration;
12+
import java.util.Hashtable;
13+
import java.util.Properties;
14+
15+
public class EncryptedDataSourceFactory implements ObjectFactory {
16+
17+
private final static String USERNAME = "username";
18+
private final static String PASSWORD = "password";
19+
20+
@Override
21+
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
22+
if (obj instanceof Reference) {
23+
Reference ref = (Reference) obj;
24+
Properties properties = new Properties();
25+
26+
Enumeration<javax.naming.RefAddr> addresses = ref.getAll();
27+
while (addresses.hasMoreElements()) {
28+
javax.naming.RefAddr addr = addresses.nextElement();
29+
properties.setProperty(addr.getType(), (String) addr.getContent());
30+
}
31+
32+
IniFile config;
33+
try {
34+
Class<?> gxCfg = Class.forName(properties.getProperty("gxcfg"));
35+
Connect.init();
36+
config = com.genexus.ConfigFileFinder.getConfigFile(null, "client.cfg", gxCfg);
37+
}
38+
catch (Exception e) {
39+
System.out.println("ERROR com.genexus.db.EncryptedDataSourceFactory - Could not found gxcfg Class");
40+
return null;
41+
}
42+
43+
String encryptedUsername = properties.getProperty(USERNAME);
44+
properties.setProperty(USERNAME, config.decryptValue(encryptedUsername, "USER_ID"));
45+
46+
String encryptedPassword = properties.getProperty(PASSWORD);
47+
properties.setProperty(PASSWORD, config.decryptValue(encryptedPassword, "USER_PASSWORD"));
48+
49+
return BasicDataSourceFactory.createDataSource(properties);
50+
}
51+
return null;
52+
}
53+
}

0 commit comments

Comments
 (0)