Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support http auth & custom index name #66

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions relay-server/elasticsearch/adapter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package elasticsearch

Check warning on line 1 in relay-server/elasticsearch/adapter.go

View workflow job for this annotation

GitHub Actions / go-lint

should have a package comment

import (
"bytes"
Expand Down Expand Up @@ -38,7 +38,7 @@
// NewElasticsearchClient creates a new Elasticsearch client with the given Elasticsearch URL
// and kubearmor LogClient with endpoint. It has a retry mechanism for certain HTTP status codes and a backoff function for retry delays.
// It then creates a new NewBulkIndexer with the esClient
func NewElasticsearchClient(esURL string) (*ElasticsearchClient, error) {
func NewElasticsearchClient(esURL string, esUser string, esPassword string) (*ElasticsearchClient, error) {
retryBackoff := backoff.NewExponentialBackOff()
cfg := elasticsearch.Config{
Addresses: []string{esURL},
Expand All @@ -56,6 +56,11 @@
MaxRetries: 5,
}

if len(esUser) != 0 && len(esPassword) != 0 {
cfg.Username = esUser
cfg.Password = esPassword
}

esClient, err := elasticsearch.NewClient(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create Elasticsearch client: %v", err)
Expand Down Expand Up @@ -115,7 +120,7 @@
// and starting goroutines to consume messages from the alert channel and bulk index them.
// The method starts a goroutine for each stream and waits for messages to be received.
// Additional goroutines consume alert from the alert channel and bulk index them.
func (ecl *ElasticsearchClient) Start() error {
func (ecl *ElasticsearchClient) Start(AlertsIndex string) error {
start = time.Now()
ecl.ctx, ecl.cancel = context.WithCancel(context.Background())
var wg sync.WaitGroup
Expand All @@ -126,7 +131,7 @@
for {
select {
case alert := <-ecl.alertCh:
ecl.bulkIndex(alert, "alert")
ecl.bulkIndex(alert, AlertsIndex)
case <-ecl.ctx.Done():
return
}
Expand Down
10 changes: 8 additions & 2 deletions relay-server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor

package main

Check warning on line 4 in relay-server/main.go

View workflow job for this annotation

GitHub Actions / go-lint

should have a package comment

import (
"os"
Expand Down Expand Up @@ -56,7 +56,13 @@

//get env
enableEsDashboards := os.Getenv("ENABLE_DASHBOARDS")
esUrl := os.Getenv("ES_URL")

Check warning on line 59 in relay-server/main.go

View workflow job for this annotation

GitHub Actions / go-lint

var esUrl should be esURL
esUser := os.Getenv("ES_USERNAME")
esPassword := os.Getenv("ES_PASSWORD")
esAlertsIndex := os.Getenv("ES_ALERTS_INDEX")
if esAlertsIndex == "" {
esAlertsIndex = "kubearmor-alerts"
}
endPoint := os.Getenv("KUBEARMOR_SERVICE")
if endPoint == "" {
endPoint = "localhost:32767"
Expand Down Expand Up @@ -84,13 +90,13 @@

// check and start an elasticsearch client
if enableEsDashboards == "true" {
esCl, err := elasticsearch.NewElasticsearchClient(esUrl)
esCl, err := elasticsearch.NewElasticsearchClient(esUrl, esUser, esPassword)
if err != nil {
kg.Warnf("Failed to start a Elasticsearch Client")
return
}
relayServer.ELKClient = esCl
go relayServer.ELKClient.Start()
go relayServer.ELKClient.Start(esAlertsIndex)
defer relayServer.ELKClient.Stop()
}

Expand Down
Loading