mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-30 02:02:43 +00:00 
			
		
		
		
	VAULT-12112: openapi response definitions: sys/audit (#18456)
* added audit-hash operations * more audit paths Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com> * added audit fields * add changelog file * dynamic fields should be nil Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com> * start to add test helper Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com> * add tests for /sys/audit openapi paths Signed-off-by: Daniel Huckins <dhuckins@users.noreply.github.com> Co-authored-by: Anton Averchenkov <anton.averchenkov@hashicorp.com>
This commit is contained in:
		
							
								
								
									
										3
									
								
								changelog/18456.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								changelog/18456.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | ```release-note:improvement | ||||||
|  | openapi: add openapi response defintions to /sys/audit endpoints | ||||||
|  | ``` | ||||||
| @@ -1,6 +1,7 @@ | |||||||
| package vault | package vault | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"net/http" | ||||||
| 	"strings" | 	"strings" | ||||||
|  |  | ||||||
| 	"github.com/hashicorp/vault/sdk/framework" | 	"github.com/hashicorp/vault/sdk/framework" | ||||||
| @@ -571,9 +572,21 @@ func (b *SystemBackend) auditPaths() []*framework.Path { | |||||||
| 					Type: framework.TypeString, | 					Type: framework.TypeString, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  | 			Operations: map[logical.Operation]framework.OperationHandler{ | ||||||
| 			Callbacks: map[logical.Operation]framework.OperationFunc{ | 				logical.UpdateOperation: &framework.PathOperation{ | ||||||
| 				logical.UpdateOperation: b.handleAuditHash, | 					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]), | 			HelpSynopsis:    strings.TrimSpace(sysHelp["audit-hash"][0]), | ||||||
| @@ -587,6 +600,13 @@ func (b *SystemBackend) auditPaths() []*framework.Path { | |||||||
| 				logical.ReadOperation: &framework.PathOperation{ | 				logical.ReadOperation: &framework.PathOperation{ | ||||||
| 					Callback: b.handleAuditTable, | 					Callback: b.handleAuditTable, | ||||||
| 					Summary:  "List the enabled audit devices.", | 					Summary:  "List the enabled audit devices.", | ||||||
|  | 					Responses: map[int][]framework.Response{ | ||||||
|  | 						http.StatusOK: {{ | ||||||
|  | 							// this response has dynamic keys | ||||||
|  | 							Description: "OK", | ||||||
|  | 							Fields:      nil, | ||||||
|  | 						}}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  |  | ||||||
| @@ -625,10 +645,20 @@ func (b *SystemBackend) auditPaths() []*framework.Path { | |||||||
| 				logical.UpdateOperation: &framework.PathOperation{ | 				logical.UpdateOperation: &framework.PathOperation{ | ||||||
| 					Callback: b.handleEnableAudit, | 					Callback: b.handleEnableAudit, | ||||||
| 					Summary:  "Enable a new audit device at the supplied path.", | 					Summary:  "Enable a new audit device at the supplied path.", | ||||||
|  | 					Responses: map[int][]framework.Response{ | ||||||
|  | 						http.StatusNoContent: {{ | ||||||
|  | 							Description: "OK", | ||||||
|  | 						}}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
| 				logical.DeleteOperation: &framework.PathOperation{ | 				logical.DeleteOperation: &framework.PathOperation{ | ||||||
| 					Callback: b.handleDisableAudit, | 					Callback: b.handleDisableAudit, | ||||||
| 					Summary:  "Disable the audit device at the given path.", | 					Summary:  "Disable the audit device at the given path.", | ||||||
|  | 					Responses: map[int][]framework.Response{ | ||||||
|  | 						http.StatusNoContent: {{ | ||||||
|  | 							Description: "OK", | ||||||
|  | 						}}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  |  | ||||||
| @@ -652,14 +682,31 @@ func (b *SystemBackend) auditPaths() []*framework.Path { | |||||||
| 				logical.UpdateOperation: &framework.PathOperation{ | 				logical.UpdateOperation: &framework.PathOperation{ | ||||||
| 					Callback: b.handleAuditedHeaderUpdate, | 					Callback: b.handleAuditedHeaderUpdate, | ||||||
| 					Summary:  "Enable auditing of a header.", | 					Summary:  "Enable auditing of a header.", | ||||||
|  | 					Responses: map[int][]framework.Response{ | ||||||
|  | 						http.StatusNoContent: {{ | ||||||
|  | 							Description: "OK", | ||||||
|  | 						}}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
| 				logical.DeleteOperation: &framework.PathOperation{ | 				logical.DeleteOperation: &framework.PathOperation{ | ||||||
| 					Callback: b.handleAuditedHeaderDelete, | 					Callback: b.handleAuditedHeaderDelete, | ||||||
| 					Summary:  "Disable auditing of the given request header.", | 					Summary:  "Disable auditing of the given request header.", | ||||||
|  | 					Responses: map[int][]framework.Response{ | ||||||
|  | 						http.StatusNoContent: {{ | ||||||
|  | 							Description: "OK", | ||||||
|  | 						}}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
| 				logical.ReadOperation: &framework.PathOperation{ | 				logical.ReadOperation: &framework.PathOperation{ | ||||||
| 					Callback: b.handleAuditedHeaderRead, | 					Callback: b.handleAuditedHeaderRead, | ||||||
| 					Summary:  "List the information for the given request header.", | 					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, | ||||||
|  | 						}}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  |  | ||||||
| @@ -674,6 +721,17 @@ func (b *SystemBackend) auditPaths() []*framework.Path { | |||||||
| 				logical.ReadOperation: &framework.PathOperation{ | 				logical.ReadOperation: &framework.PathOperation{ | ||||||
| 					Callback: b.handleAuditedHeadersRead, | 					Callback: b.handleAuditedHeadersRead, | ||||||
| 					Summary:  "List the request headers that are configured to be audited.", | 					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, | ||||||
|  | 								}, | ||||||
|  | 							}, | ||||||
|  | 						}}, | ||||||
|  | 					}, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  |  | ||||||
|   | |||||||
| @@ -33,6 +33,7 @@ import ( | |||||||
| 	"github.com/hashicorp/vault/sdk/helper/jsonutil" | 	"github.com/hashicorp/vault/sdk/helper/jsonutil" | ||||||
| 	"github.com/hashicorp/vault/sdk/helper/pluginutil" | 	"github.com/hashicorp/vault/sdk/helper/pluginutil" | ||||||
| 	"github.com/hashicorp/vault/sdk/helper/salt" | 	"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/sdk/logical" | ||||||
| 	"github.com/hashicorp/vault/version" | 	"github.com/hashicorp/vault/version" | ||||||
| 	"github.com/mitchellh/mapstructure" | 	"github.com/mitchellh/mapstructure" | ||||||
| @@ -2194,6 +2195,7 @@ func TestSystemBackend_enableAudit(t *testing.T) { | |||||||
|  |  | ||||||
| func TestSystemBackend_auditHash(t *testing.T) { | func TestSystemBackend_auditHash(t *testing.T) { | ||||||
| 	c, b, _ := testCoreSystemBackend(t) | 	c, b, _ := testCoreSystemBackend(t) | ||||||
|  | 	paths := b.(*SystemBackend).auditPaths() | ||||||
| 	c.auditBackends["noop"] = func(ctx context.Context, config *audit.BackendConfig) (audit.Backend, error) { | 	c.auditBackends["noop"] = func(ctx context.Context, config *audit.BackendConfig) (audit.Backend, error) { | ||||||
| 		view := &logical.InmemStorage{} | 		view := &logical.InmemStorage{} | ||||||
| 		view.Put(namespace.RootContext(nil), &logical.StorageEntry{ | 		view.Put(namespace.RootContext(nil), &logical.StorageEntry{ | ||||||
| @@ -2221,6 +2223,12 @@ func TestSystemBackend_auditHash(t *testing.T) { | |||||||
| 	if resp != nil { | 	if resp != nil { | ||||||
| 		t.Fatalf("bad: %v", resp) | 		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 = logical.TestRequest(t, logical.UpdateOperation, "audit-hash/foo") | ||||||
| 	req.Data["input"] = "bar" | 	req.Data["input"] = "bar" | ||||||
| @@ -2232,6 +2240,13 @@ func TestSystemBackend_auditHash(t *testing.T) { | |||||||
| 	if resp == nil || resp.Data == nil { | 	if resp == nil || resp.Data == nil { | ||||||
| 		t.Fatalf("response or its data was 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"] | 	hash, ok := resp.Data["hash"] | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		t.Fatalf("did not get hash back in response, response was %#v", resp.Data) | 		t.Fatalf("did not get hash back in response, response was %#v", resp.Data) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Daniel Huckins
					Daniel Huckins