Skip to content

fix: resolve concurrent map writes for audit cache #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions internal/audit/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ type Labels = map[string]any
type Agent interface {
Trace(context.Context, Audit)
Cache() AuditCache
SetGlobalLabels(labels Labels)
}

type AgentPool interface {
New() Agent
}

// noopAgent is a lazy agent that does nothing :(
type noopAgent struct{}
type noopAgentPool struct{}

func NewNoopAgent() Agent { return &noopAgent{} }
func (p *noopAgentPool) New() Agent { return &noopAgent{} }

func NewNoopAgentPool() AgentPool { return &noopAgentPool{} }

type noopAgent struct{}

func (a *noopAgent) Trace(context.Context, Audit) {}
func (a *noopAgent) Cache() AuditCache { return &SingleRecordCache{} }
func (a *noopAgent) SetGlobalLabels(labels Labels) {}
func (a *noopAgent) Trace(context.Context, Audit) {}
func (a *noopAgent) Cache() AuditCache { return &SingleRecordCache{} }
36 changes: 23 additions & 13 deletions internal/audit/agent_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,40 @@ import (
"github.com/rond-authz/rond/logging"
)

type logAgent struct {
l logging.Logger
cache AuditCache
globals map[string]any
type agentPool struct {
l logging.Logger
labels Labels
}

func NewLogAgent(l logging.Logger) Agent {
return &logAgent{
l: l,
func (p *agentPool) New() Agent {
agent := &logAgent{
l: p.l,
cache: &SingleRecordCache{},
}

if p.labels != nil {
agent.cache.Store(p.labels)
}

return agent
}
func (a *logAgent) SetGlobalLabels(labels Labels) {
a.globals = labels

func NewLogAgentPool(l logging.Logger, labels Labels) AgentPool {
return &agentPool{
l: l,
labels: labels,
}
}

type logAgent struct {
l logging.Logger
cache AuditCache
}

func (a *logAgent) Trace(_ context.Context, auditInput Audit) {
data := a.cache.Load()

auditData := auditInput.toPrint()
if a.globals != nil {
auditData.applyDataFromPolicy(a.globals)
}

if data != nil {
auditData.applyDataFromPolicy(data)
}
Expand Down
12 changes: 8 additions & 4 deletions internal/audit/agent_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"github.com/stretchr/testify/require"
)

func TestLogAgent(t *testing.T) {
func TestLogAgentPool(t *testing.T) {
l, hook := test.NewNullLogger()
agent := NewLogAgent(rondlogrus.NewLogger(l))
pool := NewLogAgentPool(rondlogrus.NewLogger(l), nil)

agent := pool.New()

agent.Cache().Store(Data{
"authorization.permission": "my-permission",
Expand Down Expand Up @@ -83,11 +85,13 @@ func TestLogAgent(t *testing.T) {

func TestLogAgentSetGlobalLabels(t *testing.T) {
l, hook := test.NewNullLogger()
agent := NewLogAgent(rondlogrus.NewLogger(l))
agent.SetGlobalLabels(Labels{
pool := NewLogAgentPool(rondlogrus.NewLogger(l), Labels{
AuditAdditionalDataRequestTargetServiceKey: "some-service",
"some-label": "label_val",
})

agent := pool.New()

agent.Cache().Store(Data{
"authorization.permission": "my-permission",
"authorization.binding": "my-binding",
Expand Down
9 changes: 5 additions & 4 deletions internal/audit/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"github.com/stretchr/testify/require"
)

func TestNoopAgentDoesnotBreakStuff(t *testing.T) {
a := NewNoopAgent()
require.NotNil(t, a)
func TestNoopAgentDoesNotBreakStuff(t *testing.T) {
p := NewNoopAgentPool()
require.NotNil(t, p)

a := p.New()

a.SetGlobalLabels(map[string]any{"a": "b"})
a.Trace(context.Background(), Audit{})

require.NotNil(t, a.Cache())
Expand Down
10 changes: 10 additions & 0 deletions internal/audit/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package audit

import "sync"

type Data map[string]any

type AuditCache interface {
Expand All @@ -22,10 +24,15 @@ type AuditCache interface {
}

type SingleRecordCache struct {
sync.RWMutex

data Data
}

func (c *SingleRecordCache) Store(d Data) {
c.Lock()
defer c.Unlock()

if c.data == nil {
c.data = make(Data)
}
Expand All @@ -36,5 +43,8 @@ func (c *SingleRecordCache) Store(d Data) {
}

func (c *SingleRecordCache) Load() Data {
c.RLock()
defer c.RUnlock()

return c.data
}
24 changes: 12 additions & 12 deletions logging/test/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type testLogger struct {
}

func (l *testLogger) setRecord(level string, msg any) {
l.mu.RLock()
defer l.mu.RUnlock()
l.mu.Lock()
defer l.mu.Unlock()

l.entry.records = append(l.entry.records, Record{
Fields: l.Fields,
Expand All @@ -52,8 +52,8 @@ func (l *testLogger) setRecord(level string, msg any) {
})

if originalLogger := l.entry.originalLogger; originalLogger != nil {
originalLogger.mu.RLock()
defer originalLogger.mu.RUnlock()
originalLogger.mu.Lock()
defer originalLogger.mu.Unlock()

originalLogger.entry.records = append(originalLogger.entry.records, Record{
Fields: l.Fields,
Expand Down Expand Up @@ -84,8 +84,8 @@ func (l *testLogger) Trace(msg any) {
}

func (l *testLogger) WithFields(fields map[string]any) logging.Logger {
l.mu.RLock()
defer l.mu.RUnlock()
l.mu.Lock()
defer l.mu.Unlock()

clonedFields := map[string]any{}
for k, v := range l.Fields {
Expand Down Expand Up @@ -115,8 +115,8 @@ func (l *testLogger) WithFields(fields map[string]any) logging.Logger {
}

func (l *testLogger) WithField(key string, value any) logging.Logger {
l.mu.RLock()
defer l.mu.RUnlock()
l.mu.Lock()
defer l.mu.Unlock()

clonedFields := map[string]any{}
for k, v := range l.Fields {
Expand Down Expand Up @@ -144,14 +144,14 @@ func (l *testLogger) WithField(key string, value any) logging.Logger {
}

func (e *entry) AllRecords() []Record {
e.mu.Lock()
defer e.mu.Unlock()
e.mu.RLock()
defer e.mu.RUnlock()
return e.records
}

func (l *testLogger) OriginalLogger() *entry {
l.mu.Lock()
defer l.mu.Unlock()
l.mu.RLock()
defer l.mu.RUnlock()
return l.entry
}

Expand Down
8 changes: 4 additions & 4 deletions sdk/audit_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"github.com/rond-authz/rond/logging"
)

func buildAuditAgent(options *Options, logger logging.Logger) audit.Agent {
func buildAuditAgent(options *Options, logger logging.Logger) audit.AgentPool {
if options == nil || options.EvaluatorOptions == nil {
return audit.NewNoopAgent()
return audit.NewNoopAgentPool()
}

if !options.EvaluatorOptions.EnableAuditTracing {
return audit.NewNoopAgent()
return audit.NewNoopAgentPool()
}
return audit.NewLogAgent(logger)
return audit.NewLogAgentPool(logger, options.AuditLabels)
}
64 changes: 37 additions & 27 deletions sdk/audit_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,42 @@ import (
)

func TestBuildAuditAgent(t *testing.T) {
res := buildAuditAgent(nil, nil)
require.NotNil(t, res)

res = buildAuditAgent(&Options{
EvaluatorOptions: nil,
}, nil)
require.NotNil(t, res)

res = buildAuditAgent(&Options{
EvaluatorOptions: &EvaluatorOptions{
EnableAuditTracing: false,
},
}, nil)
require.NotNil(t, res)

log, hook := test.NewNullLogger()
res = buildAuditAgent(&Options{
EvaluatorOptions: &EvaluatorOptions{
EnableAuditTracing: true,
},
}, rondlogrus.NewEntry(logrus.NewEntry(log)))
require.NotNil(t, res)

res.Cache().Store(audit.Data{"test": "123"})
res.Trace(context.Background(), audit.Audit{
AggregationID: "a-id",
t.Run("nil options", func(t *testing.T) {
res := buildAuditAgent(nil, nil)
require.NotNil(t, res)
})

t.Run("nil evaluator options", func(t *testing.T) {
res := buildAuditAgent(&Options{
EvaluatorOptions: nil,
}, nil)
require.NotNil(t, res)
})

t.Run("nil logger", func(t *testing.T) {
res := buildAuditAgent(&Options{
EvaluatorOptions: &EvaluatorOptions{
EnableAuditTracing: false,
},
}, nil)
require.NotNil(t, res)
})

t.Run("with setup trace", func(t *testing.T) {
log, hook := test.NewNullLogger()
res := buildAuditAgent(&Options{
EvaluatorOptions: &EvaluatorOptions{
EnableAuditTracing: true,
},
}, rondlogrus.NewEntry(logrus.NewEntry(log)))
require.NotNil(t, res)

agent := res.New()

agent.Cache().Store(audit.Data{"test": "123"})
agent.Trace(context.Background(), audit.Audit{
AggregationID: "a-id",
})
require.Len(t, hook.Entries, 1)
})
require.Len(t, hook.Entries, 1)
}
16 changes: 9 additions & 7 deletions sdk/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type evaluator struct {

evaluatorOptions *EvaluatorOptions
policyEvaluationOptions *core.PolicyEvaluationOptions
auditAgent audit.Agent
auditAgentPool audit.AgentPool
}

func (e evaluator) Config() core.RondConfig {
Expand Down Expand Up @@ -91,7 +91,8 @@ func (e evaluator) EvaluateRequestPolicy(ctx context.Context, rondInput core.Inp

opaEvaluatorOptions := e.evaluatorOptions.opaEvaluatorOptions(logger)

ctx = audit.WithAuditCache(ctx, e.auditAgent)
auditAgent := e.auditAgentPool.New()
ctx = audit.WithAuditCache(ctx, auditAgent)
evaluatorAllowPolicy, err := e.partialResultEvaluators.GetEvaluatorFromPolicy(ctx, rondConfig.RequestFlow.PolicyName, opaEvaluatorOptions)
if err != nil {
return PolicyResult{}, err
Expand All @@ -108,7 +109,7 @@ func (e evaluator) EvaluateRequestPolicy(ctx context.Context, rondInput core.Inp
"message": err.Error(),
}).Error("RBAC policy evaluation failed")

e.auditAgent.Trace(ctx, audit.Audit{
auditAgent.Trace(ctx, audit.Audit{
AggregationID: options.Audit.AggregationID,
Authorization: audit.AuthzInfo{
Allowed: false,
Expand Down Expand Up @@ -139,7 +140,7 @@ func (e evaluator) EvaluateRequestPolicy(ctx context.Context, rondInput core.Inp
}
}

e.auditAgent.Trace(ctx, audit.Audit{
auditAgent.Trace(ctx, audit.Audit{
AggregationID: options.Audit.AggregationID,
Authorization: audit.AuthzInfo{
Allowed: true,
Expand Down Expand Up @@ -178,15 +179,16 @@ func (e evaluator) EvaluateResponsePolicy(ctx context.Context, rondInput core.In

opaEvaluatorOptions := e.evaluatorOptions.opaEvaluatorOptions(logger)

ctx = audit.WithAuditCache(ctx, e.auditAgent)
auditAgent := e.auditAgentPool.New()
ctx = audit.WithAuditCache(ctx, auditAgent)
evaluator, err := e.partialResultEvaluators.GetEvaluatorFromPolicy(ctx, e.rondConfig.ResponseFlow.PolicyName, opaEvaluatorOptions)
if err != nil {
return nil, err
}

bodyToProxy, err := evaluator.Evaluate(logger, evalInput, e.policyEvaluationOptions)
if err != nil {
e.auditAgent.Trace(ctx, audit.Audit{
auditAgent.Trace(ctx, audit.Audit{
AggregationID: options.Audit.AggregationID,
Authorization: audit.AuthzInfo{
Allowed: false,
Expand All @@ -205,7 +207,7 @@ func (e evaluator) EvaluateResponsePolicy(ctx context.Context, rondInput core.In
return nil, err
}

e.auditAgent.Trace(ctx, audit.Audit{
auditAgent.Trace(ctx, audit.Audit{
AggregationID: options.Audit.AggregationID,
Authorization: audit.AuthzInfo{
Allowed: true,
Expand Down
Loading
Loading