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

Commit8443d3e

Browse files
authored
fix(enterprise): ensure audit log json fields are formatted correctly (#9397)
1 parenta572800 commit8443d3e

File tree

3 files changed

+86
-11
lines changed

3 files changed

+86
-11
lines changed

‎enterprise/audit/audittest/rand.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ func RandomLog() database.AuditLog {
2323
IPNet:*inet,
2424
Valid:true,
2525
},
26-
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},
27-
ResourceType:database.ResourceTypeOrganization,
28-
ResourceID:uuid.New(),
29-
ResourceTarget:"colin's organization",
30-
Action:database.AuditActionDelete,
31-
Diff: []byte("{}"),
32-
StatusCode:http.StatusNoContent,
26+
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},
27+
ResourceType:database.ResourceTypeOrganization,
28+
ResourceID:uuid.New(),
29+
ResourceTarget:"colin's organization",
30+
Action:database.AuditActionDelete,
31+
Diff: []byte("{}"),
32+
StatusCode:http.StatusNoContent,
33+
AdditionalFields: []byte("{}"),
3334
}
3435
}

‎enterprise/audit/backends/slog.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package backends
22

33
import (
44
"context"
5+
"database/sql"
56

67
"github.com/fatih/structs"
8+
"github.com/sqlc-dev/pqtype"
79

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

1719
funcNewSlog(logger slog.Logger) audit.Backend {
18-
returnslogBackend{log:logger}
20+
return&slogBackend{log:logger}
1921
}
2022

21-
func (slogBackend)Decision() audit.FilterDecision {
23+
func (*slogBackend)Decision() audit.FilterDecision {
2224
returnaudit.FilterDecisionExport
2325
}
2426

25-
func (bslogBackend)Export(ctx context.Context,alog database.AuditLog)error {
27+
func (b*slogBackend)Export(ctx context.Context,alog database.AuditLog)error {
2628
// We don't use structs.Map because we don't want to recursively convert
2729
// fields into maps. When we keep the type information, slog can more
2830
// pleasantly format the output. For example, the clean result of
2931
// (*NullString).Value() may be printed instead of {String: "foo", Valid: true}.
3032
sfs:=structs.Fields(alog)
3133
varfields []any
3234
for_,sf:=rangesfs {
33-
fields=append(fields,slog.F(sf.Name(),sf.Value()))
35+
fields=append(fields,b.fieldToSlog(sf))
3436
}
3537

3638
b.log.Info(ctx,"audit_log",fields...)
3739
returnnil
3840
}
41+
42+
func (*slogBackend)fieldToSlog(field*structs.Field) slog.Field {
43+
val:=field.Value()
44+
45+
switchty:=field.Value().(type) {
46+
case pqtype.Inet:
47+
val=ty.IPNet.IP.String()
48+
case sql.NullString:
49+
val=ty.String
50+
}
51+
52+
returnslog.F(field.Name(),val)
53+
}

‎enterprise/audit/backends/slog_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
package backends_test
22

33
import (
4+
"bytes"
45
"context"
6+
"database/sql"
7+
"encoding/json"
8+
"net"
9+
"net/http"
510
"testing"
11+
"time"
612

713
"github.com/fatih/structs"
14+
"github.com/google/uuid"
15+
"github.com/sqlc-dev/pqtype"
16+
"github.com/stretchr/testify/assert"
817
"github.com/stretchr/testify/require"
918

1019
"cdr.dev/slog"
20+
"cdr.dev/slog/sloggers/slogjson"
21+
"github.com/coder/coder/v2/coderd/database"
1122
"github.com/coder/coder/v2/enterprise/audit/audittest"
1223
"github.com/coder/coder/v2/enterprise/audit/backends"
1324
)
@@ -34,6 +45,54 @@ func TestSlogBackend(t *testing.T) {
3445
require.Equal(t,sink.entries[0].Message,"audit_log")
3546
require.Len(t,sink.entries[0].Fields,len(structs.Fields(alog)))
3647
})
48+
49+
t.Run("FormatsCorrectly",func(t*testing.T) {
50+
t.Parallel()
51+
52+
var (
53+
ctx,cancel=context.WithCancel(context.Background())
54+
55+
buf=bytes.NewBuffer(nil)
56+
logger=slog.Make(slogjson.Sink(buf))
57+
backend=backends.NewSlog(logger)
58+
59+
_,inet,_=net.ParseCIDR("127.0.0.1/32")
60+
alog= database.AuditLog{
61+
ID: uuid.UUID{1},
62+
Time:time.Unix(1257894000,0),
63+
UserID: uuid.UUID{2},
64+
OrganizationID: uuid.UUID{3},
65+
Ip: pqtype.Inet{
66+
IPNet:*inet,
67+
Valid:true,
68+
},
69+
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},
70+
ResourceType:database.ResourceTypeOrganization,
71+
ResourceID: uuid.UUID{4},
72+
ResourceTarget:"colin's organization",
73+
ResourceIcon:"photo.png",
74+
Action:database.AuditActionDelete,
75+
Diff: []byte(`{"1": 2}`),
76+
StatusCode:http.StatusNoContent,
77+
AdditionalFields: []byte(`{"name":"doug","species":"cat"}`),
78+
RequestID: uuid.UUID{5},
79+
}
80+
)
81+
defercancel()
82+
83+
err:=backend.Export(ctx,alog)
84+
require.NoError(t,err)
85+
logger.Sync()
86+
87+
s:=struct {
88+
Fields json.RawMessage`json:"fields"`
89+
}{}
90+
err=json.Unmarshal(buf.Bytes(),&s)
91+
require.NoError(t,err)
92+
93+
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"}`
94+
assert.Equal(t,expected,string(s.Fields))
95+
})
3796
}
3897

3998
typefakeSinkstruct {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp