Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(enterprise): ensure audit log json fields are formatted correctly#9397

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
coadler merged 1 commit intomainfromcolin/audit-json
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletionsenterprise/audit/audittest/rand.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,12 +23,13 @@ func RandomLog() database.AuditLog {
IPNet: *inet,
Valid: true,
},
UserAgent: sql.NullString{String: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", Valid: true},
ResourceType: database.ResourceTypeOrganization,
ResourceID: uuid.New(),
ResourceTarget: "colin's organization",
Action: database.AuditActionDelete,
Diff: []byte("{}"),
StatusCode: http.StatusNoContent,
UserAgent: sql.NullString{String: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", Valid: true},
ResourceType: database.ResourceTypeOrganization,
ResourceID: uuid.New(),
ResourceTarget: "colin's organization",
Action: database.AuditActionDelete,
Diff: []byte("{}"),
StatusCode: http.StatusNoContent,
AdditionalFields: []byte("{}"),
}
}
23 changes: 19 additions & 4 deletionsenterprise/audit/backends/slog.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,10 @@ package backends

import (
"context"
"database/sql"

"github.com/fatih/structs"
"github.com/sqlc-dev/pqtype"

"cdr.dev/slog"
"github.com/coder/coder/v2/coderd/database"
Expand All@@ -15,24 +17,37 @@ type slogBackend struct {
}

func NewSlog(logger slog.Logger) audit.Backend {
return slogBackend{log: logger}
return&slogBackend{log: logger}
}

func (slogBackend) Decision() audit.FilterDecision {
func (*slogBackend) Decision() audit.FilterDecision {
return audit.FilterDecisionExport
}

func (b slogBackend) Export(ctx context.Context, alog database.AuditLog) error {
func (b*slogBackend) Export(ctx context.Context, alog database.AuditLog) error {
// We don't use structs.Map because we don't want to recursively convert
// fields into maps. When we keep the type information, slog can more
// pleasantly format the output. For example, the clean result of
// (*NullString).Value() may be printed instead of {String: "foo", Valid: true}.
sfs := structs.Fields(alog)
var fields []any
for _, sf := range sfs {
fields = append(fields,slog.F(sf.Name(), sf.Value()))
fields = append(fields,b.fieldToSlog(sf))
}

b.log.Info(ctx, "audit_log", fields...)
return nil
}

func (*slogBackend) fieldToSlog(field *structs.Field) slog.Field {
val := field.Value()

switch ty := field.Value().(type) {
case pqtype.Inet:
val = ty.IPNet.IP.String()
case sql.NullString:
val = ty.String
}

return slog.F(field.Name(), val)
}
59 changes: 59 additions & 0 deletionsenterprise/audit/backends/slog_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
package backends_test

import (
"bytes"
"context"
"database/sql"
"encoding/json"
"net"
"net/http"
"testing"
"time"

"github.com/fatih/structs"
"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogjson"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/enterprise/audit/audittest"
"github.com/coder/coder/v2/enterprise/audit/backends"
)
Expand All@@ -34,6 +45,54 @@ func TestSlogBackend(t *testing.T) {
require.Equal(t, sink.entries[0].Message, "audit_log")
require.Len(t, sink.entries[0].Fields, len(structs.Fields(alog)))
})

t.Run("FormatsCorrectly", func(t *testing.T) {
t.Parallel()

var (
ctx, cancel = context.WithCancel(context.Background())

buf = bytes.NewBuffer(nil)
logger = slog.Make(slogjson.Sink(buf))
backend = backends.NewSlog(logger)

_, inet, _ = net.ParseCIDR("127.0.0.1/32")
alog = database.AuditLog{
ID: uuid.UUID{1},
Time: time.Unix(1257894000, 0),
UserID: uuid.UUID{2},
OrganizationID: uuid.UUID{3},
Ip: pqtype.Inet{
IPNet: *inet,
Valid: true,
},
UserAgent: sql.NullString{String: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", Valid: true},
ResourceType: database.ResourceTypeOrganization,
ResourceID: uuid.UUID{4},
ResourceTarget: "colin's organization",
ResourceIcon: "photo.png",
Action: database.AuditActionDelete,
Diff: []byte(`{"1": 2}`),
StatusCode: http.StatusNoContent,
AdditionalFields: []byte(`{"name":"doug","species":"cat"}`),
RequestID: uuid.UUID{5},
}
)
defer cancel()

err := backend.Export(ctx, alog)
require.NoError(t, err)
logger.Sync()

s := struct {
Fields json.RawMessage `json:"fields"`
}{}
err = json.Unmarshal(buf.Bytes(), &s)
require.NoError(t, err)

expected := `{"ID":"01000000-0000-0000-0000-000000000000","Time":"2009-11-10T23:00:00Z","UserID":"02000000-0000-0000-0000-000000000000","OrganizationID":"03000000-0000-0000-0000-000000000000","Ip":"127.0.0.1","UserAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36","ResourceType":"organization","ResourceID":"04000000-0000-0000-0000-000000000000","ResourceTarget":"colin's organization","Action":"delete","Diff":{"1":2},"StatusCode":204,"AdditionalFields":{"name":"doug","species":"cat"},"RequestID":"05000000-0000-0000-0000-000000000000","ResourceIcon":"photo.png"}`
assert.Equal(t, expected, string(s.Fields))
})
}

type fakeSink struct {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp