From 7bb3121aa651517e0421e3173f331533402bae77 Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Mon, 23 Jan 2023 17:42:22 +0000 Subject: [PATCH 1/7] added query --- coderd/audit.go | 14 ++++++++++++++ coderd/database/queries.sql.go | 10 ++++++++++ coderd/database/queries/auditlogs.sql | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/coderd/audit.go b/coderd/audit.go index 54f1791589950..4dea6bc54d53f 100644 --- a/coderd/audit.go +++ b/coderd/audit.go @@ -441,6 +441,7 @@ func auditSearchQuery(query string) (database.GetAuditLogsOffsetParams, []coders Email: parser.String(searchParams, "", "email"), DateFrom: parsedDateFrom, DateTo: parsedDateTo, + BuildReason: buildReasonFromString(parser.String(searchParams, "", "build_reason")), } return filter, parser.Errors @@ -486,3 +487,16 @@ func actionFromString(actionString string) string { } return "" } + +func buildReasonFromString(buildReasonString string) string { + switch codersdk.BuildReason(buildReasonString) { + case codersdk.BuildReasonInitiator: + return buildReasonString + case codersdk.BuildReasonAutostart: + return buildReasonString + case codersdk.BuildReasonAutostop: + return buildReasonString + default: + } + return "" +} diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 198ed29bd15a6..ebe0b1e3eea5a 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -379,6 +379,8 @@ FROM audit_logs LEFT JOIN users ON audit_logs.user_id = users.id +LEFT JOIN + workspace_builds on audit_logs.resource_type = "workspace_build" AND audit_logs.resource_id = workspace_builds.id WHERE -- Filter resource_type CASE @@ -428,6 +430,12 @@ WHERE "time" <= $10 ELSE true END + -- Filter by build_reason + AND CASE + WHEN $11 :: text != '' THEN + workspace_builds.reason = $11 + ELSE true + END ORDER BY "time" DESC LIMIT @@ -447,6 +455,7 @@ type GetAuditLogsOffsetParams struct { Email string `db:"email" json:"email"` DateFrom time.Time `db:"date_from" json:"date_from"` DateTo time.Time `db:"date_to" json:"date_to"` + BuildReason string `db:"build_reason" json:"build_reason"` } type GetAuditLogsOffsetRow struct { @@ -488,6 +497,7 @@ func (q *sqlQuerier) GetAuditLogsOffset(ctx context.Context, arg GetAuditLogsOff arg.Email, arg.DateFrom, arg.DateTo, + arg.BuildReason, ) if err != nil { return nil, err diff --git a/coderd/database/queries/auditlogs.sql b/coderd/database/queries/auditlogs.sql index 920b93461d77d..57a5cd506e701 100644 --- a/coderd/database/queries/auditlogs.sql +++ b/coderd/database/queries/auditlogs.sql @@ -14,6 +14,8 @@ FROM audit_logs LEFT JOIN users ON audit_logs.user_id = users.id +LEFT JOIN + workspace_builds on audit_logs.resource_type = "workspace_build" AND audit_logs.resource_id = workspace_builds.id WHERE -- Filter resource_type CASE @@ -63,6 +65,12 @@ WHERE "time" <= @date_to ELSE true END + -- Filter by build_reason + AND CASE + WHEN @build_reason :: text != '' THEN + workspace_builds.reason = @build_reason + ELSE true + END ORDER BY "time" DESC LIMIT From 85c1fd636687e091b711fac65dc9821482d8646d Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Mon, 23 Jan 2023 22:51:28 +0000 Subject: [PATCH 2/7] fixed query --- coderd/audit.go | 1 + coderd/database/queries.sql.go | 22 ++++++++++++++++++++-- coderd/database/queries/auditlogs.sql | 22 ++++++++++++++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/coderd/audit.go b/coderd/audit.go index 4dea6bc54d53f..d2e0a5fd0fa69 100644 --- a/coderd/audit.go +++ b/coderd/audit.go @@ -67,6 +67,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) { Email: filter.Email, DateFrom: filter.DateFrom, DateTo: filter.DateTo, + BuildReason: filter.BuildReason, }) if err != nil { httpapi.InternalServerError(rw, err) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index ebe0b1e3eea5a..5ec48ef5e5fbc 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -380,7 +380,25 @@ FROM LEFT JOIN users ON audit_logs.user_id = users.id LEFT JOIN - workspace_builds on audit_logs.resource_type = "workspace_build" AND audit_logs.resource_id = workspace_builds.id + -- 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 -- Filter resource_type CASE @@ -433,7 +451,7 @@ WHERE -- Filter by build_reason AND CASE WHEN $11 :: text != '' THEN - workspace_builds.reason = $11 + workspace_builds.reason :: text = $11 ELSE true END ORDER BY diff --git a/coderd/database/queries/auditlogs.sql b/coderd/database/queries/auditlogs.sql index 57a5cd506e701..9411a43349086 100644 --- a/coderd/database/queries/auditlogs.sql +++ b/coderd/database/queries/auditlogs.sql @@ -15,7 +15,25 @@ FROM LEFT JOIN users ON audit_logs.user_id = users.id LEFT JOIN - workspace_builds on audit_logs.resource_type = "workspace_build" AND audit_logs.resource_id = workspace_builds.id + -- 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 -- Filter resource_type CASE @@ -68,7 +86,7 @@ WHERE -- Filter by build_reason AND CASE WHEN @build_reason :: text != '' THEN - workspace_builds.reason = @build_reason + workspace_builds.reason :: text = @build_reason ELSE true END ORDER BY From 484418e672910591687f84c3a718570eb632f453 Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Mon, 23 Jan 2023 22:53:24 +0000 Subject: [PATCH 3/7] added example to dropdown --- site/src/pages/AuditPage/AuditPageView.tsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/site/src/pages/AuditPage/AuditPageView.tsx b/site/src/pages/AuditPage/AuditPageView.tsx index dd057985a7f2b..1347a362c035b 100644 --- a/site/src/pages/AuditPage/AuditPageView.tsx +++ b/site/src/pages/AuditPage/AuditPageView.tsx @@ -40,6 +40,10 @@ const presetFilters = [ query: "resource_type:workspace_build action:start", name: "Started builds", }, + { + query: "resource_type:workspace_build action:start build_reason:initiator", + name: "Builds started by a user", + }, ] export interface AuditPageViewProps { From 79fafe764d527b94eb21399c79dae88a440f3963 Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Mon, 23 Jan 2023 22:59:08 +0000 Subject: [PATCH 4/7] added documentation --- docs/admin/audit-logs.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/admin/audit-logs.md b/docs/admin/audit-logs.md index 4a05bbb7f6bbe..71398362edce0 100644 --- a/docs/admin/audit-logs.md +++ b/docs/admin/audit-logs.md @@ -31,7 +31,8 @@ The supported filters are: - `username` - The username of the user who triggered the action. - `email` - The email of the user who triggered the action. - `date_from` - The inclusive start date with format `YYYY-MM-DD`. -- `date_to ` - the inclusive end date with format `YYYY-MM-DD`. +- `date_to` - The inclusive end date with format `YYYY-MM-DD`. +- `build_reason` - To be used with `resource_type:workspace_build`, the [initiator](https://pkg.go.dev/github.com/coder/coder/codersdk#BuildReason) behind the build start or stop. ## Enabling this feature From dca2263e9bf812988b5dd1b851e23438ce57c19c Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Mon, 23 Jan 2023 23:13:03 +0000 Subject: [PATCH 5/7] added test --- coderd/apidoc/docs.go | 12 ++++++++++++ coderd/apidoc/swagger.json | 8 ++++++++ coderd/audit_test.go | 5 +++++ coderd/database/databasefake/databasefake.go | 6 ++++++ codersdk/audit.go | 1 + docs/api/audit.md | 1 + docs/api/schemas.md | 5 +++++ site/src/api/typesGenerated.ts | 1 + 8 files changed, 39 insertions(+) diff --git a/coderd/apidoc/docs.go b/coderd/apidoc/docs.go index c7b08e9ac0e3e..e04b641026d22 100644 --- a/coderd/apidoc/docs.go +++ b/coderd/apidoc/docs.go @@ -5547,6 +5547,18 @@ const docTemplate = `{ } ] }, + "build_reason": { + "enum": [ + "autostart", + "autostop", + "initiator" + ], + "allOf": [ + { + "$ref": "#/definitions/codersdk.BuildReason" + } + ] + }, "resource_id": { "type": "string", "format": "uuid" diff --git a/coderd/apidoc/swagger.json b/coderd/apidoc/swagger.json index 23ef9e49cfd53..33d252487b5c3 100644 --- a/coderd/apidoc/swagger.json +++ b/coderd/apidoc/swagger.json @@ -4911,6 +4911,14 @@ } ] }, + "build_reason": { + "enum": ["autostart", "autostop", "initiator"], + "allOf": [ + { + "$ref": "#/definitions/codersdk.BuildReason" + } + ] + }, "resource_id": { "type": "string", "format": "uuid" diff --git a/coderd/audit_test.go b/coderd/audit_test.go index 8fcc479c23de0..d0060a1f3b3f2 100644 --- a/coderd/audit_test.go +++ b/coderd/audit_test.go @@ -179,6 +179,11 @@ func TestAuditLogsFilter(t *testing.T) { SearchQuery: "resource_type:workspace_build action:stop", ExpectedResult: 1, }, + { + Name: "FilterOnWorkspaceBuildStartByInitiator", + SearchQuery: "resource_type:workspace_build action:start build_reason:start", + ExpectedResult: 1, + }, } for _, testCase := range testCases { diff --git a/coderd/database/databasefake/databasefake.go b/coderd/database/databasefake/databasefake.go index 51a7134ab1d33..c9e997aa55eed 100644 --- a/coderd/database/databasefake/databasefake.go +++ b/coderd/database/databasefake/databasefake.go @@ -3678,6 +3678,12 @@ func (q *fakeQuerier) GetAuditLogsOffset(ctx context.Context, arg database.GetAu continue } } + if arg.BuildReason != "" { + workspaceBuild, err := q.GetWorkspaceBuildByID(context.Background(), alog.ResourceID) + if err == nil && !strings.EqualFold(arg.BuildReason, string(workspaceBuild.Reason)) { + continue + } + } user, err := q.GetUserByID(ctx, alog.UserID) userValid := err == nil diff --git a/codersdk/audit.go b/codersdk/audit.go index 4ae98466c3798..4d6ff25bf07f8 100644 --- a/codersdk/audit.go +++ b/codersdk/audit.go @@ -125,6 +125,7 @@ type CreateTestAuditLogRequest struct { ResourceType ResourceType `json:"resource_type,omitempty" enums:"organization,template,template_version,user,workspace,workspace_build,git_ssh_key,api_key,group"` ResourceID uuid.UUID `json:"resource_id,omitempty" format:"uuid"` Time time.Time `json:"time,omitempty" format:"date-time"` + BuildReason BuildReason `json:"build_reason,omitempty" enums:"autostart,autostop,initiator"` } // AuditLogs retrieves audit logs from the given page. diff --git a/docs/api/audit.md b/docs/api/audit.md index 6157c2b1dc922..3f62c5b261f25 100644 --- a/docs/api/audit.md +++ b/docs/api/audit.md @@ -106,6 +106,7 @@ curl -X POST http://coder-server:8080/api/v2/audit/testgenerate \ ```json { "action": "create", + "build_reason": "autostart", "resource_id": "4d5215ed-38bb-48ed-879a-fdb9ca58522f", "resource_type": "organization", "time": "2019-08-24T14:15:22Z" diff --git a/docs/api/schemas.md b/docs/api/schemas.md index 8b86ca483f088..0072040624031 100644 --- a/docs/api/schemas.md +++ b/docs/api/schemas.md @@ -783,6 +783,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a ```json { "action": "create", + "build_reason": "autostart", "resource_id": "4d5215ed-38bb-48ed-879a-fdb9ca58522f", "resource_type": "organization", "time": "2019-08-24T14:15:22Z" @@ -794,6 +795,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a | Name | Type | Required | Restrictions | Description | | --------------- | ---------------------------------------------- | -------- | ------------ | ----------- | | `action` | [codersdk.AuditAction](#codersdkauditaction) | false | | | +| `build_reason` | [codersdk.BuildReason](#codersdkbuildreason) | false | | | | `resource_id` | string | false | | | | `resource_type` | [codersdk.ResourceType](#codersdkresourcetype) | false | | | | `time` | string | false | | | @@ -807,6 +809,9 @@ CreateParameterRequest is a structure used to create a new parameter value for a | `action` | `delete` | | `action` | `start` | | `action` | `stop` | +| `build_reason` | `autostart` | +| `build_reason` | `autostop` | +| `build_reason` | `initiator` | | `resource_type` | `organization` | | `resource_type` | `template` | | `resource_type` | `template_version` | diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index 6fd81556aae37..a61a5f025dfa2 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -209,6 +209,7 @@ export interface CreateTestAuditLogRequest { readonly resource_type?: ResourceType readonly resource_id?: string readonly time?: string + readonly build_reason?: BuildReason } // From codersdk/apikey.go From bb7444a7f952323bed3f6539cd220bda945ec93e Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Tue, 24 Jan 2023 15:59:27 +0000 Subject: [PATCH 6/7] fixed formatting --- coderd/database/queries.sql.go | 180 +++++++++++-------------- coderd/database/queries/auditlogs.sql | 181 ++++++++++++-------------- 2 files changed, 159 insertions(+), 202 deletions(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 5ec48ef5e5fbc..bc5bf9f19a634 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -367,99 +367,93 @@ func (q *sqlQuerier) UpdateAPIKeyByID(ctx context.Context, arg UpdateAPIKeyByIDP const getAuditLogsOffset = `-- name: GetAuditLogsOffset :many SELECT - audit_logs.id, audit_logs.time, audit_logs.user_id, audit_logs.organization_id, audit_logs.ip, audit_logs.user_agent, audit_logs.resource_type, audit_logs.resource_id, audit_logs.resource_target, audit_logs.action, audit_logs.diff, audit_logs.status_code, audit_logs.additional_fields, audit_logs.request_id, audit_logs.resource_icon, + audit_logs.id, audit_logs.time, audit_logs.user_id, audit_logs.organization_id, audit_logs.ip, audit_logs.user_agent, audit_logs.resource_type, audit_logs.resource_id, audit_logs.resource_target, audit_logs.action, audit_logs.diff, audit_logs.status_code, audit_logs.additional_fields, audit_logs.request_id, audit_logs.resource_icon, 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 + 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 + audit_logs + LEFT JOIN users ON audit_logs.user_id = users.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) + -- 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 -- Filter resource_type - CASE - WHEN $3 :: text != '' THEN - resource_type = $3 :: resource_type - ELSE true - END - -- Filter resource_id - AND CASE - WHEN $4 :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN - resource_id = $4 - ELSE true - END - -- Filter by resource_target - AND CASE - WHEN $5 :: text != '' THEN - resource_target = $5 - ELSE true - END - -- Filter action - AND CASE - WHEN $6 :: text != '' THEN - action = $6 :: audit_action - ELSE true - END - -- Filter by username - AND CASE - WHEN $7 :: text != '' THEN - users.username = $7 - ELSE true - END - -- Filter by user_email - AND CASE - WHEN $8 :: text != '' THEN - users.email = $8 - ELSE true - END - -- Filter by date_from - AND CASE - WHEN $9 :: timestamp with time zone != '0001-01-01 00:00:00' THEN - "time" >= $9 - ELSE true - END - -- Filter by date_to - AND CASE - WHEN $10 :: timestamp with time zone != '0001-01-01 00:00:00' THEN - "time" <= $10 - ELSE true - END - -- Filter by build_reason - AND CASE - WHEN $11 :: text != '' THEN - workspace_builds.reason :: text = $11 - ELSE true - END + CASE WHEN $3::text != '' THEN + resource_type = $3::resource_type + ELSE + TRUE + END + -- Filter resource_id + AND CASE WHEN $4::uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN + resource_id = $4 + ELSE + TRUE + END + -- Filter by resource_target + AND CASE WHEN $5::text != '' THEN + resource_target = $5 + ELSE + TRUE + END + -- Filter action + AND CASE WHEN $6::text != '' THEN + action = $6::audit_action + ELSE + TRUE + END + -- Filter by username + AND CASE WHEN $7::text != '' THEN + users.username = $7 + ELSE + TRUE + END + -- Filter by user_email + AND CASE WHEN $8::text != '' THEN + users.email = $8 + ELSE + TRUE + END + -- Filter by date_from + AND CASE WHEN $9::timestamp with time zone != '0001-01-01 00:00:00' THEN + "time" >= $9 + ELSE + TRUE + END + -- Filter by date_to + AND CASE WHEN $10::timestamp with time zone != '0001-01-01 00:00:00' THEN + "time" <= $10 + ELSE + TRUE + END + -- Filter by build_reason + AND CASE WHEN $11::text != '' THEN + workspace_builds.reason::text = $11 + ELSE + TRUE + END ORDER BY "time" DESC -LIMIT - $1 -OFFSET - $2 +LIMIT $1 OFFSET $2 ` type GetAuditLogsOffsetParams struct { @@ -562,26 +556,10 @@ func (q *sqlQuerier) GetAuditLogsOffset(ctx context.Context, arg GetAuditLogsOff } const insertAuditLog = `-- name: InsertAuditLog :one -INSERT INTO - audit_logs ( - id, - "time", - user_id, - organization_id, - ip, - user_agent, - resource_type, - resource_id, - resource_target, - action, - diff, - status_code, - additional_fields, - request_id, - resource_icon - ) -VALUES - ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING id, time, user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon +INSERT INTO audit_logs (id, "time", user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) +RETURNING + id, time, user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon ` type InsertAuditLogParams struct { diff --git a/coderd/database/queries/auditlogs.sql b/coderd/database/queries/auditlogs.sql index 9411a43349086..6795567ba9e93 100644 --- a/coderd/database/queries/auditlogs.sql +++ b/coderd/database/queries/auditlogs.sql @@ -2,118 +2,97 @@ -- ID. -- name: GetAuditLogsOffset :many SELECT - audit_logs.*, + 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 + 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 + audit_logs + LEFT JOIN users ON audit_logs.user_id = users.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) + -- 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 -- Filter resource_type - CASE - WHEN @resource_type :: text != '' THEN - resource_type = @resource_type :: resource_type - ELSE true - END - -- Filter resource_id - AND CASE - WHEN @resource_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN - resource_id = @resource_id - ELSE true - END - -- Filter by resource_target - AND CASE - WHEN @resource_target :: text != '' THEN - resource_target = @resource_target - ELSE true - END - -- Filter action - AND CASE - WHEN @action :: text != '' THEN - action = @action :: audit_action - ELSE true - END - -- Filter by username - AND CASE - WHEN @username :: text != '' THEN - users.username = @username - ELSE true - END - -- Filter by user_email - AND CASE - WHEN @email :: text != '' THEN - 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 - -- Filter by build_reason - AND CASE - WHEN @build_reason :: text != '' THEN - workspace_builds.reason :: text = @build_reason - ELSE true - END + CASE WHEN @resource_type::text != '' THEN + resource_type = @resource_type::resource_type + ELSE + TRUE + END + -- Filter resource_id + AND CASE WHEN @resource_id::uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN + resource_id = @resource_id + ELSE + TRUE + END + -- Filter by resource_target + AND CASE WHEN @resource_target::text != '' THEN + resource_target = @resource_target + ELSE + TRUE + END + -- Filter action + AND CASE WHEN @action::text != '' THEN + action = @action::audit_action + ELSE + TRUE + END + -- Filter by username + AND CASE WHEN @username::text != '' THEN + users.username = @username + ELSE + TRUE + END + -- Filter by user_email + AND CASE WHEN @email::text != '' THEN + 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 + -- Filter by build_reason + AND CASE WHEN @build_reason::text != '' THEN + workspace_builds.reason::text = @build_reason + ELSE + TRUE + END ORDER BY "time" DESC -LIMIT - $1 -OFFSET - $2; +LIMIT $1 OFFSET $2; -- name: InsertAuditLog :one -INSERT INTO - audit_logs ( - id, - "time", - user_id, - organization_id, - ip, - user_agent, - resource_type, - resource_id, - resource_target, - action, - diff, - status_code, - additional_fields, - request_id, - resource_icon - ) -VALUES - ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING *; +INSERT INTO audit_logs (id, "time", user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) +RETURNING + *; + From 7788420d314c4db27f3d499b8f61fcb4a46b7b08 Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Tue, 24 Jan 2023 20:01:07 +0000 Subject: [PATCH 7/7] fixed format --- coderd/database/queries.sql.go | 171 ++++++++++++++----------- coderd/database/queries/auditlogs.sql | 172 +++++++++++++++----------- 2 files changed, 196 insertions(+), 147 deletions(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 1aaeabac8136b..938bdf878177a 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -379,81 +379,90 @@ 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) + -- 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 -- Filter resource_type - CASE WHEN $3::text != '' THEN - resource_type = $3::resource_type - ELSE - TRUE - END - -- Filter resource_id - AND CASE WHEN $4::uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN - resource_id = $4 - ELSE - TRUE - END - -- Filter by resource_target - AND CASE WHEN $5::text != '' THEN - resource_target = $5 - ELSE - TRUE - END - -- Filter action - AND CASE WHEN $6::text != '' THEN - action = $6::audit_action - ELSE - TRUE - END - -- Filter by username - AND CASE WHEN $7::text != '' THEN - users.username = $7 - ELSE - TRUE - END - -- Filter by user_email - AND CASE WHEN $8::text != '' THEN - users.email = $8 - ELSE - TRUE - END - -- Filter by date_from - AND CASE WHEN $9::timestamp with time zone != '0001-01-01 00:00:00' THEN - "time" >= $9 - ELSE - TRUE - END - -- Filter by date_to - AND CASE WHEN $10::timestamp with time zone != '0001-01-01 00:00:00' THEN - "time" <= $10 - ELSE - TRUE - END + CASE + WHEN $3 :: text != '' THEN + resource_type = $3 :: resource_type + ELSE true + END + -- Filter resource_id + AND CASE + WHEN $4 :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN + resource_id = $4 + ELSE true + END + -- Filter by resource_target + AND CASE + WHEN $5 :: text != '' THEN + resource_target = $5 + ELSE true + END + -- Filter action + AND CASE + WHEN $6 :: text != '' THEN + action = $6 :: audit_action + ELSE true + END + -- Filter by username + AND CASE + WHEN $7 :: text != '' THEN + users.username = $7 + ELSE true + END + -- Filter by user_email + AND CASE + WHEN $8 :: text != '' THEN + users.email = $8 + ELSE true + END + -- Filter by date_from + AND CASE + WHEN $9 :: timestamp with time zone != '0001-01-01 00:00:00' THEN + "time" >= $9 + ELSE true + END + -- Filter by date_to + AND CASE + WHEN $10 :: timestamp with time zone != '0001-01-01 00:00:00' THEN + "time" <= $10 + ELSE true + END -- Filter by build_reason - AND CASE WHEN $11::text != '' THEN - workspace_builds.reason::text = $11 - ELSE - TRUE + AND CASE + WHEN $11::text != '' THEN + workspace_builds.reason::text = $11 + ELSE true END ORDER BY "time" DESC -LIMIT $1 OFFSET $2 +LIMIT + $1 +OFFSET + $2 ` type GetAuditLogsOffsetParams struct { @@ -556,10 +565,26 @@ func (q *sqlQuerier) GetAuditLogsOffset(ctx context.Context, arg GetAuditLogsOff } const insertAuditLog = `-- name: InsertAuditLog :one -INSERT INTO audit_logs (id, "time", user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) -RETURNING - id, time, user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon +INSERT INTO + audit_logs ( + id, + "time", + user_id, + organization_id, + ip, + user_agent, + resource_type, + resource_id, + resource_target, + action, + diff, + status_code, + additional_fields, + request_id, + resource_icon + ) +VALUES + ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING id, time, user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon ` type InsertAuditLogParams struct { diff --git a/coderd/database/queries/auditlogs.sql b/coderd/database/queries/auditlogs.sql index 6795567ba9e93..bffebae7c3961 100644 --- a/coderd/database/queries/auditlogs.sql +++ b/coderd/database/queries/auditlogs.sql @@ -14,85 +14,109 @@ 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) + -- 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 -- Filter resource_type - CASE WHEN @resource_type::text != '' THEN - resource_type = @resource_type::resource_type - ELSE - TRUE - END - -- Filter resource_id - AND CASE WHEN @resource_id::uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN - resource_id = @resource_id - ELSE - TRUE - END - -- Filter by resource_target - AND CASE WHEN @resource_target::text != '' THEN - resource_target = @resource_target - ELSE - TRUE - END - -- Filter action - AND CASE WHEN @action::text != '' THEN - action = @action::audit_action - ELSE - TRUE - END - -- Filter by username - AND CASE WHEN @username::text != '' THEN - users.username = @username - ELSE - TRUE - END - -- Filter by user_email - AND CASE WHEN @email::text != '' THEN - 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 + CASE + WHEN @resource_type :: text != '' THEN + resource_type = @resource_type :: resource_type + ELSE true + END + -- Filter resource_id + AND CASE + WHEN @resource_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN + resource_id = @resource_id + ELSE true + END + -- Filter by resource_target + AND CASE + WHEN @resource_target :: text != '' THEN + resource_target = @resource_target + ELSE true + END + -- Filter action + AND CASE + WHEN @action :: text != '' THEN + action = @action :: audit_action + ELSE true + END + -- Filter by username + AND CASE + WHEN @username :: text != '' THEN + users.username = @username + ELSE true + END + -- Filter by user_email + AND CASE + WHEN @email :: text != '' THEN + 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 -- Filter by build_reason - AND CASE WHEN @build_reason::text != '' THEN - workspace_builds.reason::text = @build_reason - ELSE - TRUE + AND CASE + WHEN @build_reason::text != '' THEN + workspace_builds.reason::text = @build_reason + ELSE true END ORDER BY "time" DESC -LIMIT $1 OFFSET $2; +LIMIT + $1 +OFFSET + $2; -- name: InsertAuditLog :one -INSERT INTO audit_logs (id, "time", user_id, organization_id, ip, user_agent, resource_type, resource_id, resource_target, action, diff, status_code, additional_fields, request_id, resource_icon) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) -RETURNING - *; - +INSERT INTO + audit_logs ( + id, + "time", + user_id, + organization_id, + ip, + user_agent, + resource_type, + resource_id, + resource_target, + action, + diff, + status_code, + additional_fields, + request_id, + resource_icon + ) +VALUES + ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING *;