From 14bcd9ddec0407daf00ebbb6fb07d079f2a34b08 Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Fri, 22 Jan 2021 15:39:15 -0700 Subject: [PATCH] added user flag validation and improved some test messages --- cmd/minikube/cmd/root.go | 5 ++++ pkg/minikube/audit/audit.go | 6 ++-- pkg/minikube/audit/audit_test.go | 2 +- pkg/minikube/config/user.go | 22 +++++++++++++++ pkg/minikube/config/user_test.go | 42 ++++++++++++++++++++++++++++ test/integration/functional_test.go | 4 +-- test/integration/json_output_test.go | 2 +- test/integration/util_test.go | 2 +- 8 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 pkg/minikube/config/user.go create mode 100644 pkg/minikube/config/user_test.go diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 5f79ff60ea..a1710bb05a 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -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" ) @@ -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.") + } }, } diff --git a/pkg/minikube/audit/audit.go b/pkg/minikube/audit/audit.go index c9c6bd6cb4..83afccd671 100644 --- a/pkg/minikube/audit/audit.go +++ b/pkg/minikube/audit/audit.go @@ -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 @@ -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) } diff --git a/pkg/minikube/audit/audit_test.go b/pkg/minikube/audit/audit_test.go index 40afc65014..7deba0b7e7 100644 --- a/pkg/minikube/audit/audit_test.go +++ b/pkg/minikube/audit/audit_test.go @@ -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) diff --git a/pkg/minikube/config/user.go b/pkg/minikube/config/user.go new file mode 100644 index 0000000000..3cbd340874 --- /dev/null +++ b/pkg/minikube/config/user.go @@ -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 +} diff --git a/pkg/minikube/config/user_test.go b/pkg/minikube/config/user_test.go new file mode 100644 index 0000000000..57994fed5c --- /dev/null +++ b/pkg/minikube/config/user_test.go @@ -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) + } + } + }) +} diff --git a/test/integration/functional_test.go b/test/integration/functional_test.go index 00abe33d76..48b2c1b709 100644 --- a/test/integration/functional_test.go +++ b/test/integration/functional_test.go @@ -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) @@ -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) diff --git a/test/integration/json_output_test.go b/test/integration/json_output_test.go index 7ab3b64d07..1d4173725e 100644 --- a/test/integration/json_output_test.go +++ b/test/integration/json_output_test.go @@ -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") diff --git a/test/integration/util_test.go b/test/integration/util_test.go index 5aea77f133..c924b78c2e 100644 --- a/test/integration/util_test.go +++ b/test/integration/util_test.go @@ -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()