Skip to content

Commit

Permalink
Merge pull request #243 from datafuselabs/fix/special-char-password
Browse files Browse the repository at this point in the history
fix: encode speacial char in password
  • Loading branch information
hantmac authored Aug 7, 2024
2 parents eefb92d + 4b6eb2a commit 6aeadef
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URI;
import java.sql.Array;
import java.sql.Blob;
Expand Down Expand Up @@ -54,7 +51,6 @@ public class DatabendConnection implements Connection, FileTransferAPI, Consumer
private static final Logger logger = Logger.getLogger(DatabendConnection.class.getPackage().getName());
private static FileHandler FILE_HANDLER;


private final AtomicBoolean closed = new AtomicBoolean();
private final AtomicBoolean autoCommit = new AtomicBoolean(true);
private final URI httpUri;
Expand All @@ -68,8 +64,16 @@ public class DatabendConnection implements Connection, FileTransferAPI, Consumer

private void initializeFileHandler() {
if (this.debug()) {
File file = new File("databend-jdbc-debug.log");
if (!file.canWrite()) {
logger.warning("No write access to file: " + file.getAbsolutePath());
return;
}
try {
FILE_HANDLER = new FileHandler("databend-jdbc-debug.log");
System.setProperty("java.util.logging.FileHandler.limit", "524288000"); // 500MB
System.setProperty("java.util.logging.FileHandler.count", "10");
System.setProperty("java.util.logging.FileHandler.append", "true"); // Enable log file reuse
FILE_HANDLER = new FileHandler(file.getAbsolutePath(), true); // Pass 'true' to the FileHandler constructor to enable appending
FILE_HANDLER.setLevel(Level.ALL);
FILE_HANDLER.setFormatter(new SimpleFormatter());
logger.addHandler(FILE_HANDLER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -136,8 +137,9 @@ private static String tryParseUriUserPassword(String url, Map<String, String> pr
if (colonPos > 0) {
String user = userPass.substring(0, colonPos);
String pass = userPass.substring(colonPos + 1);
String encodePass = URLUtils.urlEncode(pass);
properties.put(USER.getKey(), user);
properties.put(ConnectionProperties.PASSWORD.getKey(), pass);
properties.put(ConnectionProperties.PASSWORD.getKey(), encodePass);
} else {
properties.put(USER.getKey(), userPass);
}
Expand Down
20 changes: 20 additions & 0 deletions databend-jdbc/src/main/java/com/databend/jdbc/URLUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.databend.jdbc;

import javax.annotation.Nullable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


public class URLUtils {
@Nullable
public static String urlEncode(String target) {
String encodedTarget;
try {
encodedTarget = URLEncoder.encode(target, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException uex) {
return null;
}
return encodedTarget;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ private Connection createConnection()
throws SQLException {
String url = "jdbc:databend://localhost:8000/default";
return DriverManager.getConnection(url, "databend", "databend");

}

private Connection createConnection(boolean presignDisabled) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class TestPrepareStatement {
private Connection createConnection()
throws SQLException {
String url = "jdbc:databend://localhost:8000";
String url = "jdbc:databend://localhost:8000?debug=true";
return DriverManager.getConnection(url, "databend", "databend");
}

Expand Down Expand Up @@ -552,4 +552,15 @@ public void testSelectWithClusterKey() throws SQLException {
}
}
}

@Test
public void testEncodePass() throws SQLException {
Connection conn = createConnection();
conn.createStatement().execute("create user if not exists 'u01' identified by 'mS%aFRZW*GW';");
conn.createStatement().execute("GRANT ALL PRIVILEGES ON default.* TO 'u01'@'%'");

Connection conn2 = DriverManager.getConnection("jdbc:databend://localhost:8000", "u01", "mS%aFRZW*GW");
conn2.createStatement().execute("select 1");
conn.createStatement().execute("drop user if exists 'u01'");
}
}

0 comments on commit 6aeadef

Please sign in to comment.