From 505e3f9a8935de18dbafc7b160dbd1921c9b4146 Mon Sep 17 00:00:00 2001 From: Chris Capurso Date: Mon, 12 Jul 2021 13:39:28 -0400 Subject: [PATCH] [VAULT-2825] Fix erroneous 500 resp for field validation errors (#12042) * [VAULT-2825] Correctly respond with 400 rather than 500 for field validation errors * [VAULT-2825] Add changelog entry * [VAULT-2825] Simplify test assertion --- changelog/12042.txt | 3 +++ sdk/framework/backend.go | 2 +- sdk/framework/backend_test.go | 9 ++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 changelog/12042.txt diff --git a/changelog/12042.txt b/changelog/12042.txt new file mode 100644 index 000000000..2ec23c834 --- /dev/null +++ b/changelog/12042.txt @@ -0,0 +1,3 @@ +```release-note:changes +api: A request that fails field validation will now be responded to with a 400 rather than 500. +``` diff --git a/sdk/framework/backend.go b/sdk/framework/backend.go index 2687045ce..a7a7f9adb 100644 --- a/sdk/framework/backend.go +++ b/sdk/framework/backend.go @@ -267,7 +267,7 @@ func (b *Backend) HandleRequest(ctx context.Context, req *logical.Request) (*log if req.Operation != logical.HelpOperation { err := fd.Validate() if err != nil { - return nil, err + return logical.ErrorResponse(fmt.Sprintf("Field validation failed: %s", err.Error())), nil } } diff --git a/sdk/framework/backend_test.go b/sdk/framework/backend_test.go index 9abd49d63..3d215080e 100644 --- a/sdk/framework/backend_test.go +++ b/sdk/framework/backend_test.go @@ -240,14 +240,17 @@ func TestBackendHandleRequest_badwrite(t *testing.T) { }, } - _, err := b.HandleRequest(context.Background(), &logical.Request{ + resp, err := b.HandleRequest(context.Background(), &logical.Request{ Operation: logical.UpdateOperation, Path: "foo/bar", Data: map[string]interface{}{"value": "3false3"}, }) + if err != nil { + t.Fatalf("err: %s", err) + } - if err == nil { - t.Fatalf("should have thrown a conversion error") + if !strings.Contains(resp.Data["error"].(string), "Field validation failed") { + t.Fatalf("bad: %#v", resp) } }