Commit Graph
38 Commits
Author SHA1 Message Date
Jasper Wiegratz 23dbeabf37 Merge pull request #166 from stevelr/main
fix: bitwise flags or with more than two operands
2025-08-15 15:49:28 +02:00
Paul Holzinger 54ba9a30e9 fix: accept single or multiple set/map flags
Like for fib and synproxy flags with nft 1.1.4 a single flag value is
printed as string not array so the parser need to account for that.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:34:35 +02:00
Paul Holzinger c7ce1b0c4a fix: accept single or multiple synproxy flags
Like for fib flags with nft 1.1.4 a single flag value is printed as
string not array so the parser need to account for that.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:34:35 +02:00
Paul Holzinger d234af6ea1 fix: accept fib flags as string or array
nft 1.1.4 changed the json output to return only a single string if
there is only one flag set.[1,2]

The same logic applies to some other types as well which I update in the
following commits.

[1] https://github.com/nftables-rs/nftables-rs/issues/183
[2] https://bugzilla.netfilter.org/show_bug.cgi?id=1806

Fixes #183

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:25:23 +02:00
jwhb 6bd0d2b61f fix(stmt): serialize anonymouscounter none as zero 2025-06-26 11:15:37 +02:00
stevelr 9024a91fc4 fix: bitwise flags or with more than two operands
Fixes #165
- changed BinaryOperation::OR to accept Vec<Expression> instead of exactly two.
- added regression test (tests/test_bit_flags)
- note: The fix was tested with deserialization only. I did not test adding an nftables
  rule with bit flags

Signed-off-by: stevelr <stevelr.git@pm.me>
2025-06-19 06:03:55 +00:00
jwhb 94d2a014dd fix: clippy string format lint 2025-05-08 15:43:29 +02:00
Eric Long 021668a923 feat(helper): generalize arguments
Use IntoIterator for arguments to generalize for all containers

Based on a suggestion from Eric Long in #108
2025-01-07 14:16:41 +01:00
Michael Buesch ccee3c243f helper: Change API to AsRef<OsStr> based API
This improves the versatility of the API.
With the new API there are more ideomatic options for the user like
&Path, PathBuf and also still &str and more.

This is an API incompatible change.
I think it's not really possible to make this API compatible,
because the None-values need explicit type annotations due to the trait based API.
The default None-based API has been replaced by functions without arguments
to make the common case easier to use.

The user code changes are probably minimal in most cases, because None arguments
can just be removed and string arguments keep working as-is due to the std
implementations of AsRef.
Convenience constants are provided to make use of the None-arguments
to the _with_args() functions more readable and easier to use.

As a side effect this change also removes an unnecessary allocation of the program
name string in case of a successful execution.
2025-01-07 14:05:58 +01:00
wiegratz 3948819e10 fix(tests): fix datatest_stable::harness macro usage 2025-01-07 13:13:11 +01:00
Eric Long c22a2a47d6 feat!: replace Cow<'static, _> with 'a
This adds a bunch of lifetimes, but it works with `.into()` when a borrowed value's array container is stored in another place, without the need of `Cow::Borrowed`:

```
let objects = [
    NfObject::ListObject(NfListObject::Table(Table {
        family: NfFamily::INet,
        name: "some_inet_table".into(), // here
        handle: None,
    })),
    // ...
];
let expected = Nftables {
    objects: objects.into(); // and here
};
```

However, `Cow::{Borrowed, Owned}` still needs to be used when you want to write the above in one statement.
2024-12-21 21:19:10 +01:00
Eric Long 75b7f48795 fix(expr)!: revert recursive Cow<[Expression]> back to Vec
This fails to build on 1.65:

```
error[E0277]: the trait bound `[SetItem]: ToOwned` is not satisfied in `Expression`
   --> src/stmt.rs:245:14
    |
245 |     pub dev: Option<Expression>,
    |              ^^^^^^^^^^^^^^^^^^ within `Expression`, the trait `ToOwned` is not implemented for `[SetItem]`, which is required by `Expression: Sized`
    |
    = help: the trait `ToOwned` is implemented for `[T]`
note: required because it appears within the type `Expression`
   --> src/expr.rs:10:10
    |
10  | pub enum Expression {
    |          ^^^^^^^^^^
note: required by an implicit `Sized` bound in `std::option::Option`
   --> /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/option.rs:572:1

...and many, many similar errors
```

It compiles successfully on Rust >=1.79, so this commit can be reverted once our MSRV reaches it.
2024-12-21 21:19:10 +01:00
Eric Long 8ddb5ff132 feat!: use Cow whenever possible instead of owned values
A lot of strings and arrays used in nftables structures are static, but
currently we need to do allocations everywhere using owned String and Vec.
Replace them with Cow so we can pass &'static str or &'static [_] whenever
possible, while returning owned value when deserializing.
2024-12-21 21:19:10 +01:00
Eric Long 091adb4313 feat(helper)!: make helper APIs accept borrowed values 2024-12-21 21:19:10 +01:00
wiegratz 68332fd8df test(deserialize): generate deserialize tests with harness 2024-11-10 18:04:37 +01:00
wiegratz 95e6fb5023 ci: run tests rootless with unshare 2024-10-30 00:14:02 +00:00
wiegratz abd3156e84 feat(schema)!: add default impl, add doc comments
added default impl for table, chain, rule, set, map, element, flowtable, counter, quota, cthelper, limit, ruleset, metainfoobject, cttimeout.
Default impls replace ::new() functions.

replace family: String by family: NfFamily in flowtable, counter, quota, cthelper, limit
2024-10-24 20:51:06 +02:00
wiegratz fd8857314d test(stmt): add serialization test for flow, flowtable 2024-10-21 10:55:10 +02:00
wiegratz 9f109c51e4 fix(stmt): fix named counter
BREAKING CHANGE: Use new enum variants of `Counter`: `Named` and `Anonymous`.
2024-10-06 18:14:32 +02:00
wiegratz 51ccf106da fix: store NfListObjects in heap
BREAKING CHANGE: With enum `NfObject::ListObject`, use `Box::new(NfListObject...)`.
2024-10-06 16:52:49 +02:00
wiegratz c20bcee69d fix race condition in tests
cargo runs tests in parallel.
tests that work on real nftables may be conflicting.
the serial crate is used to mark tests to be run serially.
2024-03-16 15:45:14 +00:00
wiegratz ea5ad7b54d add helper tests and setup
adds a flush command to the top of the example ruleset so that nftables rules are applied on a clean state.
adds tests that check args passing to nft with get_current_ruleset and specifying invalid nft binary.
2024-03-16 15:07:50 +00:00
wiegratz bab0641187 Merge branch 'payload' 2024-03-16 14:59:48 +00:00
wiegratz 6ca00192b3 Run nft tests in netns
Tests that use nft command alter the system's
nftables. By creating a fresh net namespace for
each run, tests will not break system networking.
2024-03-15 23:57:54 +00:00
Paul Holzinger 0deaf7d864 fix payload raw expression
The current code cannot handle the ParloadRaw type, serde is unable to
deserialize the json with a raw payload fields.

This is because serde cannot handle two fields with the same name and I
haven't found a way to make this work like that. Instead we have to use
a untagged enum that share both possible payload values.

Therefore this is an API break: NamedExpression now only contains one
Payload field which contains the Payload enum, the old Payload type is
renamed to PayloadField.

Lastly, export the PayloadRaw fields so they can be used externally.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-11 16:35:25 +01:00
wiegratz fd2bd09ad1 chore: fix clippy warnings 2024-01-22 11:56:31 +01:00
wiegratz 04e29d85e8 Merge branch 'add_missing_things' 2024-01-22 11:36:00 +01:00
Marcin Osypka 3c87c233e9 Add the comment field to a map
The comments can be added to the map.
2024-01-18 18:50:51 +01:00
Marcin Osypka 8a31a720a7 Add comment field to set
The comment field on a set is not present in documentation but it is
working and sometimes convenient to use.
2024-01-18 18:48:35 +01:00
Marcin Osypka d1ac323bf9 Implement map support
A Verdict enum was moved from a NamedExpression to an Expression,
placing it in a NamedExpression was a bug. Accroding to
libnftables-json(5) (section VERDICT) a Verdict does not have a
"verdict" key, it is "anonymous". e.g.: { "jump": { "target":
"my_target"}, and not: { "verdict": { "jump": { "target": "my_target"} }
}

Additionally a `Verdict::Jump` and a `Verdict::Goto` data was changed to
a `stmt::JumpTarget` struct from a `String`.

A `Map` and a `Set` use almost the same fields, a map additinally uses a
map field of type `String`.
2024-01-17 19:40:23 +01:00
wiegratz 1bd71ea992 tests: test for error when deleting unknown table 2023-08-12 18:07:27 +02:00
bits0rcerer fd01cfe50d return error instead of panic 2023-08-11 10:54:06 +02:00
Noel 4f427820a7 Fix queue JSON serialisation when flag is none 2023-07-07 14:01:56 +02:00
Noel d08001ffb8 Update queue flags to be optional 2023-07-07 12:00:46 +02:00
wiegratz 720d2c75db schema: implement set type, add test for set 2023-03-19 23:38:56 +00:00
Thomas Spellman eeda434ac9 change insert to match add
This change adds the necessary {"rule": ...} json object, similar to how the add command works.
2022-12-15 21:34:43 -08:00
Thomas Spellman 0c80cf4832 Fix test imports 2022-12-15 21:32:21 -08:00
wiegratz e331c6f8ad lib: first commit 2022-09-30 17:32:07 +00:00