Skip to content

Commit

Permalink
Made changes so that we get logs when a command starts no matter that…
Browse files Browse the repository at this point in the history
… it is finished or not
  • Loading branch information
NikhilSharmaWe committed Jan 7, 2022
1 parent 72e946c commit c7159e7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
13 changes: 10 additions & 3 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"path/filepath"
"runtime"
"strings"
"time"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -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
Expand Down
39 changes: 34 additions & 5 deletions pkg/minikube/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions pkg/minikube/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
3 changes: 2 additions & 1 deletion pkg/minikube/audit/logFile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"k8s.io/minikube/pkg/minikube/localpath"
)

Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/minikube/audit/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type row struct {
startTime string
user string
version string
id string
Data map[string]string `json:"data"`
}

Expand Down Expand Up @@ -72,19 +73,19 @@ 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]
}
return &row{
args: args,
command: command,
endTime: endTime.Format(constants.TimeFormat),
profile: p,
startTime: startTime.Format(constants.TimeFormat),
user: user,
version: version,
id: id,
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/minikube/audit/row_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"k8s.io/minikube/pkg/minikube/constants"
)

Expand All @@ -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 {
Expand Down

0 comments on commit c7159e7

Please sign in to comment.