diff --git a/envoyauth/response.go b/envoyauth/response.go index 3eb5b75c5..7ea46cc21 100644 --- a/envoyauth/response.go +++ b/envoyauth/response.go @@ -35,13 +35,20 @@ type StopFunc = func() type TransactionCloser func(ctx context.Context, err error) error // NewEvalResult creates a new EvalResult and a StopFunc that is used to stop the timer for metrics -func NewEvalResult() (*EvalResult, StopFunc, error) { +func NewEvalResult(opts ...func(*EvalResult)) (*EvalResult, StopFunc, error) { var err error - er := EvalResult{ + er := &EvalResult{ Metrics: metrics.New(), } - er.DecisionID, err = util.UUID4() + + for _, opt := range opts { + opt(er) + } + + if er.DecisionID == "" { + er.DecisionID, err = util.UUID4() + } if err != nil { return nil, nil, err @@ -53,7 +60,7 @@ func NewEvalResult() (*EvalResult, StopFunc, error) { _ = er.Metrics.Timer(metrics.ServerHandler).Stop() } - return &er, stop, nil + return er, stop, nil } // GetTxn creates a read transaction suitable for the configured EvalResult object diff --git a/envoyauth/response_test.go b/envoyauth/response_test.go index 1631aa1d8..38c1cbf64 100644 --- a/envoyauth/response_test.go +++ b/envoyauth/response_test.go @@ -396,3 +396,23 @@ func TestGetDynamicMetadataWithBooleanDecision(t *testing.T) { t.Fatalf("Expected no result but got %v", result) } } + +func TestNewEvalResultWithDecisionID(t *testing.T) { + type Opt func(*EvalResult) + + withDecisionID := func(decisionID string) Opt { + return func(result *EvalResult) { + result.DecisionID = decisionID + } + } + + expectedDecisionID := "some-decision-id" + + er, _, err := NewEvalResult(withDecisionID(expectedDecisionID)) + if err != nil { + t.Fatalf("NewEvalResult() error = %v, wantErr %v", err, false) + } + if er.DecisionID != expectedDecisionID { + t.Errorf("Expected DecisionID to be '%v', got '%v'", expectedDecisionID, er.DecisionID) + } +}