Skip to content

chore: Add Audit Log components and service to load from the API #3782

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 31 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b8fa378
Add basic auditXService code
BrunoQuaresma Aug 30, 2022
38955a8
Add base audit log component
BrunoQuaresma Aug 30, 2022
785f67b
Add basic diff
BrunoQuaresma Aug 31, 2022
7479538
Check emtpy diffs
BrunoQuaresma Aug 31, 2022
b30165a
Improve human readable message
BrunoQuaresma Aug 31, 2022
f9a7ed7
Move types
BrunoQuaresma Aug 31, 2022
9db3385
Extract components and add storybook
BrunoQuaresma Aug 31, 2022
294dceb
Fix status pill
BrunoQuaresma Aug 31, 2022
1b0ed59
Add tests to check if the audit logs are showing
BrunoQuaresma Aug 31, 2022
6cf89a4
Address PR review
BrunoQuaresma Aug 31, 2022
a5da88b
Add i18n
BrunoQuaresma Aug 31, 2022
0267d4e
Merge branch 'main' of github.com:coder/coder into bq/feat-3251
BrunoQuaresma Aug 31, 2022
a0ce84e
Fix audit log row to match the new API types
BrunoQuaresma Aug 31, 2022
b7ccb0f
Fix types and minor issues on mobile
BrunoQuaresma Aug 31, 2022
03eadb6
Handle errors
BrunoQuaresma Sep 1, 2022
77fa647
Add pagination
BrunoQuaresma Sep 1, 2022
dc1543c
Fix table alignment
BrunoQuaresma Sep 1, 2022
901c5f2
Add handler for /count
BrunoQuaresma Sep 1, 2022
ebe2792
Update site/src/xServices/audit/auditXService.ts
BrunoQuaresma Sep 1, 2022
75f19b5
Merge branch 'main' of github.com:coder/coder into bq/feat-3251
BrunoQuaresma Sep 7, 2022
cfe9d7d
Fix types
BrunoQuaresma Sep 7, 2022
f1ba4ac
Fix empty state
BrunoQuaresma Sep 7, 2022
8f1bce7
Display user agent info
BrunoQuaresma Sep 7, 2022
b2fd91b
Merge branch 'bq/feat-3251' of github.com:coder/coder into bq/feat-3251
BrunoQuaresma Sep 7, 2022
b72c3a9
Fix audit log pagination
BrunoQuaresma Sep 7, 2022
0e052ae
Remove console log
BrunoQuaresma Sep 7, 2022
acaa45a
Fix handlers and format
BrunoQuaresma Sep 7, 2022
168cb16
Remove one test
BrunoQuaresma Sep 7, 2022
c48a72e
Update user agent on mocks
BrunoQuaresma Sep 7, 2022
c398be2
Fix switch color on workspace schedule form
BrunoQuaresma Sep 7, 2022
6d09dfa
Fix button
BrunoQuaresma Sep 7, 2022
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
6 changes: 3 additions & 3 deletions codersdk/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const (
type AuditDiff map[string]AuditDiffField

type AuditDiffField struct {
Old any
New any
Secret bool
Old any `json:"old"`
New any `json:"new"`
Secret bool `json:"secret"`
}

type AuditLog struct {
Expand Down
24 changes: 10 additions & 14 deletions site/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RequirePermission } from "components/RequirePermission/RequirePermissio
import { SetupPage } from "pages/SetupPage/SetupPage"
import { TemplateSettingsPage } from "pages/TemplateSettingsPage/TemplateSettingsPage"
import { FC, lazy, Suspense, useContext } from "react"
import { Navigate, Route, Routes } from "react-router-dom"
import { Route, Routes } from "react-router-dom"
import { selectPermissions } from "xServices/auth/authSelectors"
import { selectFeatureVisibility } from "xServices/entitlements/entitlementsSelectors"
import { XServiceContext } from "xServices/StateContext"
Expand Down Expand Up @@ -139,19 +139,15 @@ export const AppRouter: FC = () => {
<Route
index
element={
process.env.NODE_ENV === "production" ? (
<Navigate to="/workspaces" />
) : (
<AuthAndFrame>
<RequirePermission
isFeatureVisible={
featureVisibility[FeatureNames.AuditLog] && !!permissions?.viewAuditLog
}
>
<AuditPage />
</RequirePermission>
</AuthAndFrame>
)
<AuthAndFrame>
<RequirePermission
isFeatureVisible={
featureVisibility[FeatureNames.AuditLog] && !!permissions?.viewAuditLog
}
>
<AuditPage />
</RequirePermission>
</AuthAndFrame>
}
></Route>
</Route>
Expand Down
15 changes: 15 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,18 @@ export const getEntitlements = async (): Promise<TypesGen.Entitlements> => {
const response = await axios.get("/api/v2/entitlements")
return response.data
}

interface GetAuditLogsOptions {
limit: number
offset: number
}

export const getAuditLogs = async (options: GetAuditLogsOptions): Promise<TypesGen.AuditLog[]> => {
const response = await axios.get(`/api/v2/audit?limit=${options.limit}&offset=${options.offset}`)
return response.data
}

export const getAuditLogsCount = async (): Promise<number> => {
const response = await axios.get(`/api/v2/audit/count`)
return response.data
}
6 changes: 3 additions & 3 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export type AuditDiff = Record<string, AuditDiffField>
// From codersdk/audit.go
export interface AuditDiffField {
// eslint-disable-next-line
readonly Old: any
readonly old: any
// eslint-disable-next-line
readonly New: any
readonly Secret: boolean
readonly new: any
readonly secret: boolean
}

// From codersdk/audit.go
Expand Down
107 changes: 107 additions & 0 deletions site/src/components/AuditLogRow/AuditLogDiff.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { makeStyles } from "@material-ui/core/styles"
import { AuditLog } from "api/typesGenerated"
import { colors } from "theme/colors"
import { combineClasses } from "util/combineClasses"

const getDiffValue = (value: number | string | boolean) => {
if (typeof value === "string") {
return `"${value}"`
}

return value.toString()
}

export const AuditLogDiff: React.FC<{ diff: AuditLog["diff"] }> = ({ diff }) => {
const styles = useStyles()
const diffEntries = Object.entries(diff)

return (
<div className={styles.diff}>
<div className={combineClasses([styles.diffColumn, styles.diffOld])}>
{diffEntries.map(([attrName, valueDiff], index) => (
<div key={attrName} className={styles.diffRow}>
<div className={styles.diffLine}>{index + 1}</div>
<div className={styles.diffIcon}>-</div>
<div>
{attrName}:{" "}
<span className={combineClasses([styles.diffValue, styles.diffValueOld])}>
{getDiffValue(valueDiff.old)}
</span>
</div>
</div>
))}
</div>
<div className={combineClasses([styles.diffColumn, styles.diffNew])}>
{diffEntries.map(([attrName, valueDiff], index) => (
<div key={attrName} className={styles.diffRow}>
<div className={styles.diffLine}>{index + 1}</div>
<div className={styles.diffIcon}>+</div>
<div>
{attrName}:{" "}
<span className={combineClasses([styles.diffValue, styles.diffValueNew])}>
{getDiffValue(valueDiff.new)}
</span>
</div>
</div>
))}
</div>
</div>
)
}

const useStyles = makeStyles((theme) => ({
diff: {
display: "flex",
alignItems: "flex-start",
fontSize: theme.typography.body2.fontSize,
borderTop: `1px solid ${theme.palette.divider}`,
},

diffColumn: {
flex: 1,
paddingTop: theme.spacing(2),
paddingBottom: theme.spacing(2.5),
lineHeight: "160%",
},

diffOld: {
backgroundColor: theme.palette.error.dark,
color: theme.palette.error.contrastText,
},

diffRow: {
display: "flex",
alignItems: "baseline",
},

diffLine: {
opacity: 0.5,
width: theme.spacing(8),
textAlign: "right",
flexShrink: 0,
},

diffIcon: {
width: theme.spacing(4),
textAlign: "center",
fontSize: theme.typography.body1.fontSize,
},

diffNew: {
backgroundColor: theme.palette.success.dark,
color: theme.palette.success.contrastText,
},

diffValue: {
padding: 1,
borderRadius: theme.shape.borderRadius / 2,
},

diffValueOld: {
backgroundColor: colors.red[12],
},

diffValueNew: {
backgroundColor: colors.green[12],
},
}))
40 changes: 40 additions & 0 deletions site/src/components/AuditLogRow/AuditLogRow.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Table from "@material-ui/core/Table"
import TableBody from "@material-ui/core/TableBody"
import TableCell from "@material-ui/core/TableCell"
import TableContainer from "@material-ui/core/TableContainer"
import TableHead from "@material-ui/core/TableHead"
import TableRow from "@material-ui/core/TableRow"
import { ComponentMeta, Story } from "@storybook/react"
import { MockAuditLog, MockAuditLog2 } from "testHelpers/entities"
import { AuditLogRow, AuditLogRowProps } from "./AuditLogRow"

export default {
title: "components/AuditLogRow",
component: AuditLogRow,
} as ComponentMeta<typeof AuditLogRow>

const Template: Story<AuditLogRowProps> = (args) => (
<TableContainer>
<Table>
<TableHead>
<TableRow>
<TableCell style={{ paddingLeft: 32 }}>Logs</TableCell>
</TableRow>
</TableHead>
<TableBody>
<AuditLogRow {...args} />
</TableBody>
</Table>
</TableContainer>
)

export const NoDiff = Template.bind({})
NoDiff.args = {
auditLog: MockAuditLog,
}

export const WithDiff = Template.bind({})
WithDiff.args = {
auditLog: MockAuditLog2,
defaultIsDiffOpen: true,
}
Loading