Skip to content

Commit

Permalink
Merge pull request #141 from datafuselabs/feat/add-sslmode-param
Browse files Browse the repository at this point in the history
feat: add sslmode param in dsn
  • Loading branch information
hantmac authored Jan 25, 2024
2 parents 83d71a8 + 6e8fc5e commit 9d668a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class ConnectionProperties {
public static final ConnectionProperty<String> PASSWORD = new Password();
public static final ConnectionProperty<Boolean> SSL = new Ssl();
public static final ConnectionProperty<String> WAREHOUSE = new Warehouse();
public static final ConnectionProperty<String> SSL_MODE = new SSLMode();
static final ConnectionProperty<String> TENANT = new Tenant();
public static final ConnectionProperty<String> DATABASE = new Database();
public static final ConnectionProperty<String> ACCESS_TOKEN = new AccessToken();
Expand All @@ -39,6 +40,7 @@ public final class ConnectionProperties {
.add(PASSWORD)
.add(SSL)
.add(WAREHOUSE)
.add(SSL_MODE)
.add(TENANT)
.add(DATABASE)
.add(ACCESS_TOKEN)
Expand Down Expand Up @@ -96,6 +98,12 @@ public Warehouse() {
}
}

private static class SSLMode extends AbstractConnectionProperty<String> {
public SSLMode() {
super("sslmode", Optional.of("disable"), NOT_REQUIRED, ALLOWED, STRING_CONVERTER);
}
}

private static class Tenant
extends AbstractConnectionProperty<String> {
public Tenant() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public final class DatabendDriverUri {
private static final Splitter QUERY_SPLITTER = Splitter.on('&').omitEmptyStrings();
private static final Splitter ARG_SPLITTER = Splitter.on('=').limit(2);
private static final int DEFAULT_HTTPS_PORT = 443;
private static final int DEFAULT_HTTP_PORT = 80;
private static final int DEFAULT_HTTP_PORT = 8000;
private final HostAndPort address;
private final Properties properties;
private final URI uri;
private final boolean useSecureConnection;
private final String warehouse;
private final String sslmode;
private final String tenant;
private final boolean copyPurge;
private final String nullDisplay;
Expand All @@ -62,8 +63,9 @@ private DatabendDriverUri(String url, Properties driverProperties)
this.properties = mergeProperties(uriAndProperties.getKey(), uriAndProperties.getValue(), driverProperties);
this.useSecureConnection = SSL.getValue(properties).orElse(false);
this.warehouse = WAREHOUSE.getValue(properties).orElse("");
this.sslmode = SSL_MODE.getValue(properties).orElse("disable");
this.tenant = TENANT.getValue(properties).orElse("");
this.uri = parseFinalURI(uriAndProperties.getKey(), this.useSecureConnection);
this.uri = parseFinalURI(uriAndProperties.getKey(), this.useSecureConnection, this.sslmode);
this.address = HostAndPort.fromParts(uri.getHost(), uri.getPort());
this.database = DATABASE.getValue(properties).orElse("default");
this.presignedUrlDisabled = PRESIGNED_URL_DISABLED.getRequiredValue(properties);
Expand Down Expand Up @@ -96,11 +98,11 @@ private static void initDatabase(URI uri, Map<String, String> uriProperties) thr
uriProperties.put(DATABASE.getKey(), db);
}

private static URI parseFinalURI(URI uri, boolean isSSLSecured) throws SQLException {
private static URI parseFinalURI(URI uri, boolean isSSLSecured, String sslmode) throws SQLException {
requireNonNull(uri, "uri is null");
String authority = uri.getAuthority();
String scheme;
if (isSSLSecured) {
if (isSSLSecured || sslmode.equals("enable")) {
scheme = "https";
} else {
scheme = "http";
Expand Down Expand Up @@ -181,11 +183,14 @@ private static Map.Entry<URI, Map<String, String>> parse(String url)
raw = tryParseUriUserPassword(raw, uriProperties);
if (raw.startsWith("https://")) {
uriProperties.put(SSL.getKey(), "true");
uriProperties.put(SSL_MODE.getKey(), "enable");
} else if (raw.startsWith("http://")) {
uriProperties.put(SSL.getKey(), "false");
uriProperties.put(SSL_MODE.getKey(), "disable");
} else {
raw = "http://" + raw;
uriProperties.put(SSL.getKey(), "false");
uriProperties.put(SSL_MODE.getKey(), "disable");
}
try {
URI uri = new URI(raw);
Expand Down Expand Up @@ -253,6 +258,10 @@ public String getWarehouse() {
return warehouse;
}

public String getSslmode() {
return sslmode;
}

public String getTenant() {
return tenant;
}
Expand Down Expand Up @@ -303,7 +312,7 @@ public void setupClient(OkHttpClient.Builder builder) throws SQLException {
if (!password.isEmpty()) {
builder.addInterceptor(basicAuthInterceptor(USER.getValue(properties).orElse(""), password));
}
if (useSecureConnection) {
if (useSecureConnection || sslmode.equals("enable")) {
setupInsecureSsl(builder);
}
if (ACCESS_TOKEN.getValue(properties).isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void testUserDatabaseProp() throws SQLException {
props.setProperty("database", "db3");
props.setProperty("SSL", "true");
props.setProperty("binary_format", "base64");
props.setProperty("sslmode", "enable");
DatabendDriverUri uri = DatabendDriverUri.create("jdbc:databend://u1@localhost:33101/db1?password=p1&database=db2&query_timeout=120&connection_timeout=15&socket_timeout=15", props);

Assert.assertEquals(uri.getProperties().get("user"), "u1");
Expand All @@ -147,6 +148,7 @@ public void testUserDatabaseProp() throws SQLException {
Assert.assertTrue(uri.copyPurge().booleanValue());
Assert.assertEquals("\\N", uri.nullDisplay().toString());
Assert.assertEquals("base64", uri.binaryFormat().toString());
Assert.assertEquals("enable", uri.getSslmode().toString());
}

@Test(groups = {"unit"})
Expand Down
1 change: 1 addition & 0 deletions docs/Connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ String url="jdbc:databend://databend:secret@0.0.0.0:8000/hello_databend";
| user | Databend user name | none | jdbc:databend://0.0.0.0:8000/hello_databend?user=test |
| password | Databend user password | none | jdbc:databend://0.0.0.0:8000/hello_databend?password=secret |
| SSL | Enable SSL | false | jdbc:databend://0.0.0.0:8000/hello_databend?SSL=true |
| sslmode | SSL mode | disable | jdbc:databend://0.0.0.0:8000/hello_databend?sslmode=enable |
| copy_purge | If True, the command will purge the files in the stage after they are loaded successfully into the table | false | jdbc:databend://0.0.0.0:8000/hello_databend?copy_purge=true |
| presigned_url_disabled | whether use presigned url to upload data, generally if you use local disk as your storage layer, it should be set as true | false | jdbc:databend://0.0.0.0:8000/hello_databend?presigned_url_disabled=true |
| wait_time_secs | Restful query api blocking time, if the query is not finished, the api will block for wait_time_secs seconds | 10 | jdbc:databend://0.0.0.0:8000/hello_databend?wait_time_secs=10 |
Expand Down

0 comments on commit 9d668a9

Please sign in to comment.