From c5f74a1ddae555d3da84ff46edec11a1548bff35 Mon Sep 17 00:00:00 2001 From: Felipe Zipitria Date: Mon, 21 Oct 2024 16:54:56 -0300 Subject: [PATCH 1/2] chore: expose request timestamp Signed-off-by: Felipe Zipitria --- internal/corazawaf/transaction.go | 5 +++++ internal/corazawaf/transaction_test.go | 9 +++++++++ types/transaction.go | 2 ++ 3 files changed, 16 insertions(+) diff --git a/internal/corazawaf/transaction.go b/internal/corazawaf/transaction.go index c05128271..9daa7acef 100644 --- a/internal/corazawaf/transaction.go +++ b/internal/corazawaf/transaction.go @@ -1504,6 +1504,11 @@ func (tx *Transaction) AuditLog() *auditlog.Log { return al } +// UnixTimestamp returns the timestamp when the request was received. +func (tx *Transaction) UnixTimestamp() int64 { + return tx.Timestamp +} + // Close closes the transaction after phase 5 // This method helps the GC to clean up the transaction faster and release resources // It also allows caches the transaction back into the sync.Pool diff --git a/internal/corazawaf/transaction_test.go b/internal/corazawaf/transaction_test.go index bfff8821a..6006c171a 100644 --- a/internal/corazawaf/transaction_test.go +++ b/internal/corazawaf/transaction_test.go @@ -1716,6 +1716,15 @@ func TestForceRequestBodyOverride(t *testing.T) { } } +func TestGetUnixTimestamp(t *testing.T) { + tx := makeTransaction(t) + stamp := tx.UnixTimestamp() + t.Logf("stamp: %d", stamp) + if stamp <= 0 { + t.Fatalf("no timestamp found") + } +} + func TestCloseFails(t *testing.T) { waf := NewWAF() tx := waf.NewTransaction() diff --git a/types/transaction.go b/types/transaction.go index dfbb3c66e..a031a9e68 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -193,6 +193,8 @@ type Transaction interface { // ID returns the transaction ID. ID() string + // UnixTimestamp returns the transaction timestamp + UnixTimestamp() int64 // Closer closes the transaction and releases any resources associated with it such as request/response bodies. io.Closer } From 3f609f20cb135a186b14af9fb04dcb36ee7959c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Zipitr=C3=ADa?= <3012076+fzipi@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:56:36 -0300 Subject: [PATCH 2/2] Apply suggestions from code review --- internal/corazawaf/transaction_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/corazawaf/transaction_test.go b/internal/corazawaf/transaction_test.go index 6006c171a..5f0aa8261 100644 --- a/internal/corazawaf/transaction_test.go +++ b/internal/corazawaf/transaction_test.go @@ -1719,7 +1719,6 @@ func TestForceRequestBodyOverride(t *testing.T) { func TestGetUnixTimestamp(t *testing.T) { tx := makeTransaction(t) stamp := tx.UnixTimestamp() - t.Logf("stamp: %d", stamp) if stamp <= 0 { t.Fatalf("no timestamp found") }