Skip to content
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

Allow to set the decisionID from an external function #516

Merged
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
15 changes: 11 additions & 4 deletions envoyauth/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions envoyauth/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}