Skip to content

Commit

Permalink
Add EnableLogging and LogLabel supports for Node NetworkPolicy
Browse files Browse the repository at this point in the history
This commit introduces limited support for traffic logging in Node
NetworkPolicy. The limitations are:

- Traffic logs are written only to the system log (not managed by Antrea).
  Users can filter logs using syslog filters.
- The `LogLabel` for Node NetworkPolicy is restricted to a maximum of
  12 characters.

Node NetworkPolicy's data path is implemented via iptables. An iptables
"non-terminating target" `LOG` is added before the final matching rule to
log packets to the system kernel log. The logs provide packet match details,
such as:

```
Sep  2 10:31:07 k8s-node-control-plane kernel: [6657320.789675] Antrea:I:Allow:allow-http:IN=ens224 OUT= MAC=00:50:56:a7:fb:18:00:50:56:a7:23:47:08:00 SRC=10.10.0.10 DST=192.168.240.200 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=52813 DF PROTO=TCP SPT=57658 DPT=80 WINDOW=64240 RES=0x00 SYN URGP=0
Sep  2 10:31:11 k8s-node-control-plane kernel: [6657324.899219] Antrea:I:Drop:default-drop:IN=ens224 OUT= MAC=00:50:56:a7:fb:18:00:50:56:a7:23:47:08:00 SRC=192.168.240.201 DST=192.168.240.200 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=27486 DF PROTO=TCP SPT=33152 DPT=80 WINDOW=64240 RES=0x00 SYN URGP=0
```

The log prefix (e.g., `Antrea:I:Allow:allow-http:`) is up to 29 characters long
and includes a user-provided log label (up to 12 characters). The log prefix format:

```
|---1--| |2| |---3--| |----------4--------|
|Antrea|:|I|:|Reject|:|user-provided label|:|
|6     |1|1|1|4-6   |1|1-12               |1|
```

- Part 1: Fixed, "Antrea"
- Part 2: Direction, "I" (In) or "O" (Out)
- Part 3: Action, "Allow", "Drop", or "Reject"
- Part 4: User-provided log label, up to 12 characters

Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
  • Loading branch information
hongliangl committed Sep 24, 2024
1 parent 1821317 commit ed07d42
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions test/e2e/nodenetworkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package e2e

import (
"antrea.io/antrea/pkg/agent/util/sysctl"
"fmt"
"os/exec"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -898,12 +898,13 @@ func testNodeACNPNestedIPBlockClusterGroupCreateAndUpdate(t *testing.T) {

func testNodeACNPAuditLogging(t *testing.T, data *TestData) {
if testOptions.providerName == "kind" {
// Let kernel messages generated by iptables LOG targets in network namespaces of Kind Nodes.
cmd := exec.Command("sh", "-c", "sysctl -w \"/proc/sys/net/netfilter/nf_log_all_netns=1\"")
require.NoError(t, cmd.Run())
if err := sysctl.EnsureSysctlNetValue("netfilter/nf_log_all_netns", 1); err != nil {
t.Fatalf("failed to set", err)

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (ubuntu-latest)

printf: (*testing.common).Fatalf call has arguments but no formatting directives (govet)

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

printf: (*testing.common).Fatalf call has arguments but no formatting directives (govet)

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux with all features enabled

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux with non default values (proxyAll=true, LoadBalancerMode=DSR...

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / API compatible with client version N-1

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Upgrade from Antrea version N-1

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux (noEncap)

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Upgrade from Antrea version N-2

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux (hybrid)

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / API compatible with client version N-2

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux for Flow Visibility

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 902 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / build-and-test-ubi

(*testing.common).Fatalf call has arguments but no formatting directives
}
defer func() {
cmd := exec.Command("sh", "-c", "sysctl -w \"/proc/sys/net/netfilter/nf_log_all_netns=0\"")
require.NoError(t, cmd.Run())
if err := sysctl.EnsureSysctlNetValue("netfilter/nf_log_all_netns", 0); err != nil {
t.Fatalf("failed to set", err)

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (ubuntu-latest)

printf: (*testing.common).Fatalf call has arguments but no formatting directives (govet)

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

printf: (*testing.common).Fatalf call has arguments but no formatting directives (govet)

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux with all features enabled

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux with non default values (proxyAll=true, LoadBalancerMode=DSR...

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / API compatible with client version N-1

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Upgrade from Antrea version N-1

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux (noEncap)

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / Upgrade from Antrea version N-2

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux (hybrid)

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / API compatible with client version N-2

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / E2e tests on a Kind cluster on Linux for Flow Visibility

(*testing.common).Fatalf call has arguments but no formatting directives

Check failure on line 906 in test/e2e/nodenetworkpolicy_test.go

View workflow job for this annotation

GitHub Actions / build-and-test-ubi

(*testing.common).Fatalf call has arguments but no formatting directives
}
}()
}
randomLogLabel := uuid.New().String()[:12] // Generate a random logLabel with 12 characters long.
Expand Down

0 comments on commit ed07d42

Please sign in to comment.