未验证 提交 f4c7821f 编写于 作者: H huolibo 提交者: GitHub

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
上级 a8ca5d31
......@@ -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
*/
......
......@@ -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
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);
......
......@@ -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;
}
......
......@@ -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();
......
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册