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

Limit number of audit entries #14695

Merged
merged 2 commits into from
Aug 2, 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
4 changes: 4 additions & 0 deletions cmd/minikube/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ var settings = []Setting{
name: config.Rootless,
set: SetBool,
},
{
name: config.MaxAuditEntries,
set: SetInt,
},
}

// ConfigCmd represents the config command
Expand Down
1 change: 1 addition & 0 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func setupViper() {
viper.SetDefault(config.ReminderWaitPeriodInHours, 24)
viper.SetDefault(config.WantNoneDriverWarning, true)
viper.SetDefault(config.WantVirtualBoxDriverWarning, true)
viper.SetDefault(config.MaxAuditEntries, 1000)
}

func addToPath(dir string) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/minikube/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func LogCommandEnd(id string) error {
return err
}
var entriesNeedsToUpdate int

startIndex := getStartIndex(len(rowSlice))
rowSlice = rowSlice[startIndex:]
for _, v := range rowSlice {
if v.id == id {
v.endTime = time.Now().Format(constants.TimeFormat)
Expand All @@ -118,6 +121,15 @@ func LogCommandEnd(id string) error {
return nil
}

func getStartIndex(entryCount int) int {
maxEntries := viper.GetInt(config.MaxAuditEntries)
startIndex := entryCount - maxEntries
if maxEntries <= 0 || startIndex <= 0 {
return 0
}
return startIndex
}

// shouldLog returns if the command should be logged.
func shouldLog() bool {
// in rare chance we get here without a command, don't log
Expand Down
40 changes: 38 additions & 2 deletions pkg/minikube/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package audit

import (
"io"
"os"
"os/exec"
"os/user"
"strings"
"testing"

"github.com/spf13/pflag"
Expand All @@ -27,6 +30,33 @@ import (
)

func TestAudit(t *testing.T) {
var auditFilename string

t.Run("setup", func(t *testing.T) {
f, err := os.CreateTemp("", "audit.json")
if err != nil {
t.Fatalf("failed creating temporary file: %v", err)
}
auditFilename = f.Name()

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

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

currentLogFile = f
viper.Set(config.MaxAuditEntries, 3)
})

t.Run("username", func(t *testing.T) {
u, err := user.Current()
if err != nil {
Expand Down Expand Up @@ -168,7 +198,6 @@ func TestAudit(t *testing.T) {
mockArgs(t, test.args)

got := isDeletePurge()

if got != test.want {
t.Errorf("test.args = %q; isDeletePurge() = %t; want %t", test.args, got, test.want)
}
Expand Down Expand Up @@ -211,11 +240,18 @@ func TestAudit(t *testing.T) {
if err != nil {
t.Fatal("start failed")
}
err = LogCommandEnd(auditID)
if err := LogCommandEnd(auditID); err != nil {
t.Fatal(err)
}

b, err := exec.Command("wc", "-l", auditFilename).Output()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(b), "3") {
t.Errorf("MaxAuditEntries did not work, expected 3 lines in the audit log found %s", string(b))
}

})

t.Run("LogCommandEndNonExistingID", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const (
AddonListFlag = "addons"
// EmbedCerts represents the config for embedding certificates in kubeconfig
EmbedCerts = "EmbedCerts"
// MaxAuditEntries is the maximum number of audit entries to retain
MaxAuditEntries = "MaxAuditEntries"
)

var (
Expand Down