Skip to content

feat: add audit log filter for autostarted and autostopped workspace builds #5830

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 9 commits into from
Jan 24, 2023

Conversation

Kira-Pilot
Copy link
Member

@Kira-Pilot Kira-Pilot commented Jan 23, 2023

follow-up to #5813. resolves #4729

We now can filter workspace_build resources by build_reason. Users will be able to filter out builds that were autostarted/autostopped.

Screen Shot 2023-01-23 at 6 00 06 PM

(audit_logs.resource_type = 'workspace' AND
audit_logs.action = 'create' AND
workspaces.id = workspace_builds.workspace_id AND
workspace_builds.build_number = 1)
Copy link
Member Author

Choose a reason for hiding this comment

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

The above is a bit obnoxious, but it exists to handle the case when a workspace is first created. In these situations, the resource_type is workspace, not workspace_build.

Otherwise, we could simplify:
LEFT JOIN workspace_builds on audit_logs.resource_type = ‘workspace_build’ AND audit_logs.resource_id = workspace_builds.id

Copy link
Member

Choose a reason for hiding this comment

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

I think we need to fix the formatting on this sql.

👍

Copy link
Member Author

Choose a reason for hiding this comment

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

@Emyrk I think I fixed it with pgFormatter but please let me know if it still looks wonky.

Copy link
Member

@Emyrk Emyrk Jan 24, 2023

Choose a reason for hiding this comment

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

I don't think it did, but we don't abide by some standard sql formatting because the sqlc directives are not valid sql, and the formatters do not know what to do 🤷.

-- GetAuditLogsBefore retrieves `row_limit` number of audit logs before the provided
-- ID.
-- name: GetAuditLogsOffset :many
SELECT
    audit_logs.*,
    users.username AS user_username,
    users.email AS user_email,
    users.created_at AS user_created_at,
    users.status AS user_status,
    users.rbac_roles AS user_roles,
    users.avatar_url AS user_avatar_url,
    COUNT(audit_logs.*) OVER() AS count
FROM
    audit_logs
    LEFT JOIN
        users ON audit_logs.user_id = users.id
    LEFT JOIN
        -- First join on workspaces to get the initial workspace create
        -- to workspace build 1 id. This is because the first create is
        -- is a different audit log than subsequent starts.
        workspaces on
            audit_logs.resource_type = 'workspace'
            AND audit_logs.resource_id = workspaces.id
    LEFT JOIN
        workspace_builds on
            -- Get the reason from the build if the resource type
            -- is a workspace_build
            (
                audit_logs.resource_type = 'workspace_build' AND
                audit_logs.resource_id = workspace_builds.id
            )
            OR
            -- Get the reason from the build #1 if this is the first
            -- workspace create.
            (
                audit_logs.resource_type = 'workspace' AND
                audit_logs.action = 'create' AND
                workspaces.id = workspace_builds.workspace_id AND
                workspace_builds.build_number = 1
            )
-- WHERE clause stuff below. I think the indentation is messed up there too a bit.

Be sure to avoid using tabs. I notice we inconsistently use tabs and spaces, so I converted all tabs to 4 spaces.

@Kira-Pilot Kira-Pilot requested review from Emyrk and coadler January 23, 2023 23:19
(audit_logs.resource_type = 'workspace' AND
audit_logs.action = 'create' AND
workspaces.id = workspace_builds.workspace_id AND
workspace_builds.build_number = 1)
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to fix the formatting on this sql.

👍

@Kira-Pilot Kira-Pilot merged commit 322a4d9 into main Jan 24, 2023
@Kira-Pilot Kira-Pilot deleted the add-build-reason-filter/kira-pilot branch January 24, 2023 20:34
@github-actions github-actions bot locked and limited conversation to collaborators Jan 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

audit: differentiate between auto-start/auto-stop build events, and user started/stopped build events
3 participants