mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-03 03:58:01 +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.
|
||||
// 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"
|
||||
|
||||
if salter == nil {
|
||||
@@ -50,7 +50,7 @@ func NewEntryFormatter(config FormatterConfig, salter Salter, headersConfig Head
|
||||
return &EntryFormatter{
|
||||
salter: salter,
|
||||
config: config,
|
||||
headersConfig: headersConfig,
|
||||
headerFormatter: opts.withHeaderFormatter,
|
||||
prefix: opts.withPrefix,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ func TestNewEntryFormatter(t *testing.T) {
|
||||
|
||||
cfg, err := NewFormatterConfig(tc.Options...)
|
||||
require.NoError(t, err)
|
||||
f, err := NewEntryFormatter(cfg, ss, nil, tc.Options...)
|
||||
f, err := NewEntryFormatter(cfg, ss, tc.Options...)
|
||||
|
||||
switch {
|
||||
case tc.IsErrorExpected:
|
||||
@@ -150,7 +150,7 @@ func TestEntryFormatter_Reopen(t *testing.T) {
|
||||
cfg, err := NewFormatterConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
||||
f, err := NewEntryFormatter(cfg, ss)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, f)
|
||||
require.NoError(t, f.Reopen())
|
||||
@@ -162,7 +162,7 @@ func TestEntryFormatter_Type(t *testing.T) {
|
||||
cfg, err := NewFormatterConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
||||
f, err := NewEntryFormatter(cfg, ss)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, f)
|
||||
require.Equal(t, eventlogger.NodeTypeFormatter, f.Type())
|
||||
@@ -305,7 +305,7 @@ func TestEntryFormatter_Process(t *testing.T) {
|
||||
cfg, err := NewFormatterConfig(WithFormat(tc.RequiredFormat.String()))
|
||||
require.NoError(t, err)
|
||||
|
||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
||||
f, err := NewEntryFormatter(cfg, ss)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, f)
|
||||
|
||||
@@ -372,7 +372,7 @@ func BenchmarkAuditFileSink_Process(b *testing.B) {
|
||||
cfg, err := NewFormatterConfig()
|
||||
require.NoError(b, err)
|
||||
ss := newStaticSalt(b)
|
||||
formatter, err := NewEntryFormatter(cfg, ss, nil)
|
||||
formatter, err := NewEntryFormatter(cfg, ss)
|
||||
require.NoError(b, err)
|
||||
require.NotNil(b, formatter)
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ func NewTemporaryFormatter(requiredFormat, prefix string) (*EntryFormatterWriter
|
||||
return nil, err
|
||||
}
|
||||
|
||||
eventFormatter, err := NewEntryFormatter(cfg, &nonPersistentSalt{}, nil, WithPrefix(prefix))
|
||||
eventFormatter, err := NewEntryFormatter(cfg, &nonPersistentSalt{}, WithPrefix(prefix))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ func TestNewEntryFormatterWriter(t *testing.T) {
|
||||
|
||||
var f Formatter
|
||||
if !tc.UseNilFormatter {
|
||||
tempFormatter, err := NewEntryFormatter(cfg, s, nil)
|
||||
tempFormatter, err := NewEntryFormatter(cfg, s)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, tempFormatter)
|
||||
f = tempFormatter
|
||||
@@ -192,7 +192,7 @@ func TestEntryFormatter_FormatRequest(t *testing.T) {
|
||||
ss := newStaticSalt(t)
|
||||
cfg, err := NewFormatterConfig()
|
||||
require.NoError(t, err)
|
||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
||||
f, err := NewEntryFormatter(cfg, ss)
|
||||
require.NoError(t, err)
|
||||
|
||||
var ctx context.Context
|
||||
@@ -259,7 +259,7 @@ func TestEntryFormatter_FormatResponse(t *testing.T) {
|
||||
ss := newStaticSalt(t)
|
||||
cfg, err := NewFormatterConfig()
|
||||
require.NoError(t, err)
|
||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
||||
f, err := NewEntryFormatter(cfg, ss)
|
||||
require.NoError(t, err)
|
||||
|
||||
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{},
|
||||
) {
|
||||
f, err := NewEntryFormatter(config, &tfw, nil)
|
||||
f, err := NewEntryFormatter(config, &tfw)
|
||||
require.NoError(t, err)
|
||||
formatter, err := NewEntryFormatterWriter(config, f, &tfw)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -148,3 +148,11 @@ func WithHMACAccessor(h bool) Option {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ type options struct {
|
||||
withElision bool
|
||||
withOmitTime bool
|
||||
withHMACAccessor bool
|
||||
withHeaderFormatter HeaderFormatter
|
||||
}
|
||||
|
||||
// Salter is an interface that provides a way to obtain a Salt for hashing.
|
||||
@@ -97,7 +98,7 @@ type HeaderFormatter interface {
|
||||
// EntryFormatter should be used to format audit requests and responses.
|
||||
type EntryFormatter struct {
|
||||
salter Salter
|
||||
headersConfig HeaderFormatter
|
||||
headerFormatter HeaderFormatter
|
||||
config FormatterConfig
|
||||
prefix string
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func TestFormatJSON_formatRequest(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
cfg, err := NewFormatterConfig()
|
||||
require.NoError(t, err)
|
||||
f, err := NewEntryFormatter(cfg, ss, nil)
|
||||
f, err := NewEntryFormatter(cfg, ss)
|
||||
require.NoError(t, err)
|
||||
formatter := EntryFormatterWriter{
|
||||
Formatter: f,
|
||||
|
||||
@@ -119,7 +119,7 @@ func TestFormatJSONx_formatRequest(t *testing.T) {
|
||||
WithFormat(JSONxFormat.String()),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
f, err := NewEntryFormatter(cfg, tempStaticSalt, nil)
|
||||
f, err := NewEntryFormatter(cfg, tempStaticSalt)
|
||||
require.NoError(t, err)
|
||||
writer := &JSONxWriter{Prefix: tc.Prefix}
|
||||
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))
|
||||
|
||||
// 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 {
|
||||
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.
|
||||
f, err := audit.NewEntryFormatter(b.formatConfig, b, headersConfig)
|
||||
f, err := audit.NewEntryFormatter(b.formatConfig, b, audit.WithHeaderFormatter(headersConfig))
|
||||
if err != nil {
|
||||
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.
|
||||
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 {
|
||||
return nil, fmt.Errorf("error creating formatter: %w", err)
|
||||
}
|
||||
|
||||
@@ -252,7 +252,7 @@ func NewNoopAudit(config map[string]string) (*NoopAudit, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f, err := audit.NewEntryFormatter(cfg, n, nil)
|
||||
f, err := audit.NewEntryFormatter(cfg, n)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating formatter: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user