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

Skip to content

chore: change build audit log string to be clearer #6093

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 10 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 2 additions & 15 deletions coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
StatusCode: dblog.StatusCode,
AdditionalFields: dblog.AdditionalFields,
User: user,
Description: auditLogDescription(dblog, additionalFields),
Description: auditLogDescription(dblog),
ResourceLink: resourceLink,
IsDeleted: isDeleted,
}
}

func auditLogDescription(alog database.GetAuditLogsOffsetRow, additionalFields audit.AdditionalFields) string {
func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
str := fmt.Sprintf("{user} %s",
codersdk.AuditAction(alog.Action).Friendly(),
)
Expand All @@ -261,19 +261,6 @@ func auditLogDescription(alog database.GetAuditLogsOffsetRow, additionalFields a
return str
}

// Strings for starting/stopping workspace builds follow the below format:
// "{user | 'Coder automatically'} started build #{build_number} for workspace {target}"
// where target is a workspace (name) instead of a workspace build
// passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35
if alog.ResourceType == database.ResourceTypeWorkspaceBuild && alog.Action != database.AuditActionDelete {
if len(additionalFields.BuildNumber) == 0 {
str += " build for"
} else {
str += fmt.Sprintf(" build #%s for",
additionalFields.BuildNumber)
}
}

// We don't display the name (target) for git ssh keys. It's fairly long and doesn't
// make too much sense to display.
if alog.ResourceType == database.ResourceTypeGitSshKey {
Expand Down
2 changes: 1 addition & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@xstate/inspect": "0.6.5",
"@xstate/react": "3.0.1",
"axios": "0.26.1",
"canvas": "^2.11.0",
Copy link
Member Author

Choose a reason for hiding this comment

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

We're getting changes here because I ran into this issue and had to re-add canvas.

"chart.js": "3.9.1",
"chartjs-adapter-date-fns": "3.0.0",
"color-convert": "2.0.1",
Expand Down Expand Up @@ -110,7 +111,6 @@
"@typescript-eslint/eslint-plugin": "5.50.0",
"@typescript-eslint/parser": "5.45.1",
"@xstate/cli": "0.3.0",
"canvas": "2.10.0",
"chromatic": "6.15.0",
"eslint": "8.33.0",
"eslint-config-prettier": "8.5.0",
Expand Down
80 changes: 0 additions & 80 deletions site/src/components/AuditLogRow/AuditLogDescription.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import {
MockAuditLogUnsuccessfulLoginUnknownUser,
} from "testHelpers/entities"
import { AuditLogDescription } from "./AuditLogDescription"
import { AuditLogRow } from "./AuditLogRow"
import { render } from "../../testHelpers/renderHelpers"
import { AuditLogRow } from "../AuditLogRow"
Copy link
Member Author

Choose a reason for hiding this comment

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

These tests aren't perfect - I was having difficulty testing strings with an embedded Link. I will return at a later time and maybe turn these into stories or create a custom matcher.

import { render } from "testHelpers/renderHelpers"
import { screen } from "@testing-library/react"
import { i18n } from "i18n"

const t = (str: string, variables?: Record<string, unknown>) =>
i18n.t<string>(str, variables)

const getByTextContent = (text: string) => {
return screen.getByText((_, element) => {
Expand All @@ -25,17 +29,14 @@ describe("AuditLogDescription", () => {
it("renders the correct string for a workspace create audit log", async () => {
render(<AuditLogDescription auditLog={MockAuditLog} />)

expect(
getByTextContent("TestUser created workspace bruno-dev"),
).toBeDefined()
expect(screen.getByText("TestUser created workspace")).toBeDefined()
expect(screen.getByText("bruno-dev")).toBeDefined()
})

it("renders the correct string for a workspace_build stop audit log", async () => {
render(<AuditLogDescription auditLog={MockAuditLogWithWorkspaceBuild} />)

expect(
getByTextContent("TestUser stopped build for workspace test2"),
).toBeDefined()
expect(getByTextContent("TestUser stopped workspace test2")).toBeDefined()
})

it("renders the correct string for a workspace_build audit log with a duplicate word", async () => {
Expand All @@ -48,7 +49,7 @@ describe("AuditLogDescription", () => {
render(<AuditLogDescription auditLog={AuditLogWithRepeat} />)

expect(
getByTextContent("TestUser stopped build for workspace workspace"),
getByTextContent("TestUser stopped workspace workspace"),
).toBeDefined()
})
it("renders the correct string for a workspace created for a different owner", async () => {
Expand All @@ -57,27 +58,68 @@ describe("AuditLogDescription", () => {
auditLog={MockWorkspaceCreateAuditLogForDifferentOwner}
/>,
)

expect(
getByTextContent(
`TestUser created workspace bruno-dev on behalf of ${MockWorkspaceCreateAuditLogForDifferentOwner.additional_fields.workspace_owner}`,
screen.getByText(
`on behalf of ${MockWorkspaceCreateAuditLogForDifferentOwner.additional_fields.workspace_owner}`,
{ exact: false },
),
).toBeDefined()
})
it("renders the correct string for successful login", async () => {
render(<AuditLogRow auditLog={MockAuditLogSuccessfulLogin} />)
expect(getByTextContent(`TestUser logged in`)).toBeDefined()

expect(
screen.getByText(
t("auditLog:table.logRow.description.unlinkedAuditDescription", {
truncatedDescription: `${MockAuditLogSuccessfulLogin.user?.username} logged in`,
target: "",
onBehalfOf: undefined,
})
.replace(/<[^>]*>/g, " ")
.replace(/\s{2,}/g, " ")
.trim(),
),
).toBeInTheDocument()

const statusPill = screen.getByRole("status")
expect(statusPill).toHaveTextContent("201")
})
it("renders the correct string for unsuccessful login for a known user", async () => {
render(<AuditLogRow auditLog={MockAuditLogUnsuccessfulLoginKnownUser} />)
expect(getByTextContent(`TestUser logged in`)).toBeDefined()

expect(
screen.getByText(
t("auditLog:table.logRow.description.unlinkedAuditDescription", {
truncatedDescription: `${MockAuditLogUnsuccessfulLoginKnownUser.user?.username} logged in`,
target: "",
onBehalfOf: undefined,
})
.replace(/<[^>]*>/g, " ")
.replace(/\s{2,}/g, " ")
.trim(),
),
).toBeInTheDocument()

const statusPill = screen.getByRole("status")
expect(statusPill).toHaveTextContent("401")
})
it("renders the correct string for unsuccessful login for an unknown user", async () => {
render(<AuditLogRow auditLog={MockAuditLogUnsuccessfulLoginUnknownUser} />)
expect(getByTextContent(`an unknown user logged in`)).toBeDefined()

expect(
screen.getByText(
t("auditLog:table.logRow.description.unlinkedAuditDescription", {
truncatedDescription: "an unknown user logged in",
target: "",
onBehalfOf: undefined,
})
.replace(/<[^>]*>/g, " ")
.replace(/\s{2,}/g, " ")
.trim(),
),
).toBeInTheDocument()

const statusPill = screen.getByRole("status")
expect(statusPill).toHaveTextContent("401")
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { FC } from "react"
import { AuditLog } from "api/typesGenerated"
import { Link as RouterLink } from "react-router-dom"
import Link from "@material-ui/core/Link"
import { Trans, useTranslation } from "react-i18next"
import { BuildAuditDescription } from "./BuildAuditDescription"

export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
auditLog,
}): JSX.Element => {
const { t } = useTranslation("auditLog")

let target = auditLog.resource_target.trim()
const user = auditLog.user ? auditLog.user.username.trim() : "an unknown user"

if (auditLog.resource_type === "workspace_build") {
return <BuildAuditDescription auditLog={auditLog} />
}

// SSH key entries have no links
if (auditLog.resource_type === "git_ssh_key") {
target = ""
}

const truncatedDescription = auditLog.description
.replace("{user}", `${user}`)
.replace("{target}", "")

// logs for workspaces created on behalf of other users indicate ownership in the description
const onBehalfOf =
auditLog.additional_fields.workspace_owner &&
auditLog.additional_fields.workspace_owner !== "unknown" &&
auditLog.additional_fields.workspace_owner !== auditLog.user?.username
? `on behalf of ${auditLog.additional_fields.workspace_owner}`
: ""

if (auditLog.resource_link) {
return (
<span>
<Trans
t={t}
i18nKey="table.logRow.description.linkedAuditDescription"
values={{ truncatedDescription, target, onBehalfOf }}
>
{"{{truncatedDescription}}"}
<Link component={RouterLink} to={auditLog.resource_link}>
<strong>{"{{target}}"}</strong>
</Link>
{"{{onBehalfOf}}"}
</Trans>
</span>
)
}

return (
<span>
<Trans
t={t}
i18nKey="table.logRow.description.unlinkedAuditDescription"
values={{ truncatedDescription, target, onBehalfOf }}
>
{"{{truncatedDescription}}"}
<strong>{"{{target}}"}</strong>
{"{{onBehalfOf}}"}
</Trans>
</span>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Trans, useTranslation } from "react-i18next"
import { AuditLog } from "api/typesGenerated"
import { FC } from "react"
import { Link as RouterLink } from "react-router-dom"
import Link from "@material-ui/core/Link"

export const BuildAuditDescription: FC<{ auditLog: AuditLog }> = ({
auditLog,
}): JSX.Element => {
const { t } = useTranslation("auditLog")

const workspaceName = auditLog.additional_fields?.workspace_name?.trim()
// workspaces can be started/stopped by a user, or kicked off automatically by Coder
const user =
auditLog.additional_fields?.build_reason &&
auditLog.additional_fields?.build_reason !== "initiator"
? "Coder automatically"
: auditLog.user?.username.trim()

const action = auditLog.action === "start" ? "started" : "stopped"

if (auditLog.resource_link) {
return (
<span>
<Trans
t={t}
i18nKey="table.logRow.description.linkedWorkspaceBuild"
values={{ user, action, workspaceName }}
>
{"{{user}}"}
<Link component={RouterLink} to={auditLog.resource_link}>
{"{{action}}"}
</Link>
workspace{"{{workspaceName}}"}
</Trans>
</span>
)
}

return (
<span>
<Trans
t={t}
i18nKey="table.logRow.description.unlinkedWorkspaceBuild"
values={{ user, action, workspaceName }}
>
{"{{user}}"}
{"{{action}}"}workspace{"{{workspaceName}}"}
</Trans>
</span>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AuditLogDescription } from "./AuditLogDescription"
2 changes: 2 additions & 0 deletions site/src/components/AuditLogRow/AuditLogDiff/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AuditLogDiff } from "./AuditLogDiff"
export { determineGroupDiff } from "./auditUtils"
Loading