From 16343ffd6b377a2e0ded0b130b62bf743e8ea026 Mon Sep 17 00:00:00 2001 From: Nikhil Sharma Date: Fri, 7 Jan 2022 10:18:51 +0530 Subject: [PATCH] Made changes so that we get logs when a command starts no matter that it is finished or not --- cmd/minikube/cmd/root.go | 13 ++++++++++--- pkg/minikube/audit/audit.go | 39 ++++++++++++++++++++++++++++++++----- pkg/minikube/audit/row.go | 5 +++-- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index dd5d239a8e5f..8cf860a036a4 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -23,7 +23,6 @@ import ( "path/filepath" "runtime" "strings" - "time" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -78,8 +77,16 @@ var RootCmd = &cobra.Command{ // Execute adds all child commands to the root command sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - defer audit.Log(time.Now()) - + auditID, err := audit.LogCommandStart() + if err != nil{ + klog.Errorf("%v", err) + } + defer func(){ + err := audit.LogCommandEnd(auditID) + if err != nil{ + klog.Errorf("%v", err) + } + }() // Check whether this is a windows binary (.exe) running inisde WSL. if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() { var found = false diff --git a/pkg/minikube/audit/audit.go b/pkg/minikube/audit/audit.go index 7deb87972526..a02379df0f14 100644 --- a/pkg/minikube/audit/audit.go +++ b/pkg/minikube/audit/audit.go @@ -17,14 +17,17 @@ limitations under the License. package audit import ( + "encoding/json" + "fmt" "os" "os/user" "strings" "time" + "github.com/google/uuid" "github.com/spf13/viper" - "k8s.io/klog/v2" "k8s.io/minikube/pkg/minikube/config" + "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/version" ) @@ -51,14 +54,40 @@ func args() string { } // Log details about the executed command. -func Log(startTime time.Time) { +func LogCommandStart() (string, error) { if len(os.Args) < 2 || !shouldLog() { - return + return "", fmt.Errorf("This command should not be logged and len(os.Args) : %v, should be less than 2", len(os.Args)) } - r := newRow(os.Args[1], args(), userName(), version.GetVersion(), startTime, time.Now()) + id := uuid.New().String() + r := newRow(os.Args[1], args(), userName(), version.GetVersion(), time.Now(), id) if err := appendToLog(r); err != nil { - klog.Warning(err) + return "", fmt.Errorf("%v", err) } + return r.id, nil +} + +func LogCommandEnd(id string) error { + if currentLogFile == nil { + if err := setLogFile(); err != nil { + return fmt.Errorf("failed to set the log file: %v", err) + } + } + var auditLogs []byte + _, err := currentLogFile.Read(auditLogs) + if err != nil { + return fmt.Errorf("%v", err) + } + var rowSlice []row + err = json.Unmarshal(auditLogs, &rowSlice) + if err != nil { + return fmt.Errorf("%v", err) + } + for _, v := range rowSlice { + if v.id == id { + v.endTime = time.Now().Format(constants.TimeFormat) + } + } + return nil } // shouldLog returns if the command should be logged. diff --git a/pkg/minikube/audit/row.go b/pkg/minikube/audit/row.go index fb5991a0085a..949c66b058c3 100644 --- a/pkg/minikube/audit/row.go +++ b/pkg/minikube/audit/row.go @@ -37,6 +37,7 @@ type row struct { startTime string user string version string + id string Data map[string]string `json:"data"` } @@ -72,7 +73,7 @@ func (e *row) toMap() map[string]string { } // newRow creates a new audit row. -func newRow(command string, args string, user string, version string, startTime time.Time, endTime time.Time, profile ...string) *row { +func newRow(command string, args string, user string, version string, startTime time.Time, id string, profile ...string) *row { p := viper.GetString(config.ProfileName) if len(profile) > 0 { p = profile[0] @@ -80,11 +81,11 @@ func newRow(command string, args string, user string, version string, startTime return &row{ args: args, command: command, - endTime: endTime.Format(constants.TimeFormat), profile: p, startTime: startTime.Format(constants.TimeFormat), user: user, version: version, + id: id, } }