Skip to content

Audit date filter/kira pilot #4845

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 11 commits into from
Nov 3, 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
fix: ensure date_from and date_to are applied correct audit logs
  • Loading branch information
deansheather committed Nov 2, 2022
commit d9eab84cf91879a18f03daa471391dd054d886cc
2 changes: 2 additions & 0 deletions coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func (api *API) auditLogCount(rw http.ResponseWriter, r *http.Request) {
Action: filter.Action,
Username: filter.Username,
Email: filter.Email,
DateFrom: filter.DateFrom,
DateTo: filter.DateTo,
})
if err != nil {
httpapi.InternalServerError(rw, err)
Expand Down
20 changes: 20 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,16 @@ func (q *fakeQuerier) GetAuditLogsOffset(ctx context.Context, arg database.GetAu
continue
}
}
if arg.DateFrom != (time.Time{}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all of these could be if !arg.DateFrom.IsZero()

if alog.Time.Before(arg.DateFrom) {
continue
}
}
if arg.DateTo != (time.Time{}) {
if alog.Time.After(arg.DateTo) {
continue
}
}

user, err := q.GetUserByID(ctx, alog.UserID)
userValid := err == nil
Expand Down Expand Up @@ -3057,6 +3067,16 @@ func (q *fakeQuerier) GetAuditLogCount(_ context.Context, arg database.GetAuditL
continue
}
}
if arg.DateFrom != (time.Time{}) {
if alog.Time.Before(arg.DateFrom) {
continue
}
}
if arg.DateTo != (time.Time{}) {
if alog.Time.After(arg.DateTo) {
continue
}
}

logs = append(logs, alog)
}
Expand Down
18 changes: 17 additions & 1 deletion coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion coderd/database/queries/auditlogs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SELECT
users.avatar_url AS user_avatar_url
FROM
audit_logs
LEFT JOIN
LEFT JOIN
users ON audit_logs.user_id = users.id
WHERE
-- Filter resource_type
Expand Down Expand Up @@ -110,6 +110,18 @@ WHERE
WHEN @email :: text != '' THEN
user_id = (SELECT id from users WHERE users.email = @email )
ELSE true
END
-- Filter by date_from
AND CASE
WHEN @date_from :: timestamp with time zone != '0001-01-01 00:00:00' THEN
"time" >= @date_from
ELSE true
END
-- Filter by date_to
AND CASE
WHEN @date_to :: timestamp with time zone != '0001-01-01 00:00:00' THEN
"time" <= @date_to
ELSE true
END;

-- name: InsertAuditLog :one
Expand Down