Skip to content

Commit 7dc73ed

Browse files
authored
feat: add description to audit log responses (#3949)
1 parent 5e04a2f commit 7dc73ed

File tree

10 files changed

+75
-8
lines changed

10 files changed

+75
-8
lines changed

coderd/audit.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package coderd
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net"
67
"net/http"
78
"net/netip"
@@ -167,7 +168,14 @@ func convertAuditLog(dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog {
167168
Diff: diff,
168169
StatusCode: dblog.StatusCode,
169170
AdditionalFields: dblog.AdditionalFields,
170-
Description: "",
171+
Description: auditLogDescription(dblog),
171172
User: user,
172173
}
173174
}
175+
176+
func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
177+
return fmt.Sprintf("{user} %s %s {target}",
178+
codersdk.AuditAction(alog.Action).FriendlyString(),
179+
codersdk.ResourceType(alog.ResourceType).FriendlyString(),
180+
)
181+
}

coderd/database/databasefake/databasefake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ func (q *fakeQuerier) GetAuditLogsOffset(ctx context.Context, arg database.GetAu
23082308
OrganizationID: alog.OrganizationID,
23092309
Ip: alog.Ip,
23102310
UserAgent: alog.UserAgent,
2311-
ResourceType: database.ResourceType(alog.UserAgent),
2311+
ResourceType: alog.ResourceType,
23122312
ResourceID: alog.ResourceID,
23132313
ResourceTarget: alog.ResourceTarget,
23142314
ResourceIcon: alog.ResourceIcon,

coderd/database/dump.sql

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-- It's not possible to drop enum values from enum types, so the UP has "IF NOT
22
-- EXISTS".
33

4-
-- Delete all jobs that use the new enum value.
4+
-- Delete all audit logs that use the new enum values.
55
DELETE FROM
6-
provisioner_jobs
6+
audit_logs
77
WHERE
8-
type = 'template_version_dry_run'
9-
;
8+
resource_type = 'git_ssh_key' OR
9+
resource_type = 'api_key';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- It's not possible to drop enum values from enum types, so the UP has "IF NOT
2+
-- EXISTS".
3+
4+
-- Delete all jobs that use the new enum value.
5+
DELETE FROM
6+
provisioner_jobs
7+
WHERE
8+
type = 'template_version_dry_run';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'git_ssh_key';
2+
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'api_key';

coderd/database/models.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/audit.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,31 @@ const (
1818
ResourceTypeTemplateVersion ResourceType = "template_version"
1919
ResourceTypeUser ResourceType = "user"
2020
ResourceTypeWorkspace ResourceType = "workspace"
21+
ResourceTypeGitSSHKey ResourceType = "git_ssh_key"
22+
ResourceTypeAPIKey ResourceType = "api_key"
2123
)
2224

25+
func (r ResourceType) FriendlyString() string {
26+
switch r {
27+
case ResourceTypeOrganization:
28+
return "organization"
29+
case ResourceTypeTemplate:
30+
return "template"
31+
case ResourceTypeTemplateVersion:
32+
return "template version"
33+
case ResourceTypeUser:
34+
return "user"
35+
case ResourceTypeWorkspace:
36+
return "workspace"
37+
case ResourceTypeGitSSHKey:
38+
return "git ssh key"
39+
case ResourceTypeAPIKey:
40+
return "api key"
41+
default:
42+
return "unknown"
43+
}
44+
}
45+
2346
type AuditAction string
2447

2548
const (
@@ -28,6 +51,19 @@ const (
2851
AuditActionDelete AuditAction = "delete"
2952
)
3053

54+
func (a AuditAction) FriendlyString() string {
55+
switch a {
56+
case AuditActionCreate:
57+
return "created"
58+
case AuditActionWrite:
59+
return "updated"
60+
case AuditActionDelete:
61+
return "deleted"
62+
default:
63+
return "unknown"
64+
}
65+
}
66+
3167
type AuditDiff map[string]AuditDiffField
3268

3369
type AuditDiffField struct {

site/src/api/typesGenerated.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,14 @@ export type ProvisionerStorageMethod = "file"
692692
export type ProvisionerType = "echo" | "terraform"
693693

694694
// From codersdk/audit.go
695-
export type ResourceType = "organization" | "template" | "template_version" | "user" | "workspace"
695+
export type ResourceType =
696+
| "api_key"
697+
| "git_ssh_key"
698+
| "organization"
699+
| "template"
700+
| "template_version"
701+
| "user"
702+
| "workspace"
696703

697704
// From codersdk/users.go
698705
export type UserStatus = "active" | "suspended"

site/src/components/AuditLogRow/AuditLogRow.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const resourceLabelByResourceType: Record<AuditLog["resource_type"], string> = {
3838
template_version: "template version",
3939
user: "user",
4040
workspace: "workspace",
41+
git_ssh_key: "git ssh key",
42+
api_key: "api key",
4143
}
4244

4345
const readableActionMessage = (auditLog: AuditLog) => {

0 commit comments

Comments
 (0)