Skip to content

Commit 02958db

Browse files
authored
Merge pull request #37 from Zadamsa/list_detector
Introduce new module List detector
2 parents 8721e98 + caef81b commit 02958db

53 files changed

Lines changed: 2398 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/actions/install-dependencies/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ runs:
2929
if: ${{ inputs.clang-tools == 'true' }}
3030
shell: bash
3131
run: |
32-
dnf install -y clang clang-tools-extra
32+
dnf install -y clang clang-tools-extra

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ jobs:
3232
uses: ./.github/workflows/build.yml
3333
with:
3434
os: ${{ matrix.os }}
35+
tests:
36+
needs: [build-os-matrix, build]
37+
uses: ./.github/workflows/tests.yml
38+
with:
39+
os: "oraclelinux:9"
3540
rpm-install:
3641
needs: [build-os-matrix, build]
3742
strategy:

.github/workflows/tests.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: tests
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
os:
7+
required: true
8+
type: string
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-latest
13+
container: ${{ inputs.os }}
14+
steps:
15+
- name: Install git
16+
run: dnf install -y git
17+
- name: Check out repository code
18+
uses: actions/checkout@v4
19+
- name: Install dependencies
20+
uses: ./.github/actions/install-dependencies
21+
- name: Install nemea
22+
run: |
23+
dnf copr enable @CESNET/NEMEA-testing
24+
dnf copr enable @CESNET/NEMEA
25+
dnf install -y epel-release
26+
dnf install -y nemea-framework-devel
27+
dnf install -y nemea
28+
dnf install -y procps-ng autoconf
29+
echo PATH=/usr/bin/nemea:$PATH >> $GITHUB_ENV
30+
- name: Compile modules
31+
run: |
32+
cmake -S . -B build -DNM_NG_ENABLE_TESTS=On
33+
make -C build install
34+
- name: Run tests
35+
run: |
36+
echo "Path=$PATH"
37+
make -C build tests

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2020
option(NM_NG_ENABLE_DOC_DOXYGEN "Enable build of code documentation" OFF)
2121
option(NM_NG_BUILD_WITH_ASAN "Build with Address Sanitizer (only for CMAKE_BUILD_TYPE=Debug)" OFF)
2222
option(NM_NG_BUILD_WITH_UBSAN "Build with Undefined Behavior Sanitizer (only for CMAKE_BUILD_TYPE=Debug)" OFF)
23+
option(NM_NG_ENABLE_TESTS "Build with tests of modules" OFF)
2324

2425
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Wunused -Wconversion -Wsign-conversion")
2526
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -Werror")
@@ -41,3 +42,7 @@ add_subdirectory(modules)
4142
add_subdirectory(common)
4243
add_subdirectory(pkg)
4344
add_subdirectory(doc)
45+
46+
if (NM_NG_ENABLE_TESTS)
47+
include(cmake/tests.cmake)
48+
endif()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This repository contains basic modules of the [NEMEA
44
system](https://github.com/CESNET/Nemea). The modules and their
55
functionality/purposes are:
66

7+
* [ListDetector](modules/listdetector/): forwards records that match rules list.
78
* [Sampler](modules/sampler/): sample records at the given rate.
89
* [Telemetry](modules/telemetry/): provides unirec telemetry of the input interface.
910
* [Deduplicator](modules/deduplicator/): omit duplicate records.

cmake/tests.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# File searches for test.sh script in each module subdirectory and runs it if present
2+
3+
file(GLOB MODULE_DIRS RELATIVE ${CMAKE_SOURCE_DIR}/modules ${CMAKE_SOURCE_DIR}/modules/*)
4+
enable_testing()
5+
6+
add_custom_target(tests
7+
COMMAND ctest --output-on-failure
8+
VERBATIM
9+
)
10+
11+
foreach(MODULE ${MODULE_DIRS})
12+
if (NOT IS_DIRECTORY ${CMAKE_SOURCE_DIR}/modules/${MODULE})
13+
continue()
14+
endif()
15+
set(TEST_SCRIPT "${CMAKE_SOURCE_DIR}/modules/${MODULE}/tests/test.sh")
16+
17+
if (EXISTS ${TEST_SCRIPT})
18+
add_test(NAME Test${MODULE} COMMAND bash ${TEST_SCRIPT} ${CMAKE_BINARY_DIR}/modules/${MODULE}/src/${MODULE})
19+
add_dependencies(tests ${MODULE})
20+
else()
21+
add_custom_target(print_missing_test_for_${MODULE}
22+
COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --yellow --bold "No test.sh found for: ${MODULE}. Skipping..."
23+
VERBATIM
24+
)
25+
add_dependencies(tests print_missing_test_for_${MODULE})
26+
endif()
27+
endforeach()

modules/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(listDetector)
12
add_subdirectory(sampler)
23
add_subdirectory(telemetry)
34
add_subdirectory(deduplicator)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(src)

modules/listDetector/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# ListDetector module - README
2+
3+
## Description
4+
The module analyzes Unirec records by comparing them against a set of predefined rules in a rule list.
5+
Rule list can be blacklist or whitelist. It identifies and forwards records that do not match to the whitelist rules or match blacklist rules.
6+
7+
## Interfaces
8+
- Input: 1
9+
- Output: 1
10+
11+
## Parameters
12+
### Common TRAP parameters
13+
- `-h [trap,1]` Print help message for this module / for libtrap specific parameters.
14+
- `-i IFC_SPEC` Specification of interface types and their parameters.
15+
- `-v` Be verbose.
16+
- `-vv` Be more verbose.
17+
- `-vvv` Be even more verbose.
18+
19+
### Module specific parameters
20+
- `-r, --rules <file>` ListDetector module rules in CSV format
21+
- `-lm, --listmode <file>` ListDetector mode - whitelist or blacklist
22+
- `-m, --appfs-mountpoint <path>` Path where the appFs directory will be mounted
23+
24+
## CSV rules format
25+
The first row of CSV specifies the unirec types and names of fields that will be
26+
used for whitelisting or blacklisting.
27+
28+
The supported unirec types are: `uint8`, `int8`, `uint16`, `int16`, `uint32`, `int32`,
29+
`uint64`, `int64`, `char`, `ipaddr` and `string`.
30+
31+
- Empty values match everyting.
32+
33+
- Numeric types match the exact value.
34+
35+
- IP address (`ipaddr`) can be either ipv4 or ipv6 address.
36+
The ip address can optionally have a prefix.
37+
If there is no prefix, the address must match exactly.
38+
- Examples: `127.0.0.1`, `127.0.0.0/24`
39+
40+
- String match a regex pattern. Regex patterns support extended grep syntax.
41+
- Examples: `R"(^www.google.com$)"`, `R"(.*google\.com$)"`
42+
43+
### Example CSV file
44+
45+
```
46+
ipaddr SRC_IP,uint16 DST_PORT,uint16 SRC_PORT
47+
10.0.0.1,443,53530
48+
10.0.0.2,443,53531
49+
```
50+
51+
```
52+
ipaddr SCR_IP,string QUIC_SNI
53+
10.0.0.1/24,R"(.*google\.com$)"
54+
```
55+
56+
## Usage Examples
57+
```
58+
# Data from the input unix socket interface "trap_in" is processed, and entries that
59+
do not match the defined rules in the "csvWhitelist.csv" file are forwarded to the
60+
output interface "trap_out."
61+
62+
$ listDetector -i u:trap_in,u:trap_out -r csvWhitelist.csv
63+
```
64+
```
65+
# Data from the input unix socket interface "trap_in" is processed, and entries that
66+
match the defined rules in the "csvblacklist.csv" file are forwarded to the
67+
output interface "trap_out."
68+
69+
$ listDetector -i u:trap_in,u:trap_out -lm bl -r csvBlacklist.csv
70+
```
71+
72+
## Telemetry data format
73+
```
74+
├─ input/
75+
│ └─ stats
76+
└─ listDetector/
77+
├─ aggStats
78+
└─ rules/
79+
├─ 0
80+
├─ 1
81+
└ ...
82+
```
83+
84+
Each rule has its own file named according to the order of the rules in the configuration file.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
add_executable(listDetector
2+
main.cpp
3+
configParser.cpp
4+
csvConfigParser.cpp
5+
ipAddressPrefix.cpp
6+
rule.cpp
7+
ruleBuilder.cpp
8+
listDetector.cpp
9+
ipAddressFieldMatcher.cpp
10+
fieldsMatcher.cpp
11+
rulesMatcher.cpp
12+
)
13+
14+
target_link_libraries(listDetector PRIVATE
15+
telemetry::telemetry
16+
telemetry::appFs
17+
common
18+
rapidcsv
19+
unirec::unirec++
20+
unirec::unirec
21+
trap::trap
22+
argparse
23+
xxhash
24+
)
25+
26+
install(TARGETS listDetector DESTINATION ${INSTALL_DIR_BIN})

0 commit comments

Comments
 (0)