提交 ea471098 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add transparent key to Console tool that unlocks creation of new databases

上级 bfe98ee6
...@@ -21,6 +21,8 @@ Change Log ...@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>Issue #1751: making it easier to open console and create local databases
</li>
<li>Issue #1750: JOIN t ON t.col IN (SELECT ...) throws AssertionError <li>Issue #1750: JOIN t ON t.col IN (SELECT ...) throws AssertionError
</li> </li>
</ul> </ul>
......
...@@ -937,7 +937,7 @@ public class WebApp { ...@@ -937,7 +937,7 @@ public class WebApp {
prof.startCollecting(); prof.startCollecting();
Connection conn; Connection conn;
try { try {
conn = server.getConnection(driver, url, user, password); conn = server.getConnection(driver, url, user, password, null);
} finally { } finally {
prof.stopCollecting(); prof.stopCollecting();
profOpen = prof.getTop(3); profOpen = prof.getTop(3);
...@@ -998,7 +998,7 @@ public class WebApp { ...@@ -998,7 +998,7 @@ public class WebApp {
session.put("maxrows", "1000"); session.put("maxrows", "1000");
boolean isH2 = url.startsWith("jdbc:h2:"); boolean isH2 = url.startsWith("jdbc:h2:");
try { try {
Connection conn = server.getConnection(driver, url, user, password); Connection conn = server.getConnection(driver, url, user, password, (String) session.get("key"));
session.setConnection(conn); session.setConnection(conn);
session.put("url", url); session.put("url", url);
session.put("user", user); session.put("user", user);
......
...@@ -168,6 +168,7 @@ public class WebServer implements Service { ...@@ -168,6 +168,7 @@ public class WebServer implements Service {
private ShutdownHandler shutdownHandler; private ShutdownHandler shutdownHandler;
private Thread listenerThread; private Thread listenerThread;
private boolean ifExists = true; private boolean ifExists = true;
private String key;
private boolean trace; private boolean trace;
private TranslateThread translateThread; private TranslateThread translateThread;
private boolean allowChunked = true; private boolean allowChunked = true;
...@@ -266,6 +267,15 @@ public class WebServer implements Service { ...@@ -266,6 +267,15 @@ public class WebServer implements Service {
return startDateTime; return startDateTime;
} }
/**
* Sets the key for privileged connections.
*
* @param key key, or null
*/
public void setKey(String key) {
this.key = key;
}
@Override @Override
public void init(String... args) { public void init(String... args) {
// set the serverPropertiesDir, because it's used in loadProperties() // set the serverPropertiesDir, because it's used in loadProperties()
...@@ -336,8 +346,12 @@ public class WebServer implements Service { ...@@ -336,8 +346,12 @@ public class WebServer implements Service {
private void updateURL() { private void updateURL() {
try { try {
url = (ssl ? "https" : "http") + "://" + StringBuilder builder = new StringBuilder(ssl ? "https" : "http").append("://")
NetUtils.getLocalAddress() + ":" + port; .append(NetUtils.getLocalAddress()).append(':').append(port);
if (key != null) {
builder.append("?key=").append(key);
}
url = builder.toString();
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
// Google App Engine does not allow java.net.InetAddress // Google App Engine does not allow java.net.InetAddress
} }
...@@ -718,10 +732,11 @@ public class WebServer implements Service { ...@@ -718,10 +732,11 @@ public class WebServer implements Service {
* @param databaseUrl the database URL * @param databaseUrl the database URL
* @param user the user name * @param user the user name
* @param password the password * @param password the password
* @param userKey the key of privileged user
* @return the database connection * @return the database connection
*/ */
Connection getConnection(String driver, String databaseUrl, String user, Connection getConnection(String driver, String databaseUrl, String user,
String password) throws SQLException { String password, String userKey) throws SQLException {
driver = driver.trim(); driver = driver.trim();
databaseUrl = databaseUrl.trim(); databaseUrl = databaseUrl.trim();
Properties p = new Properties(); Properties p = new Properties();
...@@ -730,10 +745,12 @@ public class WebServer implements Service { ...@@ -730,10 +745,12 @@ public class WebServer implements Service {
// encrypted H2 database with empty user password doesn't work // encrypted H2 database with empty user password doesn't work
p.setProperty("password", password); p.setProperty("password", password);
if (databaseUrl.startsWith("jdbc:h2:")) { if (databaseUrl.startsWith("jdbc:h2:")) {
if (key == null || !key.equals(userKey)) {
if (ifExists) { if (ifExists) {
databaseUrl += ";IFEXISTS=TRUE"; databaseUrl += ";IFEXISTS=TRUE";
} }
} }
}
return JdbcUtils.getConnection(driver, databaseUrl, p); return JdbcUtils.getConnection(driver, databaseUrl, p);
} }
......
...@@ -78,6 +78,9 @@ class WebThread extends WebApp implements Runnable { ...@@ -78,6 +78,9 @@ class WebThread extends WebApp implements Runnable {
if (requestedFile.length() == 0) { if (requestedFile.length() == 0) {
return "index.do"; return "index.do";
} }
if (requestedFile.charAt(0) == '?') {
return "index.do" + requestedFile;
}
return requestedFile; return requestedFile;
} }
...@@ -122,10 +125,12 @@ class WebThread extends WebApp implements Runnable { ...@@ -122,10 +125,12 @@ class WebThread extends WebApp implements Runnable {
attributes = new Properties(); attributes = new Properties();
int paramIndex = file.indexOf('?'); int paramIndex = file.indexOf('?');
session = null; session = null;
String key = null;
if (paramIndex >= 0) { if (paramIndex >= 0) {
String attrib = file.substring(paramIndex + 1); String attrib = file.substring(paramIndex + 1);
parseAttributes(attrib); parseAttributes(attrib);
String sessionId = attributes.getProperty("jsessionid"); String sessionId = attributes.getProperty("jsessionid");
key = attributes.getProperty("key");
file = file.substring(0, paramIndex); file = file.substring(0, paramIndex);
session = server.getSession(sessionId); session = server.getSession(sessionId);
} }
...@@ -150,6 +155,9 @@ class WebThread extends WebApp implements Runnable { ...@@ -150,6 +155,9 @@ class WebThread extends WebApp implements Runnable {
message += "Content-Length: " + bytes.length + "\r\n"; message += "Content-Length: " + bytes.length + "\r\n";
} else { } else {
if (session != null && file.endsWith(".jsp")) { if (session != null && file.endsWith(".jsp")) {
if (key != null) {
session.put("key", key);
}
String page = new String(bytes, StandardCharsets.UTF_8); String page = new String(bytes, StandardCharsets.UTF_8);
if (SysProperties.CONSOLE_STREAM) { if (SysProperties.CONSOLE_STREAM) {
Iterator<String> it = (Iterator<String>) session.map.remove("chunks"); Iterator<String> it = (Iterator<String>) session.map.remove("chunks");
......
...@@ -9,6 +9,8 @@ import java.sql.Connection; ...@@ -9,6 +9,8 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import org.h2.server.ShutdownHandler; import org.h2.server.ShutdownHandler;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.StringUtils;
import org.h2.util.Tool; import org.h2.util.Tool;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -88,6 +90,7 @@ public class Console extends Tool implements ShutdownHandler { ...@@ -88,6 +90,7 @@ public class Console extends Tool implements ShutdownHandler {
boolean tcpShutdown = false, tcpShutdownForce = false; boolean tcpShutdown = false, tcpShutdownForce = false;
String tcpPassword = ""; String tcpPassword = "";
String tcpShutdownServer = ""; String tcpShutdownServer = "";
boolean ifExists = false;
for (int i = 0; args != null && i < args.length; i++) { for (int i = 0; args != null && i < args.length; i++) {
String arg = args[i]; String arg = args[i];
if (arg == null) { if (arg == null) {
...@@ -168,6 +171,7 @@ public class Console extends Tool implements ShutdownHandler { ...@@ -168,6 +171,7 @@ public class Console extends Tool implements ShutdownHandler {
// no parameters // no parameters
} else if ("-ifExists".equals(arg)) { } else if ("-ifExists".equals(arg)) {
// no parameters // no parameters
ifExists = true;
} else if ("-baseDir".equals(arg)) { } else if ("-baseDir".equals(arg)) {
i++; i++;
} else { } else {
...@@ -196,7 +200,8 @@ public class Console extends Tool implements ShutdownHandler { ...@@ -196,7 +200,8 @@ public class Console extends Tool implements ShutdownHandler {
if (webStart) { if (webStart) {
try { try {
web = Server.createWebServer(args); String webKey = ifExists ? null : StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32));
web = Server.createWebServer(args, webKey);
web.setShutdownHandler(this); web.setShutdownHandler(this);
web.start(); web.start();
if (printStatus) { if (printStatus) {
......
...@@ -426,7 +426,19 @@ public class Server extends Tool implements Runnable, ShutdownHandler { ...@@ -426,7 +426,19 @@ public class Server extends Tool implements Runnable, ShutdownHandler {
* @return the server * @return the server
*/ */
public static Server createWebServer(String... args) throws SQLException { public static Server createWebServer(String... args) throws SQLException {
return createWebServer(args, null);
}
/**
* Create a new web server, but does not start it yet.
*
* @param args the argument list
* @param key key, or null
* @return the server
*/
static Server createWebServer(String[] args, String key) throws SQLException {
WebServer service = new WebServer(); WebServer service = new WebServer();
service.setKey(key);
Server server = new Server(service, args); Server server = new Server(service, args);
service.setShutdownHandler(server); service.setShutdownHandler(server);
return server; return server;
...@@ -492,7 +504,12 @@ public class Server extends Tool implements Runnable, ShutdownHandler { ...@@ -492,7 +504,12 @@ public class Server extends Tool implements Runnable, ShutdownHandler {
try { try {
started = true; started = true;
service.start(); service.start();
String name = service.getName() + " (" + service.getURL() + ")"; String url = service.getURL();
int idx = url.indexOf('?');
if (idx >= 0) {
url = url.substring(0, idx);
}
String name = service.getName() + " (" + url + ')';
Thread t = new Thread(this, name); Thread t = new Thread(this, name);
t.setDaemon(service.isDaemon()); t.setDaemon(service.isDaemon());
t.start(); t.start();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论