Skip to content

Commit 31ebdac

Browse files
committed
Small improvements.
1 parent 7855e39 commit 31ebdac

File tree

6 files changed

+146
-76
lines changed

6 files changed

+146
-76
lines changed

auditing/auditing-interceptor.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func UnaryServerInterceptor(a Auditing, logger *slog.Logger, shouldAudit func(fu
7171
auditReqContext.StatusCode = statusCodeFromGrpc(err)
7272

7373
if err != nil {
74-
auditReqContext.Error = err.Error()
74+
auditReqContext.Error = err
7575
err2 := a.Index(auditReqContext)
7676
if err2 != nil {
7777
logger.Error("unable to index", "error", err2)
@@ -129,7 +129,7 @@ func StreamServerInterceptor(a Auditing, logger *slog.Logger, shouldAudit func(f
129129
auditReqContext.StatusCode = statusCodeFromGrpc(err)
130130

131131
if err != nil {
132-
auditReqContext.Error = err.Error()
132+
auditReqContext.Error = err
133133
err2 := a.Index(auditReqContext)
134134
if err2 != nil {
135135
logger.Error("unable to index", "error", err2)
@@ -244,7 +244,7 @@ func (a auditingConnectInterceptor) WrapStreamingHandler(next connect.StreamingH
244244
auditReqContext.StatusCode = statusCodeFromGrpc(err)
245245

246246
if err != nil {
247-
auditReqContext.Error = err.Error()
247+
auditReqContext.Error = err
248248
err2 := a.auditing.Index(auditReqContext)
249249
if err2 != nil {
250250
a.logger.Error("unable to index", "error", err2)
@@ -311,7 +311,7 @@ func (i auditingConnectInterceptor) WrapUnary(next connect.UnaryFunc) connect.Un
311311
auditReqContext.StatusCode = statusCodeFromGrpc(err)
312312

313313
if err != nil {
314-
auditReqContext.Error = err.Error()
314+
auditReqContext.Error = err
315315
err2 := i.auditing.Index(auditReqContext)
316316
if err2 != nil {
317317
i.logger.Error("unable to index", "error", err2)
@@ -432,7 +432,7 @@ func HttpFilter(a Auditing, logger *slog.Logger) (restful.FilterFunction, error)
432432
err = json.Unmarshal(body, &auditReqContext.Body)
433433
if err != nil {
434434
auditReqContext.Body = strBody
435-
auditReqContext.Error = err.Error()
435+
auditReqContext.Error = err
436436
}
437437

438438
err = a.Index(auditReqContext)

auditing/auditing.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auditing
22

33
import (
4+
"context"
45
"log/slog"
56
"os"
67
"path/filepath"
@@ -49,39 +50,39 @@ const (
4950
const EntryFilterDefaultLimit int64 = 100
5051

5152
type Entry struct {
52-
Id string `db:"-"` // filled by the auditing driver
53+
Id string // filled by the auditing driver
5354

54-
Component string `db:"component"`
55-
RequestId string `db:"rqid" json:"rqid"`
56-
Type EntryType `db:"type"`
57-
Timestamp time.Time `db:"timestamp"`
55+
Component string
56+
RequestId string `json:"rqid"`
57+
Type EntryType
58+
Timestamp time.Time
5859

59-
User string `db:"userid"`
60-
Tenant string `db:"tenant"`
60+
User string
61+
Tenant string
6162

6263
// For `EntryDetailHTTP` the HTTP method get, post, put, delete, ...
6364
// For `EntryDetailGRPC` unary, stream
64-
Detail EntryDetail `db:"detail"`
65+
Detail EntryDetail
6566
// e.g. Request, Response, Error, Opened, Close
66-
Phase EntryPhase `db:"phase"`
67+
Phase EntryPhase
6768
// For `EntryDetailHTTP` /api/v1/...
6869
// For `EntryDetailGRPC` /api.v1/... (the method name)
69-
Path string `db:"path"`
70-
ForwardedFor string `db:"forwardedfor"`
71-
RemoteAddr string `db:"remoteaddr"`
70+
Path string
71+
ForwardedFor string
72+
RemoteAddr string
7273

73-
Body any `db:"body"` // JSON, string or numbers
74-
StatusCode int `db:"statuscode"` // for `EntryDetailHTTP` the HTTP status code, for EntryDetailGRPC` the grpc status code
74+
Body any // JSON, string or numbers
75+
StatusCode int // for `EntryDetailHTTP` the HTTP status code, for EntryDetailGRPC` the grpc status code
7576

7677
// Internal errors
77-
Error string `db:"error"`
78+
Error error
7879
}
7980

8081
func (e *Entry) prepareForNextPhase() {
8182
e.Id = ""
8283
e.Timestamp = time.Now()
8384
e.Body = nil
84-
e.Error = ""
85+
e.Error = nil
8586

8687
switch e.Phase {
8788
case EntryPhaseRequest:
@@ -133,7 +134,7 @@ type Auditing interface {
133134
// Searches for entries matching the given filter.
134135
// By default only recent entries will be returned.
135136
// The returned entries will be sorted by timestamp in descending order.
136-
Search(EntryFilter) ([]Entry, error)
137+
Search(context.Context, EntryFilter) ([]Entry, error)
137138
}
138139

139140
func defaultComponent() (string, error) {

auditing/meilisearch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (a *meiliAuditing) Index(entry Entry) error {
129129
return nil
130130
}
131131

132-
func (a *meiliAuditing) Search(filter EntryFilter) ([]Entry, error) {
132+
func (a *meiliAuditing) Search(_ context.Context, filter EntryFilter) ([]Entry, error) {
133133
predicates := make([]string, 0)
134134
if filter.Component != "" {
135135
predicates = append(predicates, fmt.Sprintf("component = %q", filter.Component))
@@ -274,7 +274,7 @@ func (a *meiliAuditing) encodeEntry(entry Entry) map[string]any {
274274
if entry.StatusCode != 0 {
275275
doc["status-code"] = entry.StatusCode
276276
}
277-
if entry.Error != "" {
277+
if entry.Error != nil {
278278
doc["error"] = entry.Error
279279
}
280280
if entry.Body != nil {
@@ -347,7 +347,7 @@ func (a *meiliAuditing) decodeEntry(doc map[string]any) Entry {
347347
entry.StatusCode = int(statusCode)
348348
}
349349
if err, ok := doc["error"].(string); ok {
350-
entry.Error = err
350+
entry.Error = errors.New(err)
351351
}
352352
if body, ok := doc["body"]; ok {
353353
entry.Body = body

auditing/meilisearch_integration_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func StartMeilisearch(t testing.TB) (container testcontainers.Container, c *conn
7070
}
7171

7272
func TestAuditing_Meilisearch(t *testing.T) {
73+
ctx := context.Background()
7374
container, c, err := StartMeilisearch(t)
7475
require.NoError(t, err)
7576
defer func() {
@@ -99,7 +100,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
99100
RemoteAddr: "10.0.0.0",
100101
Body: "This is the body of 00000000-0000-0000-0000-000000000000",
101102
StatusCode: 200,
102-
Error: "",
103+
Error: nil,
103104
},
104105
{
105106
Component: "auditing.test",
@@ -115,7 +116,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
115116
RemoteAddr: "10.0.0.1",
116117
Body: "This is the body of 00000000-0000-0000-0000-000000000001",
117118
StatusCode: 201,
118-
Error: "",
119+
Error: nil,
119120
},
120121
{
121122
Component: "auditing.test",
@@ -131,7 +132,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
131132
RemoteAddr: "10.0.0.2",
132133
Body: "This is the body of 00000000-0000-0000-0000-000000000002",
133134
StatusCode: 0,
134-
Error: "",
135+
Error: nil,
135136
},
136137
}
137138
}
@@ -143,7 +144,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
143144
{
144145
name: "no entries, no search results",
145146
t: func(t *testing.T, a Auditing) {
146-
entries, err := a.Search(EntryFilter{})
147+
entries, err := a.Search(ctx, EntryFilter{})
147148
require.NoError(t, err)
148149
assert.Empty(t, entries)
149150
},
@@ -158,7 +159,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
158159
err = a.Flush()
159160
require.NoError(t, err)
160161

161-
entries, err := a.Search(EntryFilter{
162+
entries, err := a.Search(ctx, EntryFilter{
162163
Body: "test",
163164
})
164165
require.NoError(t, err)
@@ -177,7 +178,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
177178
err = a.Flush()
178179
require.NoError(t, err)
179180

180-
entries, err := a.Search(EntryFilter{})
181+
entries, err := a.Search(ctx, EntryFilter{})
181182
require.NoError(t, err)
182183
assert.Len(t, entries, len(es))
183184

@@ -187,7 +188,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
187188
t.Errorf("diff (+got -want):\n %s", diff)
188189
}
189190

190-
entries, err = a.Search(EntryFilter{
191+
entries, err = a.Search(ctx, EntryFilter{
191192
Body: "This",
192193
})
193194
require.NoError(t, err)
@@ -206,7 +207,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
206207
err = a.Flush()
207208
require.NoError(t, err)
208209

209-
entries, err := a.Search(EntryFilter{
210+
entries, err := a.Search(ctx, EntryFilter{
210211
RequestId: es[0].RequestId,
211212
})
212213
require.NoError(t, err)
@@ -234,7 +235,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
234235
err = a.Flush()
235236
require.NoError(t, err)
236237

237-
entries, err := a.Search(EntryFilter{
238+
entries, err := a.Search(ctx, EntryFilter{
238239
Phase: EntryPhaseResponse,
239240
})
240241
require.NoError(t, err)
@@ -259,7 +260,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
259260
err = a.Flush()
260261
require.NoError(t, err)
261262

262-
entries, err := a.Search(EntryFilter{
263+
entries, err := a.Search(ctx, EntryFilter{
263264
// we want to run a phrase search as otherwise we return the other entries as well
264265
// https://www.meilisearch.com/docs/reference/api/search#phrase-search-2
265266
Body: fmt.Sprintf("%q", es[0].Body.(string)),

0 commit comments

Comments
 (0)