Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize audit logging #14596

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions pkg/minikube/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ func LogCommandEnd(id string) error {
if err != nil {
return fmt.Errorf("failed to convert logs to rows: %v", err)
}
auditContents := ""
// have to truncate the audit log while closed as Windows can't truncate an open file
if err := os.Truncate(localpath.AuditLog(), 0); err != nil {
return fmt.Errorf("failed to truncate audit log: %v", err)
}
if err := openAuditLog(); err != nil {
return err
}
var entriesNeedsToUpdate int
for _, v := range rowSlice {
if v.id == id {
Expand All @@ -102,21 +108,13 @@ func LogCommandEnd(id string) error {
if err != nil {
return err
}
auditContents += string(auditLog) + "\n"
if _, err = currentLogFile.WriteString(string(auditLog) + "\n"); err != nil {
return fmt.Errorf("failed to write to audit log: %v", err)
}
}
if entriesNeedsToUpdate == 0 {
return fmt.Errorf("failed to find a log row with id equals to %v", id)
}
// have to truncate the audit log while closed as Windows can't truncate an open file
if err := os.Truncate(localpath.AuditLog(), 0); err != nil {
return fmt.Errorf("failed to truncate audit log: %v", err)
}
if err := openAuditLog(); err != nil {
return err
}
if _, err = currentLogFile.Write([]byte(auditContents)); err != nil {
return fmt.Errorf("failed to write to audit log: %v", err)
}
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/minikube/audit/logFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var currentLogFile *os.File

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

// appendToLog appends the row to the log file.
Expand Down
11 changes: 7 additions & 4 deletions pkg/minikube/audit/logFile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,30 @@ func TestLogFile(t *testing.T) {
if err := openAuditLog(); err != nil {
t.Fatal(err)
}
closeAuditLog()
})

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

oldLogFile := *currentLogFile
defer func() { currentLogFile = &oldLogFile }()
currentLogFile = f
defer closeAuditLog()

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)
}

currentLogFile, err = os.Open(f.Name())
if err != nil {
t.Fatal(err)
}
b := make([]byte, 100)
if _, err := f.Read(b); err != nil && err != io.EOF {
if _, err := currentLogFile.Read(b); err != nil && err != io.EOF {
t.Errorf("Log was not appended to file: %v", err)
}
})
Expand Down
9 changes: 5 additions & 4 deletions pkg/minikube/audit/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package audit

import (
"io"
"os"
"testing"
)
Expand All @@ -30,18 +31,18 @@ func TestReport(t *testing.T) {

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"}
{"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"}
{"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"}`
{"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"}
`

if _, err := f.WriteString(s); err != nil {
t.Fatalf("failed writing to file: %v", err)
}
if _, err := f.Seek(0, 0); err != nil {
if _, err := f.Seek(0, io.SeekStart); err != nil {
t.Fatalf("failed seeking to start of file: %v", err)
}

oldLogFile := *currentLogFile
defer func() { currentLogFile = &oldLogFile }()
currentLogFile = f
defer closeAuditLog()

wantedLines := 2
r, err := Report(wantedLines)
Expand Down