Skip to content

feature: gate audit log by permissions #3464

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 5 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions coderd/rbac/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var (
// Should be able to read all template details, even in orgs they
// are not in.
ResourceTemplate: {ActionRead},
ResourceAuditLog: {ActionRead},
}),
}
},
Expand Down
6 changes: 6 additions & 0 deletions coderd/rbac/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ var (
Type: "workspace",
}

// ResourceAuditLog
// read = access audit log
ResourceAuditLog = Object{
Type: "audit_log",
}

// ResourceTemplate CRUD. Org owner only.
// create/delete = Make or delete a new template
// update = Update the template, make new template versions
Expand Down
230 changes: 119 additions & 111 deletions site/src/AppRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FC, lazy, Suspense } from "react"
import { useActor } from "@xstate/react"
import { FC, lazy, Suspense, useContext } from "react"
import { Navigate, Route, Routes } from "react-router-dom"
import { XServiceContext } from "xServices/StateContext"
import { AuthAndFrame } from "./components/AuthAndFrame/AuthAndFrame"
import { RequireAuth } from "./components/RequireAuth/RequireAuth"
import { SettingsLayout } from "./components/SettingsLayout/SettingsLayout"
Expand Down Expand Up @@ -27,167 +29,173 @@ const WorkspacesPage = lazy(() => import("./pages/WorkspacesPage/WorkspacesPage"
const CreateWorkspacePage = lazy(() => import("./pages/CreateWorkspacePage/CreateWorkspacePage"))
const AuditPage = lazy(() => import("./pages/AuditPage/AuditPage"))

export const AppRouter: FC = () => (
<Suspense fallback={<></>}>
<Routes>
<Route
index
element={
<RequireAuth>
<IndexPage />
</RequireAuth>
}
/>
export const AppRouter: FC = () => {
const xServices = useContext(XServiceContext)
const [authState] = useActor(xServices.authXService)
const { permissions } = authState.context
Copy link
Contributor

@presleyp presleyp Aug 10, 2022

Choose a reason for hiding this comment

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

This is probably a good place to use useSelector instead of useActor so that we don't get re-renders for anything except permissions. In fact, I think my plan was to do that within a wrapper component so that it only gets accessed on pages that need it - see RequireLicense here https://github.com/coder/coder/pull/3008/files#diff-8b4f166561cf1e9c08183827ef490e7741d6e79022122df7d835292bc3b3d0e7

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm realizing now that I was thinking about licensing instead of role-based permissions, but maybe it's still a good idea? Up to you!

Copy link
Member Author

Choose a reason for hiding this comment

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

That is a good idea! I will play around with the wrapper component when I get to licensing, and make sure permissions go in there, too, if possible.


<Route path="login" element={<LoginPage />} />
<Route path="healthz" element={<HealthzPage />} />
<Route
path="cli-auth"
element={
<RequireAuth>
<CliAuthenticationPage />
</RequireAuth>
}
/>

<Route path="workspaces">
return (
<Suspense fallback={<></>}>
<Routes>
<Route
index
element={
<AuthAndFrame>
<WorkspacesPage />
</AuthAndFrame>
<RequireAuth>
<IndexPage />
</RequireAuth>
}
/>
</Route>

<Route path="templates">
<Route path="login" element={<LoginPage />} />
<Route path="healthz" element={<HealthzPage />} />
<Route
index
path="cli-auth"
element={
<AuthAndFrame>
<TemplatesPage />
</AuthAndFrame>
<RequireAuth>
<CliAuthenticationPage />
</RequireAuth>
}
/>

<Route path=":template">
<Route path="workspaces">
<Route
index
element={
<AuthAndFrame>
<TemplatePage />
<WorkspacesPage />
</AuthAndFrame>
}
/>
<Route
path="workspace"
element={
<RequireAuth>
<CreateWorkspacePage />
</RequireAuth>
}
/>
</Route>
</Route>

<Route path="users">
<Route
index
element={
<AuthAndFrame>
<UsersPage />
</AuthAndFrame>
}
/>
<Route
path="create"
element={
<RequireAuth>
<CreateUserPage />
</RequireAuth>
}
/>
</Route>

{/* REMARK: Route under construction
Eventually, we should gate this page
with permissions and licensing */}
<Route path="/audit">
<Route
index
element={
process.env.NODE_ENV === "production" ? (
<Navigate to="/workspaces" />
) : (
<Route path="templates">
<Route
index
element={
<AuthAndFrame>
<AuditPage />
<TemplatesPage />
</AuthAndFrame>
)
}
></Route>
</Route>
}
/>

<Route path="settings" element={<SettingsLayout />}>
<Route path="account" element={<AccountPage />} />
<Route path="security" element={<SecurityPage />} />
<Route path="ssh-keys" element={<SSHKeysPage />} />
</Route>
<Route path=":template">
<Route
index
element={
<AuthAndFrame>
<TemplatePage />
</AuthAndFrame>
}
/>
<Route
path="workspace"
element={
<RequireAuth>
<CreateWorkspacePage />
</RequireAuth>
}
/>
</Route>
</Route>

<Route path="/@:username">
<Route path=":workspace">
<Route path="users">
<Route
index
element={
<AuthAndFrame>
<WorkspacePage />
<UsersPage />
</AuthAndFrame>
}
/>
<Route
path="schedule"
path="create"
element={
<RequireAuth>
<WorkspaceSchedulePage />
<CreateUserPage />
</RequireAuth>
}
/>
</Route>

{/* REMARK: Route under construction
Eventually, we should gate this page
with permissions and licensing */}
<Route path="/audit">
<Route
path="terminal"
index
element={
<RequireAuth>
<TerminalPage />
</RequireAuth>
process.env.NODE_ENV === "production" || !permissions?.viewAuditLog ? (
<Navigate to="/workspaces" />
) : (
<AuthAndFrame>
<AuditPage />
</AuthAndFrame>
)
}
/>
></Route>
</Route>

<Route path="settings" element={<SettingsLayout />}>
<Route path="account" element={<AccountPage />} />
<Route path="security" element={<SecurityPage />} />
<Route path="ssh-keys" element={<SSHKeysPage />} />
</Route>

<Route path="apps">
<Route path="/@:username">
<Route path=":workspace">
<Route
path=":app/*"
index
element={
<AuthAndFrame>
<WorkspaceAppErrorPage />
<WorkspacePage />
</AuthAndFrame>
}
/>
</Route>
<Route
path="schedule"
element={
<RequireAuth>
<WorkspaceSchedulePage />
</RequireAuth>
}
/>

<Route
path="builds/:buildNumber"
element={
<AuthAndFrame>
<WorkspaceBuildPage />
</AuthAndFrame>
}
/>
<Route
path="terminal"
element={
<RequireAuth>
<TerminalPage />
</RequireAuth>
}
/>

<Route path="apps">
<Route
path=":app/*"
element={
<AuthAndFrame>
<WorkspaceAppErrorPage />
</AuthAndFrame>
}
/>
</Route>

<Route
path="builds/:buildNumber"
element={
<AuthAndFrame>
<WorkspaceBuildPage />
</AuthAndFrame>
}
/>
</Route>
</Route>
</Route>

{/* Using path="*"" means "match anything", so this route
{/* Using path="*"" means "match anything", so this route
acts like a catch-all for URLs that we don't have explicit
routes for. */}
<Route path="*" element={<NotFoundPage />} />
</Routes>
</Suspense>
)
<Route path="*" element={<NotFoundPage />} />
</Routes>
</Suspense>
)
}
10 changes: 8 additions & 2 deletions site/src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import { NavbarView } from "../NavbarView/NavbarView"
export const Navbar: React.FC = () => {
const xServices = useContext(XServiceContext)
const [authState, authSend] = useActor(xServices.authXService)
const { me } = authState.context
const { me, permissions } = authState.context
const onSignOut = () => authSend("SIGN_OUT")

return <NavbarView user={me} onSignOut={onSignOut} />
return (
<NavbarView
user={me}
onSignOut={onSignOut}
canViewAuditLog={permissions?.viewAuditLog ?? false}
/>
)
}
Loading