| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 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 | |
| 17 | package knftables |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "fmt" |
| 22 | ) |
| 23 | |
| 24 | // Transaction represents an nftables transaction |
| 25 | type Transaction struct { |
| 26 | *nftContext |
| 27 | |
| 28 | operations []operation |
| 29 | err error |
| 30 | } |
| 31 | |
| 32 | // operation contains a single nftables operation (eg "add table", "flush chain") |
| 33 | type operation struct { |
| 34 | verb verb |
| 35 | obj Object |
| 36 | } |
| 37 | |
| 38 | // verb is used internally to represent the different "nft" verbs |
| 39 | type verb string |
| 40 | |
| 41 | const ( |
| 42 | addVerb verb = "add" |
| 43 | createVerb verb = "create" |
| 44 | insertVerb verb = "insert" |
| 45 | replaceVerb verb = "replace" |
| 46 | deleteVerb verb = "delete" |
| 47 | flushVerb verb = "flush" |
| 48 | ) |
| 49 | |
| 50 | // populateCommandBuf populates the transaction as series of nft commands to the given bytes.Buffer. |
| 51 | func (tx *Transaction) populateCommandBuf(buf *bytes.Buffer) error { |
| 52 | if tx.err != nil { |
| 53 | return tx.err |
| 54 | } |
| 55 | |
| 56 | for _, op := range tx.operations { |
| 57 | op.obj.writeOperation(op.verb, tx.nftContext, buf) |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // String returns the transaction as a string containing the nft commands; if there is |
| 63 | // a pending error, it will be output as a comment at the end of the transaction. |
| 64 | func (tx *Transaction) String() string { |
| 65 | buf := &bytes.Buffer{} |
| 66 | for _, op := range tx.operations { |
| 67 | op.obj.writeOperation(op.verb, tx.nftContext, buf) |
| 68 | } |
| 69 | |
| 70 | if tx.err != nil { |
| 71 | fmt.Fprintf(buf, "# ERROR: %v", tx.err) |
| 72 | } |
| 73 | |
| 74 | return buf.String() |
| 75 | } |
| 76 | |
| 77 | // NumOperations returns the number of operations queued in the transaction. |
| 78 | func (tx *Transaction) NumOperations() int { |
| 79 | return len(tx.operations) |
| 80 | } |
| 81 | |
| 82 | func (tx *Transaction) operation(verb verb, obj Object) { |
| 83 | if tx.err != nil { |
| 84 | return |
| 85 | } |
| 86 | if tx.err = obj.validate(verb); tx.err != nil { |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | tx.operations = append(tx.operations, operation{verb: verb, obj: obj}) |
| 91 | } |
| 92 | |
| 93 | // Add adds an "nft add" operation to tx, ensuring that obj exists by creating it if it |
| 94 | // did not already exist. (If obj is a Rule, it will be appended to the end of its chain, |
| 95 | // or else added after the Rule indicated by this rule's Index or Handle.) The Add() call |
| 96 | // always succeeds, but if obj is invalid, or inconsistent with the existing nftables |
| 97 | // state, then an error will be returned when the transaction is Run. |
| 98 | func (tx *Transaction) Add(obj Object) { |
| 99 | tx.operation(addVerb, obj) |
| 100 | } |
| 101 | |
| 102 | // Create adds an "nft create" operation to tx, creating obj, which must not already |
| 103 | // exist. (If obj is a Rule, it will be appended to the end of its chain, or else added |
| 104 | // after the Rule indicated by this rule's Index or Handle.) The Create() call always |
| 105 | // succeeds, but if obj is invalid, already exists, or is inconsistent with the existing |
| 106 | // nftables state, then an error will be returned when the transaction is Run. |
| 107 | func (tx *Transaction) Create(obj Object) { |
| 108 | tx.operation(createVerb, obj) |
| 109 | } |
| 110 | |
| 111 | // Insert adds an "nft insert" operation to tx, inserting obj (which must be a Rule) at |
| 112 | // the start of its chain, or before the other Rule indicated by this rule's Index or |
| 113 | // Handle. The Insert() call always succeeds, but if obj is invalid or is inconsistent |
| 114 | // with the existing nftables state, then an error will be returned when the transaction |
| 115 | // is Run. |
| 116 | func (tx *Transaction) Insert(obj Object) { |
| 117 | tx.operation(insertVerb, obj) |
| 118 | } |
| 119 | |
| 120 | // Replace adds an "nft replace" operation to tx, replacing an existing rule with obj |
| 121 | // (which must be a Rule). The Replace() call always succeeds, but if obj is invalid, does |
| 122 | // not contain the Handle of an existing rule, or is inconsistent with the existing |
| 123 | // nftables state, then an error will be returned when the transaction is Run. |
| 124 | func (tx *Transaction) Replace(obj Object) { |
| 125 | tx.operation(replaceVerb, obj) |
| 126 | } |
| 127 | |
| 128 | // Flush adds an "nft flush" operation to tx, clearing the contents of obj. The Flush() |
| 129 | // call always succeeds, but if obj does not exist (or does not support flushing) then an |
| 130 | // error will be returned when the transaction is Run. |
| 131 | func (tx *Transaction) Flush(obj Object) { |
| 132 | tx.operation(flushVerb, obj) |
| 133 | } |
| 134 | |
| 135 | // Delete adds an "nft delete" operation to tx, deleting obj. The Delete() call always |
| 136 | // succeeds, but if obj does not exist or cannot be deleted based on the information |
| 137 | // provided (eg, Handle is required but not set) then an error will be returned when the |
| 138 | // transaction is Run. |
| 139 | func (tx *Transaction) Delete(obj Object) { |
| 140 | tx.operation(deleteVerb, obj) |
| 141 | } |