VAULT-12144: add openapi responses for /sys/tools endpoints (#18626)

* add struct for /sys/tools/hash

Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>

* added responses for /sys/tools paths

Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>

* add changelog

* verify respose structure for hash

Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>

* verify respose structure for hash/random

Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>

* use newer testing funct

Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>

* use new test method

Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>

---------

Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com>
This commit is contained in:
Daniel Huckins 2023-03-24 19:11:39 -04:00 committed by GitHub
parent 27103f9908
commit e3d3d6e528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 6 deletions

3
changelog/18626.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
openapi: add openapi response definitions to /sys/tool endpoints
```

View File

@ -1542,8 +1542,21 @@ func (b *SystemBackend) toolsPaths() []*framework.Path {
}, },
}, },
Callbacks: map[logical.Operation]framework.OperationFunc{ Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: b.pathHashWrite, logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathHashWrite,
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: "OK",
Fields: map[string]*framework.FieldSchema{
"sum": {
Type: framework.TypeString,
Required: true,
},
},
}},
},
},
}, },
HelpSynopsis: strings.TrimSpace(sysHelp["hash"][0]), HelpSynopsis: strings.TrimSpace(sysHelp["hash"][0]),
@ -1577,8 +1590,21 @@ func (b *SystemBackend) toolsPaths() []*framework.Path {
}, },
}, },
Callbacks: map[logical.Operation]framework.OperationFunc{ Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: b.pathRandomWrite, logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRandomWrite,
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: "OK",
Fields: map[string]*framework.FieldSchema{
"random_bytes": {
Type: framework.TypeString,
Required: true,
},
},
}},
},
},
}, },
HelpSynopsis: strings.TrimSpace(sysHelp["random"][0]), HelpSynopsis: strings.TrimSpace(sysHelp["random"][0]),

View File

@ -3469,10 +3469,16 @@ func TestSystemBackend_ToolsHash(t *testing.T) {
req.Data = map[string]interface{}{ req.Data = map[string]interface{}{
"input": "dGhlIHF1aWNrIGJyb3duIGZveA==", "input": "dGhlIHF1aWNrIGJyb3duIGZveA==",
} }
_, err := b.HandleRequest(namespace.RootContext(nil), req) resp, err := b.HandleRequest(namespace.RootContext(nil), req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*SystemBackend).Route(req.Path), req.Operation),
resp,
true,
)
doRequest := func(req *logical.Request, errExpected bool, expected string) { doRequest := func(req *logical.Request, errExpected bool, expected string) {
t.Helper() t.Helper()
@ -3483,12 +3489,21 @@ func TestSystemBackend_ToolsHash(t *testing.T) {
if resp == nil { if resp == nil {
t.Fatal("expected non-nil response") t.Fatal("expected non-nil response")
} }
if errExpected { if errExpected {
if !resp.IsError() { if !resp.IsError() {
t.Fatalf("bad: got error response: %#v", *resp) t.Fatalf("bad: got error response: %#v", *resp)
} }
return return
} else {
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*SystemBackend).Route(req.Path), req.Operation),
resp,
true,
)
} }
if resp.IsError() { if resp.IsError() {
t.Fatalf("bad: got error response: %#v", *resp) t.Fatalf("bad: got error response: %#v", *resp)
} }
@ -3554,10 +3569,16 @@ func TestSystemBackend_ToolsRandom(t *testing.T) {
b := testSystemBackend(t) b := testSystemBackend(t)
req := logical.TestRequest(t, logical.UpdateOperation, "tools/random") req := logical.TestRequest(t, logical.UpdateOperation, "tools/random")
_, err := b.HandleRequest(namespace.RootContext(nil), req) resp, err := b.HandleRequest(namespace.RootContext(nil), req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*SystemBackend).Route(req.Path), req.Operation),
resp,
true,
)
doRequest := func(req *logical.Request, errExpected bool, format string, numBytes int) { doRequest := func(req *logical.Request, errExpected bool, format string, numBytes int) {
t.Helper() t.Helper()
@ -3574,7 +3595,15 @@ func TestSystemBackend_ToolsRandom(t *testing.T) {
t.Fatalf("bad: got error response: %#v", *resp) t.Fatalf("bad: got error response: %#v", *resp)
} }
return nil return nil
} else {
schema.ValidateResponse(
t,
schema.GetResponseSchema(t, b.(*SystemBackend).Route(req.Path), req.Operation),
resp,
true,
)
} }
if resp.IsError() { if resp.IsError() {
t.Fatalf("bad: got error response: %#v", *resp) t.Fatalf("bad: got error response: %#v", *resp)
} }