-
Notifications
You must be signed in to change notification settings - Fork 0
Add audit package and simplify rules to boolean logic #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4178ac
Add audit package for request access logging
f0ssel cdb7563
Remove Action type from rules package, simplify to boolean logic
f0ssel c5d726a
Refactor audit tests to use buffer-based logging and add constant for…
f0ssel 9d90af3
Refactor main.go into CLI package and extend audit test coverage
f0ssel 88cbda5
Restructure audit package and remove Reason field
f0ssel 7be8449
Remove TestHTTPRequestToAuditRequest_NilRequest test
f0ssel 4e2a4d3
Rename EvaluateWithRule to Evaluate and fix test compatibility
f0ssel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package audit | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
) | ||
|
||
// Request represents information about an HTTP request for auditing | ||
type Request struct { | ||
Method string | ||
URL string | ||
Allowed bool | ||
Rule string // The rule that matched (if any) | ||
Reason string // Reason for the action (e.g., "no matching allow rules") | ||
} | ||
|
||
// Auditor handles audit logging for HTTP requests | ||
type Auditor interface { | ||
// AuditRequest logs information about an HTTP request and the action taken | ||
AuditRequest(req *Request) | ||
f0ssel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
// LoggingAuditor implements Auditor by logging to slog | ||
type LoggingAuditor struct { | ||
logger *slog.Logger | ||
} | ||
|
||
// NewLoggingAuditor creates a new LoggingAuditor | ||
func NewLoggingAuditor(logger *slog.Logger) *LoggingAuditor { | ||
return &LoggingAuditor{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
// AuditRequest logs the request using structured logging | ||
func (a *LoggingAuditor) AuditRequest(req *Request) { | ||
if req.Allowed { | ||
a.logger.Info("ALLOW", | ||
"method", req.Method, | ||
"url", req.URL, | ||
"rule", req.Rule) | ||
} else { | ||
a.logger.Warn("DENY", | ||
"method", req.Method, | ||
"url", req.URL, | ||
"reason", req.Reason) | ||
} | ||
} | ||
|
||
// HTTPRequestToAuditRequest converts an http.Request to an audit.Request | ||
func HTTPRequestToAuditRequest(httpReq *http.Request) *Request { | ||
return &Request{ | ||
Method: httpReq.Method, | ||
URL: httpReq.URL.String(), | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package audit | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestLoggingAuditor(t *testing.T) { | ||
// Create a logger that discards output during tests | ||
logger := slog.New(slog.NewTextHandler(nil, &slog.HandlerOptions{ | ||
Level: slog.LevelError + 1, // Higher than any level to suppress all logs | ||
})) | ||
|
||
auditor := NewLoggingAuditor(logger) | ||
|
||
tests := []struct { | ||
name string | ||
request *Request | ||
}{ | ||
{ | ||
name: "allow request", | ||
request: &Request{ | ||
Method: "GET", | ||
URL: "https://github.com", | ||
Allowed: true, | ||
Rule: "allow github.com", | ||
}, | ||
}, | ||
{ | ||
name: "deny request", | ||
request: &Request{ | ||
Method: "POST", | ||
URL: "https://example.com", | ||
Allowed: false, | ||
Reason: "no matching allow rules", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Should not panic | ||
auditor.AuditRequest(tt.request) | ||
}) | ||
} | ||
} | ||
|
||
func TestHTTPRequestToAuditRequest(t *testing.T) { | ||
req, err := http.NewRequest("GET", "https://example.com/path?query=value", nil) | ||
if err != nil { | ||
t.Fatalf("failed to create HTTP request: %v", err) | ||
} | ||
|
||
auditReq := HTTPRequestToAuditRequest(req) | ||
|
||
if auditReq.Method != "GET" { | ||
t.Errorf("expected method GET, got %s", auditReq.Method) | ||
} | ||
|
||
expectedURL := "https://example.com/path?query=value" | ||
if auditReq.URL != expectedURL { | ||
t.Errorf("expected URL %s, got %s", expectedURL, auditReq.URL) | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.