Skip to content

Commit

Permalink
Use flowId for decision ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushpalanka committed Feb 19, 2024
1 parent da3d600 commit d23a07f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
21 changes: 16 additions & 5 deletions filters/openpolicyagent/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ import (
)

func (opa *OpenPolicyAgentInstance) Eval(ctx context.Context, req *ext_authz_v3.CheckRequest) (*envoyauth.EvalResult, error) {
result, stopeval, err := envoyauth.NewEvalResult()
span := opentracing.SpanFromContext(ctx)
if span != nil {
span.SetTag("opa.decision_id", result.DecisionID)
}

decisionId, err := opa.idGenerator.Generate()

if err != nil {
opa.Logger().WithFields(map[string]interface{}{"err": err}).Error("Unable to generate decision ID.")
return nil, err
}

result, stopeval, err := envoyauth.NewEvalResult(WithDecisionID(decisionId))
span := opentracing.SpanFromContext(ctx)
if span != nil {
span.SetTag("opa.decision_id", result.DecisionID)
}

var input map[string]interface{}
defer func() {
stopeval()
Expand Down Expand Up @@ -71,3 +74,11 @@ func (opa *OpenPolicyAgentInstance) logDecision(ctx context.Context, input inter

return decisionlog.LogDecision(ctx, opa.manager, info, result, err)
}

type Opt func(*envoyauth.EvalResult)

func WithDecisionID(decisionID string) Opt {
return func(result *envoyauth.EvalResult) {
result.DecisionID = decisionID
}
}
7 changes: 7 additions & 0 deletions filters/openpolicyagent/openpolicyagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"github.com/zalando/skipper/filters/flowid"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -360,6 +361,8 @@ type OpenPolicyAgentInstance struct {

maxBodyBytes int64
bodyReadBufferSize int64

idGenerator flowid.Generator
}

func envVariablesMap() map[string]string {
Expand Down Expand Up @@ -395,6 +398,8 @@ func interpolateConfigTemplate(configTemplate []byte, bundleName string) ([]byte
// new returns a new OPA object.
func (registry *OpenPolicyAgentRegistry) new(store storage.Store, configBytes []byte, instanceConfig OpenPolicyAgentInstanceConfig, filterName string, bundleName string, maxBodyBytes int64, bodyReadBufferSize int64) (*OpenPolicyAgentInstance, error) {
id := uuid.New().String()
uniqueIDGenerator, _ := flowid.NewStandardGenerator(32)

opaConfig, err := config.ParseConfig(configBytes, id)
if err != nil {
return nil, err
Expand Down Expand Up @@ -428,6 +433,8 @@ func (registry *OpenPolicyAgentRegistry) new(store storage.Store, configBytes []

preparedQueryDoOnce: new(sync.Once),
interQueryBuiltinCache: iCache.NewInterQueryCache(manager.InterQueryBuiltinCacheConfig()),

idGenerator: uniqueIDGenerator,
}

manager.RegisterCompilerTrigger(opa.compilerUpdated)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,6 @@ require (
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/open-policy-agent/opa-envoy-plugin => /Users/pjayawardhan/opa-envoy-plugin //ToDO

go 1.21

0 comments on commit d23a07f

Please sign in to comment.