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

introduced an output flag #446

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Use: "logs",
Short: "Observe Logs from KubeArmor",
Long: `Observe Logs from KubeArmor`,
RunE: func(cmd *cobra.Command, args []string) error {

Check warning on line 18 in cmd/log.go

View workflow job for this annotation

GitHub Actions / go-lint

parameter 'cmd' seems to be unused, consider removing or renaming it as _
log.StopChan = make(chan struct{})
return log.StartObserver(client, logOptions)
},
Expand All @@ -33,6 +33,7 @@
logCmd.Flags().StringVar(&logOptions.LogPath, "logPath", "stdout", "Output location for alerts and logs, {path|stdout|none}")
logCmd.Flags().StringVar(&logOptions.LogFilter, "logFilter", "policy", "Filter for what kinds of alerts and logs to receive, {policy|system|all}")
logCmd.Flags().BoolVar(&logOptions.JSON, "json", false, "Flag to print alerts and logs in the JSON format")
logCmd.Flags().StringVar(&logOptions.Output, "output", "text", "Output format: text, json, or pretty-json")
logCmd.Flags().StringVarP(&logOptions.Namespace, "namespace", "n", "", "k8s namespace filter")
logCmd.Flags().StringVar(&logOptions.Operation, "operation", "", "Give the type of the operation (Eg:Process/File/Network)")
logCmd.Flags().StringVar(&logOptions.LogType, "logType", "", "Log type you want (Eg:ContainerLog/HostLog) ")
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.5.1 h1:FK6RCIUSfmbnI/imIICmboyQBkOckutaa6R5YYlLZyo=
github.com/DATA-DOG/go-sqlmock v1.5.1/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24/go.mod h1:4UJr5HIiMZrwgkSPdsjy2uOQExX/WEILpIrO9UPGuXs=
Expand Down Expand Up @@ -2259,8 +2257,6 @@ gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
helm.sh/helm/v3 v3.14.2 h1:V71fv+NGZv0icBlr+in1MJXuUIHCiPG1hW9gEBISTIA=
helm.sh/helm/v3 v3.14.2/go.mod h1:2itvvDv2WSZXTllknfQo6j7u3VVgMAvm8POCDgYH424=
helm.sh/helm/v3 v3.14.3 h1:HmvRJlwyyt9HjgmAuxHbHv3PhMz9ir/XNWHyXfmnOP4=
helm.sh/helm/v3 v3.14.3/go.mod h1:v6myVbyseSBJTzhmeE39UcPLNv6cQK6qss3dvgAySaE=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
7 changes: 5 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
type regexType *regexp.Regexp

const (
SelfCertProvider string = "self"

Check warning on line 25 in log/log.go

View workflow job for this annotation

GitHub Actions / go-lint

exported const SelfCertProvider should have comment (or a comment on this block) or be unexported
ExternalCertLoader string = "external"
)

Expand All @@ -41,13 +41,14 @@
type Options struct {
GRPC string
Secure bool
TlsCertPath string

Check warning on line 44 in log/log.go

View workflow job for this annotation

GitHub Actions / go-lint

struct field TlsCertPath should be TLSCertPath
TlsCertProvider string

Check warning on line 45 in log/log.go

View workflow job for this annotation

GitHub Actions / go-lint

struct field TlsCertProvider should be TLSCertProvider
ReadCAFromSecret bool
MsgPath string
LogPath string
LogFilter string
JSON bool
Output string
Namespace string
LogType string
Operation string
Expand Down Expand Up @@ -125,8 +126,10 @@
StopChan = nil
}

// StartObserver Function
// StartObserver Functionhere
// Use formatOutput(message, outputFormat) to format the output

func StartObserver(c *k8s.Client, o Options) error {

Check warning on line 132 in log/log.go

View workflow job for this annotation

GitHub Actions / go-lint

exported function StartObserver should have comment or be unexported
gRPC := ""
targetSvc := "kubearmor-relay"

Expand Down Expand Up @@ -179,7 +182,7 @@

if o.MsgPath != "none" {
// watch messages
go logClient.WatchMessages(o.MsgPath, o.JSON)
go logClient.WatchMessages(o.MsgPath, o.Output)
fmt.Fprintln(os.Stderr, "Started to watch messages")
}

Expand Down
29 changes: 20 additions & 9 deletions log/logClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (fd *Feeder) DoHealthCheck() bool {
}

// WatchMessages Function
func (fd *Feeder) WatchMessages(msgPath string, jsonFormat bool) error {
func (fd *Feeder) WatchMessages(msgPath string, outputFormat string) error {
fd.WgClient.Add(1)
defer fd.WgClient.Done()

Expand All @@ -204,9 +204,14 @@ func (fd *Feeder) WatchMessages(msgPath string, jsonFormat bool) error {

str := ""

if jsonFormat {
if outputFormat == "json" {
arr, _ := json.Marshal(res)
str = fmt.Sprintf("%s\n", string(arr))
}
if outputFormat == "pretty-json" {
arr, _ := json.MarshalIndent(res, "", " ")
str = fmt.Sprintf("%s\n", string(arr))

} else {
updatedTime := strings.Replace(res.UpdatedTime, "T", " ", -1)
updatedTime = strings.Replace(updatedTime, "Z", "", -1)
Expand Down Expand Up @@ -313,6 +318,7 @@ func WatchTelemetryHelper(arr []byte, t string, o Options) {
if err != nil {
return
}

// Filter Telemetry based on provided options
if len(o.Selector) != 0 && res["Labels"] != nil {
labels := strings.Split(res["Labels"].(string), ",")
Expand Down Expand Up @@ -399,17 +405,23 @@ func WatchTelemetryHelper(arr []byte, t string, o Options) {
}
}

str := ""

// Pass Events to Channel for further handling
// Send events to channel if provided
if o.EventChan != nil {
o.EventChan <- EventInfo{Data: arr, Type: t}
}

if o.JSON {
var str string

if o.Output == "pretty-json" {
indentedJSON, err := json.MarshalIndent(res, "", " ")
if err != nil {
return
}
str = fmt.Sprintf("%s\n", string(indentedJSON))

} else if o.JSON || o.Output == "json" {
str = fmt.Sprintf("%s\n", string(arr))
} else {

if time, ok := res["UpdatedTime"]; ok {
updatedTime := strings.Replace(time.(string), "T", " ", -1)
updatedTime = strings.Replace(updatedTime, "Z", "", -1)
Expand Down Expand Up @@ -453,7 +465,7 @@ func WatchTelemetryHelper(arr []byte, t string, o Options) {
sort.Strings(additionalKeys)
telKeys = append(telKeys, additionalKeys...)

for i := 2; i < len(telKeys); i++ { // Starting the loop from index 2 to skip printing timestamp again
for i := 2; i < len(telKeys); i++ {// Starting the loop from index 2 to skip printing timestamp again
k := telKeys[i]
// Check if fields are present in the structure and if present verifying that they are not empty
// Certain fields like Container* are not present in HostLogs, this check handles that and other edge cases
Expand All @@ -468,7 +480,6 @@ func WatchTelemetryHelper(arr []byte, t string, o Options) {
} else if o.LogPath != "" {
StrToFile(str, o.LogPath)
}

}

// DestroyClient Function
Expand Down
Loading