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

non-k8s: improve karmor profile for host logs #462

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/signal"
"regexp"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -138,7 +139,11 @@ func StartObserver(c *k8s.Client, o Options) error {
} else {
pf, err := utils.InitiatePortForward(c, port, port, matchLabels, targetSvc)
if err != nil {
return err
if strings.Contains(err.Error(), "connection refused") && utils.IsSystemdMode() {
// do nothing
} else {
return err
}
}
gRPC = "localhost:" + strconv.FormatInt(pf.LocalPort, 10)
}
Expand Down
11 changes: 2 additions & 9 deletions probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/kubearmor/kubearmor-client/utils"
"io"
"log"
"os"
"os/exec"
"runtime"
"sort"
"strconv"
Expand Down Expand Up @@ -80,7 +80,7 @@ func PrintProbeResult(c *k8s.Client, o Options) error {
return errors.New("unsupported environment or cluster not configured correctly")
}
}
if isSystemdMode() {
if utils.IsSystemdMode() {
kd, err := probeSystemdMode()
if err != nil {
return err
Expand Down Expand Up @@ -566,13 +566,6 @@ func getPostureData(probeData []KubeArmorProbeData) map[string]string {
return postureData
}

// sudo systemctl status kubearmor
func isSystemdMode() bool {
cmd := exec.Command("systemctl", "status", "kubearmor")
_, err := cmd.CombinedOutput()
return err == nil
}

func probeSystemdMode() (KubeArmorProbeData, error) {
jsonFile, err := os.Open("/tmp/karmorProbeData.cfg")
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions profile/Client/profileClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/kubearmor/kubearmor-client/utils"
"os"
"strings"
"time"
Expand Down Expand Up @@ -103,6 +104,8 @@ func waitForActivity() tea.Cmd {
var o1 Options

func generateColumns(Operation string) []table.Column {
var columns []table.Column

CountCol := table.NewFlexColumn(ColumnCount, "Count", 1).WithStyle(ColumnStyle).WithFiltered(true)

Namespace := table.NewFlexColumn(ColumnNamespace, "Namespace", 2).WithStyle(ColumnStyle).WithFiltered(true)
Expand All @@ -120,15 +123,22 @@ func generateColumns(Operation string) []table.Column {

Timestamp := table.NewFlexColumn(ColumnTimestamp, "TimeStamp", 3).WithStyle(ColumnStyle)

return []table.Column{
Namespace,
ContainerName,
if !utils.IsSystemdMode() {
columns = append(columns, []table.Column{
Namespace,
ContainerName,
}...)
}

columns = append(columns, []table.Column{
ProcName,
Resource,
Result,
CountCol,
Timestamp,
}
}...)

return columns
}

// Init calls initial functions if needed
Expand Down
10 changes: 10 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

import "os/exec"

// IsSystemdMode checks if kubearmor is running in systemd mode
func IsSystemdMode() bool {
cmd := exec.Command("systemctl", "status", "kubearmor")
_, err := cmd.CombinedOutput()
return err == nil
}