| 1 | // Copyright 2023 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | . "github.com/onsi/ginkgo/v2" |
| 22 | . "github.com/onsi/gomega" |
| 23 | "sigs.k8s.io/knftables" |
| 24 | |
| 25 | "github.com/containernetworking/cni/pkg/types" |
| 26 | ) |
| 27 | |
| 28 | var _ = Describe("portmapping configuration (nftables)", func() { |
| 29 | containerID := "icee6giejonei6so" |
| 30 | configTmpl := `{ |
| 31 | "name": "test", |
| 32 | "type": "portmap", |
| 33 | "cniVersion": "%s", |
| 34 | "backend": "nftables", |
| 35 | "runtimeConfig": { |
| 36 | "portMappings": [ |
| 37 | { "hostPort": 8080, "containerPort": 80, "protocol": "tcp"}, |
| 38 | { "hostPort": 8081, "containerPort": 80, "protocol": "tcp"}, |
| 39 | { "hostPort": 8080, "containerPort": 81, "protocol": "udp"}, |
| 40 | { "hostPort": 8082, "containerPort": 82, "protocol": "udp"}, |
| 41 | { "hostPort": 8083, "containerPort": 83, "protocol": "tcp", "hostIP": "192.168.0.2"}, |
| 42 | { "hostPort": 8084, "containerPort": 84, "protocol": "tcp", "hostIP": "0.0.0.0"}, |
| 43 | { "hostPort": 8085, "containerPort": 85, "protocol": "tcp", "hostIP": "2001:db8:a::1"}, |
| 44 | { "hostPort": 8086, "containerPort": 86, "protocol": "tcp", "hostIP": "::"} |
| 45 | ] |
| 46 | }, |
| 47 | "snat": true, |
| 48 | "conditionsV4": ["a", "b"], |
| 49 | "conditionsV6": ["c", "d"] |
| 50 | }` |
| 51 | containerNet4, err := types.ParseCIDR("10.0.0.2/24") |
| 52 | Expect(err).NotTo(HaveOccurred()) |
| 53 | containerNet6, err := types.ParseCIDR("2001:db8::2/64") |
| 54 | Expect(err).NotTo(HaveOccurred()) |
| 55 | |
| 56 | for _, ver := range []string{"0.3.0", "0.3.1", "0.4.0", "1.0.0"} { |
| 57 | // Redefine ver inside for scope so real value is picked up by each dynamically defined It() |
| 58 | // See Gingkgo's "Patterns for dynamically generating tests" documentation. |
| 59 | ver := ver |
| 60 | |
| 61 | Describe("nftables rules", func() { |
| 62 | var pmNFT *portMapperNFTables |
| 63 | var ipv4Fake, ipv6Fake *knftables.Fake |
| 64 | BeforeEach(func() { |
| 65 | ipv4Fake = knftables.NewFake(knftables.IPv4Family, tableName) |
| 66 | ipv6Fake = knftables.NewFake(knftables.IPv6Family, tableName) |
| 67 | pmNFT = &portMapperNFTables{ |
| 68 | ipv4: ipv4Fake, |
| 69 | ipv6: ipv6Fake, |
| 70 | } |
| 71 | }) |
| 72 | |
| 73 | It(fmt.Sprintf("[%s] generates correct rules", ver), func() { |
| 74 | configBytes := []byte(fmt.Sprintf(configTmpl, ver)) |
| 75 | |
| 76 | conf, _, err := parseConfig(configBytes, "foo") |
| 77 | Expect(err).NotTo(HaveOccurred()) |
| 78 | conf.ContainerID = containerID |
| 79 | |
| 80 | err = pmNFT.forwardPorts(conf, *containerNet4) |
| 81 | Expect(err).NotTo(HaveOccurred()) |
| 82 | |
| 83 | expectedRules := strings.TrimSpace(` |
| 84 | add table ip cni_hostport { comment "CNI portmap plugin" ; } |
| 85 | add chain ip cni_hostport hostip_hostports |
| 86 | add chain ip cni_hostport hostports |
| 87 | add chain ip cni_hostport hostports_all |
| 88 | add chain ip cni_hostport masquerading { type nat hook postrouting priority 100 ; } |
| 89 | add chain ip cni_hostport output { type nat hook output priority -100 ; } |
| 90 | add chain ip cni_hostport prerouting { type nat hook prerouting priority -100 ; } |
| 91 | add rule ip cni_hostport hostports tcp dport 8080 dnat to 10.0.0.2:80 comment "icee6giejonei6so" |
| 92 | add rule ip cni_hostport hostports tcp dport 8081 dnat to 10.0.0.2:80 comment "icee6giejonei6so" |
| 93 | add rule ip cni_hostport hostports udp dport 8080 dnat to 10.0.0.2:81 comment "icee6giejonei6so" |
| 94 | add rule ip cni_hostport hostports udp dport 8082 dnat to 10.0.0.2:82 comment "icee6giejonei6so" |
| 95 | add rule ip cni_hostport hostports ip daddr 192.168.0.2 tcp dport 8083 dnat to 10.0.0.2:83 comment "icee6giejonei6so" |
| 96 | add rule ip cni_hostport hostports tcp dport 8084 dnat to 10.0.0.2:84 comment "icee6giejonei6so" |
| 97 | add rule ip cni_hostport hostports_all jump hostip_hostports |
| 98 | add rule ip cni_hostport hostports_all jump hostports |
| 99 | add rule ip cni_hostport masquerading ip saddr 10.0.0.2 ip daddr 10.0.0.2 masquerade comment "icee6giejonei6so" |
| 100 | add rule ip cni_hostport masquerading ip saddr 127.0.0.1 ip daddr 10.0.0.2 masquerade comment "icee6giejonei6so" |
| 101 | add rule ip cni_hostport output a b fib daddr type local jump hostports_all |
| 102 | add rule ip cni_hostport prerouting a b fib daddr type local jump hostports_all |
| 103 | `) |
| 104 | actualRules := strings.TrimSpace(ipv4Fake.Dump()) |
| 105 | Expect(actualRules).To(Equal(expectedRules)) |
| 106 | |
| 107 | // Disable snat, generate IPv6 rules |
| 108 | *conf.SNAT = false |
| 109 | |
| 110 | err = pmNFT.forwardPorts(conf, *containerNet6) |
| 111 | Expect(err).NotTo(HaveOccurred()) |
| 112 | |
| 113 | expectedRules = strings.TrimSpace(` |
| 114 | add table ip6 cni_hostport { comment "CNI portmap plugin" ; } |
| 115 | add chain ip6 cni_hostport hostip_hostports |
| 116 | add chain ip6 cni_hostport hostports |
| 117 | add chain ip6 cni_hostport hostports_all |
| 118 | add chain ip6 cni_hostport output { type nat hook output priority -100 ; } |
| 119 | add chain ip6 cni_hostport prerouting { type nat hook prerouting priority -100 ; } |
| 120 | add rule ip6 cni_hostport hostports tcp dport 8080 dnat to [2001:db8::2]:80 comment "icee6giejonei6so" |
| 121 | add rule ip6 cni_hostport hostports tcp dport 8081 dnat to [2001:db8::2]:80 comment "icee6giejonei6so" |
| 122 | add rule ip6 cni_hostport hostports udp dport 8080 dnat to [2001:db8::2]:81 comment "icee6giejonei6so" |
| 123 | add rule ip6 cni_hostport hostports udp dport 8082 dnat to [2001:db8::2]:82 comment "icee6giejonei6so" |
| 124 | add rule ip6 cni_hostport hostports ip6 daddr 2001:db8:a::1 tcp dport 8085 dnat to [2001:db8::2]:85 comment "icee6giejonei6so" |
| 125 | add rule ip6 cni_hostport hostports tcp dport 8086 dnat to [2001:db8::2]:86 comment "icee6giejonei6so" |
| 126 | add rule ip6 cni_hostport hostports_all jump hostip_hostports |
| 127 | add rule ip6 cni_hostport hostports_all jump hostports |
| 128 | add rule ip6 cni_hostport output c d fib daddr type local jump hostports_all |
| 129 | add rule ip6 cni_hostport prerouting c d fib daddr type local jump hostports_all |
| 130 | `) |
| 131 | actualRules = strings.TrimSpace(ipv6Fake.Dump()) |
| 132 | Expect(actualRules).To(Equal(expectedRules)) |
| 133 | |
| 134 | err = pmNFT.unforwardPorts(conf) |
| 135 | Expect(err).NotTo(HaveOccurred()) |
| 136 | |
| 137 | expectedRules = strings.TrimSpace(` |
| 138 | add table ip cni_hostport { comment "CNI portmap plugin" ; } |
| 139 | add chain ip cni_hostport hostip_hostports |
| 140 | add chain ip cni_hostport hostports |
| 141 | add chain ip cni_hostport hostports_all |
| 142 | add chain ip cni_hostport masquerading { type nat hook postrouting priority 100 ; } |
| 143 | add chain ip cni_hostport output { type nat hook output priority -100 ; } |
| 144 | add chain ip cni_hostport prerouting { type nat hook prerouting priority -100 ; } |
| 145 | add rule ip cni_hostport hostports_all jump hostip_hostports |
| 146 | add rule ip cni_hostport hostports_all jump hostports |
| 147 | add rule ip cni_hostport output a b fib daddr type local jump hostports_all |
| 148 | add rule ip cni_hostport prerouting a b fib daddr type local jump hostports_all |
| 149 | `) |
| 150 | actualRules = strings.TrimSpace(ipv4Fake.Dump()) |
| 151 | Expect(actualRules).To(Equal(expectedRules)) |
| 152 | |
| 153 | expectedRules = strings.TrimSpace(` |
| 154 | add table ip6 cni_hostport { comment "CNI portmap plugin" ; } |
| 155 | add chain ip6 cni_hostport hostip_hostports |
| 156 | add chain ip6 cni_hostport hostports |
| 157 | add chain ip6 cni_hostport hostports_all |
| 158 | add chain ip6 cni_hostport output { type nat hook output priority -100 ; } |
| 159 | add chain ip6 cni_hostport prerouting { type nat hook prerouting priority -100 ; } |
| 160 | add rule ip6 cni_hostport hostports_all jump hostip_hostports |
| 161 | add rule ip6 cni_hostport hostports_all jump hostports |
| 162 | add rule ip6 cni_hostport output c d fib daddr type local jump hostports_all |
| 163 | add rule ip6 cni_hostport prerouting c d fib daddr type local jump hostports_all |
| 164 | `) |
| 165 | actualRules = strings.TrimSpace(ipv6Fake.Dump()) |
| 166 | Expect(actualRules).To(Equal(expectedRules)) |
| 167 | }) |
| 168 | |
| 169 | It(fmt.Sprintf("[%s] has working CHECK", ver), func() { |
| 170 | configBytes := []byte(fmt.Sprintf(configTmpl, ver)) |
| 171 | |
| 172 | conf, _, err := parseConfig(configBytes, "foo") |
| 173 | Expect(err).NotTo(HaveOccurred()) |
| 174 | conf.ContainerID = containerID |
| 175 | |
| 176 | // CHECK KO: Missing all rules |
| 177 | err = pmNFT.checkPorts(conf, *containerNet4) |
| 178 | Expect(err).To(HaveOccurred()) |
| 179 | err = pmNFT.checkPorts(conf, *containerNet6) |
| 180 | Expect(err).To(HaveOccurred()) |
| 181 | |
| 182 | *conf.SNAT = false |
| 183 | |
| 184 | // ADD with SNAT=false |
| 185 | err = pmNFT.forwardPorts(conf, *containerNet4) |
| 186 | Expect(err).NotTo(HaveOccurred()) |
| 187 | err = pmNFT.forwardPorts(conf, *containerNet6) |
| 188 | Expect(err).NotTo(HaveOccurred()) |
| 189 | |
| 190 | // CHECK OK: no SNAT rules |
| 191 | err = pmNFT.checkPorts(conf, *containerNet4) |
| 192 | Expect(err).NotTo(HaveOccurred()) |
| 193 | err = pmNFT.checkPorts(conf, *containerNet6) |
| 194 | Expect(err).NotTo(HaveOccurred()) |
| 195 | |
| 196 | *conf.SNAT = true |
| 197 | |
| 198 | // CHECK KO: missing SNAT rules |
| 199 | err = pmNFT.checkPorts(conf, *containerNet4) |
| 200 | Expect(err).To(HaveOccurred()) |
| 201 | err = pmNFT.checkPorts(conf, *containerNet6) |
| 202 | Expect(err).To(HaveOccurred()) |
| 203 | |
| 204 | // ADD with SNAT=true |
| 205 | err = pmNFT.forwardPorts(conf, *containerNet4) |
| 206 | Expect(err).NotTo(HaveOccurred()) |
| 207 | err = pmNFT.forwardPorts(conf, *containerNet6) |
| 208 | Expect(err).NotTo(HaveOccurred()) |
| 209 | |
| 210 | // CHECK OK: All rules present |
| 211 | err = pmNFT.checkPorts(conf, *containerNet4) |
| 212 | Expect(err).NotTo(HaveOccurred()) |
| 213 | err = pmNFT.checkPorts(conf, *containerNet6) |
| 214 | Expect(err).NotTo(HaveOccurred()) |
| 215 | |
| 216 | // DELETE |
| 217 | err = pmNFT.unforwardPorts(conf) |
| 218 | Expect(err).NotTo(HaveOccurred()) |
| 219 | |
| 220 | // CHECK KO: Missing all rules |
| 221 | err = pmNFT.checkPorts(conf, *containerNet4) |
| 222 | Expect(err).To(HaveOccurred()) |
| 223 | err = pmNFT.checkPorts(conf, *containerNet6) |
| 224 | Expect(err).To(HaveOccurred()) |
| 225 | }) |
| 226 | }) |
| 227 | } |
| 228 | }) |