-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support writing audit events to stdout for log #3113
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
47c3608
feat: support writing audit events to stdout for log
markphelps a4f41e1
chore: simplify audit log default stdout logic
markphelps 9ee4f0b
Merge branch 'main' into audit-logs-stdout
markphelps fed391e
feat: support writing audit events to stdout for log
markphelps 8ebf2c8
chore: simplify audit log default stdout logic
markphelps 5a59b77
chore: switch to zap logger
markphelps 8734335
chore: merge
markphelps fbe9ed8
Merge branch 'main' into audit-logs-stdout
markphelps 6c1e0f5
chore: fix schemas
markphelps f5887bd
Merge branch 'audit-logs-stdout' of https://github.com/flipt-io/flipt…
markphelps 19e40a4
chore: key for AUDIT; inherit global log encoding if not set
markphelps 8e98c39
chore: add audit log test
markphelps e2b4355
chore: change error msg
markphelps 997a00c
chore: push failing non-existent dir test
markphelps b314368
chore: create dir if not exists
markphelps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"go.flipt.io/flipt/internal/config" | ||
"go.flipt.io/flipt/internal/server/audit" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
const ( | ||
sinkType = "log" | ||
auditKey = "AUDIT" | ||
) | ||
|
||
// Sink is the structure in charge of sending audit events to a specified file location. | ||
type Sink struct { | ||
logger *zap.Logger | ||
} | ||
|
||
type logOptions struct { | ||
path string | ||
encoding config.LogEncoding | ||
} | ||
|
||
type Option func(*logOptions) | ||
|
||
func WithPath(path string) Option { | ||
return func(o *logOptions) { | ||
o.path = path | ||
} | ||
} | ||
|
||
func WithEncoding(encoding config.LogEncoding) Option { | ||
return func(o *logOptions) { | ||
o.encoding = encoding | ||
} | ||
} | ||
|
||
// NewSink is the constructor for a Sink. | ||
func NewSink(opts ...Option) (audit.Sink, error) { | ||
oo := &logOptions{} | ||
|
||
for _, o := range opts { | ||
o(oo) | ||
} | ||
|
||
return newSink(*oo) | ||
} | ||
|
||
var ( | ||
consoleEncoderConfig = zapcore.EncoderConfig{ | ||
TimeKey: "T", | ||
LevelKey: zapcore.OmitKey, | ||
NameKey: "N", | ||
CallerKey: zapcore.OmitKey, | ||
FunctionKey: zapcore.OmitKey, | ||
MessageKey: "M", | ||
StacktraceKey: zapcore.OmitKey, | ||
LineEnding: zapcore.DefaultLineEnding, | ||
EncodeLevel: zapcore.CapitalColorLevelEncoder, | ||
EncodeTime: zapcore.RFC3339TimeEncoder, | ||
EncodeDuration: zapcore.StringDurationEncoder, | ||
EncodeCaller: zapcore.ShortCallerEncoder, | ||
} | ||
|
||
fileEncoderConfig = zapcore.EncoderConfig{ | ||
TimeKey: zapcore.OmitKey, | ||
LevelKey: zapcore.OmitKey, | ||
NameKey: zapcore.OmitKey, | ||
CallerKey: zapcore.OmitKey, | ||
FunctionKey: zapcore.OmitKey, | ||
MessageKey: zapcore.OmitKey, | ||
StacktraceKey: zapcore.OmitKey, | ||
LineEnding: zapcore.DefaultLineEnding, | ||
EncodeLevel: zapcore.CapitalColorLevelEncoder, | ||
EncodeTime: zapcore.RFC3339TimeEncoder, | ||
EncodeDuration: zapcore.StringDurationEncoder, | ||
EncodeCaller: zapcore.ShortCallerEncoder, | ||
} | ||
) | ||
|
||
// newSink is the constructor for a Sink visible for testing. | ||
func newSink(opts logOptions) (audit.Sink, error) { | ||
cfg := zap.Config{ | ||
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel), | ||
Development: false, | ||
Encoding: "console", | ||
EncoderConfig: consoleEncoderConfig, | ||
OutputPaths: []string{"stdout"}, | ||
ErrorOutputPaths: []string{"stderr"}, | ||
} | ||
|
||
if opts.encoding != "" { | ||
cfg.Encoding = string(opts.encoding) | ||
} | ||
|
||
if opts.path != "" { | ||
// check path dir exists, if not create it | ||
dir := filepath.Dir(opts.path) | ||
if _, err := os.Stat(dir); err != nil { | ||
if !os.IsNotExist(err) { | ||
return nil, fmt.Errorf("checking log file path: %w", err) | ||
} | ||
|
||
if err := os.MkdirAll(dir, 0755); err != nil { | ||
return nil, fmt.Errorf("creating log file path: %w", err) | ||
} | ||
} | ||
|
||
cfg.OutputPaths = []string{opts.path} | ||
markphelps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cfg.EncoderConfig = fileEncoderConfig | ||
} | ||
|
||
return &Sink{ | ||
logger: zap.Must(cfg.Build()), | ||
}, nil | ||
} | ||
|
||
func (l *Sink) SendAudits(_ context.Context, events []audit.Event) error { | ||
for _, e := range events { | ||
l.logger.Info(auditKey, zap.Any("event", e)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (l *Sink) Close() error { | ||
// see: https://github.com/uber-go/zap/issues/991 | ||
//nolint:errcheck | ||
_ = l.logger.Sync() | ||
return nil | ||
} | ||
|
||
func (l *Sink) String() string { | ||
return sinkType | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
""
is required so that by default it inherits the same encoding aslog.encoding
, although i guess we could make an explicitinherit
option?