Skip to content

fix(dbpurge): use dbauthz.AsSystemRestricted #7017

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 2 commits into from
Apr 5, 2023
Merged
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
Next Next commit
fix(dbpurge): use dbauthz.AsSystemRestricted
Fixes: #7016
  • Loading branch information
coadler committed Apr 5, 2023
commit 80ef27f29c9776a89029a814d3096e73d66d2cba
16 changes: 13 additions & 3 deletions coderd/database/dbpurge/dbpurge.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (

"cdr.dev/slog"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
)

const (
delay = 24 * time.Hour
)

// New creates a new periodically purging database instance.
Expand All @@ -19,15 +24,18 @@ import (
func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
closed := make(chan struct{})
ctx, cancelFunc := context.WithCancel(ctx)
//nolint:gocritic // The system purges old db records without user input.
ctx = dbauthz.AsSystemRestricted(ctx)
go func() {
defer close(closed)
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()

timer := time.NewTimer(delay)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
case <-timer.C:
}

var eg errgroup.Group
Expand All @@ -44,6 +52,8 @@ func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
}
logger.Error(ctx, "failed to purge old database entries", slog.Error(err))
}

timer.Reset(delay)
}
}()
return &instance{
Expand Down