Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 322a4d9

Browse files
authored
feat: add audit log filter for autostarted and autostopped workspace builds (coder#5830)
* added query * fixed query * added example to dropdown * added documentation * added test * fixed formatting * fixed format
1 parent 36384aa commit 322a4d9

File tree

13 files changed

+130
-11
lines changed

13 files changed

+130
-11
lines changed

coderd/apidoc/docs.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/audit.go

+15
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
6767
Email: filter.Email,
6868
DateFrom: filter.DateFrom,
6969
DateTo: filter.DateTo,
70+
BuildReason: filter.BuildReason,
7071
})
7172
if err != nil {
7273
httpapi.InternalServerError(rw, err)
@@ -443,6 +444,7 @@ func auditSearchQuery(query string) (database.GetAuditLogsOffsetParams, []coders
443444
Email: parser.String(searchParams, "", "email"),
444445
DateFrom: parsedDateFrom,
445446
DateTo: parsedDateTo,
447+
BuildReason: buildReasonFromString(parser.String(searchParams, "", "build_reason")),
446448
}
447449

448450
return filter, parser.Errors
@@ -488,3 +490,16 @@ func actionFromString(actionString string) string {
488490
}
489491
return ""
490492
}
493+
494+
func buildReasonFromString(buildReasonString string) string {
495+
switch codersdk.BuildReason(buildReasonString) {
496+
case codersdk.BuildReasonInitiator:
497+
return buildReasonString
498+
case codersdk.BuildReasonAutostart:
499+
return buildReasonString
500+
case codersdk.BuildReasonAutostop:
501+
return buildReasonString
502+
default:
503+
}
504+
return ""
505+
}

coderd/audit_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ func TestAuditLogsFilter(t *testing.T) {
179179
SearchQuery: "resource_type:workspace_build action:stop",
180180
ExpectedResult: 1,
181181
},
182+
{
183+
Name: "FilterOnWorkspaceBuildStartByInitiator",
184+
SearchQuery: "resource_type:workspace_build action:start build_reason:start",
185+
ExpectedResult: 1,
186+
},
182187
}
183188

184189
for _, testCase := range testCases {

coderd/database/databasefake/databasefake.go

+6
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,12 @@ func (q *fakeQuerier) GetAuditLogsOffset(ctx context.Context, arg database.GetAu
36803680
continue
36813681
}
36823682
}
3683+
if arg.BuildReason != "" {
3684+
workspaceBuild, err := q.GetWorkspaceBuildByID(context.Background(), alog.ResourceID)
3685+
if err == nil && !strings.EqualFold(arg.BuildReason, string(workspaceBuild.Reason)) {
3686+
continue
3687+
}
3688+
}
36833689

36843690
user, err := q.GetUserByID(ctx, alog.UserID)
36853691
userValid := err == nil

coderd/database/queries.sql.go

+36-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/auditlogs.sql

+34-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,41 @@
22
-- ID.
33
-- name: GetAuditLogsOffset :many
44
SELECT
5-
audit_logs.*,
5+
audit_logs.*,
66
users.username AS user_username,
77
users.email AS user_email,
88
users.created_at AS user_created_at,
99
users.status AS user_status,
1010
users.rbac_roles AS user_roles,
1111
users.avatar_url AS user_avatar_url,
12-
COUNT(audit_logs.*) OVER() AS count
12+
COUNT(audit_logs.*) OVER () AS count
1313
FROM
14-
audit_logs
15-
LEFT JOIN
16-
users ON audit_logs.user_id = users.id
14+
audit_logs
15+
LEFT JOIN users ON audit_logs.user_id = users.id
16+
LEFT JOIN
17+
-- First join on workspaces to get the initial workspace create
18+
-- to workspace build 1 id. This is because the first create is
19+
-- is a different audit log than subsequent starts.
20+
workspaces ON
21+
audit_logs.resource_type = 'workspace' AND
22+
audit_logs.resource_id = workspaces.id
23+
LEFT JOIN
24+
workspace_builds ON
25+
-- Get the reason from the build if the resource type
26+
-- is a workspace_build
27+
(
28+
audit_logs.resource_type = 'workspace_build'
29+
AND audit_logs.resource_id = workspace_builds.id
30+
)
31+
OR
32+
-- Get the reason from the build #1 if this is the first
33+
-- workspace create.
34+
(
35+
audit_logs.resource_type = 'workspace' AND
36+
audit_logs.action = 'create' AND
37+
workspaces.id = workspace_builds.workspace_id AND
38+
workspace_builds.build_number = 1
39+
)
1740
WHERE
1841
-- Filter resource_type
1942
CASE
@@ -63,6 +86,12 @@ WHERE
6386
"time" <= @date_to
6487
ELSE true
6588
END
89+
-- Filter by build_reason
90+
AND CASE
91+
WHEN @build_reason::text != '' THEN
92+
workspace_builds.reason::text = @build_reason
93+
ELSE true
94+
END
6695
ORDER BY
6796
"time" DESC
6897
LIMIT

codersdk/audit.go

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ type CreateTestAuditLogRequest struct {
125125
ResourceType ResourceType `json:"resource_type,omitempty" enums:"organization,template,template_version,user,workspace,workspace_build,git_ssh_key,api_key,group"`
126126
ResourceID uuid.UUID `json:"resource_id,omitempty" format:"uuid"`
127127
Time time.Time `json:"time,omitempty" format:"date-time"`
128+
BuildReason BuildReason `json:"build_reason,omitempty" enums:"autostart,autostop,initiator"`
128129
}
129130

130131
// AuditLogs retrieves audit logs from the given page.

docs/admin/audit-logs.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ The supported filters are:
3131
- `username` - The username of the user who triggered the action.
3232
- `email` - The email of the user who triggered the action.
3333
- `date_from` - The inclusive start date with format `YYYY-MM-DD`.
34-
- `date_to ` - the inclusive end date with format `YYYY-MM-DD`.
34+
- `date_to` - The inclusive end date with format `YYYY-MM-DD`.
35+
- `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.
3536

3637
## Enabling this feature
3738

docs/api/audit.md

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ curl -X POST http://coder-server:8080/api/v2/audit/testgenerate \
106106
```json
107107
{
108108
"action": "create",
109+
"build_reason": "autostart",
109110
"resource_id": "4d5215ed-38bb-48ed-879a-fdb9ca58522f",
110111
"resource_type": "organization",
111112
"time": "2019-08-24T14:15:22Z"

docs/api/schemas.md

+5
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
783783
```json
784784
{
785785
"action": "create",
786+
"build_reason": "autostart",
786787
"resource_id": "4d5215ed-38bb-48ed-879a-fdb9ca58522f",
787788
"resource_type": "organization",
788789
"time": "2019-08-24T14:15:22Z"
@@ -794,6 +795,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
794795
| Name | Type | Required | Restrictions | Description |
795796
| --------------- | ---------------------------------------------- | -------- | ------------ | ----------- |
796797
| `action` | [codersdk.AuditAction](#codersdkauditaction) | false | | |
798+
| `build_reason` | [codersdk.BuildReason](#codersdkbuildreason) | false | | |
797799
| `resource_id` | string | false | | |
798800
| `resource_type` | [codersdk.ResourceType](#codersdkresourcetype) | false | | |
799801
| `time` | string | false | | |
@@ -807,6 +809,9 @@ CreateParameterRequest is a structure used to create a new parameter value for a
807809
| `action` | `delete` |
808810
| `action` | `start` |
809811
| `action` | `stop` |
812+
| `build_reason` | `autostart` |
813+
| `build_reason` | `autostop` |
814+
| `build_reason` | `initiator` |
810815
| `resource_type` | `organization` |
811816
| `resource_type` | `template` |
812817
| `resource_type` | `template_version` |

site/src/api/typesGenerated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export interface CreateTestAuditLogRequest {
209209
readonly resource_type?: ResourceType
210210
readonly resource_id?: string
211211
readonly time?: string
212+
readonly build_reason?: BuildReason
212213
}
213214

214215
// From codersdk/apikey.go

site/src/pages/AuditPage/AuditPageView.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ const presetFilters = [
4040
query: "resource_type:workspace_build action:start",
4141
name: "Started builds",
4242
},
43+
{
44+
query: "resource_type:workspace_build action:start build_reason:initiator",
45+
name: "Builds started by a user",
46+
},
4347
]
4448

4549
export interface AuditPageViewProps {

0 commit comments

Comments
 (0)