@@ -17,14 +17,17 @@ limitations under the License.
17
17
package audit
18
18
19
19
import (
20
+ "encoding/json"
21
+ "fmt"
20
22
"os"
21
23
"os/user"
22
24
"strings"
23
25
"time"
24
26
27
+ "github.com/google/uuid"
25
28
"github.com/spf13/viper"
26
- "k8s.io/klog/v2"
27
29
"k8s.io/minikube/pkg/minikube/config"
30
+ "k8s.io/minikube/pkg/minikube/constants"
28
31
"k8s.io/minikube/pkg/version"
29
32
)
30
33
@@ -51,14 +54,40 @@ func args() string {
51
54
}
52
55
53
56
// Log details about the executed command.
54
- func Log ( startTime time. Time ) {
57
+ func LogCommandStart () ( string , error ) {
55
58
if len (os .Args ) < 2 || ! shouldLog () {
56
- return
59
+ return "" , fmt . Errorf ( "This command should not be logged and len(os.Args) : %v, should be less than 2" , len ( os . Args ))
57
60
}
58
- r := newRow (os .Args [1 ], args (), userName (), version .GetVersion (), startTime , time .Now ())
61
+ id := uuid .New ().String ()
62
+ r := newRow (os .Args [1 ], args (), userName (), version .GetVersion (), time .Now (), id )
59
63
if err := appendToLog (r ); err != nil {
60
- klog . Warning ( err )
64
+ return "" , fmt . Errorf ( "%v" , err )
61
65
}
66
+ return r .id , nil
67
+ }
68
+
69
+ func LogCommandEnd (id string ) error {
70
+ if currentLogFile == nil {
71
+ if err := setLogFile (); err != nil {
72
+ return fmt .Errorf ("failed to set the log file: %v" , err )
73
+ }
74
+ }
75
+ var auditLogs []byte
76
+ _ , err := currentLogFile .Read (auditLogs )
77
+ if err != nil {
78
+ return fmt .Errorf ("%v" , err )
79
+ }
80
+ var rowSlice []row
81
+ err = json .Unmarshal (auditLogs , & rowSlice )
82
+ if err != nil {
83
+ return fmt .Errorf ("%v" , err )
84
+ }
85
+ for _ , v := range rowSlice {
86
+ if v .id == id {
87
+ v .endTime = time .Now ().Format (constants .TimeFormat )
88
+ }
89
+ }
90
+ return nil
62
91
}
63
92
64
93
// shouldLog returns if the command should be logged.
0 commit comments