Skip to content

Commit 72dd0f9

Browse files
Merge pull request #66 from kubearmor/es-http-auth
support http auth & custom index name
2 parents c1aedf0 + e155a3f commit 72dd0f9

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

relay-server/elasticsearch/adapter.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type ElasticsearchClient struct {
3838
// NewElasticsearchClient creates a new Elasticsearch client with the given Elasticsearch URL
3939
// and kubearmor LogClient with endpoint. It has a retry mechanism for certain HTTP status codes and a backoff function for retry delays.
4040
// It then creates a new NewBulkIndexer with the esClient
41-
func NewElasticsearchClient(esURL string) (*ElasticsearchClient, error) {
41+
func NewElasticsearchClient(esURL string, esUser string, esPassword string) (*ElasticsearchClient, error) {
4242
retryBackoff := backoff.NewExponentialBackOff()
4343
cfg := elasticsearch.Config{
4444
Addresses: []string{esURL},
@@ -56,6 +56,11 @@ func NewElasticsearchClient(esURL string) (*ElasticsearchClient, error) {
5656
MaxRetries: 5,
5757
}
5858

59+
if len(esUser) != 0 && len(esPassword) != 0 {
60+
cfg.Username = esUser
61+
cfg.Password = esPassword
62+
}
63+
5964
esClient, err := elasticsearch.NewClient(cfg)
6065
if err != nil {
6166
return nil, fmt.Errorf("failed to create Elasticsearch client: %v", err)
@@ -115,7 +120,7 @@ func (ecl *ElasticsearchClient) SendAlertToBuffer(alert *pb.Alert) {
115120
// and starting goroutines to consume messages from the alert channel and bulk index them.
116121
// The method starts a goroutine for each stream and waits for messages to be received.
117122
// Additional goroutines consume alert from the alert channel and bulk index them.
118-
func (ecl *ElasticsearchClient) Start() error {
123+
func (ecl *ElasticsearchClient) Start(AlertsIndex string) error {
119124
start = time.Now()
120125
ecl.ctx, ecl.cancel = context.WithCancel(context.Background())
121126
var wg sync.WaitGroup
@@ -126,7 +131,7 @@ func (ecl *ElasticsearchClient) Start() error {
126131
for {
127132
select {
128133
case alert := <-ecl.alertCh:
129-
ecl.bulkIndex(alert, "alert")
134+
ecl.bulkIndex(alert, AlertsIndex)
130135
case <-ecl.ctx.Done():
131136
return
132137
}

relay-server/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func main() {
5757
//get env
5858
enableEsDashboards := os.Getenv("ENABLE_DASHBOARDS")
5959
esUrl := os.Getenv("ES_URL")
60+
esUser := os.Getenv("ES_USERNAME")
61+
esPassword := os.Getenv("ES_PASSWORD")
62+
esAlertsIndex := os.Getenv("ES_ALERTS_INDEX")
63+
if esAlertsIndex == "" {
64+
esAlertsIndex = "kubearmor-alerts"
65+
}
6066
endPoint := os.Getenv("KUBEARMOR_SERVICE")
6167
if endPoint == "" {
6268
endPoint = "localhost:32767"
@@ -84,13 +90,13 @@ func main() {
8490

8591
// check and start an elasticsearch client
8692
if enableEsDashboards == "true" {
87-
esCl, err := elasticsearch.NewElasticsearchClient(esUrl)
93+
esCl, err := elasticsearch.NewElasticsearchClient(esUrl, esUser, esPassword)
8894
if err != nil {
8995
kg.Warnf("Failed to start a Elasticsearch Client")
9096
return
9197
}
9298
relayServer.ELKClient = esCl
93-
go relayServer.ELKClient.Start()
99+
go relayServer.ELKClient.Start(esAlertsIndex)
94100
defer relayServer.ELKClient.Stop()
95101
}
96102

0 commit comments

Comments
 (0)