-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMysqlConnectionManager.java
More file actions
119 lines (93 loc) · 4.14 KB
/
Copy pathMysqlConnectionManager.java
File metadata and controls
119 lines (93 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.jumpserver.chen.modules.mysql;
import org.apache.commons.lang3.StringUtils;
import org.jumpserver.chen.framework.datasource.Datasource;
import org.jumpserver.chen.framework.datasource.base.BaseConnectionManager;
import org.jumpserver.chen.framework.datasource.entity.DBConnectInfo;
import org.jumpserver.chen.framework.datasource.sql.SQL;
import org.jumpserver.chen.modules.base.ssl.JKSGenerator;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Properties;
public class MysqlConnectionManager extends BaseConnectionManager {
private static final String jdbcUrlTemplate = "jdbc:mysql://${host}:${port}/${db}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false";
private String jdbcUrl;
public MysqlConnectionManager(DBConnectInfo connectInfo, Datasource datasource) {
super(connectInfo, datasource);
this.sqlActuator = new MysqlActuator(this);
}
@Override
public String getDriverClassName() {
return "com.mysql.cj.jdbc.Driver";
}
@Override
public void ping() throws SQLException {
var url = this.getConnectInfo().toJDBCUrl(jdbcUrlTemplate);
this.ping(url);
this.jdbcUrl = url;
}
@Override
public Connection getConnection() throws SQLException {
var conn = super.getConnection();
setTimeZone(conn);
return conn;
}
@Override
public Connection getPhysicalConnection() throws SQLException {
var conn = super.getPhysicalConnection();
setTimeZone(conn);
return conn;
}
private void setTimeZone(Connection conn) throws SQLException {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime now = ZonedDateTime.now(zoneId);
ZoneOffset offset = now.getOffset();
String offsetStr = offset.toString();
try (Statement stmt = conn.createStatement()) {
stmt.execute("SET time_zone = '" + offsetStr + "'");
}
}
protected void setSSLProps(Properties props) {
if (this.getConnectInfo().getOptions().get("useSSL") != null
&& (boolean) this.getConnectInfo().getOptions().get("useSSL")) {
props.setProperty("useSSL", "true");
props.setProperty("requireSSL", "true");
var jksGenerator = new JKSGenerator();
if ((boolean) this.getConnectInfo().getOptions().get("verifyServerCertificate")) {
props.setProperty("verifyServerCertificate", "true");
jksGenerator.setCaCert((String) this.getConnectInfo().getOptions().get("caCert"));
var caCertPath = jksGenerator.generateCaJKS();
props.setProperty("trustCertificateKeyStoreUrl", "file:" + caCertPath);
props.setProperty("trustCertificateKeyStorePassword", JKSGenerator.JSK_PASS);
}
if (StringUtils.isNotBlank((String) this.getConnectInfo().getOptions().get("clientCert"))) {
jksGenerator.setClientCert((String) this.getConnectInfo().getOptions().get("clientCert"));
jksGenerator.setClientKey((String) this.getConnectInfo().getOptions().get("clientKey"));
var clientCertPath = jksGenerator.generateClientJKS();
props.setProperty("clientCertificateKeyStoreUrl", "file:" + clientCertPath);
props.setProperty("clientCertificateKeyStorePassword", JKSGenerator.JSK_PASS);
props.setProperty("clientKeyPassword", JKSGenerator.JSK_PASS);
}
}
}
@Override
public String getVersion() throws SQLException {
var result = this.sqlActuator.execute(SQL.of("select version()"));
return (String) result.getData().get(0).get(0);
}
@Override
public String getJDBCUrl() {
return this.jdbcUrl;
}
@Override
public String getDisplayJDBCUrl() {
return this.getConnectInfo().toDisplayJDBCUrl(jdbcUrlTemplate);
}
@Override
public String getJDBCUrl(String database) {
return this.jdbcUrl;
}
}