Skip to content

Commit a577bfd

Browse files
committed
resolves #5269
1 parent 92c5e97 commit a577bfd

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

coderd/audit.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAudit
161161
}
162162

163163
type AdditionalFields struct {
164-
WorkspaceName string
165-
BuildNumber string
164+
WorkspaceName string
165+
BuildNumber string
166+
WorkspaceOwner string
166167
}
167168

168169
func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog {
@@ -198,8 +199,9 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
198199
if err != nil {
199200
api.Logger.Error(ctx, "unmarshal additional fields", slog.Error(err))
200201
resourceInfo := map[string]string{
201-
"workspaceName": "unknown",
202-
"buildNumber": "unknown",
202+
"workspaceName": "unknown",
203+
"buildNumber": "unknown",
204+
"workspaceOwner": "unknown",
203205
}
204206
dblog.AdditionalFields, err = json.Marshal(resourceInfo)
205207
api.Logger.Error(ctx, "marshal additional fields", slog.Error(err))

coderd/workspaces.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,26 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
236236

237237
// Create a new workspace for the currently authenticated user.
238238
func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Request) {
239+
workspaceResourceInfo := map[string]string{
240+
"workspaceOwner": httpmw.UserParam(r).Username,
241+
}
242+
wriBytes, err := json.Marshal(workspaceResourceInfo)
243+
if err != nil {
244+
// log something
245+
}
246+
239247
var (
240248
ctx = r.Context()
241249
organization = httpmw.OrganizationParam(r)
242250
apiKey = httpmw.APIKey(r)
243251
auditor = api.Auditor.Load()
244252
user = httpmw.UserParam(r)
245253
aReq, commitAudit = audit.InitRequest[database.Workspace](rw, &audit.RequestParams{
246-
Audit: *auditor,
247-
Log: api.Logger,
248-
Request: r,
249-
Action: database.AuditActionCreate,
254+
Audit: *auditor,
255+
Log: api.Logger,
256+
Request: r,
257+
Action: database.AuditActionCreate,
258+
AdditionalFields: wriBytes,
250259
})
251260
)
252261
defer commitAudit()

site/src/components/AuditLogRow/AuditLogDescription.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
MockAuditLog,
33
MockAuditLogWithWorkspaceBuild,
4+
MockWorkspaceCreateAuditLogForDifferentOwner,
45
} from "testHelpers/entities"
56
import { AuditLogDescription } from "./AuditLogDescription"
67
import { render } from "../../testHelpers/renderHelpers"
@@ -46,4 +47,16 @@ describe("AuditLogDescription", () => {
4647
getByTextContent("TestUser stopped build for workspace workspace"),
4748
).toBeDefined()
4849
})
50+
it("renders the correct string for a workspace created for a different owner", async () => {
51+
render(
52+
<AuditLogDescription
53+
auditLog={MockWorkspaceCreateAuditLogForDifferentOwner}
54+
/>,
55+
)
56+
expect(
57+
getByTextContent(
58+
`TestUser created workspace bruno-dev on behalf of ${MockWorkspaceCreateAuditLogForDifferentOwner.additional_fields.workspaceOwner}`,
59+
),
60+
).toBeDefined()
61+
})
4962
})

site/src/components/AuditLogRow/AuditLogDescription.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
4545
)}
4646
{auditLog.is_deleted && (
4747
<span className={classes.deletedLabel}>
48-
<> {t("auditLog:table.logRow.deletedLabel")}</>
48+
<>{t("auditLog:table.logRow.deletedLabel")}</>
49+
</span>
50+
)}
51+
{/* logs for workspaces created on behalf of other users indicate ownership in the description */}
52+
{auditLog.additional_fields.workspaceOwner && (
53+
<span>
54+
<>
55+
{t("auditLog:table.logRow.onBehalfOf", {
56+
owner: auditLog.additional_fields.workspaceOwner,
57+
})}
58+
</>
4959
</span>
5060
)}
5161
</span>

site/src/i18n/en/auditLog.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
"emptyPage": "No audit logs available on this page",
99
"noLogs": "No audit logs available",
1010
"logRow": {
11-
"deletedLabel": "(deleted)",
11+
"deletedLabel": " (deleted)",
1212
"ip": "IP: ",
1313
"os": "OS: ",
1414
"browser": "Browser: ",
15-
"notAvailable": "Not available"
15+
"notAvailable": "Not available",
16+
"onBehalfOf": " on behalf of {{owner}}"
1617
}
1718
}
1819
}

site/src/testHelpers/entities.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,13 @@ export const MockAuditLog2: TypesGen.AuditLog = {
10201020
},
10211021
}
10221022

1023+
export const MockWorkspaceCreateAuditLogForDifferentOwner = {
1024+
...MockAuditLog,
1025+
additional_fields: {
1026+
workspaceOwner: "Member"
1027+
}
1028+
}
1029+
10231030
export const MockAuditLogWithWorkspaceBuild: TypesGen.AuditLog = {
10241031
...MockAuditLog,
10251032
id: "f90995bf-4a2b-4089-b597-e66e025e523e",

0 commit comments

Comments
 (0)