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 16343ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 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
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

0 comments on commit 16343ff

Please sign in to comment.