Skip to content

Commit

Permalink
chore: Ignore stdout/stderr sync errors in audit log (#1515)
Browse files Browse the repository at this point in the history
Calling `Sync` on stdout/stderr on Linux raises an error that can be
ignored. See uber-go/zap#328.

Fixes #1514

Signed-off-by: Charith Ellawala <charith@cerbos.dev>
  • Loading branch information
charithe committed Apr 4, 2023
1 parent a5c6b10 commit 80072f5
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions internal/audit/file/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ func init() {
}

type Log struct {
accessLog *zap.Logger
decisionLog *zap.Logger
decisionFilter audit.DecisionLogEntryFilter
accessLog *zap.Logger
decisionLog *zap.Logger
decisionFilter audit.DecisionLogEntryFilter
ignoreSyncErrors bool
}

func NewLog(conf *Conf, decisionFilter audit.DecisionLogEntryFilter) (*Log, error) {
Expand All @@ -58,9 +59,10 @@ func NewLog(conf *Conf, decisionFilter audit.DecisionLogEntryFilter) (*Log, erro
}

return &Log{
accessLog: logger.Named("cerbos.audit").With(zap.String("log.kind", "access")),
decisionLog: logger.Named("cerbos.audit").With(zap.String("log.kind", "decision")),
decisionFilter: decisionFilter,
accessLog: logger.Named("cerbos.audit").With(zap.String("log.kind", "access")),
decisionLog: logger.Named("cerbos.audit").With(zap.String("log.kind", "decision")),
decisionFilter: decisionFilter,
ignoreSyncErrors: (conf.Path == "stdout") || (conf.Path == "stderr"),
}, nil
}

Expand Down Expand Up @@ -100,10 +102,15 @@ func (l *Log) WriteDecisionLogEntry(_ context.Context, record audit.DecisionLogE
}

func (l *Log) Close() error {
return multierr.Combine(
l.accessLog.Sync(),
l.decisionLog.Sync(),
)
err1 := l.accessLog.Sync()
err2 := l.decisionLog.Sync()

// See https://github.com/uber-go/zap/issues/328
if !l.ignoreSyncErrors {
return multierr.Combine(err1, err2)
}

return nil
}

type protoMsg struct {
Expand Down

0 comments on commit 80072f5

Please sign in to comment.