Skip to content

Commit 757b877

Browse files
authored
Merge pull request #14596 from spowelljr/optimizeAudit
Optimize audit logging
2 parents 33aac3d + 709cd58 commit 757b877

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

pkg/minikube/audit/audit.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ func LogCommandEnd(id string) error {
9090
if err != nil {
9191
return fmt.Errorf("failed to convert logs to rows: %v", err)
9292
}
93-
auditContents := ""
93+
// have to truncate the audit log while closed as Windows can't truncate an open file
94+
if err := os.Truncate(localpath.AuditLog(), 0); err != nil {
95+
return fmt.Errorf("failed to truncate audit log: %v", err)
96+
}
97+
if err := openAuditLog(); err != nil {
98+
return err
99+
}
94100
var entriesNeedsToUpdate int
95101
for _, v := range rowSlice {
96102
if v.id == id {
@@ -102,21 +108,13 @@ func LogCommandEnd(id string) error {
102108
if err != nil {
103109
return err
104110
}
105-
auditContents += string(auditLog) + "\n"
111+
if _, err = currentLogFile.WriteString(string(auditLog) + "\n"); err != nil {
112+
return fmt.Errorf("failed to write to audit log: %v", err)
113+
}
106114
}
107115
if entriesNeedsToUpdate == 0 {
108116
return fmt.Errorf("failed to find a log row with id equals to %v", id)
109117
}
110-
// have to truncate the audit log while closed as Windows can't truncate an open file
111-
if err := os.Truncate(localpath.AuditLog(), 0); err != nil {
112-
return fmt.Errorf("failed to truncate audit log: %v", err)
113-
}
114-
if err := openAuditLog(); err != nil {
115-
return err
116-
}
117-
if _, err = currentLogFile.Write([]byte(auditContents)); err != nil {
118-
return fmt.Errorf("failed to write to audit log: %v", err)
119-
}
120118
return nil
121119
}
122120

pkg/minikube/audit/logFile.go

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var currentLogFile *os.File
3030

3131
// openAuditLog opens the audit log file or creates it if it doesn't exist.
3232
func openAuditLog() error {
33+
// this is so we can manually set the log file for tests
34+
if currentLogFile != nil {
35+
return nil
36+
}
3337
lp := localpath.AuditLog()
3438
f, err := os.OpenFile(lp, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
3539
if err != nil {
@@ -44,6 +48,7 @@ func closeAuditLog() {
4448
if err := currentLogFile.Close(); err != nil {
4549
klog.Errorf("failed to close the audit log: %v", err)
4650
}
51+
currentLogFile = nil
4752
}
4853

4954
// appendToLog appends the row to the log file.

pkg/minikube/audit/logFile_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,30 @@ func TestLogFile(t *testing.T) {
3636
if err := openAuditLog(); err != nil {
3737
t.Fatal(err)
3838
}
39+
closeAuditLog()
3940
})
4041

4142
t.Run("AppendToLog", func(t *testing.T) {
42-
defer closeAuditLog()
4343
f, err := os.CreateTemp("", "audit.json")
4444
if err != nil {
4545
t.Fatalf("Error creating temporary file: %v", err)
4646
}
4747
defer os.Remove(f.Name())
4848

49-
oldLogFile := *currentLogFile
50-
defer func() { currentLogFile = &oldLogFile }()
5149
currentLogFile = f
50+
defer closeAuditLog()
5251

5352
r := newRow("start", "-v", "user1", "v0.17.1", time.Now(), uuid.New().String())
5453
if err := appendToLog(r); err != nil {
5554
t.Fatalf("Error appendingToLog: %v", err)
5655
}
5756

57+
currentLogFile, err = os.Open(f.Name())
58+
if err != nil {
59+
t.Fatal(err)
60+
}
5861
b := make([]byte, 100)
59-
if _, err := f.Read(b); err != nil && err != io.EOF {
62+
if _, err := currentLogFile.Read(b); err != nil && err != io.EOF {
6063
t.Errorf("Log was not appended to file: %v", err)
6164
}
6265
})

pkg/minikube/audit/report_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package audit
1818

1919
import (
20+
"io"
2021
"os"
2122
"testing"
2223
)
@@ -30,18 +31,18 @@ func TestReport(t *testing.T) {
3031

3132
s := `{"data":{"args":"-p mini1","command":"start","endTime":"Wed, 03 Feb 2021 15:33:05 MST","profile":"mini1","startTime":"Wed, 03 Feb 2021 15:30:33 MST","user":"user1"},"datacontenttype":"application/json","id":"9b7593cb-fbec-49e5-a3ce-bdc2d0bfb208","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.si gs.minikube.audit"}
3233
{"data":{"args":"-p mini1","command":"start","endTime":"Wed, 03 Feb 2021 15:33:05 MST","profile":"mini1","startTime":"Wed, 03 Feb 2021 15:30:33 MST","user":"user1"},"datacontenttype":"application/json","id":"9b7593cb-fbec-49e5-a3ce-bdc2d0bfb208","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.si gs.minikube.audit"}
33-
{"data":{"args":"--user user2","command":"logs","endTime":"Tue, 02 Feb 2021 16:46:20 MST","profile":"minikube","startTime":"Tue, 02 Feb 2021 16:46:00 MST","user":"user2"},"datacontenttype":"application/json","id":"fec03227-2484-48b6-880a-88fd010b5efd","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.audit"}`
34+
{"data":{"args":"--user user2","command":"logs","endTime":"Tue, 02 Feb 2021 16:46:20 MST","profile":"minikube","startTime":"Tue, 02 Feb 2021 16:46:00 MST","user":"user2"},"datacontenttype":"application/json","id":"fec03227-2484-48b6-880a-88fd010b5efd","source":"https://minikube.sigs.k8s.io/","specversion":"1.0","type":"io.k8s.sigs.minikube.audit"}
35+
`
3436

3537
if _, err := f.WriteString(s); err != nil {
3638
t.Fatalf("failed writing to file: %v", err)
3739
}
38-
if _, err := f.Seek(0, 0); err != nil {
40+
if _, err := f.Seek(0, io.SeekStart); err != nil {
3941
t.Fatalf("failed seeking to start of file: %v", err)
4042
}
4143

42-
oldLogFile := *currentLogFile
43-
defer func() { currentLogFile = &oldLogFile }()
4444
currentLogFile = f
45+
defer closeAuditLog()
4546

4647
wantedLines := 2
4748
r, err := Report(wantedLines)

0 commit comments

Comments
 (0)