Skip to content

feat: add audit exporting and filtering #1314

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

Merged
merged 6 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
backend renaming and more docs
  • Loading branch information
coadler committed May 9, 2022
commit a60c1f7263e0ec79567995b814f1380005ce62af
10 changes: 5 additions & 5 deletions coderd/audit/backends/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/coder/coder/coderd/database"
)

type pgBackend struct {
type postgresBackend struct {
// internal indicates if the exporter is exporting to the Postgres database
// that the rest of Coderd uses. Since this is a generic Postgres exporter,
// we make different decisions to store the audit log based on if it's
Expand All @@ -18,19 +18,19 @@ type pgBackend struct {
db database.Store
}

func NewPGBackend(db database.Store, internal bool) audit.Backend {
return &pgBackend{db: db, internal: internal}
func NewPostgres(db database.Store, internal bool) audit.Backend {
return &postgresBackend{db: db, internal: internal}
}

func (b *pgBackend) Decision() audit.FilterDecision {
func (b *postgresBackend) Decision() audit.FilterDecision {
if b.internal {
return audit.FilterDecisionStore
}

return audit.FilterDecisionExport
}

func (b *pgBackend) Export(ctx context.Context, alog database.AuditLog) error {
func (b *postgresBackend) Export(ctx context.Context, alog database.AuditLog) error {
_, err := b.db.InsertAuditLog(ctx, database.InsertAuditLogParams{
ID: alog.ID,
Time: alog.Time,
Expand Down
2 changes: 1 addition & 1 deletion coderd/audit/backends/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestPostgresBackend(t *testing.T) {
var (
ctx, cancel = context.WithCancel(context.Background())
db = databasefake.New()
pgb = backends.NewPGBackend(db, true)
pgb = backends.NewPostgres(db, true)
alog = randomAuditLog()
)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion coderd/audit/backends/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type slogBackend struct {
log slog.Logger
}

func NewSlogBackend(logger slog.Logger) audit.Backend {
func NewSlog(logger slog.Logger) audit.Backend {
return slogBackend{log: logger}
}

Expand Down
2 changes: 1 addition & 1 deletion coderd/audit/backends/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestSlogBackend(t *testing.T) {

sink = &fakeSink{}
logger = slog.Make(sink)
backend = backends.NewSlogBackend(logger)
backend = backends.NewSlog(logger)

alog = randomAuditLog()
)
Expand Down
8 changes: 6 additions & 2 deletions coderd/audit/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ import (
"github.com/coder/coder/coderd/database"
)

// Backends can accept and store or send audit logs elsewhere.
// Backends can store or send audit logs to arbitrary locations.
type Backend interface {
// Decision determines the FilterDecisions that the backend tolerates.
Decision() FilterDecision
// Export sends an audit log to the backend.
Export(ctx context.Context, alog database.AuditLog) error
}

// Exporter exports audit logs to an arbitrary amount of backends.
// Exporter exports audit logs to an arbitrary list of backends.
type Exporter struct {
filter Filter
backends []Backend
}

// NewExporter creates an exporter from the given filter and backends.
func NewExporter(filter Filter, backends ...Backend) *Exporter {
return &Exporter{
filter: filter,
backends: backends,
}
}

// Export exports and audit log. Before exporting to a backend, it uses the
// filter to determine if the backend tolerates the audit log. If not, it is
// dropped.
func (e *Exporter) Export(ctx context.Context, alog database.AuditLog) error {
for _, backend := range e.backends {
decision, err := e.filter.Check(ctx, alog)
Expand Down