Skip to content

adding workspace_build resource #4636

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 13 commits into from
Oct 25, 2022
Merged
Prev Previous commit
Next Next commit
adding workspace name to string
  • Loading branch information
Kira-Pilot committed Oct 21, 2022
commit 69cdf39aa20d3feefe9e99e255dfa5ed1b206c92
9 changes: 8 additions & 1 deletion coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ func convertAuditLog(dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog {
}
}

type WorkspaceResourceInfo struct {
WorkspaceName string
}

func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
str := fmt.Sprintf("{user} %s %s",
codersdk.AuditAction(alog.Action).FriendlyString(),
Expand All @@ -230,7 +234,10 @@ func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
// "{user} started workspace build for workspace {target}"
// where target is a workspace instead of the workspace build
if alog.ResourceType == database.ResourceTypeWorkspaceBuild {
str += " for workspace"
workspace_bytes := []byte(alog.AdditionalFields)
var workspaceResourceInfo WorkspaceResourceInfo
json.Unmarshal(workspace_bytes, &workspaceResourceInfo)
str += " for workspace " + workspaceResourceInfo.WorkspaceName
Copy link
Member Author

Choose a reason for hiding this comment

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

I would prefer not to add this here and instead to use the ResourceTarget function.

}

// We don't display the name for git ssh keys. It's fairly long and doesn't
Expand Down
10 changes: 6 additions & 4 deletions coderd/audit/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type RequestParams struct {
Audit Auditor
Log slog.Logger

Request *http.Request
Action database.AuditAction
Request *http.Request
Action database.AuditAction
AdditionalFields json.RawMessage
}

type Request[T Auditable] struct {
Expand All @@ -44,7 +45,8 @@ func ResourceTarget[T Auditable](tgt T) string {
case database.Workspace:
return typed.Name
case database.WorkspaceBuild:
return string(typed.Transition)
// this isn't used
return string(typed.BuildNumber)
Copy link
Member Author

Choose a reason for hiding this comment

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

Ug tell me there's a better way.

case database.GitSSHKey:
return typed.PublicKey
default:
Expand Down Expand Up @@ -147,7 +149,7 @@ func InitRequest[T Auditable](w http.ResponseWriter, p *RequestParams) (*Request
Diff: diffRaw,
StatusCode: int32(sw.Status),
RequestID: httpmw.RequestID(p.Request),
AdditionalFields: json.RawMessage("{}"),
AdditionalFields: p.AdditionalFields,
})
if err != nil {
p.Log.Error(logCtx, "export audit log", slog.Error(err))
Expand Down
17 changes: 13 additions & 4 deletions coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,21 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
auditAction = database.AuditActionWrite
}

// We pass the workspace name to the Auditor so that it
// can form a friendly string for the user.
workspaceResourceInfo := map[string]string{
"workspaceName": workspace.Name,
}

wri_bytes, _ := json.Marshal(workspaceResourceInfo)

var (
aReq, commitAudit = audit.InitRequest[database.WorkspaceBuild](rw, &audit.RequestParams{
Audit: *auditor,
Log: api.Logger,
Request: r,
Action: auditAction,
Audit: *auditor,
Log: api.Logger,
Request: r,
Action: auditAction,
AdditionalFields: wri_bytes,
})
)

Expand Down