This is an automated email from the ASF dual-hosted git repository. lahirujayathilake pushed a commit to branch custos-apps in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
commit cbdb9df38591df320c69ea28650a7a119e4a323e Author: lahiruj <[email protected]> AuthorDate: Tue Nov 10 02:32:19 2026 -0500 Add the admin Apps page --- web/src/app/(portal)/admin/apps/page.tsx | 27 +++ .../core/apps/__tests__/TokenReveal.test.tsx | 44 +++++ web/src/features/core/apps/api.ts | 64 +++++++ web/src/features/core/apps/components/AppsView.tsx | 171 +++++++++++++++++++ .../core/apps/components/CreateAppDialog.tsx | 189 +++++++++++++++++++++ .../core/apps/components/NewSecretDialog.tsx | 79 +++++++++ .../features/core/apps/components/TokenReveal.tsx | 58 +++++++ web/src/features/core/apps/queries.ts | 54 ++++++ web/src/mocks/handlers.ts | 2 + web/src/mocks/handlers/apps.ts | 81 +++++++++ web/src/mocks/handlers/privileges.ts | 1 + web/src/shared/casl/abilities.ts | 1 + web/src/shared/layout/nav.ts | 8 + web/tests/fixtures/auth.ts | 1 + 14 files changed, 780 insertions(+) diff --git a/web/src/app/(portal)/admin/apps/page.tsx b/web/src/app/(portal)/admin/apps/page.tsx new file mode 100644 index 000000000..44b69afc6 --- /dev/null +++ b/web/src/app/(portal)/admin/apps/page.tsx @@ -0,0 +1,27 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import type { Metadata } from "next"; +import { AppsView } from "@/features/core/apps/components/AppsView"; + +export const metadata: Metadata = { + title: "Apps", +}; + +export default function AdminAppsPage() { + return <AppsView />; +} diff --git a/web/src/features/core/apps/__tests__/TokenReveal.test.tsx b/web/src/features/core/apps/__tests__/TokenReveal.test.tsx new file mode 100644 index 000000000..5799103c3 --- /dev/null +++ b/web/src/features/core/apps/__tests__/TokenReveal.test.tsx @@ -0,0 +1,44 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import { Dialog, DialogContent } from "@/shared/ui/dialog"; +import { TokenReveal } from "../components/TokenReveal"; + +describe("TokenReveal", () => { + // Make sure the dialog cannot be finished before the admin says the one-time token is stored. + it("keeps Done disabled until the token is marked as stored", () => { + const onDone = vi.fn(); + render( + <Dialog open> + <DialogContent> + <TokenReveal appName="Notebook Hub" token="cak_notebook-hub.SECRET" onDone={onDone} /> + </DialogContent> + </Dialog>, + ); + + const done = screen.getByRole("button", { name: "Done" }); + expect(done).toBeDisabled(); + + fireEvent.click(screen.getByRole("checkbox", { name: "I have stored the token" })); + expect(done).toBeEnabled(); + + fireEvent.click(done); + expect(onDone).toHaveBeenCalledOnce(); + }); +}); diff --git a/web/src/features/core/apps/api.ts b/web/src/features/core/apps/api.ts new file mode 100644 index 000000000..475e587fc --- /dev/null +++ b/web/src/features/core/apps/api.ts @@ -0,0 +1,64 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { z } from "zod"; +import { + type AppConnector, + type AppView, + type AppWithTokenView, + zAppConnector, + zAppView, + zAppWithTokenView, +} from "@/generated/core/zod.gen"; +import { apiFetch } from "@/shared/api/client"; + +export type App = AppView; +export type AppWithToken = AppWithTokenView; +export type { AppConnector }; + +export type NewApp = { + key: string; + name: string; + description: string; + actions: string[]; +}; + +const appPath = (id: string) => `/apps/${encodeURIComponent(id)}`; + +export async function listApps(): Promise<App[]> { + return z.array(zAppView).parse(await apiFetch("/apps")); +} + +export async function listAppConnectors(): Promise<AppConnector[]> { + return z.array(zAppConnector).parse(await apiFetch("/apps/connectors")); +} + +export async function createApp(body: NewApp): Promise<AppWithToken> { + return zAppWithTokenView.parse(await apiFetch("/apps", { method: "POST", body })); +} + +export async function disableApp(id: string): Promise<App> { + return zAppView.parse(await apiFetch(`${appPath(id)}/disable`, { method: "POST" })); +} + +export async function enableApp(id: string): Promise<App> { + return zAppView.parse(await apiFetch(`${appPath(id)}/enable`, { method: "POST" })); +} + +export async function issueAppSecret(id: string): Promise<AppWithToken> { + return zAppWithTokenView.parse(await apiFetch(`${appPath(id)}/secret`, { method: "POST" })); +} diff --git a/web/src/features/core/apps/components/AppsView.tsx b/web/src/features/core/apps/components/AppsView.tsx new file mode 100644 index 000000000..2420239fd --- /dev/null +++ b/web/src/features/core/apps/components/AppsView.tsx @@ -0,0 +1,171 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +"use client"; + +import * as React from "react"; +import { toast } from "sonner"; +import { useAbility } from "@/shared/casl/AbilityProvider"; +import { Button } from "@/shared/ui/button"; +import { ConfirmDialog } from "@/shared/ui/ConfirmDialog"; +import { DataTable, type DataTableColumn } from "@/shared/ui/DataTable"; +import { EmptyState } from "@/shared/ui/EmptyState"; +import { ErrorState } from "@/shared/ui/ErrorState"; +import { CardSkeleton } from "@/shared/ui/Loading"; +import { StatusBadge } from "@/shared/ui/StatusBadge"; +import type { App } from "../api"; +import { useApps, useDisableApp, useEnableApp } from "../queries"; +import { CreateAppDialog } from "./CreateAppDialog"; +import { NewSecretDialog } from "./NewSecretDialog"; + +export function AppsView() { + const ability = useAbility(); + const apps = useApps(); + const disable = useDisableApp(); + const enable = useEnableApp(); + const [creating, setCreating] = React.useState(false); + const [disabling, setDisabling] = React.useState<App | null>(null); + const [newSecretFor, setNewSecretFor] = React.useState<App | null>(null); + + if (ability.cannot("manage", "App")) { + return <ErrorState message="Not permitted." />; + } + + const columns: Array<DataTableColumn<App>> = [ + { + key: "name", + header: "Name", + cell: (app) => ( + <div> + <div className="font-medium">{app.name}</div> + {app.description ? ( + <div className="text-xs text-muted-foreground">{app.description}</div> + ) : null} + </div> + ), + }, + { + key: "key", + header: "App key", + cell: (app) => <span className="font-mono text-xs">{app.key}</span>, + }, + { + key: "connector", + header: "Connector", + cell: (app) => ( + <div className="space-y-1"> + <div>{app.connector}</div> + {app.needs_user_authorization ? ( + <StatusBadge variant="pending" label="Needs user authorization" /> + ) : null} + </div> + ), + }, + { + key: "status", + header: "Status", + cell: (app) => + app.status === "ACTIVE" ? ( + <StatusBadge variant="active" /> + ) : ( + <StatusBadge variant="inactive" label="Disabled" /> + ), + }, + { + key: "hint", + header: "Secret hint", + cell: (app) => ( + <span className="font-mono text-xs"> + cak_{app.key}.{app.secret_hint}... + </span> + ), + }, + { + key: "manage", + header: "", + cell: (app) => ( + <div className="flex justify-end gap-2"> + <Button size="sm" variant="outline" onClick={() => setNewSecretFor(app)}> + New secret + </Button> + {app.status === "ACTIVE" ? ( + <Button size="sm" variant="outline" onClick={() => setDisabling(app)}> + Disable + </Button> + ) : ( + <Button + size="sm" + variant="outline" + disabled={enable.isPending} + onClick={() => + enable.mutate(app.id, { onError: (error) => toast.error(error.message) }) + } + > + Enable + </Button> + )} + </div> + ), + }, + ]; + + return ( + <div className="space-y-6"> + <div className="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between"> + <div> + <h1 className="font-display text-[28px] font-bold leading-tight">Apps</h1> + <p className="mt-1 text-sm text-muted-foreground"> + Trusted tools that call Custos with their own token. + </p> + </div> + <Button onClick={() => setCreating(true)}>Create app</Button> + </div> + + {apps.isLoading ? ( + <CardSkeleton /> + ) : apps.error ? ( + <ErrorState message={apps.error.message} onRetry={() => apps.refetch()} /> + ) : apps.data?.length === 0 ? ( + <EmptyState heading="No apps yet." description="Create one to give a tool its own token." /> + ) : ( + <DataTable columns={columns} rows={apps.data ?? []} rowKey={(app) => app.id} /> + )} + + <CreateAppDialog open={creating} onOpenChange={setCreating} /> + + {newSecretFor ? ( + <NewSecretDialog app={newSecretFor} onClose={() => setNewSecretFor(null)} /> + ) : null} + + <ConfirmDialog + open={disabling !== null} + onOpenChange={(open) => { + if (!open) setDisabling(null); + }} + title={`Disable ${disabling?.name ?? "app"}?`} + confirmLabel="Disable" + pending={disable.isPending} + error={disable.error} + onConfirm={() => { + if (disabling) disable.mutate(disabling.id, { onSuccess: () => setDisabling(null) }); + }} + > + Its token stops working at once. You can enable it again. + </ConfirmDialog> + </div> + ); +} diff --git a/web/src/features/core/apps/components/CreateAppDialog.tsx b/web/src/features/core/apps/components/CreateAppDialog.tsx new file mode 100644 index 000000000..de5b05e42 --- /dev/null +++ b/web/src/features/core/apps/components/CreateAppDialog.tsx @@ -0,0 +1,189 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +"use client"; + +import * as React from "react"; +import { Button } from "@/shared/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/shared/ui/dialog"; +import { Input } from "@/shared/ui/input"; +import { Label } from "@/shared/ui/label"; +import { StatusBadge } from "@/shared/ui/StatusBadge"; +import { useAppConnectors, useCreateApp } from "../queries"; +import { TokenReveal } from "./TokenReveal"; + +const APP_KEY = /^[a-z0-9]+(-[a-z0-9]+)*$/; +const APP_KEY_MAX_LENGTH = 50; + +export type CreateAppDialogProps = { + open: boolean; + onOpenChange: (open: boolean) => void; +}; + +export function CreateAppDialog({ open, onOpenChange }: CreateAppDialogProps) { + const connectors = useAppConnectors(); + const create = useCreateApp(); + const { reset } = create; + const [key, setKey] = React.useState(""); + const [name, setName] = React.useState(""); + const [description, setDescription] = React.useState(""); + const [connectorName, setConnectorName] = React.useState(""); + const [actions, setActions] = React.useState<string[]>([]); + + React.useEffect(() => { + if (!open) { + setKey(""); + setName(""); + setDescription(""); + setConnectorName(""); + setActions([]); + reset(); + } + }, [open, reset]); + + const connector = connectors.data?.find((c) => c.name === connectorName); + const keyIsValid = APP_KEY.test(key) && key.length <= APP_KEY_MAX_LENGTH; + const canSubmit = keyIsValid && name.trim() !== "" && actions.length > 0 && !create.isPending; + const result = create.data; + + function chooseConnector(next: string) { + setConnectorName(next); + setActions(connectors.data?.find((c) => c.name === next)?.actions ?? []); + } + + function toggleAction(action: string, checked: boolean) { + setActions((current) => (checked ? [...current, action] : current.filter((a) => a !== action))); + } + + return ( + <Dialog + open={open} + onOpenChange={(next) => { + // The token is shown once, so only Done closes this state. + if (!create.isPending && !result) onOpenChange(next); + }} + > + <DialogContent> + {result ? ( + <TokenReveal + appName={result.name} + token={result.token} + onDone={() => onOpenChange(false)} + /> + ) : ( + <> + <DialogHeader> + <DialogTitle>Create app</DialogTitle> + <DialogDescription> + An app is a trusted tool that calls Custos with its own token. + </DialogDescription> + </DialogHeader> + <form + className="space-y-4" + onSubmit={(e) => { + e.preventDefault(); + if (canSubmit) create.mutate({ key, name: name.trim(), description, actions }); + }} + > + <div className="space-y-2"> + <Label htmlFor="app-key">App key</Label> + <Input + id="app-key" + value={key} + onChange={(e) => setKey(e.target.value)} + aria-describedby="app-key-help" + className="font-mono" + /> + <p id="app-key-help" className="text-xs text-muted-foreground"> + Lowercase letters, digits and hyphens, for example notebook-hub. It cannot be + changed later. + </p> + </div> + <div className="space-y-2"> + <Label htmlFor="app-name">Name</Label> + <Input id="app-name" value={name} onChange={(e) => setName(e.target.value)} /> + </div> + <div className="space-y-2"> + <Label htmlFor="app-description">Description</Label> + <Input + id="app-description" + value={description} + onChange={(e) => setDescription(e.target.value)} + /> + </div> + <fieldset className="space-y-2"> + <legend className="text-sm font-medium">Connector</legend> + {connectors.data?.length === 0 ? ( + <p className="text-sm text-muted-foreground">No connector offers app actions.</p> + ) : null} + {connectors.data?.map((c) => ( + <label key={c.name} className="flex items-center gap-2 text-sm"> + <input + type="radio" + name="app-connector" + checked={connectorName === c.name} + onChange={() => chooseConnector(c.name)} + /> + {c.name} + {c.needs_user_grant ? ( + <StatusBadge variant="pending" label="Needs user authorization" /> + ) : null} + </label> + ))} + </fieldset> + {connector ? ( + <fieldset className="space-y-2"> + <legend className="text-sm font-medium">Actions</legend> + {connector.actions?.map((action) => ( + <label key={action} className="flex items-center gap-2 font-mono text-xs"> + <input + type="checkbox" + checked={actions.includes(action)} + onChange={(e) => toggleAction(action, e.target.checked)} + /> + {action} + </label> + ))} + </fieldset> + ) : null} + {create.error ? ( + <p role="alert" className="text-sm text-destructive"> + {create.error.message} + </p> + ) : null} + <DialogFooter> + <Button type="button" variant="outline" onClick={() => onOpenChange(false)}> + Cancel + </Button> + <Button type="submit" disabled={!canSubmit}> + Create app + </Button> + </DialogFooter> + </form> + </> + )} + </DialogContent> + </Dialog> + ); +} diff --git a/web/src/features/core/apps/components/NewSecretDialog.tsx b/web/src/features/core/apps/components/NewSecretDialog.tsx new file mode 100644 index 000000000..6969058d6 --- /dev/null +++ b/web/src/features/core/apps/components/NewSecretDialog.tsx @@ -0,0 +1,79 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +"use client"; + +import { Button } from "@/shared/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/shared/ui/dialog"; +import type { App } from "../api"; +import { useIssueAppSecret } from "../queries"; +import { TokenReveal } from "./TokenReveal"; + +export type NewSecretDialogProps = { + app: App; + onClose: () => void; +}; + +export function NewSecretDialog({ app, onClose }: NewSecretDialogProps) { + const issue = useIssueAppSecret(); + const result = issue.data; + + return ( + <Dialog + open + onOpenChange={(next) => { + // The token is shown once, so only Done closes this state. + if (!next && !issue.isPending && !result) onClose(); + }} + > + <DialogContent> + {result ? ( + <TokenReveal appName={app.name} token={result.token} onDone={onClose} /> + ) : ( + <> + <DialogHeader> + <DialogTitle>New secret for {app.name}?</DialogTitle> + <DialogDescription> + The current token stops working at once. The tool must switch to the new token. + </DialogDescription> + </DialogHeader> + {issue.error ? ( + <p role="alert" className="text-sm text-destructive"> + {issue.error.message} + </p> + ) : null} + <DialogFooter> + <Button variant="outline" onClick={onClose} disabled={issue.isPending}> + Cancel + </Button> + <Button onClick={() => issue.mutate(app.id)} disabled={issue.isPending}> + Issue new secret + </Button> + </DialogFooter> + </> + )} + </DialogContent> + </Dialog> + ); +} diff --git a/web/src/features/core/apps/components/TokenReveal.tsx b/web/src/features/core/apps/components/TokenReveal.tsx new file mode 100644 index 000000000..85021bdf8 --- /dev/null +++ b/web/src/features/core/apps/components/TokenReveal.tsx @@ -0,0 +1,58 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +"use client"; + +import * as React from "react"; +import { Button } from "@/shared/ui/button"; +import { CopyValue } from "@/shared/ui/CopyValue"; +import { DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/shared/ui/dialog"; + +export type TokenRevealProps = { + appName: string; + token: string; + onDone: () => void; +}; + +export function TokenReveal({ appName, token, onDone }: TokenRevealProps) { + const [stored, setStored] = React.useState(false); + return ( + <> + <DialogHeader> + <DialogTitle>Token for {appName}</DialogTitle> + <DialogDescription> + Give this to the tool that uses this app. It is shown only now. If it is lost, issue a new + secret. + </DialogDescription> + </DialogHeader> + <div className="min-w-0 space-y-4 text-sm"> + <CopyValue value={token} label="token" explicit> + <span className="whitespace-normal break-all">{token}</span> + </CopyValue> + <label className="flex items-center gap-2"> + <input type="checkbox" checked={stored} onChange={(e) => setStored(e.target.checked)} />I + have stored the token + </label> + </div> + <DialogFooter> + <Button onClick={onDone} disabled={!stored}> + Done + </Button> + </DialogFooter> + </> + ); +} diff --git a/web/src/features/core/apps/queries.ts b/web/src/features/core/apps/queries.ts new file mode 100644 index 000000000..769da3ea7 --- /dev/null +++ b/web/src/features/core/apps/queries.ts @@ -0,0 +1,54 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +"use client"; + +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { + createApp, + disableApp, + enableApp, + issueAppSecret, + listAppConnectors, + listApps, +} from "./api"; + +const appKeys = { + list: ["apps", "list"] as const, + connectors: ["apps", "connectors"] as const, +}; + +export function useApps() { + return useQuery({ queryKey: appKeys.list, queryFn: listApps }); +} + +export function useAppConnectors() { + return useQuery({ queryKey: appKeys.connectors, queryFn: listAppConnectors }); +} + +function useAppMutation<Input, Output>(mutationFn: (input: Input) => Promise<Output>) { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn, + onSuccess: () => queryClient.invalidateQueries({ queryKey: appKeys.list }), + }); +} + +export const useCreateApp = () => useAppMutation(createApp); +export const useDisableApp = () => useAppMutation(disableApp); +export const useEnableApp = () => useAppMutation(enableApp); +export const useIssueAppSecret = () => useAppMutation(issueAppSecret); diff --git a/web/src/mocks/handlers.ts b/web/src/mocks/handlers.ts index ef32ef24d..6e8af4d28 100644 --- a/web/src/mocks/handlers.ts +++ b/web/src/mocks/handlers.ts @@ -20,6 +20,7 @@ import { agreementsHandlers } from "./handlers/agreements"; import { allocationsHandlers } from "./handlers/allocations"; import { amieHandlers } from "./handlers/amie"; import { analyticsHandlers } from "./handlers/analytics"; +import { appsHandlers } from "./handlers/apps"; import { clustersHandlers } from "./handlers/clusters"; import { eventsHandlers } from "./handlers/events"; import { healthzHandlers } from "./handlers/healthz"; @@ -50,4 +51,5 @@ export const handlers: RequestHandler[] = [ ...clustersHandlers, ...resourcesHandlers, ...agreementsHandlers, + ...appsHandlers, ]; diff --git a/web/src/mocks/handlers/apps.ts b/web/src/mocks/handlers/apps.ts new file mode 100644 index 000000000..d6a86fb75 --- /dev/null +++ b/web/src/mocks/handlers/apps.ts @@ -0,0 +1,81 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { HttpResponse, http } from "msw"; +import type { App, AppConnector, NewApp } from "@/features/core/apps/api"; + +const connectors: AppConnector[] = [ + { + name: "signer", + needs_user_grant: true, + actions: ["signer:app:generate-certificate", "signer:app:revoke-certificate"], + }, + { name: "temp-account", needs_user_grant: false, actions: ["temp-account:app:create-user"] }, +]; + +const apps: App[] = [ + { + id: "app-1", + key: "notebook-hub", + name: "Notebook Hub", + description: "Creates workshop accounts.", + connector: "temp-account", + status: "ACTIVE", + secret_hint: "9F3A", + needs_user_authorization: false, + actions: ["temp-account:app:create-user"], + created_by: "user-admin", + created_at: "2026-11-01T12:00:00Z", + updated_at: "2026-11-01T12:00:00Z", + }, +]; + +function setStatus(id: unknown, status: App["status"]) { + const app = apps.find((a) => a.id === id); + if (!app) return HttpResponse.json({ error: "not found" }, { status: 404 }); + app.status = status; + return HttpResponse.json(app); +} + +export const appsHandlers = [ + http.get("*/api/v1/apps", () => HttpResponse.json(apps)), + http.get("*/api/v1/apps/connectors", () => HttpResponse.json(connectors)), + http.post("*/api/v1/apps", async ({ request }) => { + const body = (await request.json()) as NewApp; + const connector = body.actions[0]?.split(":")[0] ?? ""; + const app: App = { + ...body, + id: `app-${apps.length + 1}`, + connector, + status: "ACTIVE", + secret_hint: "ABCD", + needs_user_authorization: connector === "signer", + created_by: "user-admin", + created_at: "2026-11-10T12:00:00Z", + updated_at: "2026-11-10T12:00:00Z", + }; + apps.push(app); + return HttpResponse.json({ ...app, token: `cak_${app.key}.ABCDEFGH` }, { status: 201 }); + }), + http.post("*/api/v1/apps/:id/disable", ({ params: { id } }) => setStatus(id, "DISABLED")), + http.post("*/api/v1/apps/:id/enable", ({ params: { id } }) => setStatus(id, "ACTIVE")), + http.post("*/api/v1/apps/:id/secret", ({ params: { id } }) => { + const app = apps.find((a) => a.id === id); + if (!app) return HttpResponse.json({ error: "not found" }, { status: 404 }); + return HttpResponse.json({ ...app, token: `cak_${app.key}.NEWSECRET` }); + }), +]; diff --git a/web/src/mocks/handlers/privileges.ts b/web/src/mocks/handlers/privileges.ts index 375d67b69..6c5d2ef69 100644 --- a/web/src/mocks/handlers/privileges.ts +++ b/web/src/mocks/handlers/privileges.ts @@ -36,6 +36,7 @@ const ALL_PRIVILEGES: Privilege[] = [ "core:roles:manage", "core:agreements:read", "core:agreements:manage", + "core:apps:manage", "amie:packets:read", "amie:packets:write", "amie:replies:read", diff --git a/web/src/shared/casl/abilities.ts b/web/src/shared/casl/abilities.ts index a54bfdfd8..38064f702 100644 --- a/web/src/shared/casl/abilities.ts +++ b/web/src/shared/casl/abilities.ts @@ -45,6 +45,7 @@ export const PRIVILEGE_ABILITY_MAP: Record<string, PrivilegeRule[]> = { "core:events:manage": [{ action: "manage", subject: "EventDelivery" }], "core:agreements:read": [{ action: "read", subject: "Agreement" }], "core:agreements:manage": [{ action: "manage", subject: "Agreement" }], + "core:apps:manage": [{ action: "manage", subject: "App" }], "amie:packets:read": [{ action: "read", subject: "AMIE" }], "amie:packets:write": [{ action: "manage", subject: "AMIE" }], "amie:replies:read": [{ action: "read", subject: "AMIE" }], diff --git a/web/src/shared/layout/nav.ts b/web/src/shared/layout/nav.ts index 22c84629c..d0782cc8e 100644 --- a/web/src/shared/layout/nav.ts +++ b/web/src/shared/layout/nav.ts @@ -17,6 +17,7 @@ import { Activity, + Blocks, Building2, ChartColumn, ClipboardList, @@ -143,4 +144,11 @@ export const portalNav: NavItem[] = [ group: "admin", ability: { action: "read", subject: "Agreement" }, }, + { + href: "/admin/apps", + label: "Apps", + icon: Blocks, + group: "admin", + ability: { action: "manage", subject: "App" }, + }, ]; diff --git a/web/tests/fixtures/auth.ts b/web/tests/fixtures/auth.ts index 29775dbc6..51de3ba40 100644 --- a/web/tests/fixtures/auth.ts +++ b/web/tests/fixtures/auth.ts @@ -66,6 +66,7 @@ const PRIVILEGES: Record<Persona, string[]> = { "core:roles:manage", "core:agreements:read", "core:agreements:manage", + "core:apps:manage", "amie:packets:read", "amie:packets:write", "amie:replies:read",
