| 1 | // Copyright 2018 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 | "bufio" |
| 19 | "fmt" |
| 20 | "os/exec" |
| 21 | "strings" |
| 22 | "sync" |
| 23 | "syscall" |
| 24 | |
| 25 | "github.com/godbus/dbus/v5" |
| 26 | . "github.com/onsi/ginkgo/v2" |
| 27 | . "github.com/onsi/gomega" |
| 28 | |
| 29 | "github.com/containernetworking/cni/pkg/invoke" |
| 30 | "github.com/containernetworking/cni/pkg/skel" |
| 31 | current "github.com/containernetworking/cni/pkg/types/100" |
| 32 | "github.com/containernetworking/plugins/pkg/ns" |
| 33 | "github.com/containernetworking/plugins/pkg/testutils" |
| 34 | ) |
| 35 | |
| 36 | const ifname = "eth0" |
| 37 | |
| 38 | type fakeFirewalld struct { |
| 39 | zone string |
| 40 | source string |
| 41 | } |
| 42 | |
| 43 | func (f *fakeFirewalld) clear() { |
| 44 | f.zone = "" |
| 45 | f.source = "" |
| 46 | } |
| 47 | |
| 48 | //nolint:unparam |
| 49 | func (f *fakeFirewalld) AddSource(zone, source string) (string, *dbus.Error) { |
| 50 | f.zone = zone |
| 51 | f.source = source |
| 52 | return "", nil |
| 53 | } |
| 54 | |
| 55 | //nolint:unparam |
| 56 | func (f *fakeFirewalld) RemoveSource(zone, source string) (string, *dbus.Error) { |
| 57 | f.zone = zone |
| 58 | f.source = source |
| 59 | return "", nil |
| 60 | } |
| 61 | |
| 62 | //nolint:unparam |
| 63 | func (f *fakeFirewalld) QuerySource(zone, source string) (bool, *dbus.Error) { |
| 64 | if f.zone != zone { |
| 65 | return false, nil |
| 66 | } |
| 67 | if f.source != source { |
| 68 | return false, nil |
| 69 | } |
| 70 | return true, nil |
| 71 | } |
| 72 | |
| 73 | func spawnSessionDbus(wg *sync.WaitGroup) (string, *exec.Cmd) { |
| 74 | // Start a private D-Bus session bus |
| 75 | path, err := invoke.FindInPath("dbus-daemon", []string{ |
| 76 | "/bin", "/sbin", "/usr/bin", "/usr/sbin", |
| 77 | }) |
| 78 | Expect(err).NotTo(HaveOccurred()) |
| 79 | cmd := exec.Command(path, "--session", "--print-address", "--nofork", "--nopidfile") |
| 80 | stdout, err := cmd.StdoutPipe() |
| 81 | Expect(err).NotTo(HaveOccurred()) |
| 82 | err = cmd.Start() |
| 83 | Expect(err).NotTo(HaveOccurred()) |
| 84 | |
| 85 | // Wait for dbus-daemon to print the bus address |
| 86 | bytes, err := bufio.NewReader(stdout).ReadString('\n') |
| 87 | Expect(err).NotTo(HaveOccurred()) |
| 88 | busAddr := strings.TrimSpace(bytes) |
| 89 | Expect(strings.HasPrefix(busAddr, "unix:abstract") || |
| 90 | strings.HasPrefix(busAddr, "unix:path")).To(BeTrue()) |
| 91 | |
| 92 | var startWg sync.WaitGroup |
| 93 | wg.Add(1) |
| 94 | startWg.Add(1) |
| 95 | go func() { |
| 96 | defer GinkgoRecover() |
| 97 | |
| 98 | startWg.Done() |
| 99 | err = cmd.Wait() |
| 100 | Expect(err).NotTo(HaveOccurred()) |
| 101 | wg.Done() |
| 102 | }() |
| 103 | |
| 104 | startWg.Wait() |
| 105 | return busAddr, cmd |
| 106 | } |
| 107 | |
| 108 | func makeFirewalldConf(ver string, ns ns.NetNS) []byte { |
| 109 | return []byte(fmt.Sprintf(`{ |
| 110 | "cniVersion": "%s", |
| 111 | "name": "firewalld-test", |
| 112 | "type": "firewall", |
| 113 | "backend": "firewalld", |
| 114 | "zone": "trusted", |
| 115 | "prevResult": { |
| 116 | "cniVersion": "%s", |
| 117 | "interfaces": [ |
| 118 | {"name": "eth0", "sandbox": "%s"} |
| 119 | ], |
| 120 | "ips": [ |
| 121 | { |
| 122 | "version": "4", |
| 123 | "address": "10.0.0.2/24", |
| 124 | "gateway": "10.0.0.1", |
| 125 | "interface": 0 |
| 126 | } |
| 127 | ] |
| 128 | } |
| 129 | }`, ver, ver, ns.Path())) |
| 130 | } |
| 131 | |
| 132 | var _ = Describe("firewalld test", func() { |
| 133 | var ( |
| 134 | targetNs ns.NetNS |
| 135 | cmd *exec.Cmd |
| 136 | conn *dbus.Conn |
| 137 | wg sync.WaitGroup |
| 138 | fwd *fakeFirewalld |
| 139 | busAddr string |
| 140 | ) |
| 141 | |
| 142 | BeforeEach(func() { |
| 143 | var err error |
| 144 | targetNs, err = testutils.NewNS() |
| 145 | Expect(err).NotTo(HaveOccurred()) |
| 146 | |
| 147 | // Start a private D-Bus session bus |
| 148 | busAddr, cmd = spawnSessionDbus(&wg) |
| 149 | conn, err = dbus.Dial(busAddr) |
| 150 | Expect(err).NotTo(HaveOccurred()) |
| 151 | err = conn.Auth(nil) |
| 152 | Expect(err).NotTo(HaveOccurred()) |
| 153 | err = conn.Hello() |
| 154 | Expect(err).NotTo(HaveOccurred()) |
| 155 | |
| 156 | // Start our fake firewalld |
| 157 | reply, err := conn.RequestName(firewalldName, dbus.NameFlagDoNotQueue) |
| 158 | Expect(err).NotTo(HaveOccurred()) |
| 159 | Expect(reply).To(Equal(dbus.RequestNameReplyPrimaryOwner)) |
| 160 | |
| 161 | fwd = &fakeFirewalld{} |
| 162 | // Because firewalld D-Bus methods start with lower-case, and |
| 163 | // because in Go lower-case methods are private, we need to remap |
| 164 | // Go public methods to the D-Bus name |
| 165 | methods := map[string]string{ |
| 166 | "AddSource": firewalldAddSourceMethod, |
| 167 | "QuerySource": firewalldQuerySourceMethod, |
| 168 | "RemoveSource": firewalldRemoveSourceMethod, |
| 169 | } |
| 170 | conn.ExportWithMap(fwd, methods, firewalldPath, firewalldZoneInterface) |
| 171 | |
| 172 | // Make sure the plugin uses our private session bus |
| 173 | testConn = conn |
| 174 | }) |
| 175 | |
| 176 | AfterEach(func() { |
| 177 | _, err := conn.ReleaseName(firewalldName) |
| 178 | Expect(err).NotTo(HaveOccurred()) |
| 179 | |
| 180 | err = cmd.Process.Signal(syscall.SIGTERM) |
| 181 | Expect(err).NotTo(HaveOccurred()) |
| 182 | |
| 183 | wg.Wait() |
| 184 | |
| 185 | Expect(targetNs.Close()).To(Succeed()) |
| 186 | Expect(testutils.UnmountNS(targetNs)).To(Succeed()) |
| 187 | }) |
| 188 | |
| 189 | // firewall plugin requires a prevResult and thus only supports 0.3.0 |
| 190 | // and later CNI versions |
| 191 | for _, ver := range []string{"0.3.0", "0.3.1", "0.4.0", "1.0.0"} { |
| 192 | // Redefine ver inside for scope so real value is picked up by each dynamically defined It() |
| 193 | // See Gingkgo's "Patterns for dynamically generating tests" documentation. |
| 194 | ver := ver |
| 195 | |
| 196 | It(fmt.Sprintf("[%s] works with a config", ver), func() { |
| 197 | Expect(isFirewalldRunning()).To(BeTrue()) |
| 198 | |
| 199 | conf := makeFirewalldConf(ver, targetNs) |
| 200 | args := &skel.CmdArgs{ |
| 201 | ContainerID: "dummy", |
| 202 | Netns: targetNs.Path(), |
| 203 | IfName: ifname, |
| 204 | StdinData: conf, |
| 205 | } |
| 206 | _, _, err := testutils.CmdAdd(targetNs.Path(), args.ContainerID, ifname, conf, func() error { |
| 207 | return cmdAdd(args) |
| 208 | }) |
| 209 | Expect(err).NotTo(HaveOccurred()) |
| 210 | Expect(fwd.zone).To(Equal("trusted")) |
| 211 | Expect(fwd.source).To(Equal("10.0.0.2/32")) |
| 212 | fwd.clear() |
| 213 | |
| 214 | err = testutils.CmdDel(targetNs.Path(), args.ContainerID, ifname, func() error { |
| 215 | return cmdDel(args) |
| 216 | }) |
| 217 | Expect(err).NotTo(HaveOccurred()) |
| 218 | Expect(fwd.zone).To(Equal("trusted")) |
| 219 | Expect(fwd.source).To(Equal("10.0.0.2/32")) |
| 220 | }) |
| 221 | |
| 222 | It(fmt.Sprintf("[%s] defaults to the firewalld backend", ver), func() { |
| 223 | Expect(isFirewalldRunning()).To(BeTrue()) |
| 224 | |
| 225 | conf := makeFirewalldConf(ver, targetNs) |
| 226 | args := &skel.CmdArgs{ |
| 227 | ContainerID: "dummy", |
| 228 | Netns: targetNs.Path(), |
| 229 | IfName: ifname, |
| 230 | StdinData: conf, |
| 231 | } |
| 232 | _, _, err := testutils.CmdAdd(targetNs.Path(), args.ContainerID, ifname, conf, func() error { |
| 233 | return cmdAdd(args) |
| 234 | }) |
| 235 | Expect(err).NotTo(HaveOccurred()) |
| 236 | Expect(fwd.zone).To(Equal("trusted")) |
| 237 | Expect(fwd.source).To(Equal("10.0.0.2/32")) |
| 238 | }) |
| 239 | |
| 240 | It(fmt.Sprintf("[%s] passes through the prevResult", ver), func() { |
| 241 | Expect(isFirewalldRunning()).To(BeTrue()) |
| 242 | |
| 243 | conf := makeFirewalldConf(ver, targetNs) |
| 244 | args := &skel.CmdArgs{ |
| 245 | ContainerID: "dummy", |
| 246 | Netns: targetNs.Path(), |
| 247 | IfName: ifname, |
| 248 | StdinData: conf, |
| 249 | } |
| 250 | r, _, err := testutils.CmdAdd(targetNs.Path(), args.ContainerID, ifname, conf, func() error { |
| 251 | return cmdAdd(args) |
| 252 | }) |
| 253 | Expect(err).NotTo(HaveOccurred()) |
| 254 | |
| 255 | result, err := current.GetResult(r) |
| 256 | Expect(err).NotTo(HaveOccurred()) |
| 257 | |
| 258 | Expect(result.Interfaces).To(HaveLen(1)) |
| 259 | Expect(result.Interfaces[0].Name).To(Equal("eth0")) |
| 260 | Expect(result.IPs).To(HaveLen(1)) |
| 261 | Expect(result.IPs[0].Address.String()).To(Equal("10.0.0.2/24")) |
| 262 | }) |
| 263 | |
| 264 | It(fmt.Sprintf("[%s] works with Check", ver), func() { |
| 265 | Expect(isFirewalldRunning()).To(BeTrue()) |
| 266 | |
| 267 | conf := makeFirewalldConf(ver, targetNs) |
| 268 | args := &skel.CmdArgs{ |
| 269 | ContainerID: "dummy", |
| 270 | Netns: targetNs.Path(), |
| 271 | IfName: ifname, |
| 272 | StdinData: conf, |
| 273 | } |
| 274 | r, _, err := testutils.CmdAddWithArgs(args, func() error { |
| 275 | return cmdAdd(args) |
| 276 | }) |
| 277 | Expect(err).NotTo(HaveOccurred()) |
| 278 | Expect(fwd.zone).To(Equal("trusted")) |
| 279 | Expect(fwd.source).To(Equal("10.0.0.2/32")) |
| 280 | |
| 281 | if testutils.SpecVersionHasCHECK(ver) { |
| 282 | _, err = current.GetResult(r) |
| 283 | Expect(err).NotTo(HaveOccurred()) |
| 284 | |
| 285 | err = testutils.CmdCheckWithArgs(args, func() error { |
| 286 | return cmdCheck(args) |
| 287 | }) |
| 288 | Expect(err).NotTo(HaveOccurred()) |
| 289 | } |
| 290 | |
| 291 | err = testutils.CmdDelWithArgs(args, func() error { |
| 292 | return cmdDel(args) |
| 293 | }) |
| 294 | Expect(err).NotTo(HaveOccurred()) |
| 295 | Expect(fwd.zone).To(Equal("trusted")) |
| 296 | Expect(fwd.source).To(Equal("10.0.0.2/32")) |
| 297 | }) |
| 298 | } |
| 299 | }) |