mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 11:38:02 +00:00
Moved HeaderFormatter to an Option for NewEntryFormatter, updated tests (#22116)
This commit is contained in:
@@ -30,7 +30,7 @@ var (
|
|||||||
|
|
||||||
// NewEntryFormatter should be used to create an EntryFormatter.
|
// NewEntryFormatter should be used to create an EntryFormatter.
|
||||||
// Accepted options: WithPrefix.
|
// Accepted options: WithPrefix.
|
||||||
func NewEntryFormatter(config FormatterConfig, salter Salter, headersConfig HeaderFormatter, opt ...Option) (*EntryFormatter, error) {
|
func NewEntryFormatter(config FormatterConfig, salter Salter, opt ...Option) (*EntryFormatter, error) {
|
||||||
const op = "audit.NewEntryFormatter"
|
const op = "audit.NewEntryFormatter"
|
||||||
|
|
||||||
if salter == nil {
|
if salter == nil {
|
||||||
@@ -48,10 +48,10 @@ func NewEntryFormatter(config FormatterConfig, salter Salter, headersConfig Head
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &EntryFormatter{
|
return &EntryFormatter{
|
||||||
salter: salter,
|
salter: salter,
|
||||||
config: config,
|
config: config,
|
||||||
headersConfig: headersConfig,
|
headerFormatter: opts.withHeaderFormatter,
|
||||||
prefix: opts.withPrefix,
|
prefix: opts.withPrefix,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ func TestNewEntryFormatter(t *testing.T) {
|
|||||||
|
|
||||||
cfg, err := NewFormatterConfig(tc.Options...)
|
cfg, err := NewFormatterConfig(tc.Options...)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
f, err := NewEntryFormatter(cfg, ss, nil, tc.Options...)
|
f, err := NewEntryFormatter(cfg, ss, tc.Options...)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case tc.IsErrorExpected:
|
case tc.IsErrorExpected:
|
||||||
@@ -150,7 +150,7 @@ func TestEntryFormatter_Reopen(t *testing.T) {
|
|||||||
cfg, err := NewFormatterConfig()
|
cfg, err := NewFormatterConfig()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
f, err := NewEntryFormatter(cfg, ss)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, f)
|
require.NotNil(t, f)
|
||||||
require.NoError(t, f.Reopen())
|
require.NoError(t, f.Reopen())
|
||||||
@@ -162,7 +162,7 @@ func TestEntryFormatter_Type(t *testing.T) {
|
|||||||
cfg, err := NewFormatterConfig()
|
cfg, err := NewFormatterConfig()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
f, err := NewEntryFormatter(cfg, ss)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, f)
|
require.NotNil(t, f)
|
||||||
require.Equal(t, eventlogger.NodeTypeFormatter, f.Type())
|
require.Equal(t, eventlogger.NodeTypeFormatter, f.Type())
|
||||||
@@ -305,7 +305,7 @@ func TestEntryFormatter_Process(t *testing.T) {
|
|||||||
cfg, err := NewFormatterConfig(WithFormat(tc.RequiredFormat.String()))
|
cfg, err := NewFormatterConfig(WithFormat(tc.RequiredFormat.String()))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
f, err := NewEntryFormatter(cfg, ss)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, f)
|
require.NotNil(t, f)
|
||||||
|
|
||||||
@@ -372,7 +372,7 @@ func BenchmarkAuditFileSink_Process(b *testing.B) {
|
|||||||
cfg, err := NewFormatterConfig()
|
cfg, err := NewFormatterConfig()
|
||||||
require.NoError(b, err)
|
require.NoError(b, err)
|
||||||
ss := newStaticSalt(b)
|
ss := newStaticSalt(b)
|
||||||
formatter, err := NewEntryFormatter(cfg, ss, nil)
|
formatter, err := NewEntryFormatter(cfg, ss)
|
||||||
require.NoError(b, err)
|
require.NoError(b, err)
|
||||||
require.NotNil(b, formatter)
|
require.NotNil(b, formatter)
|
||||||
|
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ func NewTemporaryFormatter(requiredFormat, prefix string) (*EntryFormatterWriter
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
eventFormatter, err := NewEntryFormatter(cfg, &nonPersistentSalt{}, nil, WithPrefix(prefix))
|
eventFormatter, err := NewEntryFormatter(cfg, &nonPersistentSalt{}, WithPrefix(prefix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ func TestNewEntryFormatterWriter(t *testing.T) {
|
|||||||
|
|
||||||
var f Formatter
|
var f Formatter
|
||||||
if !tc.UseNilFormatter {
|
if !tc.UseNilFormatter {
|
||||||
tempFormatter, err := NewEntryFormatter(cfg, s, nil)
|
tempFormatter, err := NewEntryFormatter(cfg, s)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, tempFormatter)
|
require.NotNil(t, tempFormatter)
|
||||||
f = tempFormatter
|
f = tempFormatter
|
||||||
@@ -192,7 +192,7 @@ func TestEntryFormatter_FormatRequest(t *testing.T) {
|
|||||||
ss := newStaticSalt(t)
|
ss := newStaticSalt(t)
|
||||||
cfg, err := NewFormatterConfig()
|
cfg, err := NewFormatterConfig()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
f, err := NewEntryFormatter(cfg, ss)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
var ctx context.Context
|
var ctx context.Context
|
||||||
@@ -259,7 +259,7 @@ func TestEntryFormatter_FormatResponse(t *testing.T) {
|
|||||||
ss := newStaticSalt(t)
|
ss := newStaticSalt(t)
|
||||||
cfg, err := NewFormatterConfig()
|
cfg, err := NewFormatterConfig()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
f, err := NewEntryFormatter(cfg, ss)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
var ctx context.Context
|
var ctx context.Context
|
||||||
@@ -361,7 +361,7 @@ func TestElideListResponses(t *testing.T) {
|
|||||||
|
|
||||||
formatResponse := func(t *testing.T, config FormatterConfig, operation logical.Operation, inputData map[string]interface{},
|
formatResponse := func(t *testing.T, config FormatterConfig, operation logical.Operation, inputData map[string]interface{},
|
||||||
) {
|
) {
|
||||||
f, err := NewEntryFormatter(config, &tfw, nil)
|
f, err := NewEntryFormatter(config, &tfw)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
formatter, err := NewEntryFormatterWriter(config, f, &tfw)
|
formatter, err := NewEntryFormatterWriter(config, f, &tfw)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
@@ -148,3 +148,11 @@ func WithHMACAccessor(h bool) Option {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithHeaderFormatter provides an Option to supply a HeaderFormatter.
|
||||||
|
func WithHeaderFormatter(f HeaderFormatter) Option {
|
||||||
|
return func(o *options) error {
|
||||||
|
o.withHeaderFormatter = f
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,15 +50,16 @@ type Option func(*options) error
|
|||||||
|
|
||||||
// options are used to represent configuration for a audit related nodes.
|
// options are used to represent configuration for a audit related nodes.
|
||||||
type options struct {
|
type options struct {
|
||||||
withID string
|
withID string
|
||||||
withNow time.Time
|
withNow time.Time
|
||||||
withSubtype subtype
|
withSubtype subtype
|
||||||
withFormat format
|
withFormat format
|
||||||
withPrefix string
|
withPrefix string
|
||||||
withRaw bool
|
withRaw bool
|
||||||
withElision bool
|
withElision bool
|
||||||
withOmitTime bool
|
withOmitTime bool
|
||||||
withHMACAccessor bool
|
withHMACAccessor bool
|
||||||
|
withHeaderFormatter HeaderFormatter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Salter is an interface that provides a way to obtain a Salt for hashing.
|
// Salter is an interface that provides a way to obtain a Salt for hashing.
|
||||||
@@ -96,10 +97,10 @@ type HeaderFormatter interface {
|
|||||||
|
|
||||||
// EntryFormatter should be used to format audit requests and responses.
|
// EntryFormatter should be used to format audit requests and responses.
|
||||||
type EntryFormatter struct {
|
type EntryFormatter struct {
|
||||||
salter Salter
|
salter Salter
|
||||||
headersConfig HeaderFormatter
|
headerFormatter HeaderFormatter
|
||||||
config FormatterConfig
|
config FormatterConfig
|
||||||
prefix string
|
prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
// EntryFormatterWriter should be used to format and write out audit requests and responses.
|
// EntryFormatterWriter should be used to format and write out audit requests and responses.
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ func TestFormatJSON_formatRequest(t *testing.T) {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
cfg, err := NewFormatterConfig()
|
cfg, err := NewFormatterConfig()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
f, err := NewEntryFormatter(cfg, ss)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
formatter := EntryFormatterWriter{
|
formatter := EntryFormatterWriter{
|
||||||
Formatter: f,
|
Formatter: f,
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ func TestFormatJSONx_formatRequest(t *testing.T) {
|
|||||||
WithFormat(JSONxFormat.String()),
|
WithFormat(JSONxFormat.String()),
|
||||||
)
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
f, err := NewEntryFormatter(cfg, tempStaticSalt, nil)
|
f, err := NewEntryFormatter(cfg, tempStaticSalt)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
writer := &JSONxWriter{Prefix: tc.Prefix}
|
writer := &JSONxWriter{Prefix: tc.Prefix}
|
||||||
formatter, err := NewEntryFormatterWriter(cfg, f, writer)
|
formatter, err := NewEntryFormatterWriter(cfg, f, writer)
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ func Factory(ctx context.Context, conf *audit.BackendConfig, useEventLogger bool
|
|||||||
b.salt.Store((*salt.Salt)(nil))
|
b.salt.Store((*salt.Salt)(nil))
|
||||||
|
|
||||||
// Configure the formatter for either case.
|
// Configure the formatter for either case.
|
||||||
f, err := audit.NewEntryFormatter(b.formatConfig, b, headersConfig, audit.WithPrefix(conf.Config["prefix"]))
|
f, err := audit.NewEntryFormatter(b.formatConfig, b, audit.WithHeaderFormatter(headersConfig), audit.WithPrefix(conf.Config["prefix"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating formatter: %w", err)
|
return nil, fmt.Errorf("error creating formatter: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ func Factory(ctx context.Context, conf *audit.BackendConfig, useEventLogger bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Configure the formatter for either case.
|
// Configure the formatter for either case.
|
||||||
f, err := audit.NewEntryFormatter(b.formatConfig, b, headersConfig)
|
f, err := audit.NewEntryFormatter(b.formatConfig, b, audit.WithHeaderFormatter(headersConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating formatter: %w", err)
|
return nil, fmt.Errorf("error creating formatter: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ func Factory(ctx context.Context, conf *audit.BackendConfig, useEventLogger bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Configure the formatter for either case.
|
// Configure the formatter for either case.
|
||||||
f, err := audit.NewEntryFormatter(b.formatConfig, b, headersConfig, audit.WithPrefix(conf.Config["prefix"]))
|
f, err := audit.NewEntryFormatter(b.formatConfig, b, audit.WithHeaderFormatter(headersConfig), audit.WithPrefix(conf.Config["prefix"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating formatter: %w", err)
|
return nil, fmt.Errorf("error creating formatter: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ func NewNoopAudit(config map[string]string) (*NoopAudit, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := audit.NewEntryFormatter(cfg, n, nil)
|
f, err := audit.NewEntryFormatter(cfg, n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating formatter: %w", err)
|
return nil, fmt.Errorf("error creating formatter: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user