-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add the context on Envoy requests to the audit logs #256
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,105 @@ | ||
package wasmplugin | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
ctypes "github.com/corazawaf/coraza/v3/types" | ||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
) | ||
|
||
// Transaction context to store against a transaction ID | ||
type TxnContext struct { | ||
envoyRequestId string | ||
} | ||
|
||
// Logger that includes context from the request in the audit logs and owns the final formatting | ||
type ContextualAuditLogger struct { | ||
IncludeRequestContext bool | ||
txnContextMap map[string]*TxnContext | ||
lock sync.Mutex | ||
} | ||
|
||
// Get the global audit logger that can be used across all requests | ||
func NewAppAuditLogger(includeRequestContext bool) *ContextualAuditLogger { | ||
return &ContextualAuditLogger{ | ||
txnContextMap: make(map[string]*TxnContext), | ||
IncludeRequestContext: includeRequestContext, | ||
} | ||
} | ||
|
||
// Register a transaction with the logger | ||
func (cal *ContextualAuditLogger) Register(txnId string, ctx *TxnContext) { | ||
cal.lock.Lock() | ||
defer cal.lock.Unlock() | ||
|
||
cal.txnContextMap[txnId] = ctx | ||
} | ||
|
||
// Remove the transaction information from the context map | ||
func (cal *ContextualAuditLogger) Unregister(txnId string) { | ||
cal.lock.Lock() | ||
defer cal.lock.Unlock() | ||
|
||
delete(cal.txnContextMap, txnId) | ||
} | ||
|
||
// Emit log on the given rule and add the txn context if available | ||
func (cal *ContextualAuditLogger) AuditLog(rule ctypes.MatchedRule) { | ||
cal.lock.Lock() | ||
defer cal.lock.Unlock() | ||
|
||
txnId := rule.TransactionID() | ||
|
||
logPrefix := "" | ||
if ctx, ok := cal.txnContextMap[txnId]; ok { | ||
if cal.IncludeRequestContext { | ||
// If we have context, add it to the log | ||
logPrefix = fmt.Sprintf("[request-id %q] ", ctx.envoyRequestId) | ||
} | ||
} | ||
|
||
logError(rule, logPrefix) | ||
} | ||
|
||
func logError(error ctypes.MatchedRule, logPrefix string) { | ||
msg := logPrefix + error.ErrorLog() | ||
switch error.Rule().Severity() { | ||
case ctypes.RuleSeverityEmergency: | ||
proxywasm.LogCritical(msg) | ||
case ctypes.RuleSeverityAlert: | ||
proxywasm.LogCritical(msg) | ||
case ctypes.RuleSeverityCritical: | ||
proxywasm.LogCritical(msg) | ||
case ctypes.RuleSeverityError: | ||
proxywasm.LogError(msg) | ||
case ctypes.RuleSeverityWarning: | ||
proxywasm.LogWarn(msg) | ||
case ctypes.RuleSeverityNotice: | ||
proxywasm.LogInfo(msg) | ||
case ctypes.RuleSeverityInfo: | ||
proxywasm.LogInfo(msg) | ||
case ctypes.RuleSeverityDebug: | ||
proxywasm.LogDebug(msg) | ||
} | ||
} | ||
|
||
const ( | ||
// This is the standard Envoy header for request IDs | ||
envoyRequestIdHeader = "x-request-id" | ||
) | ||
|
||
// A convenience method to register the request information with the audit logger if available | ||
// on the request (else ignores). Must be called in the request context. | ||
func registerRequestContextWithLogger(auditLogger *ContextualAuditLogger, txnId string) { | ||
if id, err := proxywasm.GetHttpRequestHeader(envoyRequestIdHeader); err == nil { | ||
auditLogger.Register(txnId, &TxnContext{ | ||
envoyRequestId: id, | ||
}) | ||
} | ||
} | ||
|
||
// Remove context for the given transaction ID | ||
func removeRequestContextFromLogger(auditLogger *ContextualAuditLogger, txnId string) { | ||
auditLogger.Unregister(txnId) | ||
} |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a request header, shouldn't it be automatically outputted in the headers (B) part of audit logs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see in corazawaf/coraza#711 there is the issue that all headers are printed which may not be desired. That seems like a big issue with seclang syntax, I would first add a config to seclang to set an allow or deny list for logging headers as without it the B part seems implementation seems incomplete.
While generic selection of variables for logging may have a different use case, for headers it doesn't seem like it should be needed, we already have a part for it we should improve that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could be wrong but it seems like the WASM filter here can't really print all the parts and just a fixed set of params are printed. See this issue I opened: #255
Is this not correct? I didn't find anything in the code that might print anything but these:
https://github.com/corazawaf/coraza-proxy-wasm/blob/main/wasmplugin/plugin.go#L699
The ErrorLog() method is here:
https://github.com/corazawaf/coraza/blob/f68d1c208791d741581a7d413c32777fbd74c611/internal/corazarules/rule_match.go#L254
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I am fairly new to this so I could be wrong