Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
resolves #5269
  • Loading branch information
Kira-Pilot committed Dec 9, 2022
commit a577bfdb6908338b981a6b6f361d5c804204324d
10 changes: 6 additions & 4 deletions coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAudit
}

type AdditionalFields struct {
WorkspaceName string
BuildNumber string
WorkspaceName string
BuildNumber string
WorkspaceOwner string
}

func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog {
Expand Down Expand Up @@ -198,8 +199,9 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
if err != nil {
api.Logger.Error(ctx, "unmarshal additional fields", slog.Error(err))
resourceInfo := map[string]string{
"workspaceName": "unknown",
"buildNumber": "unknown",
"workspaceName": "unknown",
"buildNumber": "unknown",
"workspaceOwner": "unknown",
}
dblog.AdditionalFields, err = json.Marshal(resourceInfo)
api.Logger.Error(ctx, "marshal additional fields", slog.Error(err))
Expand Down
17 changes: 13 additions & 4 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,26 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)

// Create a new workspace for the currently authenticated user.
func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Request) {
workspaceResourceInfo := map[string]string{
"workspaceOwner": httpmw.UserParam(r).Username,
}
wriBytes, err := json.Marshal(workspaceResourceInfo)
if err != nil {
// log something
}

var (
ctx = r.Context()
organization = httpmw.OrganizationParam(r)
apiKey = httpmw.APIKey(r)
auditor = api.Auditor.Load()
user = httpmw.UserParam(r)
aReq, commitAudit = audit.InitRequest[database.Workspace](rw, &audit.RequestParams{
Audit: *auditor,
Log: api.Logger,
Request: r,
Action: database.AuditActionCreate,
Audit: *auditor,
Log: api.Logger,
Request: r,
Action: database.AuditActionCreate,
AdditionalFields: wriBytes,
})
)
defer commitAudit()
Expand Down
13 changes: 13 additions & 0 deletions site/src/components/AuditLogRow/AuditLogDescription.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
MockAuditLog,
MockAuditLogWithWorkspaceBuild,
MockWorkspaceCreateAuditLogForDifferentOwner,
} from "testHelpers/entities"
import { AuditLogDescription } from "./AuditLogDescription"
import { render } from "../../testHelpers/renderHelpers"
Expand Down Expand Up @@ -46,4 +47,16 @@ describe("AuditLogDescription", () => {
getByTextContent("TestUser stopped build for workspace workspace"),
).toBeDefined()
})
it("renders the correct string for a workspace created for a different owner", async () => {
render(
<AuditLogDescription
auditLog={MockWorkspaceCreateAuditLogForDifferentOwner}
/>,
)
expect(
getByTextContent(
`TestUser created workspace bruno-dev on behalf of ${MockWorkspaceCreateAuditLogForDifferentOwner.additional_fields.workspaceOwner}`,
),
).toBeDefined()
})
})
12 changes: 11 additions & 1 deletion site/src/components/AuditLogRow/AuditLogDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
)}
{auditLog.is_deleted && (
<span className={classes.deletedLabel}>
<> {t("auditLog:table.logRow.deletedLabel")}</>
<>{t("auditLog:table.logRow.deletedLabel")}</>
</span>
)}
{/* logs for workspaces created on behalf of other users indicate ownership in the description */}
{auditLog.additional_fields.workspaceOwner && (
<span>
<>
{t("auditLog:table.logRow.onBehalfOf", {
owner: auditLog.additional_fields.workspaceOwner,
})}
</>
</span>
)}
</span>
Expand Down
5 changes: 3 additions & 2 deletions site/src/i18n/en/auditLog.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"emptyPage": "No audit logs available on this page",
"noLogs": "No audit logs available",
"logRow": {
"deletedLabel": "(deleted)",
"deletedLabel": " (deleted)",
"ip": "IP: ",
"os": "OS: ",
"browser": "Browser: ",
"notAvailable": "Not available"
"notAvailable": "Not available",
"onBehalfOf": " on behalf of {{owner}}"
}
}
}
7 changes: 7 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,13 @@ export const MockAuditLog2: TypesGen.AuditLog = {
},
}

export const MockWorkspaceCreateAuditLogForDifferentOwner = {
...MockAuditLog,
additional_fields: {
workspaceOwner: "Member"
}
}

export const MockAuditLogWithWorkspaceBuild: TypesGen.AuditLog = {
...MockAuditLog,
id: "f90995bf-4a2b-4089-b597-e66e025e523e",
Expand Down