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

fix logic for missing kubearmor-relay deployment #705

Merged
merged 1 commit into from
Apr 11, 2023
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
3 changes: 2 additions & 1 deletion src/cluster/k8sClientHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ func GetKubearmorRelayURL() string {
log.Error().Msg(err.Error())
return ""
}
if pods == nil {
if pods == nil || len(pods.Items) == 0 {
log.Error().Msgf("Unable to find kubearmor-relay")
return ""
}
namespace = pods.Items[0].Namespace
Expand Down
14 changes: 10 additions & 4 deletions src/plugin/kubearmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/accuknox/auto-policy-discovery/src/types"
pb "github.com/kubearmor/KubeArmor/protobuf"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

// Global Variable
Expand Down Expand Up @@ -313,21 +314,26 @@ func ConvertKubeArmorLogToKnoxSystemLog(relayLog *pb.Alert) (types.KnoxSystemLog
func ConnectKubeArmorRelay(cfg types.ConfigKubeArmorRelay) *grpc.ClientConn {
addr := net.JoinHostPort(cfg.KubeArmorRelayURL, cfg.KubeArmorRelayPort)

conn, err := grpc.Dial(addr, grpc.WithInsecure())
// Check for kubearmor-relay with 30s timeout
ctx, cf1 := context.WithTimeout(context.Background(), time.Second*30)
defer cf1()

// Blocking grpc Dial: in case of a bad connection, fails with timeout
conn, err := grpc.DialContext(ctx, addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
log.Error().Msg("err connecting kubearmor relay. " + err.Error())
log.Error().Msg("Error connecting kubearmor relay: " + err.Error())
return nil
}

log.Info().Msg("connected to kubearmor relay " + addr)
log.Info().Msg("Connected to kubearmor relay " + addr)
return conn
}

func GetSystemAlertsFromKubeArmorRelay(trigger int) []*pb.Alert {
results := []*pb.Alert{}
KubeArmorRelayLogsMutex.Lock()
if len(KubeArmorRelayLogs) == 0 {
log.Info().Msgf("KubeArmor Relay traffic flow not exist")
log.Info().Msgf("KubeArmor Relay traffic flow does not exist")
KubeArmorRelayLogsMutex.Unlock()
return results
}
Expand Down
11 changes: 11 additions & 0 deletions src/systempolicy/systemPolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,17 @@ func StartSystemLogRcvr() {
for {
if cfg.GetCfgSystemLogFrom() == "kubearmor" {
url := cluster.GetKubearmorRelayURL()
if url == "" {
log.Error().Msg("kubearmor-relay url not found, retrying...")
for i := 0; i < types.Maxtries; i++ {
time.Sleep(10 * time.Second)
url = cluster.GetKubearmorRelayURL()
if url != "" {
break
}
}
return
}
plugin.StartKubeArmorRelay(SystemStopChan, types.ConfigKubeArmorRelay{
KubeArmorRelayURL: url,
KubeArmorRelayPort: cfg.CurrentCfg.ConfigKubeArmorRelay.KubeArmorRelayPort,
Expand Down
3 changes: 3 additions & 0 deletions src/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ const (
// K8sNetworkPolicy
K8sNwPolicyAPIVersion = "networking.k8s.io/v1"
K8sNwPolicyKind = "NetworkPolicy"

// max no. of tries to connect to kubearmor-relay
Maxtries = 6
)