From 14ba3d90552a5fe8b4f93b67714dd934f4d7af72 Mon Sep 17 00:00:00 2001 From: Janitha Senani Karunarathna Date: Wed, 2 Dec 2026 22:09:57 +0530 Subject: [PATCH 1/2] build(platform): require core with ErrStepConflict Refs OpenNSW/nsw-srilanka#1158 --- platform/go.mod | 2 +- platform/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/platform/go.mod b/platform/go.mod index 4b710ee6..63a411f0 100644 --- a/platform/go.mod +++ b/platform/go.mod @@ -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 diff --git a/platform/go.sum b/platform/go.sum index 88d5c2fb..cc4072bd 100644 --- a/platform/go.sum +++ b/platform/go.sum @@ -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= From 14682e9908816309784ba41433477a3b0a538c68 Mon Sep 17 00:00:00 2001 From: Janitha Senani Karunarathna Date: Wed, 2 Dec 2026 22:09:58 +0530 Subject: [PATCH 2/2] fix(platform/tasks): answer 409 when a payment cancel finds the payment settled A cancel that reached a payment that had already settled returned a 500 because writeCompletion did not know the error. The payment plugin wraps payment.ErrPaymentSettled with orchestrator.ErrStepConflict, and writeCompletion answers any ErrStepConflict with 409 and the audit code step_conflict, so the task API needs no knowledge of any one plugin's errors. A cancel on a step with no checkout, or for a reference that is not its checkout, is a fault and stays an ordinary error. Refs OpenNSW/nsw-srilanka#1158 --- platform/tasks/http_handler.go | 5 +++++ platform/tasks/http_handler_test.go | 25 +++++++++++++++++++++++++ platform/tasktypes/payment.go | 10 ++++++++-- platform/tasktypes/payment_test.go | 9 ++++++--- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/platform/tasks/http_handler.go b/platform/tasks/http_handler.go index 33b22e52..4afb20ab 100644 --- a/platform/tasks/http_handler.go +++ b/platform/tasks/http_handler.go @@ -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" @@ -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) diff --git a/platform/tasks/http_handler_test.go b/platform/tasks/http_handler_test.go index 91d6b1d0..2d187234 100644 --- a/platform/tasks/http_handler_test.go +++ b/platform/tasks/http_handler_test.go @@ -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) diff --git a/platform/tasktypes/payment.go b/platform/tasktypes/payment.go index fb28dc03..bf267b5c 100644 --- a/platform/tasktypes/payment.go +++ b/platform/tasktypes/payment.go @@ -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 @@ -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) } diff --git a/platform/tasktypes/payment_test.go b/platform/tasktypes/payment_test.go index 20913ff0..1133cbdb 100644 --- a/platform/tasktypes/payment_test.go +++ b/platform/tasktypes/payment_test.go @@ -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") } @@ -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) }) @@ -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) }