Skip to content

Commit

Permalink
JSON Output for Summarized Data (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasin-cs-ko-ak authored Dec 2, 2022
1 parent 00d6563 commit 10a711c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
1 change: 1 addition & 0 deletions cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions recommend/runtimePolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}
}
Expand Down
13 changes: 12 additions & 1 deletion summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package summary

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strconv"

Expand All @@ -32,6 +34,7 @@ type Options struct {
ClusterName string
ContainerName string
Type string
Output string
RevDNSLookup bool
Aggregation bool
}
Expand Down Expand Up @@ -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
Expand Down
50 changes: 25 additions & 25 deletions summary/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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" {
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
Expand Down

0 comments on commit 10a711c

Please sign in to comment.