Skip to content

Commit a9cf475

Browse files
Merge pull request #13812 from tyabu12/fix/port-validation
Fix port validation error on specifying tcp/udp or range of ports.
2 parents 913ac55 + afb3956 commit a9cf475

File tree

2 files changed

+107
-24
lines changed

2 files changed

+107
-24
lines changed

cmd/minikube/cmd/start.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
"github.com/Delta456/box-cli-maker/v2"
3636
"github.com/blang/semver/v4"
37+
"github.com/docker/go-connections/nat"
3738
"github.com/docker/machine/libmachine/ssh"
3839
"github.com/google/go-containerregistry/pkg/authn"
3940
"github.com/google/go-containerregistry/pkg/name"
@@ -1246,26 +1247,22 @@ func validateFlags(cmd *cobra.Command, drvName string) {
12461247
validateInsecureRegistry()
12471248
}
12481249

1249-
// This function validates that the --ports are not below 1024 for the host and not outside range
1250+
// validatePorts validates that the --ports are not below 1024 for the host and not outside range
12501251
func validatePorts(ports []string) error {
1251-
for _, portDuplet := range ports {
1252-
parts := strings.Split(portDuplet, ":")
1253-
if len(parts) > 2 {
1254-
ip := parts[0]
1255-
if net.ParseIP(ip) == nil {
1256-
return errors.Errorf("Sorry, the IP address provided with --ports flag is invalid: %s", ip)
1257-
}
1258-
parts = parts[1:]
1259-
}
1260-
for i, port := range parts {
1261-
p, err := strconv.Atoi(port)
1252+
_, portBindingsMap, err := nat.ParsePortSpecs(ports)
1253+
if err != nil {
1254+
return errors.Errorf("Sorry, one of the ports provided with --ports flag is not valid %s (%v)", ports, err)
1255+
}
1256+
for _, portBindings := range portBindingsMap {
1257+
for _, portBinding := range portBindings {
1258+
p, err := strconv.Atoi(portBinding.HostPort)
12621259
if err != nil {
12631260
return errors.Errorf("Sorry, one of the ports provided with --ports flag is not valid %s", ports)
12641261
}
12651262
if p > 65535 || p < 1 {
12661263
return errors.Errorf("Sorry, one of the ports provided with --ports flag is outside range %s", ports)
12671264
}
1268-
if detect.IsMicrosoftWSL() && p < 1024 && i == 0 {
1265+
if detect.IsMicrosoftWSL() && p < 1024 {
12691266
return errors.Errorf("Sorry, you cannot use privileged ports on the host (below 1024) %s", ports)
12701267
}
12711268
}

cmd/minikube/cmd/start_test.go

+97-11
Original file line numberDiff line numberDiff line change
@@ -462,40 +462,126 @@ func TestValidateRuntime(t *testing.T) {
462462
}
463463

464464
func TestValidatePorts(t *testing.T) {
465+
isMicrosoftWSL := detect.IsMicrosoftWSL()
465466
type portTest struct {
467+
// isTarget indicates whether or not the test case is covered
468+
// because validatePorts behaves differently depending on whether process is running in WSL in windows or not.
469+
isTarget bool
466470
ports []string
467471
errorMsg string
468472
}
469473
var tests = []portTest{
470474
{
471-
ports: []string{"test:80"},
472-
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [test:80]",
475+
isTarget: true,
476+
ports: []string{"8080:80"},
477+
errorMsg: "",
478+
},
479+
{
480+
isTarget: true,
481+
ports: []string{"8080:80/tcp", "8080:80/udp"},
482+
errorMsg: "",
483+
},
484+
{
485+
isTarget: true,
486+
ports: []string{"test:8080"},
487+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [test:8080] (Invalid hostPort: test)",
473488
},
474489
{
490+
isTarget: true,
475491
ports: []string{"0:80"},
476492
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0:80]",
477493
},
478494
{
479-
ports: []string{"8080:80", "6443:443"},
495+
isTarget: true,
496+
ports: []string{"0:80/tcp"},
497+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0:80/tcp]",
498+
},
499+
{
500+
isTarget: true,
501+
ports: []string{"65536:80/udp"},
502+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [65536:80/udp] (Invalid hostPort: 65536)",
503+
},
504+
{
505+
isTarget: true,
506+
ports: []string{"0-1:80-81/tcp"},
507+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0-1:80-81/tcp]",
508+
},
509+
{
510+
isTarget: true,
511+
ports: []string{"0-1:80-81/udp"},
512+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0-1:80-81/udp]",
513+
},
514+
{
515+
isTarget: !isMicrosoftWSL,
516+
ports: []string{"80:80", "1023-1025:8023-8025", "1023-1025:8023-8025/tcp", "1023-1025:8023-8025/udp"},
480517
errorMsg: "",
481518
},
482519
{
483-
ports: []string{"127.0.0.1:80:80"},
520+
isTarget: isMicrosoftWSL,
521+
ports: []string{"80:80"},
522+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [80:80]",
523+
},
524+
{
525+
isTarget: isMicrosoftWSL,
526+
ports: []string{"1023-1025:8023-8025"},
527+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025]",
528+
},
529+
{
530+
isTarget: isMicrosoftWSL,
531+
ports: []string{"1023-1025:8023-8025/tcp"},
532+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025/tcp]",
533+
},
534+
{
535+
isTarget: isMicrosoftWSL,
536+
ports: []string{"1023-1025:8023-8025/udp"},
537+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025/udp]",
538+
},
539+
{
540+
isTarget: true,
541+
ports: []string{"127.0.0.1:8080:80", "127.0.0.1:8081:80/tcp", "127.0.0.1:8081:80/udp", "127.0.0.1:8082-8083:8082-8083/tcp"},
484542
errorMsg: "",
485543
},
486544
{
545+
isTarget: true,
487546
ports: []string{"1000.0.0.1:80:80"},
488-
errorMsg: "Sorry, the IP address provided with --ports flag is invalid: 1000.0.0.1",
547+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [1000.0.0.1:80:80] (Invalid ip address: 1000.0.0.1)",
548+
},
549+
{
550+
isTarget: !isMicrosoftWSL,
551+
ports: []string{"127.0.0.1:80:80", "127.0.0.1:81:81/tcp", "127.0.0.1:81:81/udp", "127.0.0.1:82-83:82-83/tcp", "127.0.0.1:82-83:82-83/udp"},
552+
errorMsg: "",
553+
},
554+
{
555+
isTarget: isMicrosoftWSL,
556+
ports: []string{"127.0.0.1:80:80"},
557+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80:80]",
558+
},
559+
{
560+
isTarget: isMicrosoftWSL,
561+
ports: []string{"127.0.0.1:81:81/tcp"},
562+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:81:81/tcp]",
563+
},
564+
{
565+
isTarget: isMicrosoftWSL,
566+
ports: []string{"127.0.0.1:81:81/udp"},
567+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:81:81/udp]",
568+
},
569+
{
570+
isTarget: isMicrosoftWSL,
571+
ports: []string{"127.0.0.1:80-83:80-83/tcp"},
572+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80-83:80-83/tcp]",
573+
},
574+
{
575+
isTarget: isMicrosoftWSL,
576+
ports: []string{"127.0.0.1:80-83:80-83/udp"},
577+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80-83:80-83/udp]",
489578
},
490-
}
491-
if detect.IsMicrosoftWSL() {
492-
tests = append(tests, portTest{
493-
ports: []string{"80:80"},
494-
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [80:80]",
495-
})
496579
}
497580
for _, test := range tests {
498581
t.Run(strings.Join(test.ports, ","), func(t *testing.T) {
582+
if !test.isTarget {
583+
return
584+
}
499585
gotError := ""
500586
got := validatePorts(test.ports)
501587
if got != nil {

0 commit comments

Comments
 (0)