Skip to content

Commit 5757142

Browse files
authored
Merge pull request #6278 from thc202/authhelper/db-tweks
authhelper: use DB details and close on notify
2 parents 9709a06 + 49311ca commit 5757142

File tree

3 files changed

+119
-30
lines changed

3 files changed

+119
-30
lines changed

addOns/authhelper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
## Unreleased
77
### Changed
88
- Use TOTP data defined under user credentials for Client Script and Browser Based Authentication, when available.
9+
- Maintenance changes.
910

1011
## [0.24.0] - 2025-03-21
1112
### Added

addOns/authhelper/src/main/java/org/zaproxy/addon/authhelper/ExtensionAuthhelper.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public class ExtensionAuthhelper extends ExtensionAdaptor {
110110

111111
private AuthDiagnosticCollector authDiagCollector;
112112
private AuthhelperParam param;
113+
private TableJdo tableJdo;
113114

114115
public ExtensionAuthhelper() {
115116
super();
@@ -196,6 +197,13 @@ public void stop() {
196197
AuthUtils.clean();
197198
}
198199

200+
@Override
201+
public void destroy() {
202+
if (tableJdo != null) {
203+
tableJdo.unload();
204+
}
205+
}
206+
199207
@Override
200208
public void hook(ExtensionHook extensionHook) {
201209
extensionHook.addSessionListener(new AuthSessionChangedListener());
@@ -382,9 +390,7 @@ public boolean supportsDb(String type) {
382390
@Override
383391
public void databaseOpen(Database db) throws DatabaseException, DatabaseUnsupportedException {
384392
try {
385-
TableJdo table = new TableJdo();
386-
db.addDatabaseListener(table);
387-
table.databaseOpen(db.getDatabaseServer());
393+
tableJdo = new TableJdo(db);
388394

389395
} catch (Exception e) {
390396
LOGGER.warn(e.getMessage(), e);

addOns/authhelper/src/main/java/org/zaproxy/addon/authhelper/internal/db/TableJdo.java

Lines changed: 109 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,144 @@
1919
*/
2020
package org.zaproxy.addon.authhelper.internal.db;
2121

22+
import java.lang.reflect.Method;
2223
import java.sql.Connection;
2324
import java.sql.SQLException;
2425
import java.util.Properties;
2526
import javax.jdo.Constants;
2627
import javax.jdo.JDOHelper;
2728
import javax.jdo.PersistenceManagerFactory;
29+
import org.apache.logging.log4j.LogManager;
30+
import org.apache.logging.log4j.Logger;
2831
import org.datanucleus.PropertyNames;
2932
import org.flywaydb.core.Flyway;
33+
import org.parosproxy.paros.db.Database;
3034
import org.parosproxy.paros.db.DatabaseException;
35+
import org.parosproxy.paros.db.DatabaseListener;
3136
import org.parosproxy.paros.db.DatabaseServer;
32-
import org.parosproxy.paros.db.DatabaseUnsupportedException;
33-
import org.parosproxy.paros.db.paros.ParosAbstractTable;
37+
import org.parosproxy.paros.db.paros.ParosDatabaseServer;
3438

35-
public class TableJdo extends ParosAbstractTable {
39+
public class TableJdo implements DatabaseListener {
3640

37-
private static final String USER = "sa";
41+
private static final Logger LOGGER = LogManager.getLogger(TableJdo.class);
42+
43+
private static Method getUrlMethod;
44+
private static Method getUserMethod;
45+
private static Method getPasswordMethod;
3846

3947
private static PersistenceManagerFactory pmf;
4048

41-
@Override
42-
public void databaseOpen(DatabaseServer server)
43-
throws DatabaseException, DatabaseUnsupportedException {
44-
if (pmf != null) {
45-
pmf.close();
46-
pmf = null;
49+
private final Database db;
50+
51+
static {
52+
try {
53+
Class<?> dbServerClass = Class.forName("org.parosproxy.paros.db.DatabaseServer");
54+
getUrlMethod = dbServerClass.getMethod("getUrl");
55+
getUserMethod = dbServerClass.getMethod("getUser");
56+
getPasswordMethod = dbServerClass.getMethod("getPassword");
57+
58+
} catch (Exception e) {
59+
LOGGER.debug("An error occurred while getting the methods:", e);
4760
}
61+
}
62+
63+
public TableJdo(Database db) throws DatabaseException {
64+
this.db = db;
4865

49-
super.databaseOpen(server);
66+
db.addDatabaseListener(this);
67+
databaseOpen(db.getDatabaseServer());
5068
}
5169

5270
@Override
53-
protected void reconnect(Connection conn) throws DatabaseException {
54-
try {
55-
String dbUrl = conn.getMetaData().getURL();
56-
ClassLoader classLoader = this.getClass().getClassLoader();
57-
Flyway.configure(classLoader)
58-
.table("AUTHHELPER_FLYWAY_SCHEMA_HISTORY")
59-
.baselineOnMigrate(true)
60-
.baselineVersion("0")
61-
.dataSource(dbUrl, USER, "")
62-
.load()
63-
.migrate();
71+
public void databaseOpen(DatabaseServer db) throws DatabaseException {
72+
if (getUrlMethod == null) {
73+
closing(db);
74+
}
75+
76+
String dbUrl = getUrl(db);
77+
String user = getUser(db);
78+
String password = getPassword(db);
79+
ClassLoader classLoader = this.getClass().getClassLoader();
80+
Flyway.configure(classLoader)
81+
.table("AUTHHELPER_FLYWAY_SCHEMA_HISTORY")
82+
.baselineOnMigrate(true)
83+
.baselineVersion("0")
84+
.dataSource(dbUrl, user, password)
85+
.load()
86+
.migrate();
6487

65-
Properties jdoProperties = new Properties();
66-
jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_URL, dbUrl);
67-
jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_USER_NAME, USER);
88+
Properties jdoProperties = new Properties();
89+
jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_URL, dbUrl);
90+
jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_USER_NAME, user);
91+
jdoProperties.setProperty(Constants.PROPERTY_CONNECTION_PASSWORD, password);
6892

69-
jdoProperties.put(PropertyNames.PROPERTY_CLASSLOADER_PRIMARY, classLoader);
93+
jdoProperties.put(PropertyNames.PROPERTY_CLASSLOADER_PRIMARY, classLoader);
94+
95+
pmf = JDOHelper.getPersistenceManagerFactory(jdoProperties, "authhelper", classLoader);
96+
}
7097

71-
pmf = JDOHelper.getPersistenceManagerFactory(jdoProperties, "authhelper", classLoader);
98+
private static String getUrl(DatabaseServer db) throws DatabaseException {
99+
try {
100+
if (getUrlMethod != null) {
101+
return (String) getUrlMethod.invoke(db);
102+
}
103+
} catch (Exception e) {
104+
LOGGER.warn("An error occurred while getting the URL:", e);
105+
}
72106

107+
try (Connection connection = getConnection(db)) {
108+
return connection.getMetaData().getURL();
73109
} catch (SQLException e) {
74110
throw new DatabaseException(e);
75111
}
76112
}
77113

114+
private static Connection getConnection(DatabaseServer db) throws SQLException {
115+
if (db instanceof ParosDatabaseServer pds) {
116+
return pds.getNewConnection();
117+
}
118+
if (db instanceof ParosDatabaseServer pds) {
119+
return pds.getNewConnection();
120+
}
121+
throw new SQLException("Unknown DB implementation");
122+
}
123+
124+
private static String getUser(DatabaseServer db) {
125+
try {
126+
if (getUserMethod != null) {
127+
return (String) getUserMethod.invoke(db);
128+
}
129+
} catch (Exception e) {
130+
LOGGER.warn("An error occurred while getting the user:", e);
131+
}
132+
133+
return "sa";
134+
}
135+
136+
private static String getPassword(DatabaseServer db) {
137+
try {
138+
if (getPasswordMethod != null) {
139+
return (String) getPasswordMethod.invoke(db);
140+
}
141+
} catch (Exception e) {
142+
LOGGER.warn("An error occurred while getting the password:", e);
143+
}
144+
145+
return "";
146+
}
147+
148+
// @Override
149+
public void closing(DatabaseServer db) {
150+
if (pmf != null) {
151+
pmf.close();
152+
pmf = null;
153+
}
154+
}
155+
156+
public void unload() {
157+
db.removeDatabaseListener(this);
158+
}
159+
78160
public static PersistenceManagerFactory getPmf() {
79161
return pmf;
80162
}

0 commit comments

Comments
 (0)