Skip to content

Commit 0c3464f

Browse files
authored
Allow use user and password encrypted in Datasource definition (#887)
* 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 * Remove comment
1 parent 9ec0a68 commit 0c3464f

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
@@ -169,6 +169,11 @@
169169
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
170170
<version>${io.opentelemetry.version}</version>
171171
</dependency>
172+
<dependency>
173+
<groupId>org.apache.commons</groupId>
174+
<artifactId>commons-dbcp2</artifactId>
175+
<version>2.9.0</version>
176+
</dependency>
172177
</dependencies>
173178

174179
<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)