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

Skip to content

Commit 1f9ccfa

Browse files
committed
adding workspace_build resource
1 parent 5d7d8c3 commit 1f9ccfa

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

coderd/audit.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ func resourceTypeFromString(resourceTypeString string) string {
289289
return resourceTypeString
290290
case codersdk.ResourceTypeWorkspace:
291291
return resourceTypeString
292+
case codersdk.ResourceTypeWorkspaceBuild:
293+
return resourceTypeString
292294
case codersdk.ResourceTypeGitSSHKey:
293295
return resourceTypeString
294296
case codersdk.ResourceTypeAPIKey:

coderd/audit/diff.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Auditable interface {
1515
database.TemplateVersion |
1616
database.User |
1717
database.Workspace |
18+
database.WorkspaceBuild |
1819
database.GitSSHKey
1920
}
2021

coderd/audit/request.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func ResourceTarget[T Auditable](tgt T) string {
4343
return typed.Username
4444
case database.Workspace:
4545
return typed.Name
46+
case database.WorkspaceBuild:
47+
return string(typed.Transition)
4648
case database.GitSSHKey:
4749
return typed.PublicKey
4850
default:
@@ -62,6 +64,8 @@ func ResourceID[T Auditable](tgt T) uuid.UUID {
6264
return typed.ID
6365
case database.Workspace:
6466
return typed.ID
67+
case database.WorkspaceBuild:
68+
return typed.ID
6569
case database.GitSSHKey:
6670
return typed.UserID
6771
default:
@@ -81,6 +85,8 @@ func ResourceType[T Auditable](tgt T) database.ResourceType {
8185
return database.ResourceTypeUser
8286
case database.Workspace:
8387
return database.ResourceTypeWorkspace
88+
case database.WorkspaceBuild:
89+
return database.ResourceTypeWorkspaceBuild
8490
case database.GitSSHKey:
8591
return database.ResourceTypeGitSshKey
8692
default:

coderd/workspacebuilds.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,11 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
278278
return
279279
}
280280

281-
// we only want to create audit logs for delete builds right now
281+
auditor := api.Auditor.Load()
282+
283+
// if user deletes a workspace, audit the workspace
282284
if action == rbac.ActionDelete {
283285
var (
284-
auditor = api.Auditor.Load()
285286
aReq, commitAudit = audit.InitRequest[database.Workspace](rw, &audit.RequestParams{
286287
Audit: *auditor,
287288
Log: api.Logger,
@@ -294,12 +295,29 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
294295
aReq.Old = workspace
295296
}
296297

298+
latestBuild, latestBuildErr := api.Database.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
299+
300+
// if a user starts/stops a workspace, audit the workspace build
301+
if action == rbac.ActionUpdate {
302+
303+
var (
304+
aReq, commitAudit = audit.InitRequest[database.WorkspaceBuild](rw, &audit.RequestParams{
305+
Audit: *auditor,
306+
Log: api.Logger,
307+
Request: r,
308+
Action: database.AuditActionWrite,
309+
})
310+
)
311+
312+
defer commitAudit()
313+
aReq.Old = latestBuild
314+
}
315+
297316
if createBuild.TemplateVersionID == uuid.Nil {
298-
latestBuild, err := api.Database.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
299-
if err != nil {
317+
if latestBuildErr != nil {
300318
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
301319
Message: "Internal error fetching the latest workspace build.",
302-
Detail: err.Error(),
320+
Detail: latestBuildErr.Error(),
303321
})
304322
return
305323
}

codersdk/audit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
ResourceTypeTemplateVersion ResourceType = "template_version"
2020
ResourceTypeUser ResourceType = "user"
2121
ResourceTypeWorkspace ResourceType = "workspace"
22+
ResourceTypeWorkspaceBuild ResourceType = "workspace_build"
2223
ResourceTypeGitSSHKey ResourceType = "git_ssh_key"
2324
ResourceTypeAPIKey ResourceType = "api_key"
2425
)
@@ -35,6 +36,8 @@ func (r ResourceType) FriendlyString() string {
3536
return "user"
3637
case ResourceTypeWorkspace:
3738
return "workspace"
39+
case ResourceTypeWorkspaceBuild:
40+
return "workspace build"
3841
case ResourceTypeGitSSHKey:
3942
return "git ssh key"
4043
case ResourceTypeAPIKey:

enterprise/audit/table.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ var AuditableResources = auditMap(map[any]map[string]Action{
101101
"ttl": ActionTrack,
102102
"last_used_at": ActionIgnore,
103103
},
104+
&database.WorkspaceBuild{}: {
105+
"id": ActionIgnore, // Unimportant to the user
106+
"created_at": ActionIgnore, // Never changes.
107+
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
108+
"workspace_id": ActionTrack,
109+
"template_version_id": ActionTrack,
110+
"build_number": ActionIgnore, // Unimportant to the user
111+
"transition": ActionTrack,
112+
"initiator_id": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
113+
"provisioner_state": ActionIgnore, // Unimportant to the user
114+
"job_id": ActionIgnore, // Unimportant to the user
115+
"deadline": ActionIgnore, // Unimportant to the user
116+
"reason": ActionTrack,
117+
},
104118
})
105119

106120
// auditMap converts a map of struct pointers to a map of struct names as

0 commit comments

Comments
 (0)