mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
VAULT-24798: audit - improve error messages (#26312)
* audit: remove 'op' from error messages and do some clean up * Allow early error checking to be concerned with vault/Core vs. audit
This commit is contained in:
@@ -5,6 +5,7 @@ package audit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/vault/internal/observability/event"
|
||||
@@ -48,12 +49,10 @@ type subtype string
|
||||
// for audit events. It will generate an ID if no ID is supplied. Supported
|
||||
// options: WithID, WithNow.
|
||||
func NewEvent(s subtype, opt ...Option) (*AuditEvent, error) {
|
||||
const op = "audit.NewEvent"
|
||||
|
||||
// Get the default options
|
||||
opts, err := getOpts(opt...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: error applying options: %w", op, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if opts.withID == "" {
|
||||
@@ -61,7 +60,7 @@ func NewEvent(s subtype, opt ...Option) (*AuditEvent, error) {
|
||||
|
||||
opts.withID, err = event.NewID(string(event.AuditType))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: error creating ID for event: %w", op, err)
|
||||
return nil, fmt.Errorf("error creating ID for event: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,34 +72,32 @@ func NewEvent(s subtype, opt ...Option) (*AuditEvent, error) {
|
||||
}
|
||||
|
||||
if err := audit.validate(); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", op, err)
|
||||
return nil, err
|
||||
}
|
||||
return audit, nil
|
||||
}
|
||||
|
||||
// validate attempts to ensure the audit event in its present state is valid.
|
||||
func (a *AuditEvent) validate() error {
|
||||
const op = "audit.(AuditEvent).validate"
|
||||
|
||||
if a == nil {
|
||||
return fmt.Errorf("%s: event is nil: %w", op, event.ErrInvalidParameter)
|
||||
return fmt.Errorf("event is nil: %w", ErrInvalidParameter)
|
||||
}
|
||||
|
||||
if a.ID == "" {
|
||||
return fmt.Errorf("%s: missing ID: %w", op, event.ErrInvalidParameter)
|
||||
return fmt.Errorf("missing ID: %w", ErrInvalidParameter)
|
||||
}
|
||||
|
||||
if a.Version != version {
|
||||
return fmt.Errorf("%s: event version unsupported: %w", op, event.ErrInvalidParameter)
|
||||
return fmt.Errorf("event version unsupported: %w", ErrInvalidParameter)
|
||||
}
|
||||
|
||||
if a.Timestamp.IsZero() {
|
||||
return fmt.Errorf("%s: event timestamp cannot be the zero time instant: %w", op, event.ErrInvalidParameter)
|
||||
return fmt.Errorf("event timestamp cannot be the zero time instant: %w", ErrInvalidParameter)
|
||||
}
|
||||
|
||||
err := a.Subtype.validate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", op, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -108,23 +105,21 @@ func (a *AuditEvent) validate() error {
|
||||
|
||||
// validate ensures that subtype is one of the set of allowed event subtypes.
|
||||
func (t subtype) validate() error {
|
||||
const op = "audit.(subtype).validate"
|
||||
switch t {
|
||||
case RequestType, ResponseType:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("%s: '%s' is not a valid event subtype: %w", op, t, event.ErrInvalidParameter)
|
||||
return fmt.Errorf("invalid event subtype %q: %w", t, ErrInvalidParameter)
|
||||
}
|
||||
}
|
||||
|
||||
// validate ensures that format is one of the set of allowed event formats.
|
||||
func (f format) validate() error {
|
||||
const op = "audit.(format).validate"
|
||||
switch f {
|
||||
case JSONFormat, JSONxFormat:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("%s: '%s' is not a valid format: %w", op, f, event.ErrInvalidParameter)
|
||||
return fmt.Errorf("invalid format %q: %w", f, ErrInvalidParameter)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,3 +158,10 @@ func (t subtype) String() string {
|
||||
func (a *AuditEvent) formattedTime() string {
|
||||
return a.Timestamp.UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
|
||||
// IsValidFormat provides a means to validate whether the supplied format is valid.
|
||||
// Examples of valid formats are JSON and JSONx.
|
||||
func IsValidFormat(v string) bool {
|
||||
err := format(strings.TrimSpace(strings.ToLower(v))).validate()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user