Skip to content

Commit 399a60e

Browse files
authored
Merge pull request #15085 from spowelljr/fixPortValidation
Allow passing only exposed port to --ports
2 parents 5b4b685 + 26ff0aa commit 399a60e

File tree

2 files changed

+94
-25
lines changed

2 files changed

+94
-25
lines changed

cmd/minikube/cmd/start.go

+36-12
Original file line numberDiff line numberDiff line change
@@ -1256,24 +1256,48 @@ func validateFlags(cmd *cobra.Command, drvName string) {
12561256

12571257
// validatePorts validates that the --ports are not below 1024 for the host and not outside range
12581258
func validatePorts(ports []string) error {
1259-
_, portBindingsMap, err := nat.ParsePortSpecs(ports)
1259+
var exposedPorts, hostPorts, portSpecs []string
1260+
for _, p := range ports {
1261+
if strings.Contains(p, ":") {
1262+
portSpecs = append(portSpecs, p)
1263+
} else {
1264+
exposedPorts = append(exposedPorts, p)
1265+
}
1266+
}
1267+
_, portBindingsMap, err := nat.ParsePortSpecs(portSpecs)
12601268
if err != nil {
12611269
return errors.Errorf("Sorry, one of the ports provided with --ports flag is not valid %s (%v)", ports, err)
12621270
}
1263-
for _, portBindings := range portBindingsMap {
1271+
for exposedPort, portBindings := range portBindingsMap {
1272+
exposedPorts = append(exposedPorts, exposedPort.Port())
12641273
for _, portBinding := range portBindings {
1265-
p, err := strconv.Atoi(portBinding.HostPort)
1266-
if err != nil {
1267-
return errors.Errorf("Sorry, one of the ports provided with --ports flag is not valid %s", ports)
1268-
}
1269-
if p > 65535 || p < 1 {
1270-
return errors.Errorf("Sorry, one of the ports provided with --ports flag is outside range %s", ports)
1271-
}
1272-
if detect.IsMicrosoftWSL() && p < 1024 {
1273-
return errors.Errorf("Sorry, you cannot use privileged ports on the host (below 1024) %s", ports)
1274-
}
1274+
hostPorts = append(hostPorts, portBinding.HostPort)
1275+
}
1276+
}
1277+
for _, p := range exposedPorts {
1278+
if err := validatePort(p, false); err != nil {
1279+
return err
12751280
}
12761281
}
1282+
for _, p := range hostPorts {
1283+
if err := validatePort(p, true); err != nil {
1284+
return err
1285+
}
1286+
}
1287+
return nil
1288+
}
1289+
1290+
func validatePort(port string, isHost bool) error {
1291+
p, err := strconv.Atoi(port)
1292+
if err != nil {
1293+
return errors.Errorf("Sorry, one of the ports provided with --ports flag is not valid: %s", port)
1294+
}
1295+
if p > 65535 || p < 1 {
1296+
return errors.Errorf("Sorry, one of the ports provided with --ports flag is outside range: %s", port)
1297+
}
1298+
if isHost && detect.IsMicrosoftWSL() && p < 1024 {
1299+
return errors.Errorf("Sorry, you cannot use privileged ports on the host (below 1024): %s", port)
1300+
}
12771301
return nil
12781302
}
12791303

cmd/minikube/cmd/start_test.go

+58-13
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,12 @@ func TestValidatePorts(t *testing.T) {
489489
{
490490
isTarget: true,
491491
ports: []string{"0:80"},
492-
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0:80]",
492+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range: 0",
493493
},
494494
{
495495
isTarget: true,
496496
ports: []string{"0:80/tcp"},
497-
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range [0:80/tcp]",
497+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range: 0",
498498
},
499499
{
500500
isTarget: true,
@@ -504,12 +504,12 @@ func TestValidatePorts(t *testing.T) {
504504
{
505505
isTarget: true,
506506
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]",
507+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range: 0",
508508
},
509509
{
510510
isTarget: true,
511511
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]",
512+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range: 0",
513513
},
514514
{
515515
isTarget: !isMicrosoftWSL,
@@ -519,22 +519,22 @@ func TestValidatePorts(t *testing.T) {
519519
{
520520
isTarget: isMicrosoftWSL,
521521
ports: []string{"80:80"},
522-
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [80:80]",
522+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 80",
523523
},
524524
{
525525
isTarget: isMicrosoftWSL,
526526
ports: []string{"1023-1025:8023-8025"},
527-
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024) [1023-1025:8023-8025]",
527+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 1023",
528528
},
529529
{
530530
isTarget: isMicrosoftWSL,
531531
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]",
532+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 1023",
533533
},
534534
{
535535
isTarget: isMicrosoftWSL,
536536
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]",
537+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 1023",
538538
},
539539
{
540540
isTarget: true,
@@ -554,27 +554,72 @@ func TestValidatePorts(t *testing.T) {
554554
{
555555
isTarget: isMicrosoftWSL,
556556
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]",
557+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 80",
558558
},
559559
{
560560
isTarget: isMicrosoftWSL,
561561
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]",
562+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 81",
563563
},
564564
{
565565
isTarget: isMicrosoftWSL,
566566
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]",
567+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 81",
568568
},
569569
{
570570
isTarget: isMicrosoftWSL,
571571
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]",
572+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 80",
573573
},
574574
{
575575
isTarget: isMicrosoftWSL,
576576
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]",
577+
errorMsg: "Sorry, you cannot use privileged ports on the host (below 1024): 80",
578+
},
579+
{
580+
isTarget: true,
581+
ports: []string{"80"},
582+
errorMsg: "",
583+
},
584+
{
585+
isTarget: true,
586+
ports: []string{"80", "65535", "65536"},
587+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range: 65536",
588+
},
589+
{
590+
isTarget: true,
591+
ports: []string{"0", "80", "65535"},
592+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range: 0",
593+
},
594+
{
595+
isTarget: true,
596+
ports: []string{"cats"},
597+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid: cats",
598+
},
599+
{
600+
isTarget: true,
601+
ports: []string{"127.0.0.1:81:0/tcp"},
602+
errorMsg: "Sorry, one of the ports provided with --ports flag is outside range: 0",
603+
},
604+
{
605+
isTarget: true,
606+
ports: []string{"127.0.0.1:81:65536/tcp"},
607+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [127.0.0.1:81:65536/tcp] (Invalid containerPort: 65536)",
608+
},
609+
{
610+
isTarget: true,
611+
ports: []string{"1-65536:80-81/tcp"},
612+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [1-65536:80-81/tcp] (Invalid hostPort: 1-65536)",
613+
},
614+
{
615+
isTarget: true,
616+
ports: []string{"1-80:0-81/tcp"},
617+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [1-80:0-81/tcp] (Invalid ranges specified for container and host Ports: 0-81 and 1-80)",
618+
},
619+
{
620+
isTarget: true,
621+
ports: []string{"1-80:1-65536/tcp"},
622+
errorMsg: "Sorry, one of the ports provided with --ports flag is not valid [1-80:1-65536/tcp] (Invalid containerPort: 1-65536)",
578623
},
579624
}
580625
for _, test := range tests {

0 commit comments

Comments
 (0)