From f4c7821fda71a1c1f4171ae731ce6f77789e418e Mon Sep 17 00:00:00 2001 From: huolibo Date: Thu, 12 May 2022 11:26:49 +0800 Subject: [PATCH] enh(driver): change auth type to basic and add token property (#12267) * enh(driver): change auth type to basic and add token property * enh(driver): jdbc websocket add token and useSSL property --- .../java/com/taosdata/jdbc/TSDBDriver.java | 5 +++ .../taosdata/jdbc/rs/RestfulConnection.java | 14 ++++++- .../com/taosdata/jdbc/rs/RestfulDriver.java | 38 +++++++++---------- .../taosdata/jdbc/rs/RestfulStatement.java | 15 ++++++-- .../jdbc/utils/HttpClientPoolUtil.java | 5 +-- .../jdbc/utils/HttpClientPoolUtilTest.java | 2 +- 6 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index 029e58bd61..ac8f68d918 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -66,6 +66,11 @@ public class TSDBDriver extends AbstractDriver { * Just for Cloud Service */ public static final String PROPERTY_KEY_TOKEN = "token"; + /** + * Use SSL (true/false) to communicate with the server. The default value is false. + * Just for Cloud Service + */ + public static final String PROPERTY_KEY_USE_SSL = "useSSL"; /** * Key for the configuration file directory of TSDB client in properties instance */ diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java index e818736096..f3ce3a742e 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java @@ -17,17 +17,21 @@ public class RestfulConnection extends AbstractConnection { private final int port; private final String url; private final String database; + private final String auth; + private final boolean useSsl; private final String token; private boolean isClosed; private final DatabaseMetaData metadata; - public RestfulConnection(String host, String port, Properties props, String database, String url, String token) { + public RestfulConnection(String host, String port, Properties props, String database, String url, String auth, boolean useSsl, String token) { super(props); this.host = host; this.port = Integer.parseInt(port); this.database = database; this.url = url; + this.auth = "Basic " + auth; + this.useSsl = useSsl; this.token = token; this.metadata = new RestfulDatabaseMetaData(url, props.getProperty(TSDBDriver.PROPERTY_KEY_USER), this); } @@ -88,4 +92,12 @@ public class RestfulConnection extends AbstractConnection { public String getToken() { return token; } + + public String getAuth() { + return auth; + } + + public boolean isUseSsl() { + return useSsl; + } } \ No newline at end of file diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java index 99b6c3bd3e..3cb4749335 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java @@ -1,8 +1,9 @@ package com.taosdata.jdbc.rs; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.taosdata.jdbc.*; +import com.taosdata.jdbc.AbstractDriver; +import com.taosdata.jdbc.TSDBDriver; +import com.taosdata.jdbc.TSDBError; +import com.taosdata.jdbc.TSDBErrorNumbers; import com.taosdata.jdbc.enums.TimestampFormat; import com.taosdata.jdbc.utils.HttpClientPoolUtil; import com.taosdata.jdbc.ws.InFlightRequest; @@ -16,6 +17,7 @@ import java.net.URISyntaxException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.sql.*; +import java.util.Base64; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -52,7 +54,6 @@ public class RestfulDriver extends AbstractDriver { String user; String password; - String cloudToken = null; try { if (!props.containsKey(TSDBDriver.PROPERTY_KEY_USER)) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_USER_IS_REQUIRED); @@ -64,15 +65,25 @@ public class RestfulDriver extends AbstractDriver { } catch (UnsupportedEncodingException e) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "unsupported UTF-8 concoding, user: " + props.getProperty(TSDBDriver.PROPERTY_KEY_USER) + ", password: " + props.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD)); } + + String cloudToken = null; if (props.containsKey(TSDBDriver.PROPERTY_KEY_TOKEN)) { cloudToken = props.getProperty(TSDBDriver.PROPERTY_KEY_TOKEN); } + boolean useSsl = Boolean.parseBoolean(props.getProperty(TSDBDriver.PROPERTY_KEY_USE_SSL, "false")); String loginUrl; String batchLoad = info.getProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD); if (Boolean.parseBoolean(batchLoad)) { - loginUrl = "ws://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + String protocol = "ws"; + if (useSsl) { + protocol = "wss"; + } + loginUrl = protocol + "://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + ":" + props.getProperty(TSDBDriver.PROPERTY_KEY_PORT) + "/rest/ws"; + if (null != cloudToken) { + loginUrl = loginUrl + "?token=" + cloudToken; + } WSClient client; Transport transport; try { @@ -106,24 +117,13 @@ public class RestfulDriver extends AbstractDriver { props.setProperty(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT, String.valueOf(TimestampFormat.TIMESTAMP)); return new WSConnection(url, props, transport, database); } - loginUrl = "http://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + ":" + props.getProperty(TSDBDriver.PROPERTY_KEY_PORT) + "/rest/login/" + user + "/" + password + ""; - if (null != cloudToken) { - loginUrl += "?token=" + cloudToken; - } int poolSize = Integer.parseInt(props.getProperty("httpPoolSize", HttpClientPoolUtil.DEFAULT_MAX_PER_ROUTE)); boolean keepAlive = Boolean.parseBoolean(props.getProperty("httpKeepAlive", HttpClientPoolUtil.DEFAULT_HTTP_KEEP_ALIVE)); - HttpClientPoolUtil.init(poolSize, keepAlive); - String result = HttpClientPoolUtil.execute(loginUrl); - JSONObject jsonResult = JSON.parseObject(result); - String status = jsonResult.getString("status"); - String token = jsonResult.getString("desc"); - - if (!status.equals("succ")) { - throw new SQLException(jsonResult.getString("desc")); - } - RestfulConnection conn = new RestfulConnection(host, port, props, database, url, token); + String auth = Base64.getEncoder().encodeToString( + (user + ":" + password).getBytes(StandardCharsets.UTF_8)); + RestfulConnection conn = new RestfulConnection(host, port, props, database, url, auth, useSsl, cloudToken); if (database != null && !database.trim().replaceAll("\\s", "").isEmpty()) { try (Statement stmt = conn.createStatement()) { stmt.execute("use " + database); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java index b145fe85e9..7befd0007f 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java @@ -63,7 +63,7 @@ public class RestfulStatement extends AbstractStatement { //如果执行了use操作应该将当前Statement的catalog设置为新的database boolean result = true; - String response = HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getToken()); + String response = HttpClientPoolUtil.execute(getUrl(), sql, this.conn.getAuth()); JSONObject jsonObject = JSON.parseObject(response); if (null == jsonObject) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "sql: " + sql); @@ -95,6 +95,10 @@ public class RestfulStatement extends AbstractStatement { private String getUrl() throws SQLException { String dbname = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME); + String protocol = "http"; +// if (conn.isUseSsl()) { +// protocol = "https"; +// } if (dbname == null || dbname.trim().isEmpty()) { dbname = ""; } else { @@ -105,13 +109,16 @@ public class RestfulStatement extends AbstractStatement { switch (timestampFormat) { case TIMESTAMP: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlt" + dbname; + url = protocol + "://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlt" + dbname; break; case UTC: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlutc" + dbname; + url = protocol + "://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlutc" + dbname; break; default: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql" + dbname; + url = protocol + "://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql" + dbname; + } + if (this.conn.getToken() != null && !"".equals(this.conn.getToken().trim())) { + url = url + "?token=" + this.conn.getToken(); } return url; } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java index cdadcd2d28..2923ad76aa 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java @@ -5,7 +5,6 @@ import com.taosdata.jdbc.TSDBErrorNumbers; import org.apache.http.HeaderElement; import org.apache.http.HeaderElementIterator; import org.apache.http.HttpEntity; -import org.apache.http.NoHttpResponseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.*; @@ -97,12 +96,12 @@ public class HttpClientPoolUtil { } /*** execute POST request ***/ - public static String execute(String uri, String data, String token) throws SQLException { + public static String execute(String uri, String data, String auth) throws SQLException { HttpEntityEnclosingRequestBase method = (HttpEntityEnclosingRequestBase) getRequest(uri, HttpPost.METHOD_NAME); method.setHeader(HTTP.CONTENT_TYPE, "text/plain"); method.setHeader(HTTP.CONN_DIRECTIVE, isKeepAlive); - method.setHeader("Authorization", "Taosd " + token); + method.setHeader("Authorization", auth); method.setEntity(new StringEntity(data, StandardCharsets.UTF_8)); HttpContext context = HttpClientContext.create(); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/HttpClientPoolUtilTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/HttpClientPoolUtilTest.java index 128a62e5d9..9c71a8c33b 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/HttpClientPoolUtilTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/HttpClientPoolUtilTest.java @@ -84,7 +84,7 @@ public class HttpClientPoolUtilTest { if (!status.equals("succ")) { throw new SQLException(jsonResult.getString("desc")); } - return token; + return "Basic " + token; } private boolean executeOneSql(String sql, String token) throws SQLException { -- GitLab