Audit: small tidy up (#22232)

* options => opts

* Standardise receiver letter, specify interface
This commit is contained in:
Peter Wilson 2023-08-08 16:05:58 +01:00 committed by GitHub
parent ea1b8e95c6
commit cd02421c7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 95 additions and 87 deletions

View File

@ -50,16 +50,16 @@ func TestOptions_WithFormat(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithFormat(tc.Value) applyOption := WithFormat(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, tc.ExpectedErrorMessage) require.EqualError(t, err, tc.ExpectedErrorMessage)
default: default:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withFormat) require.Equal(t, tc.ExpectedValue, opts.withFormat)
} }
}) })
} }
@ -95,16 +95,16 @@ func TestOptions_WithSubtype(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithSubtype(tc.Value) applyOption := WithSubtype(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, tc.ExpectedErrorMessage) require.EqualError(t, err, tc.ExpectedErrorMessage)
default: default:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withSubtype) require.Equal(t, tc.ExpectedValue, opts.withSubtype)
} }
}) })
} }
@ -136,16 +136,16 @@ func TestOptions_WithNow(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithNow(tc.Value) applyOption := WithNow(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, tc.ExpectedErrorMessage) require.EqualError(t, err, tc.ExpectedErrorMessage)
default: default:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withNow) require.Equal(t, tc.ExpectedValue, opts.withNow)
} }
}) })
} }
@ -181,16 +181,16 @@ func TestOptions_WithID(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithID(tc.Value) applyOption := WithID(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, tc.ExpectedErrorMessage) require.EqualError(t, err, tc.ExpectedErrorMessage)
default: default:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withID) require.Equal(t, tc.ExpectedValue, opts.withID)
} }
}) })
} }
@ -226,16 +226,16 @@ func TestOptions_WithPrefix(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithPrefix(tc.Value) applyOption := WithPrefix(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, tc.ExpectedErrorMessage) require.EqualError(t, err, tc.ExpectedErrorMessage)
default: default:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withPrefix) require.Equal(t, tc.ExpectedValue, opts.withPrefix)
} }
}) })
} }
@ -262,11 +262,11 @@ func TestOptions_WithRaw(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithRaw(tc.Value) applyOption := WithRaw(tc.Value)
err := applyOption(options) err := applyOption(opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withRaw) require.Equal(t, tc.ExpectedValue, opts.withRaw)
}) })
} }
} }
@ -292,11 +292,11 @@ func TestOptions_WithElision(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithElision(tc.Value) applyOption := WithElision(tc.Value)
err := applyOption(options) err := applyOption(opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withElision) require.Equal(t, tc.ExpectedValue, opts.withElision)
}) })
} }
} }
@ -322,11 +322,11 @@ func TestOptions_WithHMACAccessor(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithHMACAccessor(tc.Value) applyOption := WithHMACAccessor(tc.Value)
err := applyOption(options) err := applyOption(opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withHMACAccessor) require.Equal(t, tc.ExpectedValue, opts.withHMACAccessor)
}) })
} }
} }
@ -352,11 +352,11 @@ func TestOptions_WithOmitTime(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithOmitTime(tc.Value) applyOption := WithOmitTime(tc.Value)
err := applyOption(options) err := applyOption(opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withOmitTime) require.Equal(t, tc.ExpectedValue, opts.withOmitTime)
}) })
} }
} }

View File

@ -18,13 +18,13 @@ const (
) )
// Validate ensures that EventType is one of the set of allowed event types. // Validate ensures that EventType is one of the set of allowed event types.
func (et EventType) Validate() error { func (t EventType) Validate() error {
const op = "event.(EventType).Validate" const op = "event.(EventType).Validate"
switch et { switch t {
case AuditType: case AuditType:
return nil return nil
default: default:
return fmt.Errorf("%s: '%s' is not a valid event type: %w", op, et, ErrInvalidParameter) return fmt.Errorf("%s: '%s' is not a valid event type: %w", op, t, ErrInvalidParameter)
} }
} }

View File

@ -82,16 +82,16 @@ func TestOptions_WithID(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithID(tc.Value) applyOption := WithID(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, tc.ExpectedErrorMessage) require.EqualError(t, err, tc.ExpectedErrorMessage)
default: default:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withID) require.Equal(t, tc.ExpectedValue, opts.withID)
} }
}) })
} }
@ -222,11 +222,11 @@ func TestOptions_WithFacility(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithFacility(tc.Value) applyOption := WithFacility(tc.Value)
err := applyOption(options) err := applyOption(opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withFacility) require.Equal(t, tc.ExpectedValue, opts.withFacility)
}) })
} }
} }
@ -260,11 +260,11 @@ func TestOptions_WithTag(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithTag(tc.Value) applyOption := WithTag(tc.Value)
err := applyOption(options) err := applyOption(opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withTag) require.Equal(t, tc.ExpectedValue, opts.withTag)
}) })
} }
} }
@ -298,11 +298,11 @@ func TestOptions_WithSocketType(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithSocketType(tc.Value) applyOption := WithSocketType(tc.Value)
err := applyOption(options) err := applyOption(opts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withSocketType) require.Equal(t, tc.ExpectedValue, opts.withSocketType)
}) })
} }
} }
@ -346,16 +346,16 @@ func TestOptions_WithMaxDuration(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithMaxDuration(tc.Value) applyOption := WithMaxDuration(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
require.EqualError(t, err, tc.ExpectedErrorMessage) require.EqualError(t, err, tc.ExpectedErrorMessage)
default: default:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, options.withMaxDuration) require.Equal(t, tc.ExpectedValue, opts.withMaxDuration)
} }
}) })
} }
@ -402,9 +402,9 @@ func TestOptions_WithFileMode(t *testing.T) {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
options := &options{} opts := &options{}
applyOption := WithFileMode(tc.Value) applyOption := WithFileMode(tc.Value)
err := applyOption(options) err := applyOption(opts)
switch { switch {
case tc.IsErrorExpected: case tc.IsErrorExpected:
require.Error(t, err) require.Error(t, err)
@ -414,10 +414,10 @@ func TestOptions_WithFileMode(t *testing.T) {
switch { switch {
case tc.IsNilExpected: case tc.IsNilExpected:
// Optional Option 'not supplied' (i.e. was whitespace/empty string) // Optional Option 'not supplied' (i.e. was whitespace/empty string)
require.Nil(t, options.withFileMode) require.Nil(t, opts.withFileMode)
default: default:
// Dereference the pointer, so we can examine the file mode. // Dereference the pointer, so we can examine the file mode.
require.Equal(t, tc.ExpectedValue, *options.withFileMode) require.Equal(t, tc.ExpectedValue, *opts.withFileMode)
} }
} }
}) })

View File

@ -22,6 +22,8 @@ const (
devnull = "/dev/null" devnull = "/dev/null"
) )
var _ eventlogger.Node = (*FileSink)(nil)
// FileSink is a sink node which handles writing events to file. // FileSink is a sink node which handles writing events to file.
type FileSink struct { type FileSink struct {
file *os.File file *os.File
@ -82,7 +84,7 @@ func NewFileSink(path string, format string, opt ...Option) (*FileSink, error) {
} }
// Process handles writing the event to the file sink. // Process handles writing the event to the file sink.
func (f *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlogger.Event, error) { func (s *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlogger.Event, error) {
const op = "event.(FileSink).Process" const op = "event.(FileSink).Process"
select { select {
@ -96,16 +98,16 @@ func (f *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlog
} }
// '/dev/null' path means we just do nothing and pretend we're done. // '/dev/null' path means we just do nothing and pretend we're done.
if f.path == devnull { if s.path == devnull {
return nil, nil return nil, nil
} }
formatted, found := e.Format(f.requiredFormat) formatted, found := e.Format(s.requiredFormat)
if !found { if !found {
return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, f.requiredFormat) return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, s.requiredFormat)
} }
err := f.log(formatted) err := s.log(formatted)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: error writing file for sink: %w", op, err) return nil, fmt.Errorf("%s: error writing file for sink: %w", op, err)
} }
@ -115,33 +117,33 @@ func (f *FileSink) Process(ctx context.Context, e *eventlogger.Event) (*eventlog
} }
// Reopen handles closing and reopening the file. // Reopen handles closing and reopening the file.
func (f *FileSink) Reopen() error { func (s *FileSink) Reopen() error {
const op = "event.(FileSink).Reopen" const op = "event.(FileSink).Reopen"
// '/dev/null' path means we just do nothing and pretend we're done. // '/dev/null' path means we just do nothing and pretend we're done.
if f.path == devnull { if s.path == devnull {
return nil return nil
} }
f.fileLock.Lock() s.fileLock.Lock()
defer f.fileLock.Unlock() defer s.fileLock.Unlock()
if f.file == nil { if s.file == nil {
return f.open() return s.open()
} }
err := f.file.Close() err := s.file.Close()
// Set to nil here so that even if we error out, on the next access open() will be tried. // Set to nil here so that even if we error out, on the next access open() will be tried.
f.file = nil s.file = nil
if err != nil { if err != nil {
return fmt.Errorf("%s: unable to close file for re-opening on sink: %w", op, err) return fmt.Errorf("%s: unable to close file for re-opening on sink: %w", op, err)
} }
return f.open() return s.open()
} }
// Type describes the type of this node (sink). // Type describes the type of this node (sink).
func (_ *FileSink) Type() eventlogger.NodeType { func (s *FileSink) Type() eventlogger.NodeType {
return eventlogger.NodeTypeSink return eventlogger.NodeTypeSink
} }
@ -149,32 +151,32 @@ func (_ *FileSink) Type() eventlogger.NodeType {
// if one is not already open. // if one is not already open.
// It doesn't have any locking and relies on calling functions of FileSink to // It doesn't have any locking and relies on calling functions of FileSink to
// handle this (e.g. log and Reopen methods). // handle this (e.g. log and Reopen methods).
func (f *FileSink) open() error { func (s *FileSink) open() error {
const op = "event.(FileSink).open" const op = "event.(FileSink).open"
if f.file != nil { if s.file != nil {
return nil return nil
} }
if err := os.MkdirAll(filepath.Dir(f.path), f.fileMode); err != nil { if err := os.MkdirAll(filepath.Dir(s.path), s.fileMode); err != nil {
return fmt.Errorf("%s: unable to create file %q: %w", op, f.path, err) return fmt.Errorf("%s: unable to create file %q: %w", op, s.path, err)
} }
var err error var err error
f.file, err = os.OpenFile(f.path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, f.fileMode) s.file, err = os.OpenFile(s.path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, s.fileMode)
if err != nil { if err != nil {
return fmt.Errorf("%s: unable to open file for sink: %w", op, err) return fmt.Errorf("%s: unable to open file for sink: %w", op, err)
} }
// Change the file mode in case the log file already existed. // Change the file mode in case the log file already existed.
// We special case '/dev/null' since we can't chmod it, and bypass if the mode is zero. // We special case '/dev/null' since we can't chmod it, and bypass if the mode is zero.
switch f.path { switch s.path {
case devnull: case devnull:
default: default:
if f.fileMode != 0 { if s.fileMode != 0 {
err = os.Chmod(f.path, f.fileMode) err = os.Chmod(s.path, s.fileMode)
if err != nil { if err != nil {
return fmt.Errorf("%s: unable to change file %q permissions '%v' for sink: %w", op, f.path, f.fileMode, err) return fmt.Errorf("%s: unable to change file %q permissions '%v' for sink: %w", op, s.path, s.fileMode, err)
} }
} }
} }
@ -184,31 +186,31 @@ func (f *FileSink) open() error {
// log writes the buffer to the file. // log writes the buffer to the file.
// It acquires a lock on the file to do this. // It acquires a lock on the file to do this.
func (f *FileSink) log(data []byte) error { func (s *FileSink) log(data []byte) error {
const op = "event.(FileSink).log" const op = "event.(FileSink).log"
f.fileLock.Lock() s.fileLock.Lock()
defer f.fileLock.Unlock() defer s.fileLock.Unlock()
reader := bytes.NewReader(data) reader := bytes.NewReader(data)
if err := f.open(); err != nil { if err := s.open(); err != nil {
return fmt.Errorf("%s: unable to open file for sink: %w", op, err) return fmt.Errorf("%s: unable to open file for sink: %w", op, err)
} }
if _, err := reader.WriteTo(f.file); err == nil { if _, err := reader.WriteTo(s.file); err == nil {
return nil return nil
} }
// Otherwise, opportunistically try to re-open the FD, once per call (1 retry attempt). // Otherwise, opportunistically try to re-open the FD, once per call (1 retry attempt).
err := f.file.Close() err := s.file.Close()
if err != nil { if err != nil {
return fmt.Errorf("%s: unable to close file for sink: %w", op, err) return fmt.Errorf("%s: unable to close file for sink: %w", op, err)
} }
f.file = nil s.file = nil
if err := f.open(); err != nil { if err := s.open(); err != nil {
return fmt.Errorf("%s: unable to re-open file for sink: %w", op, err) return fmt.Errorf("%s: unable to re-open file for sink: %w", op, err)
} }
@ -217,7 +219,7 @@ func (f *FileSink) log(data []byte) error {
return fmt.Errorf("%s: unable to seek to start of file for sink: %w", op, err) return fmt.Errorf("%s: unable to seek to start of file for sink: %w", op, err)
} }
_, err = reader.WriteTo(f.file) _, err = reader.WriteTo(s.file)
if err != nil { if err != nil {
return fmt.Errorf("%s: unable to re-write to file for sink: %w", op, err) return fmt.Errorf("%s: unable to re-write to file for sink: %w", op, err)
} }

View File

@ -9,6 +9,8 @@ import (
"github.com/hashicorp/eventlogger" "github.com/hashicorp/eventlogger"
) )
var _ eventlogger.Node = (*NoopSink)(nil)
// NoopSink is a sink node which handles ignores everything. // NoopSink is a sink node which handles ignores everything.
type NoopSink struct{} type NoopSink struct{}

View File

@ -15,6 +15,8 @@ import (
"github.com/hashicorp/eventlogger" "github.com/hashicorp/eventlogger"
) )
var _ eventlogger.Node = (*SocketSink)(nil)
// SocketSink is a sink node which handles writing events to socket. // SocketSink is a sink node which handles writing events to socket.
type SocketSink struct { type SocketSink struct {
requiredFormat string requiredFormat string

View File

@ -28,7 +28,7 @@ func NewStdoutSinkNode(format string) *StdoutSink {
} }
// Process persists the provided eventlogger.Event to the standard output stream. // Process persists the provided eventlogger.Event to the standard output stream.
func (n *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*eventlogger.Event, error) { func (s *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*eventlogger.Event, error) {
const op = "event.(StdoutSink).Process" const op = "event.(StdoutSink).Process"
select { select {
@ -41,9 +41,9 @@ func (n *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*ev
return nil, fmt.Errorf("%s: event is nil: %w", op, ErrInvalidParameter) return nil, fmt.Errorf("%s: event is nil: %w", op, ErrInvalidParameter)
} }
formattedBytes, found := event.Format(n.requiredFormat) formattedBytes, found := event.Format(s.requiredFormat)
if !found { if !found {
return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, n.requiredFormat) return nil, fmt.Errorf("%s: unable to retrieve event formatted as %q", op, s.requiredFormat)
} }
_, err := os.Stdout.Write(formattedBytes) _, err := os.Stdout.Write(formattedBytes)
@ -56,11 +56,11 @@ func (n *StdoutSink) Process(ctx context.Context, event *eventlogger.Event) (*ev
} }
// Reopen is a no-op for the StdoutSink type. // Reopen is a no-op for the StdoutSink type.
func (n *StdoutSink) Reopen() error { func (s *StdoutSink) Reopen() error {
return nil return nil
} }
// Type returns the eventlogger.NodeTypeSink constant. // Type returns the eventlogger.NodeTypeSink constant.
func (n *StdoutSink) Type() eventlogger.NodeType { func (s *StdoutSink) Type() eventlogger.NodeType {
return eventlogger.NodeTypeSink return eventlogger.NodeTypeSink
} }

View File

@ -12,6 +12,8 @@ import (
"github.com/hashicorp/eventlogger" "github.com/hashicorp/eventlogger"
) )
var _ eventlogger.Node = (*SyslogSink)(nil)
// SyslogSink is a sink node which handles writing events to syslog. // SyslogSink is a sink node which handles writing events to syslog.
type SyslogSink struct { type SyslogSink struct {
requiredFormat string requiredFormat string