Skip to content
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

implement https mime #850

Merged
merged 18 commits into from
Aug 6, 2023
Merged

implement https mime #850

merged 18 commits into from
Aug 6, 2023

Conversation

jptosso
Copy link
Member

@jptosso jptosso commented Jul 25, 2023

This PR implements MIME for audit log formatters by transforming the function into an interface implementation, going from:

type AuditLogFormatter func (al plugintypes.AuditLog) ([]byte, error)

into

type AuditLogFormatter interface {
func Format(plugintypes.AuditLog) ([]byte, error)
func MIME() string
}

This way, the HTTPS audit log writer is aware of the mime type of the formatter.

@jptosso jptosso requested a review from a team as a code owner July 25, 2023 13:49
@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Patch coverage: 88.46% and project coverage change: -0.01% ⚠️

Comparison is base (cecd79c) 81.59% compared to head (f147d80) 81.58%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #850      +/-   ##
==========================================
- Coverage   81.59%   81.58%   -0.01%     
==========================================
  Files         159      159              
  Lines        9013     9021       +8     
==========================================
+ Hits         7354     7360       +6     
- Misses       1412     1414       +2     
  Partials      247      247              
Flag Coverage Δ
default 76.66% <100.00%> (+0.01%) ⬆️
examples 25.51% <27.77%> (-0.02%) ⬇️
ftw 46.82% <27.77%> (-0.04%) ⬇️
ftw-multiphase 48.94% <27.77%> (-0.04%) ⬇️
tinygo 74.83% <57.14%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
internal/auditlog/noop_formater.go 0.00% <0.00%> (ø)
experimental/plugins/auditlog.go 100.00% <100.00%> (ø)
internal/auditlog/concurrent_writer.go 79.31% <100.00%> (ø)
internal/auditlog/formats.go 100.00% <100.00%> (ø)
internal/auditlog/formats_json.go 86.76% <100.00%> (+0.82%) ⬆️
internal/auditlog/https_writer.go 47.36% <100.00%> (ø)
internal/auditlog/init.go 76.92% <100.00%> (ø)
internal/auditlog/init_tinygo.go 53.84% <100.00%> (ø)
internal/auditlog/logger.go 100.00% <100.00%> (ø)
internal/auditlog/serial_writer.go 90.62% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

if len(h.contentType) == 0 {
// we only do this once
// formatter is immutable in runtime
h.contentType = "application/octet-stream"
Copy link
Member

Choose a reason for hiding this comment

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

Curious about this and not just text/plain

// 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")
}
if ct := r.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/octet-stream") {
Copy link
Member

Choose a reason for hiding this comment

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

Is HasPrefix accurate? Can't we do direct comparison equality?

Copy link
Member Author

Choose a reason for hiding this comment

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

Im most cases yes, but at some point it would be right to use application/json;encoding=utf-8, like the automatic mime detection does

formatter plugintypes.AuditLogFormatter
url string
client *http.Client
contentType string
Copy link
Contributor

Choose a reason for hiding this comment

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

I think content type should be on the formatter, not https writer. Since it's still experimental API, we can change formatter, how about making it an actual type with Format() and ContentType() methods?

Copy link
Member

Choose a reason for hiding this comment

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

Brilliant. Let's do it.

@jcchavezs
Copy link
Member

Any movement on this @jptosso ?

@jptosso jptosso requested a review from anuraaga August 5, 2023 19:58
jptosso and others added 4 commits August 5, 2023 22:02
Co-authored-by: José Carlos Chávez <jcchavezs@gmail.com>
Co-authored-by: José Carlos Chávez <jcchavezs@gmail.com>
@jptosso jptosso requested a review from jcchavezs August 5, 2023 20:32
RegisterFormatter("json", noopFormater)
RegisterFormatter("jsonlegacy", noopFormater)
RegisterFormatter("native", nativeFormatter)
RegisterFormatter("json", &noopFormatter{})
Copy link
Member

Choose a reason for hiding this comment

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

Shall we turn this into a variable instead of instantiating this every time? cc @anuraaga

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is only called once think it's fine

@jcchavezs jcchavezs merged commit b3490b4 into main Aug 6, 2023
7 of 8 checks passed
@jcchavezs jcchavezs deleted the ct-audit branch August 6, 2023 05:10
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.

3 participants