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

Fix "karmor profile --save" command #472

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
40 changes: 31 additions & 9 deletions profile/Client/profileClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -21,6 +22,7 @@
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"
)

Expand Down Expand Up @@ -68,7 +70,7 @@
Pod string
GRPC string
Container string
Save bool
Output string
}

// Model for main Bubble Tea
Expand Down Expand Up @@ -379,25 +381,45 @@

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"))

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

View workflow job for this annotation

GitHub Actions / go-lint

don't use underscores in Go names; var file_name should be fileName
output_file, err := os.Create(file_name)

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

View workflow job for this annotation

GitHub Actions / go-lint

don't use underscores in Go names; var output_file should be outputFile
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)
}
}
}
}

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

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

View workflow job for this annotation

GitHub Actions / go-lint

exported method Profile.MarshalText should have comment or be unexported

func generateRowsFromData(data []pb.Log, Operation string) []table.Row {
var s SomeData
Expand Down Expand Up @@ -465,7 +487,7 @@
s.rows = append(s.rows, row)
}

if o1.Save {
if o1.Output != "" {
if Operation == "File" {
convertToJSON("File", jsondata)
} else if Operation == "Process" {
Expand All @@ -487,7 +509,7 @@
Pod: o.Pod,
GRPC: o.GRPC,
Container: o.Container,
Save: o.Save,
Output: o.Output,
}
p := tea.NewProgram(NewModel(), tea.WithAltScreen())
go func() {
Expand Down
20 changes: 2 additions & 18 deletions recommend/recommend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package recommend

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions utils/output.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading