Skip to content

Commit

Permalink
enhancement: Report audit close error (#1501)
Browse files Browse the repository at this point in the history
Report audit close error

Signed-off-by: Rob Crowe <nobby.crowe@gmail.com>
  • Loading branch information
rcrowe committed Mar 25, 2023
1 parent 06c514d commit eb0d3da
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
9 changes: 6 additions & 3 deletions internal/audit/file/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cerbos/cerbos/internal/audit"
"github.com/cerbos/cerbos/internal/config"
"go.elastic.co/ecszap"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -98,9 +99,11 @@ func (l *Log) WriteDecisionLogEntry(_ context.Context, record audit.DecisionLogE
return nil
}

func (l *Log) Close() {
_ = l.accessLog.Sync()
_ = l.decisionLog.Sync()
func (l *Log) Close() error {
return multierr.Combine(
l.accessLog.Sync(),
l.decisionLog.Sync(),
)
}

type protoMsg struct {
Expand Down
4 changes: 3 additions & 1 deletion internal/audit/file/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func TestLog(t *testing.T) {
log, err := file.NewLog(&file.Conf{Path: output}, decisionFilter)
require.NoError(t, err)

t.Cleanup(log.Close)
t.Cleanup(func() {
log.Close()
})

require.Equal(t, file.Backend, log.Backend())
require.True(t, log.Enabled())
Expand Down
6 changes: 4 additions & 2 deletions internal/audit/local/badgerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,14 @@ func (l *Log) getByID(ctx context.Context, prefix []byte, id audit.ID, c collect
c.done(err)
}

func (l *Log) Close() {
func (l *Log) Close() error {
var err error
l.stopOnce.Do(func() {
close(l.stopChan)
l.wg.Wait()
l.db.Close()
err = l.db.Close()
})
return err
}

func genKey(prefix []byte, id audit.IDBytes) []byte {
Expand Down
8 changes: 5 additions & 3 deletions internal/audit/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"

Expand All @@ -28,9 +29,9 @@ type Info interface {

type Log interface {
Info
io.Closer
WriteAccessLogEntry(context.Context, AccessLogEntryMaker) error
WriteDecisionLogEntry(context.Context, DecisionLogEntryMaker) error
Close()
}

type QueryableLog interface {
Expand Down Expand Up @@ -141,10 +142,11 @@ func (lw *logWrapper) WriteDecisionLogEntry(ctx context.Context, entry DecisionL
return lw.backend.WriteDecisionLogEntry(ctx, entry)
}

func (lw *logWrapper) Close() {
func (lw *logWrapper) Close() error {
if lw.backend != nil {
lw.backend.Close()
return lw.backend.Close()
}
return nil
}

type queryableLogWrapper struct {
Expand Down
4 changes: 3 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ func (s *Server) Start(ctx context.Context, param Param) error {
}

log.Debug("Shutting down the audit log")
param.AuditLog.Close()
if err := param.AuditLog.Close(); err != nil {
log.Error("Failed to cleanly close audit log", zap.Error(err))
}

log.Info("Shutdown complete")
return nil
Expand Down

0 comments on commit eb0d3da

Please sign in to comment.