Skip to content

Commit a77a9ab

Browse files
authored
chore: skip audit log filter for owner/admin users (coder#14132)
* chore: audit log filter to be skipped if user is owner/admin Optimize for speed in the case the user can read all audit_logs * fixup! chore: audit log filter to be skipped if user is owner/admin
1 parent 203f48a commit a77a9ab

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,13 @@ func (q *querier) GetApplicationName(ctx context.Context) (string, error) {
12481248
}
12491249

12501250
func (q *querier) GetAuditLogsOffset(ctx context.Context, arg database.GetAuditLogsOffsetParams) ([]database.GetAuditLogsOffsetRow, error) {
1251+
// Shortcut if the user is an owner. The SQL filter is noticeable,
1252+
// and this is an easy win for owners. Which is the common case.
1253+
err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceAuditLog)
1254+
if err == nil {
1255+
return q.db.GetAuditLogsOffset(ctx, arg)
1256+
}
1257+
12511258
prep, err := prepareSQLFilter(ctx, q.auth, policy.ActionRead, rbac.ResourceAuditLog.Type)
12521259
if err != nil {
12531260
return nil, xerrors.Errorf("(dev error) prepare sql filter: %w", err)

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ func (s *MethodTestSuite) TestAuditLogs() {
267267
_ = dbgen.AuditLog(s.T(), db, database.AuditLog{})
268268
check.Args(database.GetAuditLogsOffsetParams{
269269
LimitOpt: 10,
270-
}).Asserts()
270+
}).Asserts(rbac.ResourceAuditLog, policy.ActionRead)
271271
}))
272272
s.Run("GetAuthorizedAuditLogsOffset", s.Subtest(func(db database.Store, check *expects) {
273273
_ = dbgen.AuditLog(s.T(), db, database.AuditLog{})
274274
_ = dbgen.AuditLog(s.T(), db, database.AuditLog{})
275275
check.Args(database.GetAuditLogsOffsetParams{
276276
LimitOpt: 10,
277-
}, emptyPreparedAuthorized{}).Asserts()
277+
}, emptyPreparedAuthorized{}).Asserts(rbac.ResourceAuditLog, policy.ActionRead)
278278
}))
279279
}
280280

0 commit comments

Comments
 (0)