-
-
Notifications
You must be signed in to change notification settings - Fork 231
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 https audit log support #826
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b06eefe
add https audit log support
jptosso 586ddf4
replace json test
jptosso 751dc54
consume body
jptosso 4d12607
Merge branch 'main' into implement-https
jptosso f25d241
Merge branch 'main' into implement-https
jptosso 836ee88
fix tests
jptosso 42017a4
fix tests
jptosso f1baecd
disable for tinygo
jptosso 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,71 @@ | ||
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build !tinygo | ||
// +build !tinygo | ||
|
||
package auditlog | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" | ||
) | ||
|
||
// httpsWriter is used to store logs in a single file | ||
type httpsWriter struct { | ||
io.Closer | ||
formatter plugintypes.AuditLogFormatter | ||
url string | ||
client *http.Client | ||
} | ||
|
||
func (h *httpsWriter) Init(c plugintypes.AuditLogConfig) error { | ||
h.formatter = c.Formatter | ||
h.url = c.Target | ||
// now we validate h.url is a valid url | ||
// Although the writer type is HTTPS, we allow HTTP as well | ||
_, err := url.Parse(h.url) | ||
if err != nil { | ||
return err | ||
} | ||
h.client = &http.Client{ | ||
Timeout: time.Duration(1 * time.Second), | ||
} | ||
return nil | ||
} | ||
|
||
func (h *httpsWriter) Write(al plugintypes.AuditLog) error { | ||
body, err := h.formatter(al) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodPost, h.url, bytes.NewReader(body)) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("User-Agent", "Coraza+v3") | ||
jptosso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO: declare content type in the formatter | ||
res, err := h.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
jptosso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if res.StatusCode >= 300 || res.StatusCode < 200 { | ||
return fmt.Errorf("unexpected status code %d", res.StatusCode) | ||
} | ||
if _, err := io.Copy(io.Discard, res.Body); err != nil { | ||
// the stream failed, but the log was received, we don't return error | ||
// we cannot generate a log using the current api | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
var _ plugintypes.AuditLogWriter = (*httpsWriter)(nil) |
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,70 @@ | ||
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build !tinygo | ||
// +build !tinygo | ||
|
||
package auditlog | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" | ||
"github.com/corazawaf/coraza/v3/types" | ||
) | ||
|
||
func TestHTTPSAuditLog(t *testing.T) { | ||
writer := &httpsWriter{} | ||
formatter := nativeFormatter | ||
pts, err := types.ParseAuditLogParts("ABCDEZ") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
al := &Log{ | ||
Parts_: pts, | ||
|
||
Transaction_: Transaction{ | ||
ID_: "test123", | ||
}, | ||
Messages_: []plugintypes.AuditLogMessage{ | ||
Message{ | ||
Data_: &MessageData{ | ||
ID_: 100, | ||
Raw_: "SecAction \"id:100\"", | ||
}, | ||
}, | ||
}, | ||
} | ||
// we create a test http server | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
if r.ContentLength == 0 { | ||
t.Fatal("ContentLength is 0") | ||
} | ||
// now we get the body | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(body) == 0 { | ||
t.Fatal("Body is empty") | ||
} | ||
if !bytes.Contains(body, []byte("test123")) { | ||
t.Fatal("Body does not match") | ||
} | ||
})) | ||
defer server.Close() | ||
if err := writer.Init(plugintypes.AuditLogConfig{ | ||
Target: server.URL, | ||
Formatter: formatter, | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := writer.Write(al); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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
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.
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.
Also include this please