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/audit_test.go b/pkg/minikube/audit/audit_test.go index 7c36ae655642..7d33da5104ea 100644 --- a/pkg/minikube/audit/audit_test.go +++ b/pkg/minikube/audit/audit_test.go @@ -170,11 +170,19 @@ func TestAudit(t *testing.T) { }) // Check if logging with limited args causes a panic - t.Run("Log", func(t *testing.T) { + t.Run("LogCommandStart", func(t *testing.T) { oldArgs := os.Args defer func() { os.Args = oldArgs }() os.Args = []string{"minikube"} - Log(time.Now()) + LogCommandStart() + }) + + t.Run("LogCommandEnd", func(t *testing.T) { + oldArgs := os.Args + defer func() { os.Args = oldArgs }() + os.Args = []string{"minikube"} + + LogCommandEnd(uuid.New().String()) }) } diff --git a/pkg/minikube/audit/logFile_test.go b/pkg/minikube/audit/logFile_test.go index 617a43b8e0b6..1757df00d07e 100644 --- a/pkg/minikube/audit/logFile_test.go +++ b/pkg/minikube/audit/logFile_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "github.com/google/uuid" "k8s.io/minikube/pkg/minikube/localpath" ) @@ -48,7 +49,7 @@ func TestLogFile(t *testing.T) { defer func() { currentLogFile = &oldLogFile }() currentLogFile = f - r := newRow("start", "-v", "user1", "v0.17.1", time.Now(), time.Now()) + r := newRow("start", "-v", "user1", "v0.17.1", time.Now(), uuid.New().String()) if err := appendToLog(r); err != nil { t.Fatalf("Error appendingToLog: %v", err) } 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, } } diff --git a/pkg/minikube/audit/row_test.go b/pkg/minikube/audit/row_test.go index 88b54174dfa9..bd8f7838a320 100644 --- a/pkg/minikube/audit/row_test.go +++ b/pkg/minikube/audit/row_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "github.com/google/uuid" "k8s.io/minikube/pkg/minikube/constants" ) @@ -34,10 +35,9 @@ func TestRow(t *testing.T) { v := "v0.17.1" st := time.Now() stFormatted := st.Format(constants.TimeFormat) - et := time.Now() - etFormatted := et.Format(constants.TimeFormat) + id := uuid.New().String() - r := newRow(c, a, u, v, st, et, p) + r := newRow(c, a, u, v, st, id, p) t.Run("NewRow", func(t *testing.T) { tests := []struct {