Skip to content
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
2 changes: 1 addition & 1 deletion platform/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.26.4

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/OpenNSW/core v0.0.0-20261201081741-5471ef155aad
github.com/OpenNSW/core v0.0.0-20261202163750-7302004a6913
github.com/OpenNSW/core/authn v0.4.0
github.com/OpenNSW/core/authz v0.1.0
github.com/OpenNSW/core/htmlgen v0.0.0-20261006063237-d8b90361237e
Expand Down
4 changes: 2 additions & 2 deletions platform/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/OpenNSW/core v0.0.0-20261201081741-5471ef155aad h1:TZmU9HeJWyPU388pp6NjiiWMkV5EyTfH828uFZ7WdfE=
github.com/OpenNSW/core v0.0.0-20261201081741-5471ef155aad/go.mod h1:4h6sKjwyWrvy+ErAbt3+Mr4gWyrFenfbJKUYN3uqTwc=
github.com/OpenNSW/core v0.0.0-20261202163750-7302004a6913 h1:gwTVMHw1badMcNRKayAqwgCVtfUsnh4Wbq6cltYho8o=
github.com/OpenNSW/core v0.0.0-20261202163750-7302004a6913/go.mod h1:uDLVKWOhBImoxda8hz20PP5GDmxyvXJ9HnbyLHbLOWM=
github.com/OpenNSW/core/authn v0.4.0 h1:kZNUGp49aAZ1ui19soMKF2qGVOc2os1PyS6UuRpT7UY=
github.com/OpenNSW/core/authn v0.4.0/go.mod h1:9/yGWkx5t5u0+YBUH4FsaZO6wpq/5WK5tdyOD+yL2Ek=
github.com/OpenNSW/core/authz v0.1.0 h1:qWM+NObLNEaG6GZ2ZnKYmtkwrtlAmEzIljfpiQw2vbo=
Expand Down
5 changes: 5 additions & 0 deletions platform/tasks/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
errAuthenticationReq = "authentication required"
errForbiddenTaskAction = "you may not perform this action on this task"
errStaleStep = "this step is no longer active; refetch the task"
errStepConflict = "this step has changed since you loaded it; refetch the task"
errFileNotReference = "a file in the submission is not a file reference; upload it again"
errFileNotYours = "a file in the submission was issued to someone else"
errFileExpired = "a file in the submission has expired; refetch the task and submit again"
Expand Down Expand Up @@ -542,6 +543,10 @@ func (h *HTTPHandler) writeCompletion(w http.ResponseWriter, r *http.Request, er
slog.InfoContext(ctx, "tasks: stale step", append(logAttrs, "error", err)...)
finish(http.StatusConflict, true, "stale_step")
httputil.Error(w, r, http.StatusConflict, errStaleStep)
case errors.Is(err, orchestrator.ErrStepConflict):
slog.InfoContext(ctx, "tasks: step conflict", append(logAttrs, "error", err)...)
finish(http.StatusConflict, true, "step_conflict")
httputil.Error(w, r, http.StatusConflict, errStepConflict)
case errors.Is(err, taskauthzext.ErrUnauthenticated):
finish(http.StatusUnauthorized, true, "unauthenticated")
httputil.Error(w, r, http.StatusUnauthorized, errAuthenticationReq)
Expand Down
25 changes: 25 additions & 0 deletions platform/tasks/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,31 @@ func TestWriteCompletion_StepNotGuardedIsForbidden(t *testing.T) {
assert.Equal(t, "s1", recs[0].Metadata["stepId"])
}

// A plugin that refuses a call because the step moved on, such as a cancel for a payment that
// already settled, is a conflict and not a server fault.
func TestWriteCompletion_StepConflictIsAConflict(t *testing.T) {
tests := map[string]error{
"the sentinel itself": orchestrator.ErrStepConflict,
"wrapped as the manager wraps it": fmt.Errorf("resume step %q of task %s: %w", "s1", "t1", fmt.Errorf("%w: refused", orchestrator.ErrStepConflict)),
}
for name, err := range tests {
t.Run(name, func(t *testing.T) {
client, capture := audit.NewWithCapture()
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", nil)

(&HTTPHandler{Audit: client}).writeCompletion(recorder, req, err, nil, FileDiff{}, "taskId", "t1", "stepId", "s1")

assert.Equal(t, http.StatusConflict, recorder.Code)
assert.Contains(t, recorder.Body.String(), errStepConflict)
recs := capture.Records()
require.Len(t, recs, 1)
assert.Equal(t, audit.StatusFailure, recs[0].Status)
assert.Equal(t, "step_conflict", recs[0].Metadata["error"])
})
}
}

func TestWriteCompletion_InvalidPayloadIsABadRequest(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", nil)
Expand Down
10 changes: 8 additions & 2 deletions platform/tasktypes/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ const referenceLifetime = 365 * 24 * time.Hour
// and where Resume reads it back.
const referenceNumberKey = "reference_number"

// ErrNoCheckoutToCancel is returned, wrapped, by Resume for a cancel on a step that holds no
// checkout to close, so a host can answer it as a conflict and not as a server fault.
// ErrNoCheckoutToCancel is returned by Resume for a cancel on a step that holds no checkout to
// close. It is a fault, not a stale view: the step's template or its stored data is wrong, so a
// refetch would not repair it.
var ErrNoCheckoutToCancel = errors.New("payment: the step holds no checkout to cancel")

// NewPaymentPlugin builds the PAYMENT plugin, which opens checkouts through
Expand Down Expand Up @@ -213,6 +214,11 @@ func (p *PaymentPlugin) Resume(ctx pluginContext, _ json.RawMessage, payload map
ReferenceNumber: ref,
CallbackToken: token,
}); err != nil {
// A payment that settled before the cancel reached its checkout means the caller's view of
// the step is out of date. Any other failure is a fault and stays an ordinary error.
if errors.Is(err, payment.ErrPaymentSettled) {
err = fmt.Errorf("%w: %w", orchestrator.ErrStepConflict, err)
}
return nil, fmt.Errorf("payment: cancel checkout %s: %w", ref, err)
}

Expand Down
9 changes: 6 additions & 3 deletions platform/tasktypes/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ func TestPaymentPlugin_Resume_SettledCheckoutIsRefused(t *testing.T) {

got, err := p.Resume(resumeCtx(map[string]any{"reference_number": "REF-1"}), nil, cancelPayload())

require.ErrorIs(t, err, payment.ErrPaymentSettled, "the caller maps this to 409")
require.ErrorIs(t, err, payment.ErrPaymentSettled)
require.ErrorIs(t, err, orchestrator.ErrStepConflict, "the host maps this to 409")
assert.Nil(t, got, "a refused cancel completes nothing")
}

Expand All @@ -597,7 +598,8 @@ func TestPaymentPlugin_Resume_MissingReference(t *testing.T) {

got, err := p.Resume(ctx, nil, cancelPayload())

require.ErrorIs(t, err, ErrNoCheckoutToCancel, "the host maps this to a conflict")
require.ErrorIs(t, err, ErrNoCheckoutToCancel)
assert.NotErrorIs(t, err, orchestrator.ErrStepConflict, "a missing checkout is a fault, not a stale view")
assert.Nil(t, got)
assert.False(t, svc.cancelCalled)
})
Expand All @@ -610,7 +612,8 @@ func TestPaymentPlugin_Resume_UnknownCheckoutIsNotFound(t *testing.T) {

got, err := p.Resume(resumeCtx(map[string]any{"reference_number": "REF-1"}), nil, cancelPayload())

require.ErrorIs(t, err, payment.ErrTransactionNotFound, "the host maps this to a conflict")
require.ErrorIs(t, err, payment.ErrTransactionNotFound)
assert.NotErrorIs(t, err, orchestrator.ErrStepConflict, "an unknown checkout is a fault, not a stale view")
assert.Nil(t, got)
}

Expand Down
Loading