From 10a711c769178dc26fce7b00a5206525ba8bbe95 Mon Sep 17 00:00:00 2001 From: Mohammed Yasin <92008211+yasin-cs-ko-ak@users.noreply.github.com> Date: Fri, 2 Dec 2022 21:34:46 +0530 Subject: [PATCH] JSON Output for Summarized Data (#215) --- cmd/summary.go | 1 + go.mod | 2 +- go.sum | 4 +-- recommend/runtimePolicy.go | 4 +-- summary/summary.go | 13 +++++++++- summary/table.go | 50 +++++++++++++++++++------------------- 6 files changed, 43 insertions(+), 31 deletions(-) diff --git a/cmd/summary.go b/cmd/summary.go index c83f67d4..70cc8111 100644 --- a/cmd/summary.go +++ b/cmd/summary.go @@ -33,6 +33,7 @@ func init() { summaryCmd.Flags().StringVarP(&summaryOptions.Type, "type", "t", summary.DefaultReqType, "Summary filter type : process|file|network ") summaryCmd.Flags().StringVar(&summaryOptions.ClusterName, "cluster", "", "Cluster name") summaryCmd.Flags().StringVar(&summaryOptions.ContainerName, "container", "", "Container name") + summaryCmd.Flags().StringVarP(&summaryOptions.Output, "output", "o", "", "Export Summary Data in JSON (karmor summary -o json)") summaryCmd.Flags().BoolVar(&summaryOptions.RevDNSLookup, "rev-dns-lookup", false, "Reverse DNS Lookup") summaryCmd.Flags().BoolVar(&summaryOptions.Aggregation, "agg", false, "Aggregate destination files/folder path") } diff --git a/go.mod b/go.mod index acb2f00c..a7dc74c0 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,7 @@ require ( ) require ( - github.com/accuknox/auto-policy-discovery/src v0.0.0-20221124164201-3131f7e51a72 + github.com/accuknox/auto-policy-discovery/src v0.0.0-20221129114555-66fc8ef6cfe4 github.com/cavaliergopher/grab/v3 v3.0.1 github.com/google/go-cmp v0.5.9 github.com/google/go-github v17.0.0+incompatible diff --git a/go.sum b/go.sum index 087369c1..0f00a15e 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= -github.com/accuknox/auto-policy-discovery/src v0.0.0-20221124164201-3131f7e51a72 h1:/H5IxfE7QBqBBaCrCJh7rg3pj6lvX1HRVBWPjX2u4Ok= -github.com/accuknox/auto-policy-discovery/src v0.0.0-20221124164201-3131f7e51a72/go.mod h1:7/Tl8/jHtGKeekkOo8flYUnZMOus/3g/qW91FDzCg3Q= +github.com/accuknox/auto-policy-discovery/src v0.0.0-20221129114555-66fc8ef6cfe4 h1:YMokhKwAQ0YU518HBeo5kDfoA+lTQ6F0efBKXG0at4U= +github.com/accuknox/auto-policy-discovery/src v0.0.0-20221129114555-66fc8ef6cfe4/go.mod h1:op1lUWTnZLiYtot7e3GqQa+aZstvMg/ZvRMU0QBoNV4= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/recommend/runtimePolicy.go b/recommend/runtimePolicy.go index e1ae9bfc..e28769a0 100644 --- a/recommend/runtimePolicy.go +++ b/recommend/runtimePolicy.go @@ -93,9 +93,9 @@ func checkProcessFileData(sumResp []*opb.Response, distro string) *MatchSpec { } for _, eachResp := range sumResp { for _, fileData := range eachResp.FileData { - if strings.HasPrefix(fileData.ProcName, saPath[0]) || strings.HasPrefix(fileData.ProcName, saPath[1]) { + if strings.HasPrefix(fileData.Destination, saPath[0]) || strings.HasPrefix(fileData.Destination, saPath[1]) { fromSourceArr = append(fromSourceArr, pol.MatchSourceType{ - Path: pol.MatchPathType(fileData.ParentProcName), + Path: pol.MatchPathType(fileData.Source), }) } } diff --git a/summary/summary.go b/summary/summary.go index 30d51c39..b2e97ca4 100644 --- a/summary/summary.go +++ b/summary/summary.go @@ -6,7 +6,9 @@ package summary import ( "context" + "encoding/json" "errors" + "fmt" "os" "strconv" @@ -32,6 +34,7 @@ type Options struct { ClusterName string ContainerName string Type string + Output string RevDNSLookup bool Aggregation bool } @@ -102,8 +105,16 @@ func Summary(c *k8s.Client, o Options) error { if err != nil { return err } - DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type) + if o.Output == "" { + DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type) + } + str := "" + if o.Output == "json" { + arr, _ := json.MarshalIndent(sumResp, "", " ") + str = fmt.Sprintf("%s\n", string(arr)) + fmt.Printf("%s", str) + } } } return nil diff --git a/summary/table.go b/summary/table.go index 74760b45..e50abc2a 100644 --- a/summary/table.go +++ b/summary/table.go @@ -25,7 +25,7 @@ var ( // DisplaySummaryOutput function func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType string) { - if len(resp.ProcessData) <= 0 && len(resp.FileData) <= 0 && len(resp.InNwData) <= 0 && len(resp.OutNwData) <= 0 { + if len(resp.ProcessData) <= 0 && len(resp.FileData) <= 0 && len(resp.IngressConnection) <= 0 && len(resp.EgressConnection) <= 0 { return } @@ -42,8 +42,8 @@ func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType str fmt.Printf("\nProcess Data\n") for _, procData := range resp.ProcessData { procStrSlice := []string{} - procStrSlice = append(procStrSlice, procData.ParentProcName) - procStrSlice = append(procStrSlice, procData.ProcName) + procStrSlice = append(procStrSlice, procData.Source) + procStrSlice = append(procStrSlice, procData.Destination) procStrSlice = append(procStrSlice, procData.Count) procStrSlice = append(procStrSlice, procData.UpdatedTime) if procData.Status == "Allow" { @@ -74,8 +74,8 @@ func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType str fileRowData := [][]string{} for _, fileData := range resp.FileData { fileStrSlice := []string{} - fileStrSlice = append(fileStrSlice, fileData.ParentProcName) - fileStrSlice = append(fileStrSlice, fileData.ProcName) + fileStrSlice = append(fileStrSlice, fileData.Source) + fileStrSlice = append(fileStrSlice, fileData.Destination) fileStrSlice = append(fileStrSlice, fileData.Count) fileStrSlice = append(fileStrSlice, fileData.UpdatedTime) if fileData.Status == "Allow" { @@ -100,42 +100,42 @@ func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType str } if strings.Contains(requestType, "network") { - if len(resp.InNwData) > 0 { + if len(resp.IngressConnection) > 0 { fmt.Printf("\nIngress connections\n") // Display server conn data inNwRowData := [][]string{} - for _, inNwData := range resp.InNwData { + for _, ingressConnection := range resp.IngressConnection { inNwStrSlice := []string{} - domainName := dnsLookup(inNwData.IP, revDNSLookup) - inNwStrSlice = append(inNwStrSlice, inNwData.Protocol) - inNwStrSlice = append(inNwStrSlice, inNwData.Command) + domainName := dnsLookup(ingressConnection.IP, revDNSLookup) + inNwStrSlice = append(inNwStrSlice, ingressConnection.Protocol) + inNwStrSlice = append(inNwStrSlice, ingressConnection.Command) inNwStrSlice = append(inNwStrSlice, domainName) - inNwStrSlice = append(inNwStrSlice, inNwData.Port) - inNwStrSlice = append(inNwStrSlice, inNwData.Namespace) - inNwStrSlice = append(inNwStrSlice, inNwData.Labels) - inNwStrSlice = append(inNwStrSlice, inNwData.Count) - inNwStrSlice = append(inNwStrSlice, inNwData.UpdatedTime) + inNwStrSlice = append(inNwStrSlice, ingressConnection.Port) + inNwStrSlice = append(inNwStrSlice, ingressConnection.Namespace) + inNwStrSlice = append(inNwStrSlice, ingressConnection.Labels) + inNwStrSlice = append(inNwStrSlice, ingressConnection.Count) + inNwStrSlice = append(inNwStrSlice, ingressConnection.UpdatedTime) inNwRowData = append(inNwRowData, inNwStrSlice) } WriteTable(SysNwHeader, inNwRowData) fmt.Printf("\n") } - if len(resp.OutNwData) > 0 { + if len(resp.EgressConnection) > 0 { fmt.Printf("\nEgress connections\n") // Display server conn data outNwRowData := [][]string{} - for _, outNwData := range resp.OutNwData { + for _, egressConnection := range resp.EgressConnection { outNwStrSlice := []string{} - domainName := dnsLookup(outNwData.IP, revDNSLookup) - outNwStrSlice = append(outNwStrSlice, outNwData.Protocol) - outNwStrSlice = append(outNwStrSlice, outNwData.Command) + domainName := dnsLookup(egressConnection.IP, revDNSLookup) + outNwStrSlice = append(outNwStrSlice, egressConnection.Protocol) + outNwStrSlice = append(outNwStrSlice, egressConnection.Command) outNwStrSlice = append(outNwStrSlice, domainName) - outNwStrSlice = append(outNwStrSlice, outNwData.Port) - outNwStrSlice = append(outNwStrSlice, outNwData.Namespace) - outNwStrSlice = append(outNwStrSlice, outNwData.Labels) - outNwStrSlice = append(outNwStrSlice, outNwData.Count) - outNwStrSlice = append(outNwStrSlice, outNwData.UpdatedTime) + outNwStrSlice = append(outNwStrSlice, egressConnection.Port) + outNwStrSlice = append(outNwStrSlice, egressConnection.Namespace) + outNwStrSlice = append(outNwStrSlice, egressConnection.Labels) + outNwStrSlice = append(outNwStrSlice, egressConnection.Count) + outNwStrSlice = append(outNwStrSlice, egressConnection.UpdatedTime) outNwRowData = append(outNwRowData, outNwStrSlice) } WriteTable(SysNwHeader, outNwRowData)