Add blocked request access logging#140
Open
JasonLovesDoggo wants to merge 5 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new access_log configuration option to the defender HTTP handler so that blocked requests can be routed to one or more named Caddy access loggers, and enriches blocked-request access logs with defender.* structured fields.
Changes:
- Added
access_logconfig (AccessLogNames) to route blocked requests to named access logger(s). - Mark blocked requests with extra structured log fields:
defender.blocked,defender.action,defender.client_ip,defender.reason. - Updated documentation and added unit + E2E test coverage for access-log routing and log field enrichment.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| README.md | Documents the new access_log option in the directive syntax. |
| plugin.go | Adds the AccessLogNames field to the module configuration struct. |
| config.go | Parses access_log from Caddyfile into AccessLogNames. |
| middleware.go | Sets access logger names for blocked requests and injects extra log fields. |
| middleware_test.go | Unit test verifying blocked requests are routed to configured logger(s). |
| config_test.go | Tests parsing access_log via Caddyfile and JSON config. |
| docs/examples.md | Adds a “Log Blocked Requests” example and describes new defender.* fields. |
| docs/config.md | Documents the access_log option in Caddyfile/JSON config docs. |
| access_log_e2e_test.go | E2E test validating blocked requests hit the configured access logger and include defender.* fields. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+88
to
+93
| existing, _ := caddyhttp.GetVar(ctx, caddyhttp.AccessLoggerNameVarKey).([]any) | ||
| names := make([]any, 0, len(existing)+len(defenderLogNames)) | ||
| names = append(names, existing...) | ||
| for _, name := range defenderLogNames { | ||
| names = append(names, name) | ||
| } |
Comment on lines
+99
to
+101
| m.AccessLogNames = append(m.AccessLogNames, d.Val()) | ||
| for d.NextArg() { | ||
| m.AccessLogNames = append(m.AccessLogNames, d.Val()) |
Comment on lines
+20
to
+35
| defer tester.InitServer(`{ | ||
| admin localhost:2999 | ||
| }`, "caddyfile") | ||
|
|
||
| tester.InitServer(fmt.Sprintf(`{ | ||
| admin localhost:2999 | ||
| order defender after header | ||
| servers { | ||
| trusted_proxies static 127.0.0.1/32 ::1/128 | ||
| client_ip_headers X-Forwarded-For | ||
| } | ||
| } | ||
|
|
||
| http://localhost:9080 { | ||
| log defender_blocked { | ||
| output net %q { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an
access_logoption todefenderso blocked requests can be routed to a named Caddy access logger.Blocked access log entries also include
defender.blocked,defender.action,defender.client_ip, anddefender.reasonfields.Example Caddyfile