| 1 | // Copyright 2017 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 | |
| 20 | . "github.com/onsi/ginkgo/v2" |
| 21 | . "github.com/onsi/gomega" |
| 22 | |
| 23 | "github.com/containernetworking/cni/pkg/types" |
| 24 | ) |
| 25 | |
| 26 | var _ = Describe("portmapping configuration (iptables)", func() { |
| 27 | netName := "testNetName" |
| 28 | containerID := "icee6giejonei6sohng6ahngee7laquohquee9shiGo7fohferakah3Feiyoolu2pei7ciPhoh7shaoX6vai3vuf0ahfaeng8yohb9ceu0daez5hashee8ooYai5wa3y" |
| 29 | |
| 30 | for _, ver := range []string{"0.3.0", "0.3.1", "0.4.0", "1.0.0"} { |
| 31 | // Redefine ver inside for scope so real value is picked up by each dynamically defined It() |
| 32 | // See Gingkgo's "Patterns for dynamically generating tests" documentation. |
| 33 | ver := ver |
| 34 | |
| 35 | Describe("Generating iptables chains", func() { |
| 36 | Context("for DNAT", func() { |
| 37 | It(fmt.Sprintf("[%s] generates a correct standard container chain", ver), func() { |
| 38 | ch := genDnatChain(netName, containerID) |
| 39 | |
| 40 | Expect(ch).To(Equal(chain{ |
| 41 | table: "nat", |
| 42 | name: "CNI-DN-bfd599665540dd91d5d28", |
| 43 | entryChains: []string{TopLevelDNATChainName}, |
| 44 | })) |
| 45 | configBytes := []byte(fmt.Sprintf(`{ |
| 46 | "name": "test", |
| 47 | "type": "portmap", |
| 48 | "cniVersion": "%s", |
| 49 | "runtimeConfig": { |
| 50 | "portMappings": [ |
| 51 | { "hostPort": 8080, "containerPort": 80, "protocol": "tcp"}, |
| 52 | { "hostPort": 8081, "containerPort": 80, "protocol": "tcp"}, |
| 53 | { "hostPort": 8080, "containerPort": 81, "protocol": "udp"}, |
| 54 | { "hostPort": 8082, "containerPort": 82, "protocol": "udp"}, |
| 55 | { "hostPort": 8083, "containerPort": 83, "protocol": "tcp", "hostIP": "192.168.0.2"}, |
| 56 | { "hostPort": 8084, "containerPort": 84, "protocol": "tcp", "hostIP": "0.0.0.0"}, |
| 57 | { "hostPort": 8085, "containerPort": 85, "protocol": "tcp", "hostIP": "2001:db8:a::1"}, |
| 58 | { "hostPort": 8086, "containerPort": 86, "protocol": "tcp", "hostIP": "::"} |
| 59 | ] |
| 60 | }, |
| 61 | "snat": true, |
| 62 | "conditionsV4": ["-a", "b"], |
| 63 | "conditionsV6": ["-c", "d"] |
| 64 | }`, ver)) |
| 65 | |
| 66 | conf, _, err := parseConfig(configBytes, "foo") |
| 67 | Expect(err).NotTo(HaveOccurred()) |
| 68 | conf.ContainerID = containerID |
| 69 | |
| 70 | ch = genDnatChain(conf.Name, containerID) |
| 71 | Expect(ch).To(Equal(chain{ |
| 72 | table: "nat", |
| 73 | name: "CNI-DN-67e92b96e692a494b6b85", |
| 74 | entryChains: []string{"CNI-HOSTPORT-DNAT"}, |
| 75 | })) |
| 76 | |
| 77 | n, err := types.ParseCIDR("10.0.0.2/24") |
| 78 | Expect(err).NotTo(HaveOccurred()) |
| 79 | fillDnatRules(&ch, conf, *n) |
| 80 | |
| 81 | Expect(ch.entryRules).To(Equal([][]string{ |
| 82 | { |
| 83 | "-m", "comment", "--comment", |
| 84 | fmt.Sprintf("dnat name: \"test\" id: \"%s\"", containerID), |
| 85 | "-m", "multiport", |
| 86 | "-p", "tcp", |
| 87 | "--destination-ports", "8080,8081,8083,8084,8085,8086", |
| 88 | "-a", "b", |
| 89 | }, |
| 90 | { |
| 91 | "-m", "comment", "--comment", |
| 92 | fmt.Sprintf("dnat name: \"test\" id: \"%s\"", containerID), |
| 93 | "-m", "multiport", |
| 94 | "-p", "udp", |
| 95 | "--destination-ports", "8080,8082", |
| 96 | "-a", "b", |
| 97 | }, |
| 98 | })) |
| 99 | |
| 100 | Expect(ch.rules).To(Equal([][]string{ |
| 101 | // tcp rules and not hostIP |
| 102 | {"-p", "tcp", "--dport", "8080", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 103 | {"-p", "tcp", "--dport", "8080", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 104 | {"-p", "tcp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:80"}, |
| 105 | {"-p", "tcp", "--dport", "8081", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 106 | {"-p", "tcp", "--dport", "8081", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 107 | {"-p", "tcp", "--dport", "8081", "-j", "DNAT", "--to-destination", "10.0.0.2:80"}, |
| 108 | // udp rules and not hostIP |
| 109 | {"-p", "udp", "--dport", "8080", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 110 | {"-p", "udp", "--dport", "8080", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 111 | {"-p", "udp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:81"}, |
| 112 | {"-p", "udp", "--dport", "8082", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 113 | {"-p", "udp", "--dport", "8082", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 114 | {"-p", "udp", "--dport", "8082", "-j", "DNAT", "--to-destination", "10.0.0.2:82"}, |
| 115 | // tcp rules and hostIP |
| 116 | {"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 117 | {"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 118 | {"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-j", "DNAT", "--to-destination", "10.0.0.2:83"}, |
| 119 | // tcp rules and hostIP = "0.0.0.0" |
| 120 | {"-p", "tcp", "--dport", "8084", "-s", "10.0.0.2/24", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 121 | {"-p", "tcp", "--dport", "8084", "-s", "127.0.0.1", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 122 | {"-p", "tcp", "--dport", "8084", "-j", "DNAT", "--to-destination", "10.0.0.2:84"}, |
| 123 | })) |
| 124 | |
| 125 | ch.rules = nil |
| 126 | ch.entryRules = nil |
| 127 | |
| 128 | n, err = types.ParseCIDR("2001:db8::2/64") |
| 129 | Expect(err).NotTo(HaveOccurred()) |
| 130 | fillDnatRules(&ch, conf, *n) |
| 131 | |
| 132 | Expect(ch.rules).To(Equal([][]string{ |
| 133 | // tcp rules and not hostIP |
| 134 | {"-p", "tcp", "--dport", "8080", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 135 | {"-p", "tcp", "--dport", "8080", "-j", "DNAT", "--to-destination", "[2001:db8::2]:80"}, |
| 136 | {"-p", "tcp", "--dport", "8081", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 137 | {"-p", "tcp", "--dport", "8081", "-j", "DNAT", "--to-destination", "[2001:db8::2]:80"}, |
| 138 | // udp rules and not hostIP |
| 139 | {"-p", "udp", "--dport", "8080", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 140 | {"-p", "udp", "--dport", "8080", "-j", "DNAT", "--to-destination", "[2001:db8::2]:81"}, |
| 141 | {"-p", "udp", "--dport", "8082", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 142 | {"-p", "udp", "--dport", "8082", "-j", "DNAT", "--to-destination", "[2001:db8::2]:82"}, |
| 143 | // tcp rules and hostIP |
| 144 | {"-p", "tcp", "--dport", "8085", "-d", "2001:db8:a::1", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 145 | {"-p", "tcp", "--dport", "8085", "-d", "2001:db8:a::1", "-j", "DNAT", "--to-destination", "[2001:db8::2]:85"}, |
| 146 | // tcp rules and hostIP = "::" |
| 147 | {"-p", "tcp", "--dport", "8086", "-s", "2001:db8::2/64", "-j", "CNI-HOSTPORT-SETMARK"}, |
| 148 | {"-p", "tcp", "--dport", "8086", "-j", "DNAT", "--to-destination", "[2001:db8::2]:86"}, |
| 149 | })) |
| 150 | |
| 151 | // Disable snat, generate rules |
| 152 | ch.rules = nil |
| 153 | ch.entryRules = nil |
| 154 | fvar := false |
| 155 | conf.SNAT = &fvar |
| 156 | |
| 157 | n, err = types.ParseCIDR("10.0.0.2/24") |
| 158 | Expect(err).NotTo(HaveOccurred()) |
| 159 | fillDnatRules(&ch, conf, *n) |
| 160 | Expect(ch.rules).To(Equal([][]string{ |
| 161 | {"-p", "tcp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:80"}, |
| 162 | {"-p", "tcp", "--dport", "8081", "-j", "DNAT", "--to-destination", "10.0.0.2:80"}, |
| 163 | {"-p", "udp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:81"}, |
| 164 | {"-p", "udp", "--dport", "8082", "-j", "DNAT", "--to-destination", "10.0.0.2:82"}, |
| 165 | {"-p", "tcp", "--dport", "8083", "-d", "192.168.0.2", "-j", "DNAT", "--to-destination", "10.0.0.2:83"}, |
| 166 | {"-p", "tcp", "--dport", "8084", "-j", "DNAT", "--to-destination", "10.0.0.2:84"}, |
| 167 | })) |
| 168 | }) |
| 169 | |
| 170 | It(fmt.Sprintf("[%s] generates a correct chain with external mark", ver), func() { |
| 171 | ch := genDnatChain(netName, containerID) |
| 172 | |
| 173 | Expect(ch).To(Equal(chain{ |
| 174 | table: "nat", |
| 175 | name: "CNI-DN-bfd599665540dd91d5d28", |
| 176 | entryChains: []string{TopLevelDNATChainName}, |
| 177 | })) |
| 178 | configBytes := []byte(fmt.Sprintf(`{ |
| 179 | "name": "test", |
| 180 | "type": "portmap", |
| 181 | "cniVersion": "%s", |
| 182 | "runtimeConfig": { |
| 183 | "portMappings": [ |
| 184 | { "hostPort": 8080, "containerPort": 80, "protocol": "tcp"} |
| 185 | ] |
| 186 | }, |
| 187 | "externalSetMarkChain": "PLZ-SET-MARK", |
| 188 | "conditionsV4": ["-a", "b"], |
| 189 | "conditionsV6": ["-c", "d"] |
| 190 | }`, ver)) |
| 191 | |
| 192 | conf, _, err := parseConfig(configBytes, "foo") |
| 193 | Expect(err).NotTo(HaveOccurred()) |
| 194 | conf.ContainerID = containerID |
| 195 | |
| 196 | ch = genDnatChain(conf.Name, containerID) |
| 197 | n, err := types.ParseCIDR("10.0.0.2/24") |
| 198 | Expect(err).NotTo(HaveOccurred()) |
| 199 | fillDnatRules(&ch, conf, *n) |
| 200 | Expect(ch.rules).To(Equal([][]string{ |
| 201 | {"-p", "tcp", "--dport", "8080", "-s", "10.0.0.2/24", "-j", "PLZ-SET-MARK"}, |
| 202 | {"-p", "tcp", "--dport", "8080", "-s", "127.0.0.1", "-j", "PLZ-SET-MARK"}, |
| 203 | {"-p", "tcp", "--dport", "8080", "-j", "DNAT", "--to-destination", "10.0.0.2:80"}, |
| 204 | })) |
| 205 | }) |
| 206 | |
| 207 | It(fmt.Sprintf("[%s] generates a correct top-level chain", ver), func() { |
| 208 | ch := genToplevelDnatChain() |
| 209 | |
| 210 | Expect(ch).To(Equal(chain{ |
| 211 | table: "nat", |
| 212 | name: "CNI-HOSTPORT-DNAT", |
| 213 | entryChains: []string{"PREROUTING", "OUTPUT"}, |
| 214 | entryRules: [][]string{{"-m", "addrtype", "--dst-type", "LOCAL"}}, |
| 215 | })) |
| 216 | }) |
| 217 | |
| 218 | It(fmt.Sprintf("[%s] generates the correct mark chains", ver), func() { |
| 219 | masqBit := 5 |
| 220 | ch := genSetMarkChain(masqBit) |
| 221 | Expect(ch).To(Equal(chain{ |
| 222 | table: "nat", |
| 223 | name: "CNI-HOSTPORT-SETMARK", |
| 224 | rules: [][]string{{ |
| 225 | "-m", "comment", |
| 226 | "--comment", "CNI portfwd masquerade mark", |
| 227 | "-j", "MARK", |
| 228 | "--set-xmark", "0x20/0x20", |
| 229 | }}, |
| 230 | })) |
| 231 | |
| 232 | ch = genMarkMasqChain(masqBit) |
| 233 | Expect(ch).To(Equal(chain{ |
| 234 | table: "nat", |
| 235 | name: "CNI-HOSTPORT-MASQ", |
| 236 | entryChains: []string{"POSTROUTING"}, |
| 237 | entryRules: [][]string{{ |
| 238 | "-m", "comment", |
| 239 | "--comment", "CNI portfwd requiring masquerade", |
| 240 | }}, |
| 241 | rules: [][]string{{ |
| 242 | "-m", "mark", |
| 243 | "--mark", "0x20/0x20", |
| 244 | "-j", "MASQUERADE", |
| 245 | }}, |
| 246 | prependEntry: true, |
| 247 | })) |
| 248 | }) |
| 249 | }) |
| 250 | }) |
| 251 | } |
| 252 | }) |