Debian Codegraph @

nft/config/rule.go

raw symbols SCIP
1 /*
2 * This file is part of the go-nft project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright 2021 Red Hat, Inc.
17 *
18 */
19
20 package config
21
22 import (
23 "bytes"
24 "encoding/json"
25
26 "github.com/networkplumbing/go-nft/nft/schema"
27 )
28
29 // AddRule appends the given rule to the nftable config.
30 // The rule is added without an explicit action (`add`).
31 // Adding multiple times the same rule will result in multiple identical rules when applied.
32 func (c *Config) AddRule(rule *schema.Rule) {
33 nftable := schema.Nftable{Rule: rule}
34 c.Nftables = append(c.Nftables, nftable)
35 }
36
37 // DeleteRule appends a given rule to the nftable config
38 // with the `delete` action.
39 // A rule is identified by its handle ID and it must be present in the given rule.
40 // Attempting to delete a non-existing rule, results with a failure when the config is applied.
41 // A common usage is to use LookupRule() and then to pass the result to DeleteRule.
42 func (c *Config) DeleteRule(rule *schema.Rule) {
43 nftable := schema.Nftable{Delete: &schema.Objects{Rule: rule}}
44 c.Nftables = append(c.Nftables, nftable)
45 }
46
47 // LookupRule searches the configuration for a matching rule and returns it.
48 // The rule is matched first by the table and chain.
49 // Other matching fields are optional (nil or an empty string arguments imply no-matching).
50 // Mutating the returned chain will result in mutating the configuration.
51 func (c *Config) LookupRule(toFind *schema.Rule) []*schema.Rule {
52 var rules []*schema.Rule
53
54 for _, nftable := range c.Nftables {
55 if r := nftable.Rule; r != nil {
56 match := r.Table == toFind.Table && r.Family == toFind.Family && r.Chain == toFind.Chain
57 if match {
58 if h := toFind.Handle; h != nil {
59 match = match && r.Handle != nil && *r.Handle == *h
60 }
61 if i := toFind.Index; i != nil {
62 match = match && r.Index != nil && *r.Index == *i
63 }
64 if co := toFind.Comment; co != "" {
65 match = match && r.Comment == co
66 }
67 if toFindStatements := toFind.Expr; toFindStatements != nil {
68 if match = match && len(toFindStatements) == len(r.Expr); match {
69 for i, toFindStatement := range toFindStatements {
70 equal, err := areStatementsEqual(toFindStatement, r.Expr[i])
71 match = match && err == nil && equal
72 }
73 }
74 }
75 if match {
76 rules = append(rules, r)
77 }
78 }
79 }
80 }
81 return rules
82 }
83
84 func areStatementsEqual(statementA, statementB schema.Statement) (bool, error) {
85 statementARow, err := json.Marshal(statementA)
86 if err != nil {
87 return false, err
88 }
89 statementBRow, err := json.Marshal(statementB)
90 if err != nil {
91 return false, err
92 }
93 return bytes.Equal(statementARow, statementBRow), nil
94 }