Skip to content

Commit

Permalink
Merge pull request #1968 from Luap99/map-gw
Browse files Browse the repository at this point in the history
libnetwork/pasta: fix multiple --map-gw parsing
  • Loading branch information
openshift-merge-bot[bot] committed Apr 26, 2024
2 parents b3b3947 + 917c583 commit ae4635f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
69 changes: 38 additions & 31 deletions libnetwork/pasta/pasta_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"fmt"
"net"
"os/exec"
"slices"
"strings"

"github.com/containernetworking/plugins/pkg/ns"
Expand Down Expand Up @@ -128,9 +129,42 @@ func createPastaArgs(opts *SetupOptions) ([]string, []string, error) {
noUDPInitPorts := true
noTCPNamespacePorts := true
noUDPNamespacePorts := true
noMapGWIndex := -1
noMapGW := true

cmdArgs := []string{"--config-net"}
// first append options set in the config
cmdArgs = append(cmdArgs, opts.Config.Network.PastaOptions.Get()...)
// then append the ones that were set on the cli
cmdArgs = append(cmdArgs, opts.ExtraOptions...)

cmdArgs = slices.DeleteFunc(cmdArgs, func(s string) bool {
// --map-gw is not a real pasta(1) option so we must remove it
// and not add --no-map-gw below
if s == "--map-gw" {
noMapGW = false
return true
}
return false
})

var dnsForwardIPs []string
for i, opt := range cmdArgs {
switch opt {
case "-t", "--tcp-ports":
noTCPInitPorts = false
case "-u", "--udp-ports":
noUDPInitPorts = false
case "-T", "--tcp-ns":
noTCPNamespacePorts = false
case "-U", "--udp-ns":
noUDPNamespacePorts = false
case dnsForwardOpt:
// if there is no arg after it pasta will likely error out anyway due invalid cli args
if len(cmdArgs) > i+1 {
dnsForwardIPs = append(dnsForwardIPs, cmdArgs[i+1])
}
}
}

for _, i := range opts.Ports {
protocols := strings.Split(i.Protocol, ",")
Expand All @@ -143,8 +177,10 @@ func createPastaArgs(opts *SetupOptions) ([]string, []string, error) {

switch protocol {
case "tcp":
noTCPInitPorts = false
cmdArgs = append(cmdArgs, "-t")
case "udp":
noUDPInitPorts = false
cmdArgs = append(cmdArgs, "-u")
default:
return nil, nil, fmt.Errorf("can't forward protocol: %s", protocol)
Expand All @@ -159,32 +195,6 @@ func createPastaArgs(opts *SetupOptions) ([]string, []string, error) {
}
}

// first append options set in the config
cmdArgs = append(cmdArgs, opts.Config.Network.PastaOptions.Get()...)
// then append the ones that were set on the cli
cmdArgs = append(cmdArgs, opts.ExtraOptions...)

var dnsForwardIPs []string
for i, opt := range cmdArgs {
switch opt {
case "-t", "--tcp-ports":
noTCPInitPorts = false
case "-u", "--udp-ports":
noUDPInitPorts = false
case "-T", "--tcp-ns":
noTCPNamespacePorts = false
case "-U", "--udp-ns":
noUDPNamespacePorts = false
case "--map-gw":
noMapGWIndex = i
case dnsForwardOpt:
// if there is no arg after it pasta will likely error out anyway due invalid cli args
if len(cmdArgs) > i+1 {
dnsForwardIPs = append(dnsForwardIPs, cmdArgs[i+1])
}
}
}

if len(dnsForwardIPs) == 0 {
// the user did not request custom --dns-forward so add our own.
cmdArgs = append(cmdArgs, dnsForwardOpt, dnsForwardIpv4)
Expand All @@ -203,11 +213,8 @@ func createPastaArgs(opts *SetupOptions) ([]string, []string, error) {
if noUDPNamespacePorts {
cmdArgs = append(cmdArgs, "-U", "none")
}
if noMapGWIndex < 0 {
if noMapGW {
cmdArgs = append(cmdArgs, "--no-map-gw")
} else {
// not an actual pasta(1) option so we have to trim it out
cmdArgs = append(cmdArgs[:noMapGWIndex], cmdArgs[noMapGWIndex+1:]...)
}

// always pass --quiet to silence the info output from pasta
Expand Down
26 changes: 26 additions & 0 deletions libnetwork/pasta/pasta_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ func Test_createPastaArgs(t *testing.T) {
},
wantDnsForward: []string{dnsForwardIpv4},
},
{
name: "two --map-gw",
input: makeSetupOptions(
[]string{"--map-gw", "-T", "80"},
[]string{"--map-gw"},
nil,
),
wantArgs: []string{
"--config-net", "-T", "80", "--dns-forward", dnsForwardIpv4,
"-t", "none", "-u", "none", "-U", "none", "--quiet", "--netns", "netns123",
},
wantDnsForward: []string{dnsForwardIpv4},
},
{
name: "--dns-forward option",
input: makeSetupOptions(
Expand All @@ -225,6 +238,19 @@ func Test_createPastaArgs(t *testing.T) {
},
wantDnsForward: []string{"192.168.255.255", "::1"},
},
{
name: "port and custom opt",
input: makeSetupOptions(
nil,
[]string{"-i", "eth0"},
[]types.PortMapping{{HostPort: 80, ContainerPort: 80, Protocol: "tcp", Range: 1}},
),
wantArgs: []string{
"--config-net", "-i", "eth0", "-t", "80-80:80-80", "--dns-forward", dnsForwardIpv4,
"-u", "none", "-T", "none", "-U", "none", "--no-map-gw", "--quiet", "--netns", "netns123",
},
wantDnsForward: []string{dnsForwardIpv4},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit ae4635f

Please sign in to comment.