Skip to content

Commit

Permalink
Small improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Aug 23, 2024
1 parent 7855e39 commit 5c3a30c
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 78 deletions.
10 changes: 5 additions & 5 deletions auditing/auditing-interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func UnaryServerInterceptor(a Auditing, logger *slog.Logger, shouldAudit func(fu
auditReqContext.StatusCode = statusCodeFromGrpc(err)

if err != nil {
auditReqContext.Error = err.Error()
auditReqContext.Error = err
err2 := a.Index(auditReqContext)
if err2 != nil {
logger.Error("unable to index", "error", err2)
Expand Down Expand Up @@ -129,7 +129,7 @@ func StreamServerInterceptor(a Auditing, logger *slog.Logger, shouldAudit func(f
auditReqContext.StatusCode = statusCodeFromGrpc(err)

if err != nil {
auditReqContext.Error = err.Error()
auditReqContext.Error = err
err2 := a.Index(auditReqContext)
if err2 != nil {
logger.Error("unable to index", "error", err2)
Expand Down Expand Up @@ -244,7 +244,7 @@ func (a auditingConnectInterceptor) WrapStreamingHandler(next connect.StreamingH
auditReqContext.StatusCode = statusCodeFromGrpc(err)

if err != nil {
auditReqContext.Error = err.Error()
auditReqContext.Error = err
err2 := a.auditing.Index(auditReqContext)
if err2 != nil {
a.logger.Error("unable to index", "error", err2)
Expand Down Expand Up @@ -311,7 +311,7 @@ func (i auditingConnectInterceptor) WrapUnary(next connect.UnaryFunc) connect.Un
auditReqContext.StatusCode = statusCodeFromGrpc(err)

if err != nil {
auditReqContext.Error = err.Error()
auditReqContext.Error = err
err2 := i.auditing.Index(auditReqContext)
if err2 != nil {
i.logger.Error("unable to index", "error", err2)
Expand Down Expand Up @@ -432,7 +432,7 @@ func HttpFilter(a Auditing, logger *slog.Logger) (restful.FilterFunction, error)
err = json.Unmarshal(body, &auditReqContext.Body)
if err != nil {
auditReqContext.Body = strBody
auditReqContext.Error = err.Error()
auditReqContext.Error = err
}

err = a.Index(auditReqContext)
Expand Down
36 changes: 18 additions & 18 deletions auditing/auditing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auditing

import (
"context"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,39 +50,38 @@ const (
const EntryFilterDefaultLimit int64 = 100

type Entry struct {
Id string `db:"-"` // filled by the auditing driver

Component string `db:"component"`
RequestId string `db:"rqid" json:"rqid"`
Type EntryType `db:"type"`
Timestamp time.Time `db:"timestamp"`
Id string // filled by the auditing driver
Component string
RequestId string `json:"rqid"`
Type EntryType
Timestamp time.Time

User string `db:"userid"`
Tenant string `db:"tenant"`
User string
Tenant string

// For `EntryDetailHTTP` the HTTP method get, post, put, delete, ...
// For `EntryDetailGRPC` unary, stream
Detail EntryDetail `db:"detail"`
Detail EntryDetail
// e.g. Request, Response, Error, Opened, Close
Phase EntryPhase `db:"phase"`
Phase EntryPhase
// For `EntryDetailHTTP` /api/v1/...
// For `EntryDetailGRPC` /api.v1/... (the method name)
Path string `db:"path"`
ForwardedFor string `db:"forwardedfor"`
RemoteAddr string `db:"remoteaddr"`
Path string
ForwardedFor string
RemoteAddr string

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

// Internal errors
Error string `db:"error"`
Error error
}

func (e *Entry) prepareForNextPhase() {
e.Id = ""
e.Timestamp = time.Now()
e.Body = nil
e.Error = ""
e.Error = nil

switch e.Phase {
case EntryPhaseRequest:
Expand Down Expand Up @@ -133,7 +133,7 @@ type Auditing interface {
// Searches for entries matching the given filter.
// By default only recent entries will be returned.
// The returned entries will be sorted by timestamp in descending order.
Search(EntryFilter) ([]Entry, error)
Search(context.Context, EntryFilter) ([]Entry, error)
}

func defaultComponent() (string, error) {
Expand Down
8 changes: 4 additions & 4 deletions auditing/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (a *meiliAuditing) Index(entry Entry) error {
return nil
}

func (a *meiliAuditing) Search(filter EntryFilter) ([]Entry, error) {
func (a *meiliAuditing) Search(_ context.Context, filter EntryFilter) ([]Entry, error) {
predicates := make([]string, 0)
if filter.Component != "" {
predicates = append(predicates, fmt.Sprintf("component = %q", filter.Component))
Expand Down Expand Up @@ -274,8 +274,8 @@ func (a *meiliAuditing) encodeEntry(entry Entry) map[string]any {
if entry.StatusCode != 0 {
doc["status-code"] = entry.StatusCode
}
if entry.Error != "" {
doc["error"] = entry.Error
if entry.Error != nil {
doc["error"] = entry.Error.Error()
}
if entry.Body != nil {
doc["body"] = entry.Body
Expand Down Expand Up @@ -347,7 +347,7 @@ func (a *meiliAuditing) decodeEntry(doc map[string]any) Entry {
entry.StatusCode = int(statusCode)
}
if err, ok := doc["error"].(string); ok {
entry.Error = err
entry.Error = errors.New(err)
}
if body, ok := doc["body"]; ok {
entry.Body = body
Expand Down
21 changes: 11 additions & 10 deletions auditing/meilisearch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func StartMeilisearch(t testing.TB) (container testcontainers.Container, c *conn
}

func TestAuditing_Meilisearch(t *testing.T) {
ctx := context.Background()
container, c, err := StartMeilisearch(t)
require.NoError(t, err)
defer func() {
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
RemoteAddr: "10.0.0.0",
Body: "This is the body of 00000000-0000-0000-0000-000000000000",
StatusCode: 200,
Error: "",
Error: nil,
},
{
Component: "auditing.test",
Expand All @@ -115,7 +116,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
RemoteAddr: "10.0.0.1",
Body: "This is the body of 00000000-0000-0000-0000-000000000001",
StatusCode: 201,
Error: "",
Error: nil,
},
{
Component: "auditing.test",
Expand All @@ -131,7 +132,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
RemoteAddr: "10.0.0.2",
Body: "This is the body of 00000000-0000-0000-0000-000000000002",
StatusCode: 0,
Error: "",
Error: nil,
},
}
}
Expand All @@ -143,7 +144,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
{
name: "no entries, no search results",
t: func(t *testing.T, a Auditing) {
entries, err := a.Search(EntryFilter{})
entries, err := a.Search(ctx, EntryFilter{})
require.NoError(t, err)
assert.Empty(t, entries)
},
Expand All @@ -158,7 +159,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
err = a.Flush()
require.NoError(t, err)

entries, err := a.Search(EntryFilter{
entries, err := a.Search(ctx, EntryFilter{
Body: "test",
})
require.NoError(t, err)
Expand All @@ -177,7 +178,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
err = a.Flush()
require.NoError(t, err)

entries, err := a.Search(EntryFilter{})
entries, err := a.Search(ctx, EntryFilter{})
require.NoError(t, err)
assert.Len(t, entries, len(es))

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

entries, err = a.Search(EntryFilter{
entries, err = a.Search(ctx, EntryFilter{
Body: "This",
})
require.NoError(t, err)
Expand All @@ -206,7 +207,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
err = a.Flush()
require.NoError(t, err)

entries, err := a.Search(EntryFilter{
entries, err := a.Search(ctx, EntryFilter{
RequestId: es[0].RequestId,
})
require.NoError(t, err)
Expand Down Expand Up @@ -234,7 +235,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
err = a.Flush()
require.NoError(t, err)

entries, err := a.Search(EntryFilter{
entries, err := a.Search(ctx, EntryFilter{
Phase: EntryPhaseResponse,
})
require.NoError(t, err)
Expand All @@ -259,7 +260,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
err = a.Flush()
require.NoError(t, err)

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

0 comments on commit 5c3a30c

Please sign in to comment.