Skip to content

Commit ab5255e

Browse files
refactor(util): Move maskProxyPassword to pkg/util, update references & tests, introduced MaskProxyPasswordWithKey
1 parent 72d0f24 commit ab5255e

File tree

4 files changed

+143
-75
lines changed

4 files changed

+143
-75
lines changed

Diff for: pkg/minikube/node/config_test.go

-72
This file was deleted.

Diff for: pkg/minikube/node/start.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,7 @@ func validateNetwork(h *host.Host, r command.Runner, imageRepository string) (st
748748
k = strings.ToUpper(k) // let's get the key right away to mask password from output
749749
// If http(s)_proxy contains password, let's not splatter on the screen
750750
if k == "HTTP_PROXY" || k == "HTTPS_PROXY" {
751-
pattern := `//(\w+):.*?@`
752-
regexpPattern := regexp.MustCompile(pattern)
753-
v = regexpPattern.ReplaceAllString(v, "//$1:*****@")
751+
v = util.MaskProxyPassword(v)
754752
}
755753
out.Infof("{{.key}}={{.value}}", out.V{"key": k, "value": v})
756754
ipExcluded := proxy.IsIPExcluded(ip) // Skip warning if minikube ip is already in NO_PROXY

Diff for: pkg/util/utils.go

+40
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"os"
2222
"os/user"
2323
"path/filepath"
24+
"regexp"
2425
"strconv"
26+
"strings"
2527

2628
"github.com/blang/semver/v4"
2729
units "github.com/docker/go-units"
@@ -123,3 +125,41 @@ func RemoveDuplicateStrings(initial []string) []string {
123125
}
124126
return result
125127
}
128+
129+
// MaskProxyPassword masks the password in a proxy URL
130+
func MaskProxyPassword(proxyURL string) string {
131+
// Proxy variable values SHOULD have a value like
132+
// https(s)://<whatever>
133+
parts := strings.Split(proxyURL, "://")
134+
if len(parts) == 2 {
135+
proxyAddress := parts[1]
136+
// Let's store the username, the URL and an optional port address
137+
pattern := `([^:]+):.+(@[\w\.]+)(:\d+)?`
138+
re := regexp.MustCompile(pattern)
139+
matches := re.FindStringSubmatch(proxyAddress)
140+
mask := "*****"
141+
switch len(matches) {
142+
case 4:
143+
return fmt.Sprintf("%s://%s:%s%s%s", parts[0], matches[1], mask, matches[2], matches[3])
144+
case 3:
145+
return fmt.Sprintf("%s//%s:%s@%s", parts[0], matches[1], mask, matches[2])
146+
}
147+
}
148+
return proxyURL
149+
}
150+
151+
// MaskProxyPasswordWithKey masks the password in a proxy URL specified by a key-value pair
152+
func MaskProxyPasswordWithKey(v string) string {
153+
parts := strings.Split(v, "=")
154+
// Is it an attribution variable?
155+
if len(parts) == 2 {
156+
key := strings.ToUpper(parts[0])
157+
// Is it a proxy setting?
158+
if key == "HTTP_PROXY" || key == "HTTPS_PROXY" {
159+
proxyValue := parts[1]
160+
maskedProxyValue := MaskProxyPassword(proxyValue)
161+
return key + "=" + maskedProxyValue
162+
}
163+
}
164+
return v
165+
}

Diff for: pkg/util/utils_test.go

+102
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,105 @@ func TestRemoveDuplicateStrings(t *testing.T) {
208208
})
209209
}
210210
}
211+
212+
func TestMaskProxyPassword(t *testing.T) {
213+
type dockerOptTest struct {
214+
input string
215+
output string
216+
}
217+
var tests = []dockerOptTest{
218+
{
219+
input: "cats",
220+
output: "cats",
221+
},
222+
{
223+
input: "myDockerOption=value",
224+
output: "myDockerOption=value",
225+
},
226+
{
227+
input: "http://minikube.sigs.k8s.io",
228+
output: "http://minikube.sigs.k8s.io",
229+
},
230+
{
231+
input: "http://[email protected]:8080",
232+
output: "http://[email protected]:8080",
233+
},
234+
{
235+
input: "https://mary:[email protected]:8080",
236+
output: "https://mary:*****@minikube.sigs.k8s.io:8080",
237+
},
238+
{
239+
input: "http://jdoe:%n0tRe@al:[email protected]:8080",
240+
output: "http://jdoe:*****@minikube.sigs.k8s.io:8080",
241+
},
242+
{
243+
input: "http://jo@han:n0tRe@al:&[email protected]:8080",
244+
output: "http://jo@han:*****@minikube.sigs.k8s.io:8080",
245+
},
246+
{
247+
input: "http://k@r3n!:an0th3erF@akeP@[email protected]",
248+
output: "http://k@r3n!:*****@minikube.sigs.k8s.io",
249+
},
250+
{
251+
input: "https://fr@ank5t3in:an0th3erF@akeP@[email protected]",
252+
output: "https://fr@ank5t3in:*****@minikube.sigs.k8s.io",
253+
},
254+
}
255+
for _, test := range tests {
256+
got := MaskProxyPassword(test.input)
257+
if got != test.output {
258+
t.Errorf("MaskProxyPassword(\"%v\"): got %v, expected %v", test.input, got, test.output)
259+
}
260+
}
261+
}
262+
263+
func TestMaskProxyPasswordWithKey(t *testing.T) {
264+
type dockerOptTest struct {
265+
input string
266+
output string
267+
}
268+
var tests = []dockerOptTest{
269+
{
270+
input: "cats",
271+
output: "cats",
272+
},
273+
{
274+
input: "myDockerOption=value",
275+
output: "myDockerOption=value",
276+
},
277+
{
278+
input: "http_proxy=http://minikube.sigs.k8s.io",
279+
output: "HTTP_PROXY=http://minikube.sigs.k8s.io",
280+
},
281+
{
282+
input: "https_proxy=http://[email protected]:8080",
283+
output: "HTTPS_PROXY=http://[email protected]:8080",
284+
},
285+
{
286+
input: "https_proxy=https://mary:[email protected]:8080",
287+
output: "HTTPS_PROXY=https://mary:*****@minikube.sigs.k8s.io:8080",
288+
},
289+
{
290+
input: "http_proxy=http://jdoe:%n0tRe@al:[email protected]:8080",
291+
output: "HTTP_PROXY=http://jdoe:*****@minikube.sigs.k8s.io:8080",
292+
},
293+
{
294+
input: "http_proxy=http://jo@han:n0tRe@al:&[email protected]:8080",
295+
output: "HTTP_PROXY=http://jo@han:*****@minikube.sigs.k8s.io:8080",
296+
},
297+
{
298+
input: "http_proxy=http://k@r3n!:an0th3erF@akeP@[email protected]",
299+
output: "HTTP_PROXY=http://k@r3n!:*****@minikube.sigs.k8s.io",
300+
},
301+
{
302+
input: "https_proxy=https://fr@ank5t3in:an0th3erF@akeP@[email protected]",
303+
output: "HTTPS_PROXY=https://fr@ank5t3in:*****@minikube.sigs.k8s.io",
304+
},
305+
}
306+
for _, test := range tests {
307+
got := MaskProxyPasswordWithKey(test.input)
308+
if got != test.output {
309+
t.Errorf("MaskProxyPasswordWithKey(\"%v\"): got %v, expected %v", test.input, got, test.output)
310+
}
311+
}
312+
}

0 commit comments

Comments
 (0)