diff --git a/cmd/profile.go b/cmd/profile.go index f2317670..c5ae67d8 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -27,5 +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") + profilecmd.Flags().StringVarP(&profileOptions.Output, "output", "o", "", "Output profile data to the specified directory") } diff --git a/go.mod b/go.mod index ef482d12..93fbe2a2 100644 --- a/go.mod +++ b/go.mod @@ -159,7 +159,6 @@ require ( github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect - github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect github.com/go-chi/chi v4.1.2+incompatible // indirect github.com/go-errors/errors v1.5.1 // indirect @@ -331,7 +330,6 @@ require ( github.com/vbatts/tar-split v0.11.5 // indirect github.com/vishvananda/netlink v1.2.1-beta.2.0.20231127184239-0ced8385386a // indirect github.com/vishvananda/netns v0.0.4 // indirect - github.com/x448/float16 v0.8.4 // indirect github.com/xanzy/go-gitlab v0.95.2 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect diff --git a/go.sum b/go.sum index 4f552d05..1123d016 100644 --- a/go.sum +++ b/go.sum @@ -1274,8 +1274,6 @@ github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmV github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/fullstorydev/grpcurl v1.6.0/go.mod h1:ZQ+ayqbKMJNhzLmbpCiurTVlaK2M/3nqZCxaQ2Ze/sM= -github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= -github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v0.0.0-20180820084758-c7ce16629ff4/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -2414,8 +2412,6 @@ github.com/vishvananda/netlink v1.2.1-beta.2.0.20231127184239-0ced8385386a/go.mo github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8= github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= -github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= -github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xanzy/go-gitlab v0.95.2 h1:4p0IirHqEp5f0baK/aQqr4TR57IsD+8e4fuyAA1yi88= github.com/xanzy/go-gitlab v0.95.2/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= @@ -3462,8 +3458,6 @@ k8s.io/apimachinery v0.20.1/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRp k8s.io/apimachinery v0.20.2/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU= k8s.io/apimachinery v0.29.11 h1:55+6ue9advpA7T0sX2ZJDHCLKuiFfrAAR/39VQN9KEQ= k8s.io/apimachinery v0.29.11/go.mod h1:i3FJVwhvSp/6n8Fl4K97PJEP8C+MM+aoDq4+ZJBf70Y= -k8s.io/apimachinery v0.31.3 h1:6l0WhcYgasZ/wk9ktLq5vLaoXJJr5ts6lkaQzgeYPq4= -k8s.io/apimachinery v0.31.3/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= k8s.io/apiserver v0.20.1/go.mod h1:ro5QHeQkgMS7ZGpvf4tSMx6bBOgPfE+f52KwvXfScaU= k8s.io/apiserver v0.20.2/go.mod h1:2nKd93WyMhZx4Hp3RfgH2K5PhwyTrprrkWYnI7id7jA= k8s.io/apiserver v0.29.11 h1:EXcv4/3iIKWG5tWI2ywdMY86jpxYw6WDAdMrBKUMkSc= diff --git a/profile/Client/profileClient.go b/profile/Client/profileClient.go index a0395249..3494338d 100644 --- a/profile/Client/profileClient.go +++ b/profile/Client/profileClient.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" "strings" "time" @@ -21,6 +22,7 @@ import ( pb "github.com/kubearmor/KubeArmor/protobuf" klog "github.com/kubearmor/kubearmor-client/log" profile "github.com/kubearmor/kubearmor-client/profile" + "github.com/kubearmor/kubearmor-client/utils" log "github.com/sirupsen/logrus" ) @@ -68,7 +70,7 @@ type Options struct { Pod string GRPC string Container string - Save bool + Output string } // Model for main Bubble Tea @@ -379,17 +381,37 @@ func AggregateSummary(inputMap map[Profile]*Frequency, Operation string) map[Pro func convertToJSON(Operation string, data []Profile) { var jsonArray []string - jsonByte, _ := json.MarshalIndent(data, " ", " ") + jsonByte, err := json.MarshalIndent(data, " ", " ") + if err != nil { + log.Fatal("Cannot marshal JSON", err) + } + //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) + log.Fatal("Cannot unmarshal JSON", err) } + if len(jsonArray) > 0 { - filepath := "Profile_Summary/" - err := os.MkdirAll(filepath, 0600) - err = os.WriteFile(filepath+Operation+".json", []byte(jsonArray[0]), 0600) + + err := utils.CreateOutDir(o1.Output) if err != nil { - panic(err) + log.Fatal("Cannot create output directory", err) + } + + // Create file + file_name := filepath.Clean(filepath.Join(o1.Output, Operation+".json")) + output_file, err := os.Create(file_name) + if err != nil { + log.Fatal("Cannot create file", err) + } + defer output_file.Close() + + // Write JSON array to file + for _, line := range jsonArray { + _, err := output_file.WriteString(line + "\n") + if err != nil { + log.Fatal("Cannot write to file", err) + } } } } @@ -465,7 +487,7 @@ func generateRowsFromData(data []pb.Log, Operation string) []table.Row { s.rows = append(s.rows, row) } - if o1.Save { + if o1.Output != "" { if Operation == "File" { convertToJSON("File", jsondata) } else if Operation == "Process" { @@ -487,7 +509,7 @@ func Start(o Options) { Pod: o.Pod, GRPC: o.GRPC, Container: o.Container, - Save: o.Save, + Output: o.Output, } p := tea.NewProgram(NewModel(), tea.WithAltScreen()) go func() { diff --git a/recommend/recommend.go b/recommend/recommend.go index 9ab1ed66..a4f2c62a 100644 --- a/recommend/recommend.go +++ b/recommend/recommend.go @@ -6,7 +6,6 @@ package recommend import ( "context" - "errors" "fmt" "os" "path/filepath" @@ -19,6 +18,7 @@ import ( "github.com/kubearmor/kubearmor-client/recommend/image" "github.com/kubearmor/kubearmor-client/recommend/registry" "github.com/kubearmor/kubearmor-client/recommend/report" + utils "github.com/kubearmor/kubearmor-client/utils" "sigs.k8s.io/yaml" log "github.com/sirupsen/logrus" @@ -78,22 +78,6 @@ func unique(s []string) []string { return result } -func createOutDir(dir string) error { - if dir == "" { - return nil - } - _, err := os.Stat(dir) - if errors.Is(err, os.ErrNotExist) { - err = os.Mkdir(dir, 0750) - if err != nil { - return err - } - } else if err != nil { - return err - } - return nil -} - func finalReport() { repFile := filepath.Clean(filepath.Join(options.OutDir, options.ReportFile)) if err := report.Render(repFile); err != nil { @@ -185,7 +169,7 @@ func Recommend(c *k8s.Client, o common.Options, policyGenerators ...engines.Engi options = o reg := registry.New(o.Config) - if err = createOutDir(o.OutDir); err != nil { + if err = utils.CreateOutDir(o.OutDir); err != nil { return err } diff --git a/utils/output.go b/utils/output.go new file mode 100644 index 00000000..1611034d --- /dev/null +++ b/utils/output.go @@ -0,0 +1,18 @@ +package utils + +import ( + "os" +) + +// CreateOutDir Function to create output directory. +func CreateOutDir(dir string) error { + if dir == "" { + return nil + } + + if err := os.MkdirAll(dir, 0750); err != nil { + return err + } + + return nil +}