| 1 | // Copyright 2021 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 link_test |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | |
| 21 | "github.com/networkplumbing/go-nft/nft" |
| 22 | . "github.com/onsi/ginkgo/v2" |
| 23 | . "github.com/onsi/gomega" |
| 24 | |
| 25 | "github.com/containernetworking/plugins/pkg/link" |
| 26 | ) |
| 27 | |
| 28 | var _ = Describe("spoofcheck", func() { |
| 29 | iface := "net0" |
| 30 | mac := "02:00:00:00:12:34" |
| 31 | id := "container99-net1" |
| 32 | |
| 33 | Context("setup", func() { |
| 34 | It("succeeds", func() { |
| 35 | c := configurerStub{} |
| 36 | sc := link.NewSpoofCheckerWithConfigurer(iface, mac, id, &c) |
| 37 | Expect(sc.Setup()).To(Succeed()) |
| 38 | |
| 39 | assertExpectedTableAndChainsInSetupConfig(c) |
| 40 | assertExpectedRulesInSetupConfig(c) |
| 41 | }) |
| 42 | |
| 43 | It("fails to setup config when 1st apply is unsuccessful (declare table and chains)", func() { |
| 44 | c := &configurerStub{failFirstApplyConfig: true} |
| 45 | sc := link.NewSpoofCheckerWithConfigurer(iface, mac, id, c) |
| 46 | Expect(sc.Setup()).To(MatchError("failed to setup spoof-check: " + errorFirstApplyText)) |
| 47 | }) |
| 48 | |
| 49 | It("fails to setup config when 2nd apply is unsuccessful (flush and add the rules)", func() { |
| 50 | c := &configurerStub{failSecondApplyConfig: true} |
| 51 | sc := link.NewSpoofCheckerWithConfigurer(iface, mac, id, c) |
| 52 | Expect(sc.Setup()).To(MatchError("failed to setup spoof-check: " + errorSecondApplyText)) |
| 53 | }) |
| 54 | }) |
| 55 | |
| 56 | Context("teardown", func() { |
| 57 | It("succeeds", func() { |
| 58 | existingConfig := nft.NewConfig() |
| 59 | existingConfig.FromJSON([]byte(rowConfigWithRulesOnly())) |
| 60 | c := configurerStub{readConfig: existingConfig} |
| 61 | |
| 62 | sc := link.NewSpoofCheckerWithConfigurer("", "", id, &c) |
| 63 | Expect(sc.Teardown()).To(Succeed()) |
| 64 | |
| 65 | assertExpectedBaseChainRuleDeletionInTeardownConfig(c) |
| 66 | assertExpectedRegularChainsDeletionInTeardownConfig(c) |
| 67 | }) |
| 68 | |
| 69 | It("fails, 1st apply is unsuccessful (delete iface match rule)", func() { |
| 70 | config := nft.NewConfig() |
| 71 | config.FromJSON([]byte(rowConfigWithRulesOnly())) |
| 72 | c := &configurerStub{applyConfig: []*nft.Config{config}, readConfig: config, failFirstApplyConfig: true} |
| 73 | sc := link.NewSpoofCheckerWithConfigurer("", "", id, c) |
| 74 | Expect(sc.Teardown()).To(MatchError(fmt.Sprintf( |
| 75 | "failed to teardown spoof-check: failed to delete iface match rule: %s, <nil>", errorFirstApplyText, |
| 76 | ))) |
| 77 | }) |
| 78 | |
| 79 | It("fails, read current config is unsuccessful", func() { |
| 80 | config := nft.NewConfig() |
| 81 | config.FromJSON([]byte(rowConfigWithRulesOnly())) |
| 82 | c := &configurerStub{applyConfig: []*nft.Config{config}, readConfig: config, failReadConfig: true} |
| 83 | sc := link.NewSpoofCheckerWithConfigurer("", "", id, c) |
| 84 | Expect(sc.Teardown()).To(MatchError(fmt.Sprintf( |
| 85 | "failed to teardown spoof-check: %s, <nil>", errorReadText, |
| 86 | ))) |
| 87 | }) |
| 88 | |
| 89 | It("fails, 2nd apply is unsuccessful (delete the regular chains)", func() { |
| 90 | config := nft.NewConfig() |
| 91 | config.FromJSON([]byte(rowConfigWithRulesOnly())) |
| 92 | c := &configurerStub{applyConfig: []*nft.Config{config}, readConfig: config, failSecondApplyConfig: true} |
| 93 | sc := link.NewSpoofCheckerWithConfigurer("", "", id, c) |
| 94 | Expect(sc.Teardown()).To(MatchError(fmt.Sprintf( |
| 95 | "failed to teardown spoof-check: <nil>, failed to delete regular chains: %s", errorSecondApplyText, |
| 96 | ))) |
| 97 | }) |
| 98 | |
| 99 | It("fails, both applies are unsuccessful", func() { |
| 100 | config := nft.NewConfig() |
| 101 | config.FromJSON([]byte(rowConfigWithRulesOnly())) |
| 102 | c := &configurerStub{ |
| 103 | applyConfig: []*nft.Config{config}, |
| 104 | readConfig: config, |
| 105 | failFirstApplyConfig: true, |
| 106 | failSecondApplyConfig: true, |
| 107 | } |
| 108 | sc := link.NewSpoofCheckerWithConfigurer("", "", id, c) |
| 109 | Expect(sc.Teardown()).To(MatchError(fmt.Sprintf( |
| 110 | "failed to teardown spoof-check: "+ |
| 111 | "failed to delete iface match rule: %s, "+ |
| 112 | "failed to delete regular chains: %s", |
| 113 | errorFirstApplyText, errorSecondApplyText, |
| 114 | ))) |
| 115 | }) |
| 116 | }) |
| 117 | |
| 118 | Context("echo", func() { |
| 119 | It("succeeds, no read called", func() { |
| 120 | c := configurerStub{} |
| 121 | sc := link.NewSpoofCheckerWithConfigurer(iface, mac, id, &c) |
| 122 | Expect(sc.Setup()).To(Succeed()) |
| 123 | Expect(sc.Teardown()).To(Succeed()) |
| 124 | Expect(c.readCalled).To(BeFalse()) |
| 125 | }) |
| 126 | |
| 127 | It("succeeds, fall back to config read", func() { |
| 128 | c := configurerStub{applyReturnNil: true} |
| 129 | sc := link.NewSpoofCheckerWithConfigurer(iface, mac, id, &c) |
| 130 | Expect(sc.Setup()).To(Succeed()) |
| 131 | c.readConfig = c.applyConfig[0] |
| 132 | Expect(sc.Teardown()).To(Succeed()) |
| 133 | Expect(c.readCalled).To(BeTrue()) |
| 134 | }) |
| 135 | }) |
| 136 | }) |
| 137 | |
| 138 | func assertExpectedRegularChainsDeletionInTeardownConfig(action configurerStub) { |
| 139 | deleteRegularChainRulesJSONConfig, err := action.applyConfig[1].ToJSON() |
| 140 | ExpectWithOffset(1, err).NotTo(HaveOccurred()) |
| 141 | |
| 142 | expectedDeleteRegularChainRulesJSONConfig := ` |
| 143 | {"nftables": [ |
| 144 | {"delete": {"chain": { |
| 145 | "family": "bridge", |
| 146 | "table": "nat", |
| 147 | "name": "cni-br-iface-container99-net1" |
| 148 | }}}, |
| 149 | {"delete": {"chain": { |
| 150 | "family": "bridge", |
| 151 | "table": "nat", |
| 152 | "name": "cni-br-iface-container99-net1-mac" |
| 153 | }}} |
| 154 | ]}` |
| 155 | |
| 156 | ExpectWithOffset(1, string(deleteRegularChainRulesJSONConfig)).To(MatchJSON(expectedDeleteRegularChainRulesJSONConfig)) |
| 157 | } |
| 158 | |
| 159 | func assertExpectedBaseChainRuleDeletionInTeardownConfig(action configurerStub) { |
| 160 | deleteBaseChainRuleJSONConfig, err := action.applyConfig[0].ToJSON() |
| 161 | Expect(err).NotTo(HaveOccurred()) |
| 162 | |
| 163 | expectedDeleteIfaceMatchRuleJSONConfig := ` |
| 164 | {"nftables": [ |
| 165 | {"delete": {"rule": { |
| 166 | "family": "bridge", |
| 167 | "table": "nat", |
| 168 | "chain": "PREROUTING", |
| 169 | "expr": [ |
| 170 | {"match": { |
| 171 | "op": "==", |
| 172 | "left": {"meta": {"key": "iifname"}}, |
| 173 | "right": "net0" |
| 174 | }}, |
| 175 | {"jump": {"target": "cni-br-iface-container99-net1"}} |
| 176 | ], |
| 177 | "comment": "macspoofchk-container99-net1" |
| 178 | }}} |
| 179 | ]}` |
| 180 | Expect(string(deleteBaseChainRuleJSONConfig)).To(MatchJSON(expectedDeleteIfaceMatchRuleJSONConfig)) |
| 181 | } |
| 182 | |
| 183 | func rowConfigWithRulesOnly() string { |
| 184 | return ` |
| 185 | {"nftables":[ |
| 186 | {"rule":{"family":"bridge","table":"nat","chain":"PREROUTING", |
| 187 | "expr":[ |
| 188 | {"match":{"op":"==","left":{"meta":{"key":"iifname"}},"right":"net0"}}, |
| 189 | {"jump":{"target":"cni-br-iface-container99-net1"}} |
| 190 | ], |
| 191 | "comment":"macspoofchk-container99-net1"}}, |
| 192 | {"rule":{"family":"bridge","table":"nat","chain":"cni-br-iface-container99-net1", |
| 193 | "expr":[ |
| 194 | {"jump":{"target":"cni-br-iface-container99-net1-mac"}} |
| 195 | ], |
| 196 | "comment":"macspoofchk-container99-net1"}}, |
| 197 | {"rule":{"family":"bridge","table":"nat","chain":"cni-br-iface-container99-net1-mac", |
| 198 | "expr":[ |
| 199 | {"match":{ |
| 200 | "op":"==", |
| 201 | "left":{"payload":{"protocol":"ether","field":"saddr"}}, |
| 202 | "right":"02:00:00:00:12:34" |
| 203 | }}, |
| 204 | {"return":null} |
| 205 | ], |
| 206 | "comment":"macspoofchk-container99-net1"}}, |
| 207 | {"rule":{"family":"bridge","table":"nat","chain":"cni-br-iface-container99-net1-mac", |
| 208 | "expr":[{"drop":null}], |
| 209 | "index":0, |
| 210 | "comment":"macspoofchk-container99-net1"}} |
| 211 | ]}` |
| 212 | } |
| 213 | |
| 214 | func assertExpectedTableAndChainsInSetupConfig(c configurerStub) { |
| 215 | config := c.applyConfig[0] |
| 216 | jsonConfig, err := config.ToJSON() |
| 217 | ExpectWithOffset(1, err).NotTo(HaveOccurred()) |
| 218 | |
| 219 | expectedConfig := ` |
| 220 | {"nftables": [ |
| 221 | {"table": {"family": "bridge", "name": "nat"}}, |
| 222 | {"chain": { |
| 223 | "family": "bridge", |
| 224 | "table": "nat", |
| 225 | "name": "PREROUTING", |
| 226 | "type": "filter", |
| 227 | "hook": "prerouting", |
| 228 | "prio": -300, |
| 229 | "policy": "accept" |
| 230 | }}, |
| 231 | {"chain": { |
| 232 | "family": "bridge", |
| 233 | "table": "nat", |
| 234 | "name": "cni-br-iface-container99-net1" |
| 235 | }}, |
| 236 | {"chain": { |
| 237 | "family": "bridge", |
| 238 | "table": "nat", |
| 239 | "name": "cni-br-iface-container99-net1-mac" |
| 240 | }} |
| 241 | ]}` |
| 242 | ExpectWithOffset(1, string(jsonConfig)).To(MatchJSON(expectedConfig)) |
| 243 | } |
| 244 | |
| 245 | func assertExpectedRulesInSetupConfig(c configurerStub) { |
| 246 | config := c.applyConfig[1] |
| 247 | jsonConfig, err := config.ToJSON() |
| 248 | ExpectWithOffset(1, err).NotTo(HaveOccurred()) |
| 249 | |
| 250 | expectedConfig := ` |
| 251 | {"nftables":[ |
| 252 | {"flush":{"chain":{"family":"bridge","table":"nat","name":"cni-br-iface-container99-net1"}}}, |
| 253 | {"flush":{"chain":{"family":"bridge","table":"nat","name":"cni-br-iface-container99-net1-mac"}}}, |
| 254 | {"rule":{"family":"bridge","table":"nat","chain":"PREROUTING", |
| 255 | "expr":[ |
| 256 | {"match":{"op":"==","left":{"meta":{"key":"iifname"}},"right":"net0"}}, |
| 257 | {"jump":{"target":"cni-br-iface-container99-net1"}} |
| 258 | ], |
| 259 | "comment":"macspoofchk-container99-net1"}}, |
| 260 | {"rule":{"family":"bridge","table":"nat","chain":"cni-br-iface-container99-net1", |
| 261 | "expr":[ |
| 262 | {"jump":{"target":"cni-br-iface-container99-net1-mac"}} |
| 263 | ], |
| 264 | "comment":"macspoofchk-container99-net1"}}, |
| 265 | {"rule":{"family":"bridge","table":"nat","chain":"cni-br-iface-container99-net1-mac", |
| 266 | "expr":[ |
| 267 | {"match":{ |
| 268 | "op":"==", |
| 269 | "left":{"payload":{"protocol":"ether","field":"saddr"}}, |
| 270 | "right":"02:00:00:00:12:34" |
| 271 | }}, |
| 272 | {"return":null} |
| 273 | ], |
| 274 | "comment":"macspoofchk-container99-net1"}}, |
| 275 | {"rule":{"family":"bridge","table":"nat","chain":"cni-br-iface-container99-net1-mac", |
| 276 | "expr":[{"drop":null}], |
| 277 | "comment":"macspoofchk-container99-net1"}} |
| 278 | ]}` |
| 279 | ExpectWithOffset(1, string(jsonConfig)).To(MatchJSON(expectedConfig)) |
| 280 | } |
| 281 | |
| 282 | const ( |
| 283 | errorFirstApplyText = "1st apply failed" |
| 284 | errorSecondApplyText = "2nd apply failed" |
| 285 | errorReadText = "read failed" |
| 286 | ) |
| 287 | |
| 288 | type configurerStub struct { |
| 289 | applyConfig []*nft.Config |
| 290 | readConfig *nft.Config |
| 291 | |
| 292 | applyCounter int |
| 293 | |
| 294 | failFirstApplyConfig bool |
| 295 | failSecondApplyConfig bool |
| 296 | failReadConfig bool |
| 297 | |
| 298 | applyReturnNil bool |
| 299 | readCalled bool |
| 300 | } |
| 301 | |
| 302 | func (a *configurerStub) Apply(c *nft.Config) (*nft.Config, error) { |
| 303 | a.applyCounter++ |
| 304 | if a.failFirstApplyConfig && a.applyCounter == 1 { |
| 305 | return nil, errors.New(errorFirstApplyText) |
| 306 | } |
| 307 | if a.failSecondApplyConfig && a.applyCounter == 2 { |
| 308 | return nil, errors.New(errorSecondApplyText) |
| 309 | } |
| 310 | a.applyConfig = append(a.applyConfig, c) |
| 311 | if a.applyReturnNil { |
| 312 | return nil, nil |
| 313 | } |
| 314 | return c, nil |
| 315 | } |
| 316 | |
| 317 | func (a *configurerStub) Read(_ ...string) (*nft.Config, error) { |
| 318 | a.readCalled = true |
| 319 | if a.failReadConfig { |
| 320 | return nil, errors.New(errorReadText) |
| 321 | } |
| 322 | return a.readConfig, nil |
| 323 | } |