Skip to content

Commit 513a757

Browse files
committed
Fix port validation error on specifying tcp/udp or range of ports.
1 parent 3f25d3e commit 513a757

File tree

2 files changed

+104
-23
lines changed

2 files changed

+104
-23
lines changed

cmd/minikube/cmd/start.go

+9-12
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,24 +1247,20 @@ func validateFlags(cmd *cobra.Command, drvName string) {
12461247

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

cmd/minikube/cmd/start_test.go

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

464464
func TestValidatePorts(t *testing.T) {
465+
isMicrosoftWSL := detect.IsMicrosoftWSL()
465466
type portTest struct {
467+
isTarget bool
466468
ports []string
467469
errorMsg string
468470
}
469471
var tests = []portTest{
470472
{
471-
ports: []string{"test:80"},
472-
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [test:80]",
473+
isTarget: true,
474+
ports: []string{"8080:80"},
475+
errorMsg: "",
476+
},
477+
{
478+
isTarget: true,
479+
ports: []string{"8080:80/tcp", "8080:80/udp"},
480+
errorMsg: "",
481+
},
482+
{
483+
isTarget: true,
484+
ports: []string{"test:8080"},
485+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [test:8080] (Invalid hostPort: test)",
473486
},
474487
{
488+
isTarget: true,
475489
ports: []string{"0:80"},
476490
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0:80]",
477491
},
478492
{
479-
ports: []string{"8080:80", "6443:443"},
493+
isTarget: true,
494+
ports: []string{"0:80/tcp"},
495+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0:80/tcp]",
496+
},
497+
{
498+
isTarget: true,
499+
ports: []string{"65536:80/udp"},
500+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [65536:80/udp] (Invalid hostPort: 65536)",
501+
},
502+
{
503+
isTarget: true,
504+
ports: []string{"0-1:80-81/tcp"},
505+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0-1:80-81/tcp]",
506+
},
507+
{
508+
isTarget: true,
509+
ports: []string{"0-1:80-81/udp"},
510+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0-1:80-81/udp]",
511+
},
512+
{
513+
isTarget: !isMicrosoftWSL,
514+
ports: []string{"80:80", "1023-1025:8023-8025", "1023-1025:8023-8025/tcp", "1023-1025:8023-8025/udp"},
480515
errorMsg: "",
481516
},
482517
{
483-
ports: []string{"127.0.0.1:80:80"},
518+
isTarget: isMicrosoftWSL,
519+
ports: []string{"80:80"},
520+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [80:80]",
521+
},
522+
{
523+
isTarget: isMicrosoftWSL,
524+
ports: []string{"1023-1025:8023-8025"},
525+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025]",
526+
},
527+
{
528+
isTarget: isMicrosoftWSL,
529+
ports: []string{"1023-1025:8023-8025/tcp"},
530+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025/tcp]",
531+
},
532+
{
533+
isTarget: isMicrosoftWSL,
534+
ports: []string{"1023-1025:8023-8025/udp"},
535+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025/udp]",
536+
},
537+
{
538+
isTarget: true,
539+
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"},
484540
errorMsg: "",
485541
},
486542
{
543+
isTarget: true,
487544
ports: []string{"1000.0.0.1:80:80"},
488-
errorMsg: "Sorry, the IP address provided with --ports flag is invalid: 1000.0.0.1",
545+
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)",
546+
},
547+
{
548+
isTarget: !isMicrosoftWSL,
549+
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"},
550+
errorMsg: "",
551+
},
552+
{
553+
isTarget: isMicrosoftWSL,
554+
ports: []string{"127.0.0.1:80:80"},
555+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80:80]",
556+
},
557+
{
558+
isTarget: isMicrosoftWSL,
559+
ports: []string{"127.0.0.1:81:81/tcp"},
560+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:81:81/tcp]",
561+
},
562+
{
563+
isTarget: isMicrosoftWSL,
564+
ports: []string{"127.0.0.1:81:81/udp"},
565+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:81:81/udp]",
566+
},
567+
{
568+
isTarget: isMicrosoftWSL,
569+
ports: []string{"127.0.0.1:80-83:80-83/tcp"},
570+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80-83:80-83/tcp]",
571+
},
572+
{
573+
isTarget: isMicrosoftWSL,
574+
ports: []string{"127.0.0.1:80-83:80-83/udp"},
575+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [127.0.0.1:80-83:80-83/udp]",
489576
},
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-
})
496577
}
497578
for _, test := range tests {
498579
t.Run(strings.Join(test.ports, ","), func(t *testing.T) {
580+
if !test.isTarget {
581+
return
582+
}
499583
gotError := ""
500584
got := validatePorts(test.ports)
501585
if got != nil {

0 commit comments

Comments
 (0)