Skip to content

feat: add preset filter for audit logins #6001

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

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
auditing login
  • Loading branch information
Kira-Pilot committed Feb 1, 2023
commit a75392ef29915d46d589d6759f743ff481dae3e9
22 changes: 22 additions & 0 deletions site/src/components/AuditLogRow/AuditLogDescription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import {
MockAuditLog,
MockAuditLogWithWorkspaceBuild,
MockWorkspaceCreateAuditLogForDifferentOwner,
MockAuditLogSuccessfulLogin,
MockAuditLogUnsuccessfulLoginKnownUser,
MockAuditLogUnsuccessfulLoginUnknownUser,
} from "testHelpers/entities"
import { AuditLogDescription } from "./AuditLogDescription"
import { AuditLogRow } from "./AuditLogRow"
import { render } from "../../testHelpers/renderHelpers"
import { screen } from "@testing-library/react"

Expand Down Expand Up @@ -59,4 +63,22 @@ describe("AuditLogDescription", () => {
),
).toBeDefined()
})
it("renders the correct string for successful login", async () => {
render(<AuditLogRow auditLog={MockAuditLogSuccessfulLogin} />)
expect(getByTextContent(`TestUser logged in`)).toBeDefined()
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()
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()
const statusPill = screen.getByRole("status")
expect(statusPill).toHaveTextContent("401")
})
})
6 changes: 4 additions & 2 deletions site/src/components/AuditLogRow/AuditLogDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
const { t } = i18next

let target = auditLog.resource_target.trim()
let user = auditLog.user?.username.trim()
let user = auditLog.user
? auditLog.user.username.trim()
: t("auditLog:table.logRow.unknownUser")

if (auditLog.resource_type === "workspace_build") {
// audit logs with a resource_type of workspace build use workspace name as a target
Expand All @@ -22,7 +24,7 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
auditLog.additional_fields?.build_reason &&
auditLog.additional_fields?.build_reason !== "initiator"
? t("auditLog:table.logRow.buildReason")
: auditLog.user?.username.trim()
: user
}

// SSH key entries have no links
Expand Down
2 changes: 1 addition & 1 deletion site/src/components/AuditLogRow/AuditLogRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
className={styles.fullWidth}
>
<UserAvatar
username={auditLog.user?.username ?? ""}
username={auditLog.user?.username ?? "?"}
avatarURL={auditLog.user?.avatar_url}
/>

Expand Down
3 changes: 2 additions & 1 deletion site/src/i18n/en/auditLog.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"browser": "Browser: ",
"notAvailable": "Not available",
"onBehalfOf": " on behalf of {{owner}}",
"buildReason": "Coder automatically"
"buildReason": "Coder automatically",
"unknownUser": "An unknown user"
}
},
"paywall": {
Expand Down
20 changes: 20 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,26 @@ export const MockAuditLogGitSSH: TypesGen.AuditLog = {
},
}

export const MockAuditLogSuccessfulLogin: TypesGen.AuditLog = {
...MockAuditLog,
resource_type: "api_key",
resource_target: "",
action: "login",
status_code: 201,
description: "{user} logged in",
}

export const MockAuditLogUnsuccessfulLoginKnownUser: TypesGen.AuditLog = {
...MockAuditLogSuccessfulLogin,
status_code: 401,
}

export const MockAuditLogUnsuccessfulLoginUnknownUser: TypesGen.AuditLog = {
...MockAuditLogSuccessfulLogin,
status_code: 401,
user: undefined,
}

export const MockWorkspaceQuota: TypesGen.WorkspaceQuota = {
credits_consumed: 0,
budget: 100,
Expand Down