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

(feat) Ability export profile data #368

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ func init() {
profilecmd.Flags().StringVarP(&profileOptions.Namespace, "namespace", "n", "", "Filter using namespace")
profilecmd.Flags().StringVar(&profileOptions.Pod, "pod", "", "Filter using Pod name")
profilecmd.Flags().StringVarP(&profileOptions.Container, "container", "c", "", "name of the container ")
profilecmd.Flags().BoolVar(&profileOptions.Save, "save", false, "Save Profile data in json format")
}
62 changes: 56 additions & 6 deletions profile/Client/profileClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import (
"bytes"
"encoding/json"
"fmt"
"github.com/accuknox/auto-policy-discovery/src/common"
"github.com/charmbracelet/bubbles/help"
Expand Down Expand Up @@ -66,6 +67,7 @@
Pod string
GRPC string
Container string
Save bool
}

// Model for main Bubble Tea
Expand Down Expand Up @@ -300,12 +302,14 @@

// Profile Row Data to display
type Profile struct {
Namespace string
ContainerName string
Process string
Resource string
Result string
Data string
Namespace string `json:"namespace"`
ContainerName string `json:"container-name"`
Process string `json:"process"`
Resource string `json:"resource"`
Result string `json:"result"`
Data string `json:"data"`
Count int `json:"count"`
Time string `json:"time"`
}

// Frequency and Timestamp data for another map
Expand Down Expand Up @@ -372,8 +376,31 @@
return outputMap
}

func convertToJson(Operation string, data []Profile) {

Check warning on line 379 in profile/Client/profileClient.go

View workflow job for this annotation

GitHub Actions / go-lint

func convertToJson should be convertToJSON
var jsonArray []string
jsonByte, _ := json.MarshalIndent(data, " ", " ")
//unmarshalling here because it is marshalled two times for some reason
if err := json.Unmarshal(jsonByte, &jsonArray); err != nil {
fmt.Println("Error parsing JSON array:", err)
}
if len(jsonArray) > 0 {
filepath := "Profile_Summary/"
err := os.MkdirAll(filepath, os.ModePerm)
err = os.WriteFile(filepath+Operation+".json", []byte(jsonArray[0]), 0600)
if err != nil {
panic(err)
}
}
}

func (p Profile) MarshalText() (text []byte, err error) {
type x Profile
return json.Marshal(x(p))
}

func generateRowsFromData(data []pb.Log, Operation string) []table.Row {
var s SomeData
var jsondata []Profile
m := make(map[Profile]int)
w := make(map[Profile]*Frequency)
for _, entry := range data {
Expand Down Expand Up @@ -425,8 +452,30 @@
ColumnCount: frequency.freq,
ColumnTimestamp: frequency.time,
})
jsondata = append(jsondata, Profile{
Namespace: r.Namespace,
ContainerName: r.ContainerName,
Process: r.Process,
Resource: r.Resource,
Result: r.Result,
Count: frequency.freq,
Time: frequency.time,
})
s.rows = append(s.rows, row)
}

if o1.Save {
if Operation == "File" {
convertToJson("File", jsondata)
} else if Operation == "Process" {
convertToJson("Process", jsondata)
} else if Operation == "Network" {
convertToJson("Network", jsondata)
} else if Operation == "Syscall" {
convertToJson("Syscall", jsondata)
}
}
daemon1024 marked this conversation as resolved.
Show resolved Hide resolved

return s.rows
}

Expand All @@ -437,6 +486,7 @@
Pod: o.Pod,
GRPC: o.GRPC,
Container: o.Container,
Save: o.Save,
}
p := tea.NewProgram(NewModel(), tea.WithAltScreen())
go func() {
Expand Down
Loading