@@ -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 }
0 commit comments