Browse Source

REPORT-42622 新增https连接配置

master
richie 4 years ago
parent
commit
fda14f11b7
  1. 40
      src/main/java/com/fr/plugin/db/es/fun/ElasticsearchConnection.java
  2. 60
      src/main/java/com/fr/plugin/db/es/fun/help/ElasticsearchClientFactory.java
  3. 45
      src/main/java/com/fr/plugin/db/es/ui/ElasticsearchConnectionPane.java
  4. 7
      src/main/resources/com/fr/plugin/db/es/locale/es.properties
  5. 7
      src/main/resources/com/fr/plugin/db/es/locale/es_en_US.properties
  6. 55
      src/main/resources/com/fr/plugin/db/es/locale/es_zh_CN.properties

40
src/main/java/com/fr/plugin/db/es/fun/ElasticsearchConnection.java

@ -39,6 +39,12 @@ public class ElasticsearchConnection extends BaseConnection {
private Conf<String> username = HolderKit.simple(StringKit.EMPTY);
@Identifier("password")
private Conf<String> password = HolderKit.simple(StringKit.EMPTY);
@Identifier("useSSL")
private Conf<Boolean> useSSL = HolderKit.simple(false);
@Identifier("keyStoreFilePath")
private Conf<String> keyStoreFilePath = HolderKit.simple(StringKit.EMPTY);
@Identifier("keyStorePassword")
private Conf<String> keyStorePassword = HolderKit.simple(StringKit.EMPTY);
public String getHost() {
return host.get();
@ -72,6 +78,30 @@ public class ElasticsearchConnection extends BaseConnection {
this.password.set(password);
}
public boolean isUseSSL() {
return useSSL.get();
}
public void setUseSSL(boolean useSSL) {
this.useSSL.set(useSSL);
}
public String getKeyStoreFilePath() {
return keyStoreFilePath.get();
}
public void setKeyStoreFilePath(String keyStoreFilePath) {
this.keyStoreFilePath.set(keyStoreFilePath);
}
public String getKeyStorePassword() {
return keyStorePassword.get();
}
public void setKeyStorePassword(String keyStorePassword) {
this.keyStorePassword.set(keyStorePassword);
}
@Override
public void testConnection() throws Exception {
RestHighLevelClient client = createRestHighLevelClient();
@ -90,12 +120,16 @@ public class ElasticsearchConnection extends BaseConnection {
public RestHighLevelClient createRestHighLevelClient() {
Pair<String[], Integer[]> setting = setting();
return ElasticsearchClientFactory.buildHighLevelClient(setting.getFirst(), setting.getSecond(), getUsername(), getPassword());
return isUseSSL() ?
ElasticsearchClientFactory.buildHighLevelSSLClient(getKeyStoreFilePath(), getKeyStorePassword(), setting.getFirst(), setting.getSecond(), getUsername(), getPassword())
: ElasticsearchClientFactory.buildHighLevelClient(setting.getFirst(), setting.getSecond(), getUsername(), getPassword());
}
public RestClient createClient() {
Pair<String[], Integer[]> setting = setting();
return ElasticsearchClientFactory.buildClient(setting.getFirst(), setting.getSecond(), getUsername(), getPassword());
return isUseSSL() ?
ElasticsearchClientFactory.buildSSLClient(getKeyStoreFilePath(), getKeyStorePassword(), setting.getFirst(), setting.getSecond(), getUsername(), getPassword())
: ElasticsearchClientFactory.buildClient(setting.getFirst(), setting.getSecond(), getUsername(), getPassword());
}
private Pair<String[], Integer[]> setting() {
@ -104,7 +138,7 @@ public class ElasticsearchConnection extends BaseConnection {
String[] hosts = host.split(",");
String[] portArr = port.split(",");
Integer[] ports = new Integer[portArr.length];
for (int i = 0; i < portArr.length; i ++) {
for (int i = 0; i < portArr.length; i++) {
ports[i] = Integer.parseInt(portArr[i]);
}
return new Pair<String[], Integer[]>(hosts, ports);

60
src/main/java/com/fr/plugin/db/es/fun/help/ElasticsearchClientFactory.java

@ -1,17 +1,28 @@
package com.fr.plugin.db.es.fun.help;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.ArrayKit;
import com.fanruan.api.util.StringKit;
import com.fr.io.utils.ResourceIOUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* @author richie
* @version 10.0
@ -23,10 +34,18 @@ public class ElasticsearchClientFactory {
return new RestHighLevelClient(build(hosts, ports, username, password));
}
public static RestHighLevelClient buildHighLevelSSLClient(String keyStoreFilePath, String keyStorePassword, String[] hosts, Integer[] ports, String username, String password) {
return new RestHighLevelClient(buildSSL(keyStoreFilePath, keyStorePassword, hosts, ports, username, password));
}
public static RestClient buildClient(String[] hosts, Integer[] ports, String username, String password) {
return build(hosts, ports, username, password).build();
}
public static RestClient buildSSLClient(String keyStoreFilePath, String keyStorePassword, String[] hosts, Integer[] ports, String username, String password) {
return buildSSL(keyStoreFilePath, keyStorePassword, hosts, ports, username, password).build();
}
private static RestClientBuilder build(String[] hosts, Integer[] ports, String username, String password) {
int hostCount = ArrayKit.getLength(hosts);
int portCount = ArrayKit.getLength(ports);
@ -50,4 +69,45 @@ public class ElasticsearchClientFactory {
}
return builder;
}
private static RestClientBuilder buildSSL(String keyStoreFilePath, String keyStorePassword, String[] hosts, Integer[] ports, String username, String password) {
int hostCount = ArrayKit.getLength(hosts);
int portCount = ArrayKit.getLength(ports);
if (portCount != hostCount) {
throw new IllegalArgumentException("Hosts and ports must be matched!");
}
HttpHost[] httpHosts = new HttpHost[hostCount];
for (int i = 0; i < hostCount; i++) {
httpHosts[i] = new HttpHost(hosts[i], ports[i], "https");
}
RestClientBuilder builder = RestClient.builder(httpHosts);
if (StringKit.isNotEmpty(username)) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
if (StringKit.isEmpty(keyStoreFilePath)) {
// 没有使用自定义的ssl证书
builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
} else {
// 用自定义ssl证书
try {
KeyStore truststore = KeyStore.getInstance("jks");
try (InputStream is = ResourceIOUtils.read(keyStoreFilePath)) {
truststore.load(is, keyStorePassword.toCharArray());
}
SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, new TrustStrategy(){
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
});
final SSLContext sslContext = sslBuilder.build();
builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setSSLContext(sslContext).setDefaultCredentialsProvider(credentialsProvider));
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
}
}
}
return builder;
}
}

45
src/main/java/com/fr/plugin/db/es/ui/ElasticsearchConnectionPane.java

@ -2,6 +2,7 @@ package com.fr.plugin.db.es.ui;
import com.fanruan.api.design.DesignKit;
import com.fanruan.api.design.ui.component.UIButton;
import com.fanruan.api.design.ui.component.UICheckBox;
import com.fanruan.api.design.ui.component.UILabel;
import com.fanruan.api.design.ui.component.UIPasswordField;
import com.fanruan.api.design.ui.component.UITextField;
@ -10,9 +11,11 @@ import com.fanruan.api.design.ui.layout.TableLayoutKit;
import com.fanruan.api.design.util.GUICoreKit;
import com.fanruan.api.design.work.DatabaseConnectionPane;
import com.fanruan.api.util.IOKit;
import com.fr.design.gui.ifilechooser.UINativeFileChooser;
import com.fr.plugin.db.es.fun.ElasticsearchConnection;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -28,6 +31,10 @@ public class ElasticsearchConnectionPane extends DatabaseConnectionPane<Elastics
private UITextField portTextField;
private UITextField usernameTextField;
private UIPasswordField passwordTextField;
private UICheckBox useSSLCheckBox;
private UITextField keyStoreFilePathTextField;
private UIPasswordField keyStorePasswordTextField;
private UIButton fileSelectButton;
@Override
protected JPanel mainPanel() {
@ -37,6 +44,9 @@ public class ElasticsearchConnectionPane extends DatabaseConnectionPane<Elastics
portTextField = new UITextField();
usernameTextField = new UITextField();
passwordTextField = new UIPasswordField();
useSSLCheckBox = new UICheckBox(DesignKit.i18nText("Plugin-Elasticsearch_Use_SSL"), false);
keyStoreFilePathTextField = new UITextField();
keyStorePasswordTextField = new UIPasswordField();
JPanel globalConfigPane = new JPanel();
GridLayout gridLayout = new GridLayout(1, 2);
@ -56,19 +66,36 @@ public class ElasticsearchConnectionPane extends DatabaseConnectionPane<Elastics
}
});
useSSLCheckBox.addActionListener(e -> checkEnabled());
fileSelectButton = new UIButton("...");
fileSelectButton.addActionListener(e -> {
UINativeFileChooser fileChooser = new UINativeFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("JKS", "jks"));
int r = fileChooser.showOpenDialog(SwingUtilities.getWindowAncestor(ElasticsearchConnectionPane.this));
if (r == JFileChooser.APPROVE_OPTION) {
keyStoreFilePathTextField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
});
keyStoreFilePathTextField.setPlaceholder(DesignKit.i18nText("Plugin-Elasticsearch_Key_Store_Path_Description"));
Component[][] components = new Component[][]{
{new UILabel(DesignKit.i18nText("Plugin-Elasticsearch_Host") + ":"),
GUICoreKit.createBorderLayoutPane(hostTextField, BorderLayout.CENTER, helpButton, BorderLayout.EAST)},
{new UILabel(DesignKit.i18nText("Plugin-Elasticsearch_Port") + ":"), portTextField},
{new UILabel(DesignKit.i18nText("Plugin-Elasticsearch_Username") + ":"), usernameTextField},
{new UILabel(DesignKit.i18nText("Plugin-Elasticsearch_Password") + ":"), passwordTextField},
{new UILabel(DesignKit.i18nText("Plugin-Elasticsearch_Protocol") + ":"), useSSLCheckBox},
{new UILabel(DesignKit.i18nText("Plugin-Elasticsearch_Key_Store_Path") + ":"), GUICoreKit.createBorderLayoutPane(
keyStoreFilePathTextField, BorderLayout.CENTER,
fileSelectButton, BorderLayout.EAST
)},
{new UILabel(DesignKit.i18nText("Plugin-Elasticsearch_Key_Store_Password") + ":"), keyStorePasswordTextField}
};
double p = TableLayoutKit.PREFERRED;
double[] rowSize = new double[]{p, p, p, p, p};
double[] columnSize = new double[]{p, 400};
double[] rowSize = new double[]{p, p, p, p, p, p, p, p};
double[] columnSize = new double[]{p, 600};
JPanel settingsUI = TableLayoutKit.createTableLayoutPane(components, rowSize, columnSize);
settingsUI.setBorder(UITitledBorder.createBorderWithTitle("Elasticsearch"));
@ -79,7 +106,12 @@ public class ElasticsearchConnectionPane extends DatabaseConnectionPane<Elastics
pane.add(centerPane, BorderLayout.CENTER);
return pane;
}
private void checkEnabled() {
keyStoreFilePathTextField.setEnabled(useSSLCheckBox.isSelected());
keyStorePasswordTextField.setEnabled(useSSLCheckBox.isSelected());
fileSelectButton.setEnabled(useSSLCheckBox.isSelected());
}
@Override
@ -88,6 +120,10 @@ public class ElasticsearchConnectionPane extends DatabaseConnectionPane<Elastics
portTextField.setText(connection.getPort());
usernameTextField.setText(connection.getUsername());
passwordTextField.setText(connection.getPassword());
useSSLCheckBox.setSelected(connection.isUseSSL());
keyStoreFilePathTextField.setText(connection.getKeyStoreFilePath());
keyStorePasswordTextField.setText(connection.getKeyStorePassword());
checkEnabled();
}
@Override
@ -97,6 +133,9 @@ public class ElasticsearchConnectionPane extends DatabaseConnectionPane<Elastics
connection.setPort(portTextField.getText());
connection.setUsername(usernameTextField.getText());
connection.setPassword(passwordTextField.getText());
connection.setUseSSL(useSSLCheckBox.isSelected());
connection.setKeyStoreFilePath(keyStoreFilePathTextField.getText());
connection.setKeyStorePassword(keyStorePasswordTextField.getText());
return connection;
}

7
src/main/resources/com/fr/plugin/db/es/locale/es.properties

@ -24,4 +24,9 @@ Plugin-Elasticsearch_Host_Cluster=Cluster
Plugin-Elasticsearch_Host_Cluster_Description=Hosts and Ports must be the same count.
Plugin-Elasticsearch_Para_Type=Para Type
Plugin-Elasticsearch_Para_Type_Plain=Plain
Plugin-Elasticsearch_Para_Type_ECMAScript=ECMAScript
Plugin-Elasticsearch_Para_Type_ECMAScript=ECMAScript
Plugin-Elasticsearch_Use_SSL=Use SSL
Plugin-Elasticsearch_Protocol=Protocol
Plugin-Elasticsearch_Key_Store_Path=Key Store
Plugin-Elasticsearch_Key_Store_Password=Password
Plugin-Elasticsearch_Key_Store_Path_Description=Please select *.jks file

7
src/main/resources/com/fr/plugin/db/es/locale/es_en_US.properties

@ -24,4 +24,9 @@ Plugin-Elasticsearch_Host_Cluster=Cluster
Plugin-Elasticsearch_Host_Cluster_Description=Hosts and Ports must be the same count.
Plugin-Elasticsearch_Para_Type=Para Type
Plugin-Elasticsearch_Para_Type_Plain=Plain
Plugin-Elasticsearch_Para_Type_ECMAScript=ECMAScript
Plugin-Elasticsearch_Para_Type_ECMAScript=ECMAScript
Plugin-Elasticsearch_Use_SSL=Use SSL
Plugin-Elasticsearch_Protocol=Protocol
Plugin-Elasticsearch_Key_Store_Path=Key Store
Plugin-Elasticsearch_Key_Store_Password=Password
Plugin-Elasticsearch_Key_Store_Path_Description=Please select *.jks file

55
src/main/resources/com/fr/plugin/db/es/locale/es_zh_CN.properties

@ -1,27 +1,32 @@
Plugin_Elasticsearch_Table_Data=Elasticsearch数据集
Plugin-Elasticsearch_Host=地址
Plugin-Elasticsearch_Port=端口
Plugin-Elasticsearch_Username=用户名
Plugin-Elasticsearch_Password=密码
Plugin-Elasticsearch_Connection_Successfully=连接成功
Plugin-Elasticsearch_Connection_Failed=连接失败
Plugin-Elasticsearch_Preview=预览
Plugin-Elasticsearch_Refresh=刷新
Plugin-Elasticsearch_Auto=自动
Plugin-Elasticsearch_Program=自定义
Plugin-Elasticsearch_Sort_Default=默认
Plugin-Elasticsearch_Prepare_Keys_Default=默认
Plugin-Elasticsearch_Endpoint=端点
Plugin-Elasticsearch_Query=查询语句
Plugin-Elasticsearch_Query_Type=请求类型
Plugin-Elasticsearch_Sort_Key=键排序
Plugin-Elasticsearch_Sort_Open=开启
Plugin-Elasticsearch_Key_Prepare=预读列名
Plugin_Elasticsearch_Table_Data=Elasticsearch\u6570\u636E\u96C6
Plugin-Elasticsearch_Host=\u5730\u5740
Plugin-Elasticsearch_Port=\u7AEF\u53E3
Plugin-Elasticsearch_Username=\u7528\u6237\u540D
Plugin-Elasticsearch_Password=\u5BC6\u7801
Plugin-Elasticsearch_Connection_Successfully=\u8FDE\u63A5\u6210\u529F
Plugin-Elasticsearch_Connection_Failed=\u8FDE\u63A5\u5931\u8D25
Plugin-Elasticsearch_Preview=\u9884\u89C8
Plugin-Elasticsearch_Refresh=\u5237\u65B0
Plugin-Elasticsearch_Auto=\u81EA\u52A8
Plugin-Elasticsearch_Program=\u81EA\u5B9A\u4E49
Plugin-Elasticsearch_Sort_Default=\u9ED8\u8BA4
Plugin-Elasticsearch_Prepare_Keys_Default=\u9ED8\u8BA4
Plugin-Elasticsearch_Endpoint=\u7AEF\u70B9
Plugin-Elasticsearch_Query=\u67E5\u8BE2\u8BED\u53E5
Plugin-Elasticsearch_Query_Type=\u8BF7\u6C42\u7C7B\u578B
Plugin-Elasticsearch_Sort_Key=\u952E\u6392\u5E8F
Plugin-Elasticsearch_Sort_Open=\u5F00\u542F
Plugin-Elasticsearch_Key_Prepare=\u9884\u8BFB\u5217\u540D
Plugin-Elasticsearch_Prepare_Keys_Open=
Plugin-Elasticsearch_Result_Type=规整类型
Plugin-Elasticsearch_Result_Script=规整脚本
Plugin-Elasticsearch_Host_Cluster=集群配置说明
Plugin-Elasticsearch_Host_Cluster_Description=1、使用逗号分割集群中的多个数据库地址和端口,必须要求地址和端口数量一致;\n2、所有elasticsearch节点的用户名和密码必须相同。
Plugin-Elasticsearch_Para_Type=查询类型
Plugin-Elasticsearch_Para_Type_Plain=默认
Plugin-Elasticsearch_Result_Type=\u89C4\u6574\u7C7B\u578B
Plugin-Elasticsearch_Result_Script=\u89C4\u6574\u811A\u672C
Plugin-Elasticsearch_Host_Cluster=\u96C6\u7FA4\u914D\u7F6E\u8BF4\u660E
Plugin-Elasticsearch_Host_Cluster_Description=1\u3001\u4F7F\u7528\u9017\u53F7\u5206\u5272\u96C6\u7FA4\u4E2D\u7684\u591A\u4E2A\u6570\u636E\u5E93\u5730\u5740\u548C\u7AEF\u53E3\uFF0C\u5FC5\u987B\u8981\u6C42\u5730\u5740\u548C\u7AEF\u53E3\u6570\u91CF\u4E00\u81F4\uFF1B\n2\u3001\u6240\u6709elasticsearch\u8282\u70B9\u7684\u7528\u6237\u540D\u548C\u5BC6\u7801\u5FC5\u987B\u76F8\u540C\u3002
Plugin-Elasticsearch_Para_Type=\u67E5\u8BE2\u7C7B\u578B
Plugin-Elasticsearch_Para_Type_Plain=\u9ED8\u8BA4
Plugin-Elasticsearch_Para_Type_ECMAScript=ECMAScript
Plugin-Elasticsearch_Use_SSL=\u4F7F\u7528SSL
Plugin-Elasticsearch_Protocol=\u534F\u8BAE
Plugin-Elasticsearch_Key_Store_Path=\u8BC1\u4E66\u8DEF\u5F84
Plugin-Elasticsearch_Key_Store_Password=\u8BC1\u4E66\u79D8\u94A5
Plugin-Elasticsearch_Key_Store_Path_Description=\u8BF7\u8F93\u5165jks\u683C\u5F0F\u7684\u8BC1\u4E66\u8DEF\u5F84

Loading…
Cancel
Save