Skip to content

Commit

Permalink
Moved mask to a func and added test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmsilva1973 committed Sep 2, 2023
1 parent efd15e8 commit 5aeb4a5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
25 changes: 15 additions & 10 deletions pkg/minikube/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,29 @@ import (
"k8s.io/minikube/pkg/util/lock"
)

func maskProxyPassword(v string) string {
parts := strings.Split(v, "=")
if len(parts) == 2 {
key := strings.ToUpper(parts[0])
if key == "HTTP_PROXY" || key == "HTTPS_PROXY" {
pattern := `//([^:]+):[^\@]+@`
regexpPattern := regexp.MustCompile(pattern)
value := regexpPattern.ReplaceAllString(parts[1], "//$1:*****@")
v = key + "=" + value
}
}
return v
}

func showVersionInfo(k8sVersion string, cr cruntime.Manager) {
version, _ := cr.Version()
register.Reg.SetStep(register.PreparingKubernetes)
out.Step(cr.Style(), "Preparing Kubernetes {{.k8sVersion}} on {{.runtime}} {{.runtimeVersion}} ...", out.V{"k8sVersion": k8sVersion, "runtime": cr.Name(), "runtimeVersion": version})
for _, v := range config.DockerOpt {
v = maskProxyPassword(v)
out.Infof("opt {{.docker_option}}", out.V{"docker_option": v})
}
for _, v := range config.DockerEnv {
parts := strings.Split(v, "=")
if len(parts) == 2 {
key := strings.ToUpper(parts[0])
if key == "HTTP_PROXY" || key == "HTTPS_PROXY" {
pattern := `//(\w+):\w+@`
regexpPattern := regexp.MustCompile(pattern)
value := regexpPattern.ReplaceAllString(parts[1], "//$1:*****@")
v = key + "=" + value
}
}
out.Infof("env {{.docker_env}}", out.V{"docker_env": v})
}
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/minikube/node/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2016 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 node

import (
"testing"
)

func Test_maskProxyPassword(t *testing.T) {
type dockerOptTest struct {
input string
output string
}
var tests = []dockerOptTest{
{
input: "cats",
output: "cats",
},
{
input: "myDockerOption=value",
output: "myDockerOption=value",
},
{
input: "http_proxy=http://myproxy.company.com",
output: "HTTP_PROXY=http://myproxy.company.com",
},
{
input: "https_proxy=http://jdoe@myproxy.company.com:8080",
output: "HTTPS_PROXY=http://jdoe@myproxy.company.com:8080",
},
{
input: "https_proxy=https://mary:am$uT8zB(rP@myproxy.company.com:8080",
output: "HTTPS_PROXY=https://mary:*****@myproxy.company.com:8080",
},
{
input: "http_proxy=http://jdoe:mPu3z9uT#!@myproxy.company.com:8080",
output: "HTTP_PROXY=http://jdoe:*****@myproxy.company.com:8080",
},
}
for _, test := range tests {
got := maskProxyPassword(test.input)
if got != test.output {
t.Errorf("maskProxyPassword(\"%v\"): got %v, expected %v", test.input, got, test.output)
}
}
}

0 comments on commit 5aeb4a5

Please sign in to comment.