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

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

Merged
merged 9 commits into from
Mar 24, 2023
3 changes: 3 additions & 0 deletions changelog/18626.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
openapi: add openapi response definitions to /sys/tool endpoints
```
34 changes: 30 additions & 4 deletions vault/logical_system_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,21 @@ func (b *SystemBackend) toolsPaths() []*framework.Path {
},
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathHashWrite,
Operations: map[logical.Operation]framework.OperationHandler{
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]),
Expand Down Expand Up @@ -995,8 +1008,21 @@ func (b *SystemBackend) toolsPaths() []*framework.Path {
},
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathRandomWrite,
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRandomWrite,
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: "OK",
Fields: map[string]*framework.FieldSchema{
"random_bytes": {
dhuckins marked this conversation as resolved.
Show resolved Hide resolved
Type: framework.TypeString,
Required: true,
},
},
}},
},
},
},

HelpSynopsis: strings.TrimSpace(sysHelp["random"][0]),
Expand Down
35 changes: 33 additions & 2 deletions vault/logical_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3203,14 +3203,21 @@ func TestSystemBackend_PluginCatalog_CannotRegisterBuiltinPlugins(t *testing.T)

func TestSystemBackend_ToolsHash(t *testing.T) {
b := testSystemBackend(t)
paths := b.(*SystemBackend).toolsPaths()
req := logical.TestRequest(t, logical.UpdateOperation, "tools/hash")
req.Data = map[string]interface{}{
"input": "dGhlIHF1aWNrIGJyb3duIGZveA==",
}
_, err := b.HandleRequest(namespace.RootContext(nil), req)
resp, err := b.HandleRequest(namespace.RootContext(nil), req)
if err != nil {
t.Fatalf("err: %v", err)
}
schema.ValidateResponse(
t,
schema.FindResponseSchema(t, paths, 0, req.Operation),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot to update this one

resp,
true,
)

doRequest := func(req *logical.Request, errExpected bool, expected string) {
t.Helper()
Expand All @@ -3221,12 +3228,21 @@ func TestSystemBackend_ToolsHash(t *testing.T) {
if resp == nil {
t.Fatal("expected non-nil response")
}

if errExpected {
if !resp.IsError() {
t.Fatalf("bad: got error response: %#v", *resp)
}
return
} else {
schema.ValidateResponse(
t,
schema.FindResponseSchema(t, paths, 0, req.Operation),
resp,
true,
)
}

if resp.IsError() {
t.Fatalf("bad: got error response: %#v", *resp)
}
Expand Down Expand Up @@ -3290,12 +3306,19 @@ func TestSystemBackend_ToolsHash(t *testing.T) {

func TestSystemBackend_ToolsRandom(t *testing.T) {
b := testSystemBackend(t)
paths := b.(*SystemBackend).toolsPaths()
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 {
t.Fatalf("err: %v", err)
}
schema.ValidateResponse(
t,
schema.FindResponseSchema(t, paths, 1, req.Operation),
dhuckins marked this conversation as resolved.
Show resolved Hide resolved
resp,
true,
)

doRequest := func(req *logical.Request, errExpected bool, format string, numBytes int) {
t.Helper()
Expand All @@ -3312,7 +3335,15 @@ func TestSystemBackend_ToolsRandom(t *testing.T) {
t.Fatalf("bad: got error response: %#v", *resp)
}
return nil
} else {
schema.ValidateResponse(
t,
schema.FindResponseSchema(t, paths, 1, req.Operation),
resp,
true,
)
}

if resp.IsError() {
t.Fatalf("bad: got error response: %#v", *resp)
}
Expand Down