Skip to content

Commit

Permalink
🚧 修复Check ElasticSearch数据源时未携带认证参数 (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
youdian-xiaoshuai authored Jan 6, 2025
1 parent b4a7eb3 commit 5ce820f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pkg/provider/logs_elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package provider

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/olivere/elastic/v7"
"watchAlert/internal/models"
utilsHttp "watchAlert/pkg/tools"
Expand All @@ -11,6 +13,8 @@ import (
type ElasticSearchDsProvider struct {
cli *elastic.Client
url string
username string
password string
ExternalLabels map[string]interface{}
}

Expand All @@ -27,6 +31,8 @@ func NewElasticSearchClient(ctx context.Context, ds models.AlertDataSource) (Log
return ElasticSearchDsProvider{
cli: client,
url: ds.ElasticSearch.Url,
username: ds.ElasticSearch.Username,
password: ds.ElasticSearch.Password,
ExternalLabels: ds.Labels,
}, nil
}
Expand Down Expand Up @@ -87,13 +93,21 @@ func (e ElasticSearchDsProvider) Query(options LogQueryOptions) ([]Logs, int, er
}

func (e ElasticSearchDsProvider) Check() (bool, error) {
res, err := utilsHttp.Get(nil, e.url+"/_cat/health", 10)
header := make(map[string]string)
url := fmt.Sprintf("%s/_cat/health", e.url)
if e.username != "" {
auth := e.username + ":" + e.password
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
header["Authorization"] = basicAuth
url = fmt.Sprintf("%s/_cat/health", e.url)
}
res, err := utilsHttp.Get(header, url, 10)
if err != nil {
return false, err
}

if res.StatusCode != 200 {
return false, err
return false, fmt.Errorf("状态码非200, 当前: %d", res.StatusCode)
}
return true, nil
}
Expand Down

0 comments on commit 5ce820f

Please sign in to comment.