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-12112: openapi response definitions: sys/audit #18456

Merged
merged 8 commits into from
Jan 20, 2023
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
3 changes: 3 additions & 0 deletions changelog/18456.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
openapi: add openapi response defintions to /sys/audit endpoints
```
64 changes: 61 additions & 3 deletions vault/logical_system_paths.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vault

import (
"net/http"
"strings"

"github.com/hashicorp/vault/sdk/framework"
Expand Down Expand Up @@ -571,9 +572,21 @@ func (b *SystemBackend) auditPaths() []*framework.Path {
Type: framework.TypeString,
},
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.handleAuditHash,
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleAuditHash,
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: "OK",
Fields: map[string]*framework.FieldSchema{
"hash": {
Type: framework.TypeString,
Required: true,
},
},
}},
},
},
},

HelpSynopsis: strings.TrimSpace(sysHelp["audit-hash"][0]),
Expand All @@ -587,6 +600,13 @@ func (b *SystemBackend) auditPaths() []*framework.Path {
logical.ReadOperation: &framework.PathOperation{
Callback: b.handleAuditTable,
Summary: "List the enabled audit devices.",
Responses: map[int][]framework.Response{
http.StatusOK: {{
// this response has dynamic keys
Description: "OK",
Fields: nil,
}},
},
},
},

Expand Down Expand Up @@ -625,10 +645,20 @@ func (b *SystemBackend) auditPaths() []*framework.Path {
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleEnableAudit,
Summary: "Enable a new audit device at the supplied path.",
Responses: map[int][]framework.Response{
http.StatusNoContent: {{
Description: "OK",
}},
},
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.handleDisableAudit,
Summary: "Disable the audit device at the given path.",
Responses: map[int][]framework.Response{
http.StatusNoContent: {{
Description: "OK",
}},
},
},
},

Expand All @@ -652,14 +682,31 @@ func (b *SystemBackend) auditPaths() []*framework.Path {
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleAuditedHeaderUpdate,
Summary: "Enable auditing of a header.",
Responses: map[int][]framework.Response{
http.StatusNoContent: {{
Description: "OK",
}},
},
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.handleAuditedHeaderDelete,
Summary: "Disable auditing of the given request header.",
Responses: map[int][]framework.Response{
http.StatusNoContent: {{
Description: "OK",
}},
},
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.handleAuditedHeaderRead,
Summary: "List the information for the given request header.",
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: "OK",
// the response keys are dynamic
Fields: nil,
}},
},
},
},

Expand All @@ -674,6 +721,17 @@ func (b *SystemBackend) auditPaths() []*framework.Path {
logical.ReadOperation: &framework.PathOperation{
Callback: b.handleAuditedHeadersRead,
Summary: "List the request headers that are configured to be audited.",
Responses: map[int][]framework.Response{
http.StatusOK: {{
Description: "OK",
Fields: map[string]*framework.FieldSchema{
"headers": {
Type: framework.TypeMap,
Required: true,
},
},
}},
},
},
},

Expand Down
15 changes: 15 additions & 0 deletions vault/logical_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/hashicorp/vault/sdk/helper/jsonutil"
"github.com/hashicorp/vault/sdk/helper/pluginutil"
"github.com/hashicorp/vault/sdk/helper/salt"
"github.com/hashicorp/vault/sdk/helper/testhelpers/schema"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/version"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -2194,6 +2195,7 @@ func TestSystemBackend_enableAudit(t *testing.T) {

func TestSystemBackend_auditHash(t *testing.T) {
c, b, _ := testCoreSystemBackend(t)
paths := b.(*SystemBackend).auditPaths()
c.auditBackends["noop"] = func(ctx context.Context, config *audit.BackendConfig) (audit.Backend, error) {
view := &logical.InmemStorage{}
view.Put(namespace.RootContext(nil), &logical.StorageEntry{
Expand Down Expand Up @@ -2221,6 +2223,12 @@ func TestSystemBackend_auditHash(t *testing.T) {
if resp != nil {
t.Fatalf("bad: %v", resp)
}
schema.ValidateResponse(
t,
schema.FindResponseSchema(t, paths, 2, req.Operation),
resp,
true,
)

req = logical.TestRequest(t, logical.UpdateOperation, "audit-hash/foo")
req.Data["input"] = "bar"
Expand All @@ -2232,6 +2240,13 @@ func TestSystemBackend_auditHash(t *testing.T) {
if resp == nil || resp.Data == nil {
t.Fatalf("response or its data was nil")
}
schema.ValidateResponse(
t,
schema.FindResponseSchema(t, paths, 0, req.Operation),
resp,
true,
)

hash, ok := resp.Data["hash"]
if !ok {
t.Fatalf("did not get hash back in response, response was %#v", resp.Data)
Expand Down