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 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
Prev Previous commit
Next Next commit
general cleanup
  • Loading branch information
Kira-Pilot committed Feb 7, 2023
commit c18235724e9520b67c88807751aa7d40bf24de29
91 changes: 0 additions & 91 deletions site/src/components/AuditLogRow/AuditLogDescription.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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"

const getByTextContent = (text: string) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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
Expand Up @@ -9,7 +9,6 @@ export const BuildAuditDescription: FC<{ auditLog: AuditLog }> = ({
}): JSX.Element => {
const { t } = useTranslation("auditLog")

// audit logs with a resource_type of workspace build use workspace name as a target
const workspaceName = auditLog.additional_fields?.workspace_name?.trim()
// workspaces can be started/stopped by a user, or kicked off automatically by Coder
const user =
Expand All @@ -20,18 +19,33 @@ export const BuildAuditDescription: FC<{ auditLog: AuditLog }> = ({

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.workspaceBuild"
i18nKey="table.logRow.description.unlinkedWorkspaceBuild"
values={{ user, action, workspaceName }}
>
{"{{user}}"}
<Link component={RouterLink} to={auditLog.resource_link}>
{"{{action}}"}
</Link>
workspace{"{{workspaceName}}"}
{"{{action}}"}workspace{"{{workspaceName}}"}
</Trans>
</span>
)
Expand Down
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'
27 changes: 18 additions & 9 deletions site/src/components/AuditLogRow/AuditLogRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import { UserAvatar } from "components/UserAvatar/UserAvatar"
import { useState } from "react"
import { PaletteIndex } from "theme/palettes"
import userAgentParser from "ua-parser-js"
import { AuditLogDiff } from "./AuditLogDiff"
import i18next from "i18next"
import { AuditLogDiff, determineGroupDiff } from "./AuditLogDiff"
import { useTranslation } from "react-i18next"
import { AuditLogDescription } from "./AuditLogDescription"
import { determineGroupDiff } from "./auditUtils"

const httpStatusColor = (httpStatus: number): PaletteIndex => {
if (httpStatus >= 300 && httpStatus < 500) {
Expand All @@ -41,14 +40,14 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
defaultIsDiffOpen = false,
}) => {
const styles = useStyles()
const { t } = i18next
const { t } = useTranslation("auditLog")
const [isDiffOpen, setIsDiffOpen] = useState(defaultIsDiffOpen)
const diffs = Object.entries(auditLog.diff)
const shouldDisplayDiff = diffs.length > 0
const { os, browser } = userAgentParser(auditLog.user_agent)
const displayBrowserInfo = browser.name
? `${browser.name} ${browser.version}`
: t("auditLog:table.logRow.notAvailable")
: t("table.logRow.notAvailable")

let auditDiff = auditLog.diff

Expand Down Expand Up @@ -110,6 +109,11 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
spacing={1}
>
<AuditLogDescription auditLog={auditLog} />
{auditLog.is_deleted && (
<span className={styles.deletedLabel}>
<>{t("table.logRow.deletedLabel")}</>
</span>
)}
<span className={styles.auditLogTime}>
{new Date(auditLog.time).toLocaleTimeString()}
</span>
Expand All @@ -119,23 +123,23 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
<Stack direction="row" spacing={1} alignItems="baseline">
{auditLog.ip && (
<span className={styles.auditLogInfo}>
<>{t("auditLog:table.logRow.ip")}</>
<>{t("table.logRow.ip")}</>
<strong>{auditLog.ip}</strong>
</span>
)}

<span className={styles.auditLogInfo}>
<>{t("auditLog:table.logRow.os")}</>
<>{t("table.logRow.os")}</>
<strong>
{os.name
? os.name
: // https://github.com/i18next/next-i18next/issues/1795
t<string>("auditLog:table.logRow.notAvailable")}
t<string>("table.logRow.notAvailable")}
</strong>
</span>

<span className={styles.auditLogInfo}>
<>{t("auditLog:table.logRow.browser")}</>
<>{t("table.logRow.browser")}</>
<strong>{displayBrowserInfo}</strong>
</span>
</Stack>
Expand Down Expand Up @@ -215,4 +219,9 @@ const useStyles = makeStyles((theme) => ({
paddingRight: 10,
fontWeight: 600,
},

deletedLabel: {
...theme.typography.caption,
color: theme.palette.text.secondary,
},
}))
12 changes: 7 additions & 5 deletions site/src/i18n/en/auditLog.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
"emptyPage": "No audit logs available on this page",
"noLogs": "No audit logs available",
"logRow": {
"description": {
"linkedWorkspaceBuild": "{{user}} <1>{{action}}</1> workspace <strong>{{workspaceName}}</strong>",
"unlinkedWorkspaceBuild": "{{user}} {{action}} workspace <strong>{{workspaceName}}</strong>",
"linkedAuditDescription": "{{truncatedDescription}} <1>{{target}}</1> {{onBehalfOf}}",
"unlinkedAuditDescription": "{{truncatedDescription}} {{target}} {{onBehalfOf}}"
},
"deletedLabel": " (deleted)",
"ip": "IP: ",
"os": "OS: ",
"browser": "Browser: ",
"onBehalfOf": " on behalf of {{owner}}",
"unknownUser": "an unknown user",
"workspaceBuild": "{{user}} <1>{{action}}</1> workspace <strong>{{workspaceName}}</strong>",
"auditDescription": "{{truncatedDescription}} <1>{{target}}</1>"
"browser": "Browser: "
}
},
"paywall": {
Expand Down