Skip to content

[management] enable access log cleanup by default#5842

Merged
pascal-fischer merged 4 commits intomainfrom
fix/access-log-cleanup-by-default
Apr 10, 2026
Merged

[management] enable access log cleanup by default#5842
pascal-fischer merged 4 commits intomainfrom
fix/access-log-cleanup-by-default

Conversation

@pascal-fischer
Copy link
Copy Markdown
Collaborator

@pascal-fischer pascal-fischer commented Apr 9, 2026

Describe your changes

Previously the access log cleanup for the proxy was only executed if explicitly enabled. This PR changes the behaviour to have a default retention of 7 days. Use negative values to keep data forever.

Issue ticket number and link

#5773

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

netbirdio/docs#693

Summary by CodeRabbit

  • New Features

    • Configurable access-log retention and cleanup interval options added to server configuration.
  • Bug Fixes

    • A retention value of 0 now defaults to 7 days; negative values continue to disable periodic cleanup.
  • Documentation

    • Clarified that 0 means a 7-day default retention.
  • Style / Logging

    • Improved debug logs to show whether default or configured retention and cleanup intervals are used.
  • Tests

    • Updated tests to align with the new retention semantics.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

📝 Walkthrough

Walkthrough

StartPeriodicCleanup now treats retentionDays == 0 as a default of 7 days; only retentionDays < 0 disables periodic cleanup. Added Debugf logs reporting whether default or configured retention and cleanup-interval values are used. Two CLI config fields were added and wired into management config.

Changes

Cohort / File(s) Summary
Access Log Manager
management/internals/modules/reverseproxy/accesslogs/manager/manager.go
Changed disable check from <= 0 to < 0; if retentionDays == 0 it defaults to 7 and logs via Debugf. Added Debugf for cleanup-interval selection; background goroutine cancellation/ticker/loop unchanged.
Configuration Documentation
management/internals/server/config/config.go
Updated comment for ReverseProxy.AccessLogRetentionDays to document that 0 defaults to 7 days and negative values disable cleanup.
Combined CLI Config
combined/cmd/config.go
Added AccessLogRetentionDays and AccessLogCleanupIntervalHours to ReverseProxyConfig and populated corresponding fields in ToManagementConfig().

Sequence Diagram(s)

(omitted — changes are localized and do not introduce multi-component sequential flows)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • crn4

Poem

🐰 I hopped through configs in the pale-screen light,
A zero found seven and set the logs right.
Tickers whisper hourly, Debug sings along,
Cleanup hums steady, the manager's song.
I munch tiny carrots and cheer — code fixed, not long.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main behavioral change: enabling access log cleanup by default, which is the primary objective of this PR.
Description check ✅ Passed The description includes key sections: changes overview, linked issue ticket, and documentation updates. The checklist is completed and contributor agreement is acknowledged.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/access-log-cleanup-by-default

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
management/internals/modules/reverseproxy/accesslogs/manager/manager.go (1)

114-126: Add focused tests for the new retention semantics.

Given the behavior change (retentionDays == 0 defaults to 7, < 0 disables cleanup), please add targeted unit tests for these branches to prevent regressions.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@management/internals/modules/reverseproxy/accesslogs/manager/manager.go`
around lines 114 - 126, Add focused unit tests covering the new retention
semantics around the retentionDays variable: one test where retentionDays == 0
verifies it defaults to 7 days (and any created cleanup job uses 7), and one
test where retentionDays < 0 verifies cleanup is disabled (no cleanup
job/scheduler started). Use the same constructor or initializer used in
manager.go (the code paths that read retentionDays and cleanupIntervalHours) to
create the manager and assert resulting behavior/state; also add a test for
cleanupIntervalHours <= 0 defaulting to 24 hours if convenient. Name tests
clearly (e.g., TestRetention_DefaultsTo7_WhenZero and
TestRetention_Disabled_WhenNegative) and mock or inspect the scheduler/cleanup
job to confirm the expected behavior and logs.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@management/internals/modules/reverseproxy/accesslogs/manager/manager.go`:
- Around line 109-110: The debug message for the retention check is inconsistent
with the condition; update the log call that uses retentionDays (the
log.WithContext(ctx).Debug call in manager.go) so the message matches the check
for retentionDays < 0 (e.g., "periodic access log cleanup disabled: retention
days is negative") or alternatively change the condition to <= 0 if the intent
was to disable on zero as well; adjust the string accordingly in the
log.WithContext(ctx).Debug invocation referencing retentionDays to keep
condition and message aligned.

---

Nitpick comments:
In `@management/internals/modules/reverseproxy/accesslogs/manager/manager.go`:
- Around line 114-126: Add focused unit tests covering the new retention
semantics around the retentionDays variable: one test where retentionDays == 0
verifies it defaults to 7 days (and any created cleanup job uses 7), and one
test where retentionDays < 0 verifies cleanup is disabled (no cleanup
job/scheduler started). Use the same constructor or initializer used in
manager.go (the code paths that read retentionDays and cleanupIntervalHours) to
create the manager and assert resulting behavior/state; also add a test for
cleanupIntervalHours <= 0 defaulting to 24 hours if convenient. Name tests
clearly (e.g., TestRetention_DefaultsTo7_WhenZero and
TestRetention_Disabled_WhenNegative) and mock or inspect the scheduler/cleanup
job to confirm the expected behavior and logs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f6b5371f-82e7-4c00-9257-79195247f790

📥 Commits

Reviewing files that changed from the base of the PR and between 099c493 and 4d38d33.

📒 Files selected for processing (2)
  • management/internals/modules/reverseproxy/accesslogs/manager/manager.go
  • management/internals/server/config/config.go

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@management/internals/modules/reverseproxy/accesslogs/manager/manager.go`:
- Around line 114-119: The retention behavior in manager.go now defaults
retentionDays==0 to 7 days instead of disabling cleanup; update the unit test in
management/internals/modules/reverseproxy/accesslogs/manager/manager_test.go
(the test that covers "zero retention" around Lines 123-143) to expect cleanup
to run with a 7-day retention instead of being skipped—adjust assertions to
check that StartCleanup (or the cleanup goroutine triggered by the Manager
constructor / NewManager) is invoked and that the configured retention value
equals 7, and update any log/expectation strings from "disabled" to "defaulting
to 7 days" or the new debug message emitted by the Manager.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e6facc38-ebed-40b3-aee8-78b6a189ab47

📥 Commits

Reviewing files that changed from the base of the PR and between 4d38d33 and 94f4a26.

📒 Files selected for processing (1)
  • management/internals/modules/reverseproxy/accesslogs/manager/manager.go

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
combined/cmd/config.go (1)

360-363: ⚠️ Potential issue | 🟠 Major

Custom access-log retention/cleanup can be silently dropped in simplified mode.

At Line 361, the copy gate ignores the new fields. If users set only server.reverseProxy.accessLogRetentionDays or server.reverseProxy.accessLogCleanupIntervalHours, Line 362 never runs, so management config keeps zero values instead of the configured ones.

💡 Proposed fix
-	// Copy reverse proxy config from server
-	if len(c.Server.ReverseProxy.TrustedHTTPProxies) > 0 || c.Server.ReverseProxy.TrustedHTTPProxiesCount > 0 || len(c.Server.ReverseProxy.TrustedPeers) > 0 {
-		c.Management.ReverseProxy = c.Server.ReverseProxy
-	}
+	// Copy reverse proxy config from server
+	// In simplified mode, server.reverseProxy is the source of truth.
+	c.Management.ReverseProxy = c.Server.ReverseProxy
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@combined/cmd/config.go` around lines 360 - 363, The copy gate for reverse
proxy config only checks TrustedHTTPProxies, TrustedHTTPProxiesCount, and
TrustedPeers so custom access-log fields like AccessLogRetentionDays and
AccessLogCleanupIntervalHours can be skipped; update the condition around
assigning c.Management.ReverseProxy = c.Server.ReverseProxy to also check
c.Server.ReverseProxy.AccessLogRetentionDays and
c.Server.ReverseProxy.AccessLogCleanupIntervalHours (or simply always copy the
struct when any reverse-proxy-related field is non-zero), ensuring the
AccessLogRetentionDays and AccessLogCleanupIntervalHours values from
c.Server.ReverseProxy are propagated to c.Management.ReverseProxy.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@combined/cmd/config.go`:
- Around line 360-363: The copy gate for reverse proxy config only checks
TrustedHTTPProxies, TrustedHTTPProxiesCount, and TrustedPeers so custom
access-log fields like AccessLogRetentionDays and AccessLogCleanupIntervalHours
can be skipped; update the condition around assigning c.Management.ReverseProxy
= c.Server.ReverseProxy to also check
c.Server.ReverseProxy.AccessLogRetentionDays and
c.Server.ReverseProxy.AccessLogCleanupIntervalHours (or simply always copy the
struct when any reverse-proxy-related field is non-zero), ensuring the
AccessLogRetentionDays and AccessLogCleanupIntervalHours values from
c.Server.ReverseProxy are propagated to c.Management.ReverseProxy.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2deecda7-7678-421a-a96c-d6e2a3fd9ed4

📥 Commits

Reviewing files that changed from the base of the PR and between 94f4a26 and cdab315.

📒 Files selected for processing (1)
  • combined/cmd/config.go

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
management/internals/modules/reverseproxy/accesslogs/manager/manager_test.go (1)

124-138: ⚠️ Potential issue | 🟡 Minor

Add an explicit periodic test for the zero-retention default path.

The update at Line 138 (-1) correctly covers the disable case, but this suite now lacks a direct assertion that retentionDays == 0 triggers cleanup with the 7-day default (core behavior of this PR). Please add a subtest for that path.

Suggested test addition
@@
 	t.Run("periodic cleanup disabled with negative retention", func(t *testing.T) {
@@
 	})
+
+	t.Run("periodic cleanup defaults to 7 days when retention is zero", func(t *testing.T) {
+		ctrl := gomock.NewController(t)
+		defer ctrl.Finish()
+
+		mockStore := store.NewMockStore(ctrl)
+		mockStore.EXPECT().
+			DeleteOldAccessLogs(gomock.Any(), gomock.Any()).
+			DoAndReturn(func(_ context.Context, olderThan time.Time) (int64, error) {
+				expectedCutoff := time.Now().AddDate(0, 0, -7)
+				assert.Less(t, olderThan.Sub(expectedCutoff).Abs(), time.Second)
+				return int64(1), nil
+			}).
+			Times(1)
+
+		manager := &managerImpl{store: mockStore}
+		ctx, cancel := context.WithCancel(context.Background())
+		defer cancel()
+
+		manager.StartPeriodicCleanup(ctx, 0, 24)
+		time.Sleep(100 * time.Millisecond)
+	})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@management/internals/modules/reverseproxy/accesslogs/manager/manager_test.go`
around lines 124 - 138, Add a new subtest that calls
managerImpl.StartPeriodicCleanup with retentionDays == 0 and asserts the default
7-day retention path is used: create a gomock controller and a
store.NewMockStore(ctrl), set expectations on mockStore for a single cleanup
invocation (or whatever method managerImpl calls to purge logs) using
retentionDays == 7 (or matching a 7-day time window), instantiate manager :=
&managerImpl{store: mockStore}, start the periodic cleanup with ctx and interval
1, let it run briefly, then cancel and verify mock expectations; use symbols
StartPeriodicCleanup and managerImpl to locate where to add the test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In
`@management/internals/modules/reverseproxy/accesslogs/manager/manager_test.go`:
- Around line 124-138: Add a new subtest that calls
managerImpl.StartPeriodicCleanup with retentionDays == 0 and asserts the default
7-day retention path is used: create a gomock controller and a
store.NewMockStore(ctrl), set expectations on mockStore for a single cleanup
invocation (or whatever method managerImpl calls to purge logs) using
retentionDays == 7 (or matching a 7-day time window), instantiate manager :=
&managerImpl{store: mockStore}, start the periodic cleanup with ctx and interval
1, let it run briefly, then cancel and verify mock expectations; use symbols
StartPeriodicCleanup and managerImpl to locate where to add the test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7df96d6a-d66e-4b15-9fae-ed66126bda9e

📥 Commits

Reviewing files that changed from the base of the PR and between cdab315 and 9066761.

📒 Files selected for processing (1)
  • management/internals/modules/reverseproxy/accesslogs/manager/manager_test.go

@pascal-fischer pascal-fischer merged commit cf86b9a into main Apr 10, 2026
62 of 63 checks passed
@pascal-fischer pascal-fischer deleted the fix/access-log-cleanup-by-default branch April 10, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants