Skip to content

Commit

Permalink
Merge pull request #45 from qzhello/main
Browse files Browse the repository at this point in the history
fix: Split out  user restclient, remove default header #44
  • Loading branch information
qzhello committed Jun 4, 2024
2 parents ae33b63 + d7fb40b commit 7e2e434
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void updateProxyConfigMap(KibanaProperty newKibanaProperty) {
try {
proxyConfig.getRestClient().close();
// create a new es client
proxyConfig.setRestClient(RestUtils.initEsResClient(this.kibanaProperty.getProxy().getEs()));
proxyConfig.setRestClient(RestUtils.initEsResClient(this.kibanaProperty.getProxy().getEs(), true));
} catch (IOException e) {
log.error("close rest client error.", e);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/ly/ckibana/constants/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,8 @@ public static class IndexBuilder {

public static final String BULK_INDEX_NO_TYPE_HEADER = "{ \"index\": { \"_index\" : \"%s\", \"_id\": \"%s\"}}";
}

public static class Headers {
public static final String AUTHORIZATION = "authorization";
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/ly/ckibana/handlers/BaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ private RequestContext build(HttpServletRequest request, HttpServletResponse res
Map<String, Header> reqHeaders = HttpServletUtils.parseHttpRequestHeaders(request);
Map<String, String> defaultHeaders = proxyConfig.getKibanaItemProperty().getEs().getHeaders();
defaultHeaders.forEach((dhn, dhv) -> {
if (dhn.equalsIgnoreCase(Constants.Headers.AUTHORIZATION)) {
return;
}

for (String headerName : reqHeaders.keySet()) {
if (headerName.equalsIgnoreCase(dhn)) {
reqHeaders.put(headerName, new BasicHeader(headerName, dhv));
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/ly/ckibana/model/request/ProxyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ public class ProxyConfig {

private RestClient restClient;

private RestClient userRestClient;

private EsProxyClientConsumer esClientBuffer;

private BalancedClickhouseDataSource ckDatasource;

public ProxyConfig(KibanaItemProperty kibanaItemProperty) {
this.kibanaItemProperty = kibanaItemProperty;
this.restClient = RestUtils.initEsResClient(kibanaItemProperty.getEs());
this.restClient = RestUtils.initEsResClient(kibanaItemProperty.getEs(), true);
this.userRestClient = RestUtils.initEsResClient(kibanaItemProperty.getEs(), false);
this.esClientBuffer = new EsProxyClientConsumer();
if (kibanaItemProperty.getCk() != null) {
this.ckDatasource = CkService.initDatasource(kibanaItemProperty.getCk());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ly/ckibana/service/BlackSqlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public String addBlackSql(long range, String sql) {
}

public String removeBlackSql(String id) {
return EsClientUtil.deleteSource(proxyConfigLoader.getMetadataRestClient(), Constants.ConfigFile.BLACK_LIST_INDEX_NAME, id);
return EsClientUtil.deleteSource(requestContext.getProxyConfig().getRestClient(), Constants.ConfigFile.BLACK_LIST_INDEX_NAME, id);
}

public String getList(int size) {
return EsClientUtil.search(proxyConfigLoader.getMetadataRestClient(), Constants.ConfigFile.BLACK_LIST_INDEX_NAME, String.format("{\"size\":%s}", size));
return EsClientUtil.search(requestContext.getProxyConfig().getRestClient(), Constants.ConfigFile.BLACK_LIST_INDEX_NAME, String.format("{\"size\":%s}", size));
}

public boolean isBlackSql(long range, String sql) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/ly/ckibana/service/EsClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public class EsClientUtil {

protected static final Header[] BASE_HEADER = new Header[]{new BasicHeader("Content-Type", "application/json")};

/**
* 不指定则采用用户restclient
*/
public static String doRequest(RequestContext context) throws Exception {
return doRequest(context.getProxyConfig().getRestClient(), context.getRequestInfo(), context.getHttpResponse());
return doRequest(context.getProxyConfig().getUserRestClient(), context.getRequestInfo(), context.getHttpResponse());
}

public static String doRequest(RestClient restClient, RequestContext.RequestInfo requestInfo, HttpServletResponse response) throws Exception {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ly/ckibana/util/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public static String getClientIpAddr(HttpServletRequest request) {
/**
* 初始化es客户端.
*/
public static RestClient initEsResClient(EsProperty item) {
public static RestClient initEsResClient(EsProperty item, boolean mergeDefaultHeader) {
Map<String, String> headersMap = new HashMap<>();
if (!CollectionUtils.isEmpty(item.getHeaders())) {
if (!CollectionUtils.isEmpty(item.getHeaders()) && mergeDefaultHeader) {
headersMap.putAll(item.getHeaders());
}
return initEsRestClient(item.getHost(), headersMap);
Expand Down

0 comments on commit 7e2e434

Please sign in to comment.