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

enable ca support for elastic adapter #67

Merged
merged 3 commits into from
Dec 21, 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
2 changes: 1 addition & 1 deletion relay-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ ifeq (, $(shell which gosec))
go install github.com/securego/gosec/v2/cmd/gosec@latest;\
}
endif
cd $(CURDIR); gosec -exclude=G402 ./...
cd $(CURDIR); gosec -exclude=G402,G304 ./...
26 changes: 23 additions & 3 deletions relay-server/elasticsearch/adapter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -38,7 +41,8 @@
// 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, esUser string, esPassword string) (*ElasticsearchClient, error) {
func NewElasticsearchClient(esURL string, esUser string, esPassword string, esCaCertPath string, esAllowInsecureTLS bool) (*ElasticsearchClient, error) {

retryBackoff := backoff.NewExponentialBackOff()
cfg := elasticsearch.Config{
Addresses: []string{esURL},
Expand All @@ -54,6 +58,19 @@
return retryBackoff.NextBackOff()
},
MaxRetries: 5,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: esAllowInsecureTLS,
},
},
}

if esCaCertPath != "" {
caCertBytes, err := os.ReadFile(esCaCertPath)
if err != nil {
return nil, fmt.Errorf("failed to open Elasticsearch CA file: %v", err)
}
cfg.CACert = caCertBytes
}

if len(esUser) != 0 && len(esPassword) != 0 {
Expand All @@ -68,10 +85,13 @@
bi, err := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
Client: esClient, // The Elasticsearch client
FlushBytes: 1000000, // The flush threshold in bytes [1mb]
FlushInterval: 30 * time.Second, // The periodic flush interval [30 secs]
FlushInterval: 10 * time.Second, // The periodic flush interval [30 secs]
OnError: func(ctx context.Context, err error) {
log.Fatalf("Error creating the indexer: %v", err)
},
})
if err != nil {
log.Fatalf("Error creating the indexer: %s", err)
log.Fatalf("Error creating the indexer: %v", err)
}
alertCh := make(chan interface{}, 10000)
return &ElasticsearchClient{bulkIndexer: bi, esClient: esClient, alertCh: alertCh}, nil
Expand Down
9 changes: 7 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,9 +56,14 @@

//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")
esCaCertPath := os.Getenv("ES_CA_CERT_PATH")
esAllowInsecureTLS := false
if os.Getenv("ES_ALLOW_INSECURE_TLS") != "" {
esAllowInsecureTLS = true
}
esAlertsIndex := os.Getenv("ES_ALERTS_INDEX")
if esAlertsIndex == "" {
esAlertsIndex = "kubearmor-alerts"
Expand Down Expand Up @@ -90,9 +95,9 @@

// check and start an elasticsearch client
if enableEsDashboards == "true" {
esCl, err := elasticsearch.NewElasticsearchClient(esUrl, esUser, esPassword)
esCl, err := elasticsearch.NewElasticsearchClient(esUrl, esUser, esPassword, esCaCertPath, esAllowInsecureTLS)
if err != nil {
kg.Warnf("Failed to start a Elasticsearch Client")
kg.Warnf("Failed to start a Elasticsearch Client, %v", err)
return
}
relayServer.ELKClient = esCl
Expand Down
Loading