| 1 | // Copyright 2022 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 | "context" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | "os/exec" |
| 22 | "path/filepath" |
| 23 | |
| 24 | . "github.com/onsi/ginkgo/v2" |
| 25 | . "github.com/onsi/gomega" |
| 26 | |
| 27 | "github.com/containernetworking/cni/libcni" |
| 28 | types100 "github.com/containernetworking/cni/pkg/types/100" |
| 29 | "github.com/containernetworking/plugins/pkg/ns" |
| 30 | "github.com/containernetworking/plugins/pkg/testutils" |
| 31 | ) |
| 32 | |
| 33 | const nsCount = 3 |
| 34 | |
| 35 | // The integration tests expect the "firewall" binary to be present in $PATH. |
| 36 | // To run test, e.g, : go test -exec "sudo -E PATH=$(pwd):/opt/cni/bin:$PATH" -v -ginkgo.v |
| 37 | var _ = Describe("firewall integration tests (ingressPolicy: same-bridge)", func() { |
| 38 | // ns0: foo (10.88.3.0/24) |
| 39 | // ns1: foo (10.88.3.0/24) |
| 40 | // ns2: bar (10.88.4.0/24) |
| 41 | // |
| 42 | // ns0@foo can talk to ns1@foo, but cannot talk to ns2@bar |
| 43 | |
| 44 | var ( |
| 45 | configListFoo *libcni.NetworkConfigList // "foo", 10.88.3.0/24 |
| 46 | configListBar *libcni.NetworkConfigList // "bar", 10.88.4.0/24 |
| 47 | cniConf *libcni.CNIConfig |
| 48 | testRootNS ns.NetNS |
| 49 | namespaces [nsCount]ns.NetNS |
| 50 | results [nsCount]*types100.Result |
| 51 | dataDir string |
| 52 | ) |
| 53 | |
| 54 | createNetworkConfig := func(name string, subnet string, gateway string, ingressPolicy string) string { |
| 55 | return fmt.Sprintf(`{ |
| 56 | "cniVersion": "1.0.0", |
| 57 | "name": "%s", |
| 58 | "plugins": [ |
| 59 | { |
| 60 | "type": "bridge", |
| 61 | "bridge": "%s", |
| 62 | "isGateway": true, |
| 63 | "ipMasq": true, |
| 64 | "hairpinMode": true, |
| 65 | "ipam": { |
| 66 | "dataDir": "%s", |
| 67 | "type": "host-local", |
| 68 | "routes": [ |
| 69 | { |
| 70 | "dst": "0.0.0.0/0" |
| 71 | } |
| 72 | ], |
| 73 | "ranges": [ |
| 74 | [ |
| 75 | { |
| 76 | "subnet": "%s", |
| 77 | "gateway": "%s" |
| 78 | } |
| 79 | ] |
| 80 | ] |
| 81 | } |
| 82 | }, |
| 83 | { |
| 84 | "type": "firewall", |
| 85 | "backend": "iptables", |
| 86 | "ingressPolicy": "%s" |
| 87 | } |
| 88 | ] |
| 89 | }`, name, name, dataDir, subnet, gateway, ingressPolicy) |
| 90 | } |
| 91 | |
| 92 | BeforeEach(func() { |
| 93 | var err error |
| 94 | |
| 95 | // turn PATH in to CNI_PATH. |
| 96 | _, err = exec.LookPath("firewall") |
| 97 | Expect(err).NotTo(HaveOccurred()) |
| 98 | dirs := filepath.SplitList(os.Getenv("PATH")) |
| 99 | cniConf = &libcni.CNIConfig{Path: dirs} |
| 100 | |
| 101 | dataDir, err = os.MkdirTemp("", "firewall_test") |
| 102 | Expect(err).NotTo(HaveOccurred()) |
| 103 | |
| 104 | testRootNS, err = testutils.NewNS() |
| 105 | Expect(err).NotTo(HaveOccurred()) |
| 106 | fmt.Fprintf(GinkgoWriter, "root namespace: %s\n", testRootNS.Path()) |
| 107 | |
| 108 | for i := 0; i < nsCount; i++ { |
| 109 | targetNS, err := testutils.NewNS() |
| 110 | Expect(err).NotTo(HaveOccurred()) |
| 111 | fmt.Fprintf(GinkgoWriter, "namespace %d:%s\n", i, targetNS.Path()) |
| 112 | namespaces[i] = targetNS |
| 113 | } |
| 114 | }) |
| 115 | |
| 116 | AfterEach(func() { |
| 117 | for _, targetNS := range namespaces { |
| 118 | if targetNS != nil { |
| 119 | targetNS.Close() |
| 120 | testutils.UnmountNS(targetNS) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | Expect(testRootNS.Close()).To(Succeed()) |
| 125 | Expect(testutils.UnmountNS(testRootNS)).To(Succeed()) |
| 126 | Expect(os.RemoveAll(dataDir)).To(Succeed()) |
| 127 | }) |
| 128 | |
| 129 | Describe("Testing with ingress-policy 'same-bridge", func() { |
| 130 | BeforeEach(func() { |
| 131 | var err error |
| 132 | configListFoo, err = libcni.ConfListFromBytes([]byte( |
| 133 | createNetworkConfig("foo", "10.88.3.0/24", "10.88.3.1", "same-bridge"))) |
| 134 | Expect(err).NotTo(HaveOccurred()) |
| 135 | |
| 136 | configListBar, err = libcni.ConfListFromBytes([]byte( |
| 137 | createNetworkConfig("bar", "10.88.4.0/24", "10.88.4.1", "same-bridge"))) |
| 138 | Expect(err).NotTo(HaveOccurred()) |
| 139 | |
| 140 | results = setupNetworks(cniConf, testRootNS, namespaces, configListFoo, configListBar) |
| 141 | }) |
| 142 | |
| 143 | Context("when testing connectivity", func() { |
| 144 | It("should allow communication within foo network", func() { |
| 145 | err := ping(namespaces, results, 0, 1) |
| 146 | Expect(err).To(Succeed()) |
| 147 | err = ping(namespaces, results, 1, 0) |
| 148 | Expect(err).To(Succeed()) |
| 149 | }) |
| 150 | |
| 151 | It("should prevent communication between foo and bar networks", func() { |
| 152 | err := ping(namespaces, results, 0, 2) |
| 153 | Expect(err).To(HaveOccurred()) |
| 154 | err = ping(namespaces, results, 1, 2) |
| 155 | Expect(err).To(HaveOccurred()) |
| 156 | err = ping(namespaces, results, 2, 0) |
| 157 | Expect(err).To(HaveOccurred()) |
| 158 | err = ping(namespaces, results, 2, 1) |
| 159 | Expect(err).To(HaveOccurred()) |
| 160 | }) |
| 161 | }) |
| 162 | }) |
| 163 | |
| 164 | Describe("Testing with ingress-policy 'isolated", func() { |
| 165 | BeforeEach(func() { |
| 166 | var err error |
| 167 | configListFoo, err = libcni.ConfListFromBytes([]byte( |
| 168 | createNetworkConfig("foo", "10.88.3.0/24", "10.88.3.1", "isolated"))) |
| 169 | Expect(err).NotTo(HaveOccurred()) |
| 170 | |
| 171 | configListBar, err = libcni.ConfListFromBytes([]byte( |
| 172 | createNetworkConfig("bar", "10.88.4.0/24", "10.88.4.1", "isolated"))) |
| 173 | Expect(err).NotTo(HaveOccurred()) |
| 174 | |
| 175 | results = setupNetworks(cniConf, testRootNS, namespaces, configListFoo, configListBar) |
| 176 | }) |
| 177 | |
| 178 | Context("when testing connectivity", func() { |
| 179 | It("should prevent communication within foo network", func() { |
| 180 | err := ping(namespaces, results, 0, 1) |
| 181 | Expect(err).To(HaveOccurred()) |
| 182 | err = ping(namespaces, results, 1, 0) |
| 183 | Expect(err).To(HaveOccurred()) |
| 184 | }) |
| 185 | |
| 186 | It("should prevent communication between foo and bar networks", func() { |
| 187 | err := ping(namespaces, results, 0, 2) |
| 188 | Expect(err).To(HaveOccurred()) |
| 189 | err = ping(namespaces, results, 1, 2) |
| 190 | Expect(err).To(HaveOccurred()) |
| 191 | err = ping(namespaces, results, 2, 0) |
| 192 | Expect(err).To(HaveOccurred()) |
| 193 | err = ping(namespaces, results, 2, 1) |
| 194 | Expect(err).To(HaveOccurred()) |
| 195 | }) |
| 196 | }) |
| 197 | }) |
| 198 | }) |
| 199 | |
| 200 | func setupNetworks(cniConf *libcni.CNIConfig, testRootNS ns.NetNS, namespaces [nsCount]ns.NetNS, |
| 201 | configListFoo, configListBar *libcni.NetworkConfigList, |
| 202 | ) [nsCount]*types100.Result { |
| 203 | var results [nsCount]*types100.Result |
| 204 | |
| 205 | for i := 0; i < nsCount; i++ { |
| 206 | runtimeConfig := libcni.RuntimeConf{ |
| 207 | ContainerID: fmt.Sprintf("test-cni-firewall-%d", i), |
| 208 | NetNS: namespaces[i].Path(), |
| 209 | IfName: "eth0", |
| 210 | } |
| 211 | |
| 212 | configList := configListFoo |
| 213 | if i >= 2 { |
| 214 | configList = configListBar |
| 215 | } |
| 216 | |
| 217 | err := testRootNS.Do(func(ns.NetNS) error { |
| 218 | defer GinkgoRecover() |
| 219 | |
| 220 | // Create network |
| 221 | res, err := cniConf.AddNetworkList(context.TODO(), configList, &runtimeConfig) |
| 222 | |
| 223 | Expect(err).NotTo(HaveOccurred()) |
| 224 | |
| 225 | results[i], err = types100.NewResultFromResult(res) |
| 226 | Expect(err).NotTo(HaveOccurred()) |
| 227 | |
| 228 | // Setup cleanup |
| 229 | DeferCleanup(func() { |
| 230 | testRootNS.Do(func(ns.NetNS) error { |
| 231 | err := cniConf.DelNetworkList(context.TODO(), configList, &runtimeConfig) |
| 232 | Expect(err).NotTo(HaveOccurred()) |
| 233 | return nil |
| 234 | }) |
| 235 | }) |
| 236 | |
| 237 | return nil |
| 238 | }) |
| 239 | Expect(err).NotTo(HaveOccurred()) |
| 240 | } |
| 241 | |
| 242 | return results |
| 243 | } |
| 244 | |
| 245 | func ping(namespaces [nsCount]ns.NetNS, results [nsCount]*types100.Result, src, dst int) error { |
| 246 | return namespaces[src].Do(func(ns.NetNS) error { |
| 247 | defer GinkgoRecover() |
| 248 | saddr := results[src].IPs[0].Address.IP.String() |
| 249 | daddr := results[dst].IPs[0].Address.IP.String() |
| 250 | srcNetName := results[src].Interfaces[0].Name |
| 251 | dstNetName := results[dst].Interfaces[0].Name |
| 252 | |
| 253 | fmt.Fprintf(GinkgoWriter, "ping %s (ns%d@%s) -> %s (ns%d@%s)...", |
| 254 | saddr, src, srcNetName, daddr, dst, dstNetName) |
| 255 | timeoutSec := 1 |
| 256 | if err := testutils.Ping(saddr, daddr, timeoutSec); err != nil { |
| 257 | fmt.Fprintln(GinkgoWriter, "unpingable") |
| 258 | return err |
| 259 | } |
| 260 | fmt.Fprintln(GinkgoWriter, "pingable") |
| 261 | return nil |
| 262 | }) |
| 263 | } |