Skip to content

Add new flag --user and to log executed commands #10106

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

Merged
merged 7 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/translate"
)
Expand Down Expand Up @@ -64,6 +65,10 @@ var RootCmd = &cobra.Command{
exit.Error(reason.HostHomeMkdir, "Error creating minikube directory", err)
}
}
if !config.UserNameValid(viper.GetString(config.UserFlag)) {
out.WarningT("User name '{{.username}}' is not valid", out.V{"username": audit.UserName()})
exit.Message(reason.Usage, "User name must be 60 chars or less.")
}
},
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/minikube/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"k8s.io/minikube/pkg/minikube/config"
)

// username pulls the user flag, if empty gets the os username.
func username() string {
// UserName pulls the user flag, if empty gets the os username.
func UserName() string {
u := viper.GetString(config.UserFlag)
if u != "" {
return u
Expand All @@ -54,7 +54,7 @@ func Log(startTime time.Time) {
if !shouldLog() {
return
}
e := newEntry(os.Args[1], args(), username(), startTime, time.Now())
e := newEntry(os.Args[1], args(), UserName(), startTime, time.Now())
if err := appendToLog(e); err != nil {
klog.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestAudit(t *testing.T) {
for _, test := range tests {
viper.Set(config.UserFlag, test.userFlag)

got := username()
got := UserName()

if got != test.want {
t.Errorf("userFlag = %q; username() = %q; want %q", test.userFlag, got, test.want)
Expand Down
22 changes: 22 additions & 0 deletions pkg/minikube/config/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

// UserNameValid checks if the user name is valid.
func UserNameValid(name string) bool {
return len(name) <= 60
}
42 changes: 42 additions & 0 deletions pkg/minikube/config/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"strings"
"testing"
)

func TestUser(t *testing.T) {
t.Run("Length", func(t *testing.T) {
tests := []struct {
in string
want bool
}{
{strings.Repeat("a", 60), true},
{strings.Repeat("a", 61), false},
}

for _, tt := range tests {
got := UserNameValid(tt.in)

if got != tt.want {
t.Errorf("UserNameValid(%q, length: %d) = %t; want %t", tt.in, len(tt.in), got, tt.want)
}
}
})
}
4 changes: 2 additions & 2 deletions test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func validateStartWithProxy(ctx context.Context, t *testing.T, profile string) {
t.Run("Audit", func(t *testing.T) {
got, err := auditContains(profile)
if err != nil {
t.Fatal(err)
t.Fatalf("failed to check audit log: %v", err)
}
if !got {
t.Errorf("audit.json does not contain the profile %q", profile)
Expand Down Expand Up @@ -285,7 +285,7 @@ func validateSoftStart(ctx context.Context, t *testing.T, profile string) {
t.Run("Audit", func(t *testing.T) {
got, err := auditContains(profile)
if err != nil {
t.Fatal(err)
t.Fatalf("failed to check audit log: %v", err)
}
if !got {
t.Errorf("audit.json does not contain the profile %q", profile)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/json_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestJSONOutput(t *testing.T) {
t.Run("Audit", func(t *testing.T) {
got, err := auditContains("testUser")
if err != nil {
t.Fatal(err)
t.Fatalf("failed to check audit log: %v", err)
}
if !got {
t.Errorf("audit.json does not contain the user testUser")
Expand Down
2 changes: 1 addition & 1 deletion test/integration/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func UniqueProfileName(prefix string) string {
func auditContains(substr string) (bool, error) {
f, err := os.Open(localpath.AuditLog())
if err != nil {
return false, err
return false, fmt.Errorf("Unable to open file %s: %v", localpath.AuditLog(), err)
}
defer f.Close()

Expand Down