Repository navigation
Expand file tree
/
Copy pathDatabase.java
More file actions
327 lines (273 loc) · 11.1 KB
/
Copy pathDatabase.java
File metadata and controls
327 lines (273 loc) · 11.1 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package dbProcs;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Used to create database connections using connection pooling via HikariCP. Connections are
* obtained from the pool and returned when closed. Initiated by Getter.java, Setter.java <br>
* <br>
* This file is part of the Security Shepherd Project.
*
* <p>The Security Shepherd project is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.<br>
*
* <p>The Security Shepherd project is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.<br>
*
* <p>You should have received a copy of the GNU General Public License along with the Security
* Shepherd project. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mark
* @author Paul (connection pooling)
*/
public class Database {
private static final Logger log = LogManager.getLogger(Database.class);
/**
* This method is used by the application to get a connection for challenge schemas. Connections
* are obtained from a pool specific to the challenge.
*
* @param driverType The JDBC driver type (kept for API compatibility, but not used with pooling)
* @param connectionURL The base connection URL
* @param dbOptions Database connection options
* @param dbUsername Database username
* @param dbPassword Database password
* @return A pooled connection
* @throws SQLException if a connection cannot be obtained
*/
public static Connection getConnection(
String driverType,
String connectionURL,
String dbOptions,
String dbUsername,
String dbPassword)
throws SQLException {
// Extract the schema portion from the connectionURL for challenge connections
// The connectionURL at this point already includes the schema
return ConnectionPool.getChallengeConnection(
"", // Base URL is already included in connectionURL
connectionURL,
dbOptions,
dbUsername,
dbPassword);
}
/**
* This method is used by the application to close/return a connection to the pool. With
* connection pooling, this returns the connection to the pool for reuse.
*
* @param conn The connection to return to the pool
*/
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close(); // With HikariCP, this returns the connection to the pool
} catch (SQLException e) {
log.warn("Error returning connection to pool: " + e.getMessage());
}
}
}
/**
* This method is used by the application to get a connection to the secure database sever based
* on the input path to a specific properties file.
*
* @param ApplicationRoot The running context of the application.
* @param path The path to the properties file to use for this connection. this is filtered for
* path traversal attacks
* @return A connection to the secure database server
* @throws IOException
* @throws FileNotFoundException
* @throws SQLException
*/
public static Connection getChallengeConnection(String ApplicationRoot, String path)
throws SQLException {
// Some over paranoid input validation never hurts.
path = path.replaceAll("\\.", "").replaceAll("/", "");
log.debug("Path = " + path);
Connection conn = null;
Properties prop = new Properties();
// Pull Driver and DB URL out of database.properties
String mysql_props = Constants.MYSQL_DB_PROP;
try (InputStream mysql_input = new FileInputStream(mysql_props)) {
prop.load(mysql_input);
} catch (IOException e) {
log.error("Could not load properties file: " + e.toString());
throw new RuntimeException(e);
}
String errorBase = "Missing property :";
String connectionURL = prop.getProperty("databaseConnectionURL");
if (connectionURL == null) {
String message = errorBase + "connectionURL";
log.fatal(message);
throw new RuntimeException(message);
}
if (connectionURL.contains("?")) {
// Check for old-style challenge properties files that put options in connection url
String message = "Connection URL cannot contain options!";
log.fatal(message);
throw new RuntimeException(message);
}
String driverType = prop.getProperty("DriverType");
if (driverType == null) {
String message = errorBase + "DriverType";
log.fatal(message);
throw new RuntimeException(message);
}
// Pull DB Schema, Schema User name and Schema Password from level specific
// properties File
String challenge_props =
new File(Database.class.getResource("/challenges/" + path + ".properties").getFile())
.getAbsolutePath();
log.debug("Level Properties File = " + path + ".properties");
try (InputStream mysql_input = new FileInputStream(challenge_props)) {
prop.load(mysql_input);
} catch (IOException e) {
log.error("Could not load properties file: " + e.toString());
throw new RuntimeException(e);
}
String challenge_connectionURL = prop.getProperty("databaseConnectionURL");
if (challenge_connectionURL == null) {
throw new RuntimeException(errorBase + "connectionURL");
}
String dbOptions = prop.getProperty("databaseOptions");
if (dbOptions == null) {
log.debug(
"Did not find database options, defaulting to"
+ " useUnicode=true&character_set_server=utf8mb4");
dbOptions = "useUnicode=true&character_set_server=utf8mb4";
}
String username = prop.getProperty("databaseUsername");
if (username == null) {
throw new RuntimeException(errorBase + "databaseUsername");
}
String password = prop.getProperty("databasePassword");
if (password == null) {
throw new RuntimeException(errorBase + "databasePassword");
}
connectionURL += challenge_connectionURL;
conn = getConnection(driverType, connectionURL, dbOptions, username, password);
return conn;
}
public static Connection getCoreConnection() throws SQLException, IOException {
return getCoreConnection("");
}
/**
* Gets a connection to the core database schema from the connection pool.
*
* @param ApplicationRoot The running context of the application (kept for API compatibility)
* @return Connection to core schema from the pool
* @throws SQLException if a connection cannot be obtained
*/
public static Connection getCoreConnection(String ApplicationRoot) throws SQLException {
// Use the connection pool for core connections
return ConnectionPool.getConnection();
}
public static Connection getDatabaseConnection(String ApplicationRoot)
throws SQLException, IOException {
return getDatabaseConnection(ApplicationRoot, false);
}
/**
* This method is used by the application to get a connection to the secure database sever
*
* @param ApplicationRoot The running context of the application.
* @return A connection to the secure database server
* @throws SQLException
* @throws IOException
* @throws RuntimeException
*/
public static Connection getDatabaseConnection(String ApplicationRoot, boolean allowMulti)
throws SQLException, IOException {
Connection conn = null;
Properties prop = new Properties();
// Pull Driver and DB URL out of database.properties
String mysql_props = Constants.MYSQL_DB_PROP;
try (InputStream mysql_input = new FileInputStream(mysql_props)) {
prop.load(mysql_input);
}
String errorBase = "Missing property :";
String connectionURL = prop.getProperty("databaseConnectionURL");
if (connectionURL == null) {
throw new RuntimeException(errorBase + "connectionURL");
}
String dbOptions = prop.getProperty("databaseOptions");
if (dbOptions == null) {
throw new RuntimeException(errorBase + "databaseOptions");
}
String driverType = prop.getProperty("DriverType");
if (driverType == null) {
throw new RuntimeException(errorBase + "DriverType");
}
String username = prop.getProperty("databaseUsername");
if (username == null) {
throw new RuntimeException(errorBase + "databaseUsername");
}
String password = prop.getProperty("databasePassword");
if (password == null) {
throw new RuntimeException(errorBase + "databasePassword");
}
if (allowMulti) {
if (dbOptions.length() > 0) {
dbOptions += "&";
}
dbOptions += "allowMultiQueries=true";
}
conn = getConnection(driverType, connectionURL, dbOptions, username, password);
return conn;
}
/**
* This method is used by the application to get a connection to the secure database sever's SQL
* injection Lesson schema
*
* @param ApplicationRoot The running context of the application.
* @return A connection to the secure database server
* @throws SQLException
* @throws IOException
*/
public static Connection getSqlInjLessonConnection(String ApplicationRoot)
throws SQLException, IOException {
Connection conn = null;
Properties prop = new Properties();
String mysql_props = Constants.MYSQL_DB_PROP;
try (InputStream mysql_input = new FileInputStream(mysql_props)) {
prop.load(mysql_input);
}
String errorBase = "Missing property :";
String connectionURL = prop.getProperty("databaseConnectionURL");
if (connectionURL == null) {
throw new RuntimeException(errorBase + "connectionURL");
}
String dbOptions = prop.getProperty("databaseOptions");
if (dbOptions == null) {
throw new RuntimeException(errorBase + "databaseOptions");
}
String driverType = prop.getProperty("DriverType");
if (driverType == null) {
throw new RuntimeException(errorBase + "DriverType");
}
// Pull Schema, User name and Password from SqlInjLesson.properties
String sql_inj_props = ApplicationRoot + "/WEB-INF/classes/lessons/SqlInjLesson.properties";
try (InputStream sql_inj_input = new FileInputStream(sql_inj_props)) {
prop.load(sql_inj_input);
}
String injection_URL = prop.getProperty("databaseConnectionURL");
if (injection_URL == null) {
throw new RuntimeException(errorBase + "SQL injection connectionURL");
}
String username = prop.getProperty("databaseUsername");
if (username == null) {
throw new RuntimeException(errorBase + "SQL injection databaseUsername");
}
String password = prop.getProperty("databasePassword");
if (password == null) {
throw new RuntimeException(errorBase + "SQL injection databasePassword");
}
conn = getConnection(driverType, connectionURL + injection_URL, dbOptions, username, password);
return conn;
}
}