From 3f4608b9df454745ea6f953d9f6bcd86d8e25dd1 Mon Sep 17 00:00:00 2001 From: Michel Blaauw Date: Wed, 25 Nov 2026 14:27:40 +0100 Subject: [PATCH] fix(web): refused access saves are not reported as success; Kubernetes allowedResources follows capabilities (#397) Every resource form passed a commitAccess that dropped the access save result, so when the sharing PATCH was refused the page still said "saved" and navigated away, losing the pending access edit. commitAccess now resolves to the access result, and every create and edit page routes the outcome through resourceSaveOutcome: edit pages stay on the form with a "saved, but access could not be updated" warning; create pages go to the new resource's edit page (creating again would duplicate it). The connection form derived a Kubernetes connection's allowedResources only when the stored list was empty, so the list derived on the first save was frozen as if it were an override. It is now derived again on every save unless the stored list differs from what the loaded capabilities derive (a deliberate Advanced override), and the form shows the list that will be saved. The helpers move to lib/kubernetes-resources.ts so node --test can load them. Co-Authored-By: Claude Opus 5.5 --- apps/web/app/agent-teams/[id]/edit/page.tsx | 16 +++++-- apps/web/app/agent-teams/new/page.tsx | 16 +++++-- apps/web/app/agents/[id]/edit/page.tsx | 18 +++++--- apps/web/app/agents/new/page.tsx | 16 +++++-- apps/web/app/connections/[id]/edit/page.tsx | 16 +++++-- apps/web/app/connections/new/page.tsx | 16 +++++-- apps/web/app/environments/[id]/edit/page.tsx | 16 +++++-- apps/web/app/environments/new/page.tsx | 16 +++++-- apps/web/app/models/[id]/edit/page.tsx | 16 +++++-- apps/web/app/models/new/page.tsx | 16 +++++-- apps/web/app/skills/[id]/edit/page.tsx | 16 +++++-- apps/web/app/skills/new/page.tsx | 16 +++++-- apps/web/components/agents/agent-form.tsx | 4 +- .../connections/connection-form.tsx | 25 ++++++---- .../environments/environment-form.tsx | 4 +- .../components/providers/provider-form.tsx | 4 +- .../web/components/resource-access-fields.tsx | 12 +++-- apps/web/components/skills/skill-form.tsx | 4 +- apps/web/components/teams/team-form.tsx | 4 +- apps/web/lib/connections.ts | 22 +-------- apps/web/lib/kubernetes-resources.test.ts | 35 ++++++++++++++ apps/web/lib/kubernetes-resources.ts | 46 +++++++++++++++++++ apps/web/lib/resource-save.test.ts | 39 ++++++++++++++++ apps/web/lib/resource-save.ts | 38 +++++++++++++++ changelog.d/397.fixed.md | 8 ++++ docs/guide/kubernetes-connector.md | 7 ++- 26 files changed, 344 insertions(+), 102 deletions(-) create mode 100644 apps/web/lib/kubernetes-resources.test.ts create mode 100644 apps/web/lib/kubernetes-resources.ts create mode 100644 apps/web/lib/resource-save.test.ts create mode 100644 apps/web/lib/resource-save.ts create mode 100644 changelog.d/397.fixed.md diff --git a/apps/web/app/agent-teams/[id]/edit/page.tsx b/apps/web/app/agent-teams/[id]/edit/page.tsx index 4eb8059e..864775f9 100644 --- a/apps/web/app/agent-teams/[id]/edit/page.tsx +++ b/apps/web/app/agent-teams/[id]/edit/page.tsx @@ -1,5 +1,7 @@ "use client"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { LoadFailure } from "@/components/load-failure"; import { useState } from "react"; import { useParams, useRouter } from "next/navigation"; @@ -47,18 +49,24 @@ function EditAgentTeamEditor({ teamId }: { teamId: string }) { ); } - async function handleSubmit(input: TeamInput, commitAccess: () => Promise) { + async function handleSubmit(input: TeamInput, commitAccess: CommitAccess) { setSubmitting(true); try { // Refused with 409 if someone else saved the team after it was loaded. await updateAgentTeam(teamId, { ...input, expectedUpdatedAt: team.data?.updatedAt }); - await commitAccess(); + const accessSaved = await commitAccess(); await Promise.all([ queryClient.invalidateQueries({ queryKey: queryKeys.agentTeams }), queryClient.invalidateQueries({ queryKey: queryKeys.agentTeam(teamId) }), ]); - toast.success("Agent team saved"); - router.push(`/agent-teams/${teamId}`); + const outcome = resourceSaveOutcome({ + mode: "edit", + saved: "Agent team saved", + href: `/agent-teams/${teamId}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { toast.error(errorMessage(error)); } finally { diff --git a/apps/web/app/agent-teams/new/page.tsx b/apps/web/app/agent-teams/new/page.tsx index 03ce5cec..e6beed8a 100644 --- a/apps/web/app/agent-teams/new/page.tsx +++ b/apps/web/app/agent-teams/new/page.tsx @@ -5,6 +5,8 @@ import { useRouter } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { TeamForm } from "@/components/teams/team-form"; import { PageHeader } from "@/components/ui/page-header"; import { queryKeys } from "@/lib/queries"; @@ -16,14 +18,20 @@ export default function NewAgentTeamPage() { const queryClient = useQueryClient(); const [submitting, setSubmitting] = useState(false); - async function handleSubmit(input: TeamInput, commitAccess: (id?: string) => Promise) { + async function handleSubmit(input: TeamInput, commitAccess: CommitAccess) { setSubmitting(true); try { const team = await createAgentTeam(input); - await commitAccess(team.id); + const accessSaved = await commitAccess(team.id); await queryClient.invalidateQueries({ queryKey: queryKeys.agentTeams }); - toast.success("Agent team created"); - router.push(`/agent-teams/${team.id}`); + const outcome = resourceSaveOutcome({ + mode: "create", + saved: "Agent team created", + href: `/agent-teams/${team.id}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { toast.error(errorMessage(error)); } finally { diff --git a/apps/web/app/agents/[id]/edit/page.tsx b/apps/web/app/agents/[id]/edit/page.tsx index 7ab0908d..943966af 100644 --- a/apps/web/app/agents/[id]/edit/page.tsx +++ b/apps/web/app/agents/[id]/edit/page.tsx @@ -1,5 +1,7 @@ "use client"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { validationDetails } from "@/lib/http"; import { LoadFailure } from "@/components/load-failure"; import Link from "next/link"; @@ -81,7 +83,7 @@ function EditAgentEditor({ agentId }: { agentId: string }) { spec: parsedSpec.data, }; - async function handleSubmit(definition: AgentDefinition, commitAccess: () => Promise) { + async function handleSubmit(definition: AgentDefinition, commitAccess: CommitAccess) { setSubmitting(true); setServerIssues([]); try { @@ -95,16 +97,20 @@ function EditAgentEditor({ agentId }: { agentId: string }) { }), ); // Access changes are committed only now that the agent itself was saved. - await commitAccess(); + const accessSaved = await commitAccess(); // This save is now the baseline; the refresh below must not look like someone else's. startedFrom.current = updated.currentVersion; await queryClient.invalidateQueries({ queryKey: queryKeys.agent(agentId) }); // Name, description and labels are saved in place; only a changed spec is a new version. const version = (updated as { currentVersion?: number }).currentVersion; - toast.success( - version && version > agent.data!.currentVersion ? `Version ${version} created` : "Agent saved", - ); - router.push(`/agents/${agentId}`); + const outcome = resourceSaveOutcome({ + mode: "edit", + saved: version && version > agent.data!.currentVersion ? `Version ${version} created` : "Agent saved", + href: `/agents/${agentId}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { const details = validationDetails(error); setServerIssues(details); diff --git a/apps/web/app/agents/new/page.tsx b/apps/web/app/agents/new/page.tsx index 8184fea7..5ed3c342 100644 --- a/apps/web/app/agents/new/page.tsx +++ b/apps/web/app/agents/new/page.tsx @@ -1,5 +1,7 @@ "use client"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { validationDetails } from "@/lib/http"; import { useRouter } from "next/navigation"; import { useState } from "react"; @@ -20,7 +22,7 @@ export default function NewAgentPage() { const [submitting, setSubmitting] = useState(false); const [serverIssues, setServerIssues] = useState>([]); - async function handleSubmit(definition: AgentDefinition, commitAccess: (id?: string) => Promise) { + async function handleSubmit(definition: AgentDefinition, commitAccess: CommitAccess) { setSubmitting(true); setServerIssues([]); try { @@ -29,11 +31,17 @@ export default function NewAgentPage() { body: toAgentDefinitionBody(definition), }), ); - await commitAccess(created.id); - toast.success("Agent created"); + const accessSaved = await commitAccess(created.id); void queryClient.invalidateQueries({ queryKey: queryKeys.agents() }); void queryClient.invalidateQueries({ queryKey: queryKeys.dashboard }); - router.push(`/agents/${created.id}`); + const outcome = resourceSaveOutcome({ + mode: "create", + saved: "Agent created", + href: `/agents/${created.id}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { const details = validationDetails(error); setServerIssues(details); diff --git a/apps/web/app/connections/[id]/edit/page.tsx b/apps/web/app/connections/[id]/edit/page.tsx index 4723b128..dd1422b2 100644 --- a/apps/web/app/connections/[id]/edit/page.tsx +++ b/apps/web/app/connections/[id]/edit/page.tsx @@ -6,6 +6,8 @@ import { useParams, useRouter } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { ConnectionForm } from "@/components/connections/connection-form"; import { Button } from "@/components/ui/button"; import { EmptyState } from "@/components/ui/empty-state"; @@ -46,14 +48,20 @@ export default function EditConnectionPage() { ); } - async function handleSubmit(input: ConnectionInput, commitAccess: () => Promise) { + async function handleSubmit(input: ConnectionInput, commitAccess: CommitAccess) { setSubmitting(true); try { await updateConnection(connectionId, input); - await commitAccess(); + const accessSaved = await commitAccess(); await queryClient.invalidateQueries({ queryKey: queryKeys.connections }); - toast.success("Connection saved"); - router.push(`/connections/${connectionId}`); + const outcome = resourceSaveOutcome({ + mode: "edit", + saved: "Connection saved", + href: `/connections/${connectionId}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { toast.error(errorMessage(error)); } finally { diff --git a/apps/web/app/connections/new/page.tsx b/apps/web/app/connections/new/page.tsx index abfe4d33..ac00f10f 100644 --- a/apps/web/app/connections/new/page.tsx +++ b/apps/web/app/connections/new/page.tsx @@ -5,6 +5,8 @@ import { useRouter, useSearchParams } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { ConnectionForm } from "@/components/connections/connection-form"; import { Callout } from "@/components/ui/callout"; import { PageHeader } from "@/components/ui/page-header"; @@ -20,17 +22,23 @@ function NewConnection() { const connectors = useConnectors(); const [submitting, setSubmitting] = useState(false); - async function handleSubmit(input: ConnectionInput, commitAccess: (id?: string) => Promise) { + async function handleSubmit(input: ConnectionInput, commitAccess: CommitAccess) { setSubmitting(true); try { const created = await createConnection(input); - await commitAccess(created.id); + const accessSaved = await commitAccess(created.id); await Promise.all([ queryClient.invalidateQueries({ queryKey: queryKeys.connections }), queryClient.invalidateQueries({ queryKey: queryKeys.connectors }), ]); - toast.success("Connection created"); - router.push(`/connections/${created.id}`); + const outcome = resourceSaveOutcome({ + mode: "create", + saved: "Connection created", + href: `/connections/${created.id}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { toast.error(errorMessage(error)); } finally { diff --git a/apps/web/app/environments/[id]/edit/page.tsx b/apps/web/app/environments/[id]/edit/page.tsx index feba1fd5..8695e617 100644 --- a/apps/web/app/environments/[id]/edit/page.tsx +++ b/apps/web/app/environments/[id]/edit/page.tsx @@ -6,6 +6,8 @@ import { useParams, useRouter } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { EnvironmentForm, type EnvironmentInput } from "@/components/environments/environment-form"; import { Button } from "@/components/ui/button"; import { EmptyState } from "@/components/ui/empty-state"; @@ -47,17 +49,23 @@ export default function EditEnvironmentPage() { } const data = mapping.data; - async function handleSubmit(input: EnvironmentInput, commitAccess: () => Promise) { + async function handleSubmit(input: EnvironmentInput, commitAccess: CommitAccess) { setSubmitting(true); try { await updateEnvMapping(id, input); - await commitAccess(); + const accessSaved = await commitAccess(); await Promise.all([ queryClient.invalidateQueries({ queryKey: ["env-mappings"] }), queryClient.invalidateQueries({ queryKey: ["worker-profile-env-mappings"] }), ]); - toast.success("Environment saved"); - router.push(`/environments/${id}`); + const outcome = resourceSaveOutcome({ + mode: "edit", + saved: "Environment saved", + href: `/environments/${id}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { setServerIssues(validationDetails(error)); toast.error(errorMessage(error)); diff --git a/apps/web/app/environments/new/page.tsx b/apps/web/app/environments/new/page.tsx index e56698ca..72d3a27a 100644 --- a/apps/web/app/environments/new/page.tsx +++ b/apps/web/app/environments/new/page.tsx @@ -5,6 +5,8 @@ import { useRouter, useSearchParams } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { EnvironmentForm, type EnvironmentInput } from "@/components/environments/environment-form"; import { PageHeader } from "@/components/ui/page-header"; import { Skeleton } from "@/components/ui/skeleton"; @@ -21,14 +23,20 @@ function NewEnvironment() { const [submitting, setSubmitting] = useState(false); const [serverIssues, setServerIssues] = useState>([]); - async function handleSubmit(input: EnvironmentInput, commitAccess: (id?: string) => Promise) { + async function handleSubmit(input: EnvironmentInput, commitAccess: CommitAccess) { setSubmitting(true); try { const created = await createEnvMapping(input); - await commitAccess(created.id); + const accessSaved = await commitAccess(created.id); await queryClient.invalidateQueries({ queryKey: ["env-mappings"] }); - toast.success("Environment created"); - router.push(`/environments/${created.id}`); + const outcome = resourceSaveOutcome({ + mode: "create", + saved: "Environment created", + href: `/environments/${created.id}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { setServerIssues(validationDetails(error)); toast.error(errorMessage(error)); diff --git a/apps/web/app/models/[id]/edit/page.tsx b/apps/web/app/models/[id]/edit/page.tsx index bbc13381..5dc8aee7 100644 --- a/apps/web/app/models/[id]/edit/page.tsx +++ b/apps/web/app/models/[id]/edit/page.tsx @@ -6,6 +6,8 @@ import { useParams, useRouter } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { ProviderForm } from "@/components/providers/provider-form"; import { Button } from "@/components/ui/button"; import { EmptyState } from "@/components/ui/empty-state"; @@ -49,14 +51,20 @@ export default function EditProviderPage() { } const data = provider.data; - async function handleSubmit(draft: ProviderDraft, commitAccess: () => Promise) { + async function handleSubmit(draft: ProviderDraft, commitAccess: CommitAccess) { setSubmitting(true); try { await updateProvider(data, draft); - await commitAccess(); + const accessSaved = await commitAccess(); await queryClient.invalidateQueries({ queryKey: queryKeys.providers }); - toast.success("Provider saved"); - router.push(`/models/${providerId}`); + const outcome = resourceSaveOutcome({ + mode: "edit", + saved: "Provider saved", + href: `/models/${providerId}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { setServerIssues(validationDetails(error)); toast.error(errorMessage(error)); diff --git a/apps/web/app/models/new/page.tsx b/apps/web/app/models/new/page.tsx index cb75fb76..4b7f9cd3 100644 --- a/apps/web/app/models/new/page.tsx +++ b/apps/web/app/models/new/page.tsx @@ -5,6 +5,8 @@ import { useRouter } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { ProviderForm } from "@/components/providers/provider-form"; import { PageHeader } from "@/components/ui/page-header"; import { validationDetails } from "@/lib/http"; @@ -19,14 +21,20 @@ export default function NewProviderPage() { const [submitting, setSubmitting] = useState(false); const [serverIssues, setServerIssues] = useState>([]); - async function handleSubmit(draft: ProviderDraft, commitAccess: (id?: string) => Promise) { + async function handleSubmit(draft: ProviderDraft, commitAccess: CommitAccess) { setSubmitting(true); try { const created = await createProvider(draft); - await commitAccess(created.id); + const accessSaved = await commitAccess(created.id); await queryClient.invalidateQueries({ queryKey: queryKeys.providers }); - toast.success("Provider added"); - router.push(`/models/${created.id}`); + const outcome = resourceSaveOutcome({ + mode: "create", + saved: "Provider added", + href: `/models/${created.id}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { setServerIssues(validationDetails(error)); toast.error(errorMessage(error)); diff --git a/apps/web/app/skills/[id]/edit/page.tsx b/apps/web/app/skills/[id]/edit/page.tsx index d5dda13e..4779bb09 100644 --- a/apps/web/app/skills/[id]/edit/page.tsx +++ b/apps/web/app/skills/[id]/edit/page.tsx @@ -1,5 +1,7 @@ "use client"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { LoadFailure } from "@/components/load-failure"; import { useState } from "react"; import { useParams, useRouter } from "next/navigation"; @@ -48,18 +50,24 @@ function EditSkillEditor({ skillId }: { skillId: string }) { ); } - async function handleSubmit(input: SkillInput, commitAccess: () => Promise) { + async function handleSubmit(input: SkillInput, commitAccess: CommitAccess) { setSubmitting(true); setServerIssues([]); try { const updated = await updateSkill(skillId, input); - await commitAccess(); + const accessSaved = await commitAccess(); await Promise.all([ queryClient.invalidateQueries({ queryKey: queryKeys.skills }), queryClient.invalidateQueries({ queryKey: queryKeys.skill(skillId) }), ]); - toast.success(`Saved as version ${updated.version}`); - router.push(`/skills/${skillId}`); + const outcome = resourceSaveOutcome({ + mode: "edit", + saved: `Saved as version ${updated.version}`, + href: `/skills/${skillId}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { setServerIssues(validationDetails(error)); toast.error(errorMessage(error)); diff --git a/apps/web/app/skills/new/page.tsx b/apps/web/app/skills/new/page.tsx index cee030db..c181dfbe 100644 --- a/apps/web/app/skills/new/page.tsx +++ b/apps/web/app/skills/new/page.tsx @@ -5,6 +5,8 @@ import { useRouter } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; +import { type CommitAccess } from "@/components/resource-access-fields"; +import { resourceSaveOutcome } from "@/lib/resource-save"; import { SkillForm, type SkillInput } from "@/components/skills/skill-form"; import { PageHeader } from "@/components/ui/page-header"; import { queryKeys } from "@/lib/queries"; @@ -17,15 +19,21 @@ export default function NewSkillPage() { const [submitting, setSubmitting] = useState(false); const [serverIssues, setServerIssues] = useState>([]); - async function handleSubmit(input: SkillInput, commitAccess: (id?: string) => Promise) { + async function handleSubmit(input: SkillInput, commitAccess: CommitAccess) { setSubmitting(true); setServerIssues([]); try { const skill = await createSkill(input); - await commitAccess(skill.id); + const accessSaved = await commitAccess(skill.id); await queryClient.invalidateQueries({ queryKey: queryKeys.skills }); - toast.success("Skill created"); - router.push(`/skills/${skill.id}`); + const outcome = resourceSaveOutcome({ + mode: "create", + saved: "Skill created", + href: `/skills/${skill.id}`, + accessSaved, + }); + toast[outcome.tone](outcome.message); + if (outcome.href) router.push(outcome.href); } catch (error) { setServerIssues(validationDetails(error)); toast.error(errorMessage(error)); diff --git a/apps/web/components/agents/agent-form.tsx b/apps/web/components/agents/agent-form.tsx index 2f98de76..b5717049 100644 --- a/apps/web/components/agents/agent-form.tsx +++ b/apps/web/components/agents/agent-form.tsx @@ -396,9 +396,7 @@ export function AgentForm({ return; } setIssues([]); - const commitAccess = async (id?: string) => { - await accessRef.current?.save(id); - }; + const commitAccess = async (id?: string) => (await accessRef.current?.save(id)) ?? true; await onSubmit(agentDefinitionSchema.parse(candidate) as unknown as AgentDefinition, commitAccess); } diff --git a/apps/web/components/connections/connection-form.tsx b/apps/web/components/connections/connection-form.tsx index 22ab5a68..c897db61 100644 --- a/apps/web/components/connections/connection-form.tsx +++ b/apps/web/components/connections/connection-form.tsx @@ -34,7 +34,7 @@ import { URL_KEYS, applyUrl, defaultConfig, - kubernetesResources, + allowedKubernetesResources, urlError, urlFromConfig, type Capability, @@ -207,6 +207,9 @@ export function ConnectionForm({ }, [configText]); const isKubernetes = connectorSlug === "kubernetes"; + const effectiveResources = isKubernetes + ? allowedKubernetesResources(parsedConfig?.allowedResources, scopes, connection?.scopes ?? []) + : []; const kubernetesAuthMode = isKubernetes ? String(parsedConfig?.authMode ?? "service_account") : ""; const [namespaceText, setNamespaceText] = useState(() => Array.isArray(parsedConfig?.namespaces) ? (parsedConfig.namespaces as string[]).join(", ") : "", @@ -312,17 +315,13 @@ export function ConnectionForm({ toast.error(issues.length === 1 ? issues[0].message : "Fix the highlighted sections"); return; } - const commitAccess = async (id?: string) => { - await accessRef.current?.save(id); - }; + const commitAccess = async (id?: string) => (await accessRef.current?.save(id)) ?? true; const config = usesUrl ? applyUrl(connectorSlug, parsedConfig ?? {}, url) : { ...(parsedConfig ?? {}) }; if (isKubernetes) { config.namespaces = splitNamespaces(namespaceText); - // Normally derived from the selected capabilities. A non-empty advanced value is - // kept as an explicit override. - if (!Array.isArray(config.allowedResources) || config.allowedResources.length === 0) { - config.allowedResources = kubernetesResources(scopes); - } + // Derived from the selected capabilities on every save, unless Advanced config holds + // a deliberate override (a list the loaded capabilities would not derive). + config.allowedResources = effectiveResources; } await onSubmit( { @@ -619,6 +618,14 @@ export function ConnectionForm({ ORBIT normally limits the connection to the Kubernetes resources implied by the capabilities you allow below. Set allowedResources in Advanced config only when you need a deliberate override. + + Effective on save:{" "} + {effectiveResources.length ? ( + {effectiveResources.join(", ")} + ) : ( + "none (every tool is refused)" + )} + ) : null} diff --git a/apps/web/components/environments/environment-form.tsx b/apps/web/components/environments/environment-form.tsx index a9984e87..93977ff3 100644 --- a/apps/web/components/environments/environment-form.tsx +++ b/apps/web/components/environments/environment-form.tsx @@ -154,9 +154,7 @@ export function EnvironmentForm({ toast.error(local.length === 1 ? local[0].message : "Fix the highlighted sections"); return; } - const commitAccess = async (id?: string) => { - await accessRef.current?.save(id); - }; + const commitAccess = async (id?: string) => (await accessRef.current?.save(id)) ?? true; await onSubmit( { name: name.trim(), description: description.trim(), mappings: toEntries(rows) }, commitAccess, diff --git a/apps/web/components/providers/provider-form.tsx b/apps/web/components/providers/provider-form.tsx index e4f2ed78..cddfe253 100644 --- a/apps/web/components/providers/provider-form.tsx +++ b/apps/web/components/providers/provider-form.tsx @@ -204,9 +204,7 @@ export function ProviderForm({ toast.error(local.length === 1 ? local[0].message : "Fix the highlighted sections"); return; } - const commitAccess = async (id?: string) => { - await accessRef.current?.save(id); - }; + const commitAccess = async (id?: string) => (await accessRef.current?.save(id)) ?? true; await onSubmit({ ...draft, models }, commitAccess); } diff --git a/apps/web/components/resource-access-fields.tsx b/apps/web/components/resource-access-fields.tsx index 0eed0050..395ab38d 100644 --- a/apps/web/components/resource-access-fields.tsx +++ b/apps/web/components/resource-access-fields.tsx @@ -29,11 +29,13 @@ export type ResourceAccessHandle = { save: (resourceId?: string) => Promise; }; -/** The parent commits this only after its own save succeeded. */ -export type ResourceSubmit = ( - value: T, - commitAccess: (resourceId?: string) => Promise, -) => Promise | void; +/** + * The parent commits this only after its own save succeeded. It resolves to false when the + * access change was refused (the access fields already showed why); the page must then + * keep the user on the form or send them to the edit page, never report a full success. + */ +export type CommitAccess = (resourceId?: string) => Promise; +export type ResourceSubmit = (value: T, commitAccess: CommitAccess) => Promise | void; type ResourceAccessProps = { resource: string; diff --git a/apps/web/components/skills/skill-form.tsx b/apps/web/components/skills/skill-form.tsx index 0b8567e9..56fc112a 100644 --- a/apps/web/components/skills/skill-form.tsx +++ b/apps/web/components/skills/skill-form.tsx @@ -285,9 +285,7 @@ export function SkillForm({ return; } const extra = draft.extra.split(/\r?\n/).filter((line, i, all) => line.trim() !== "" || (i > 0 && i < all.length - 1)); - const commitAccess = async (id?: string) => { - await accessRef.current?.save(id); - }; + const commitAccess = async (id?: string) => (await accessRef.current?.save(id)) ?? true; await onSubmit( { name: draft.name.trim(), diff --git a/apps/web/components/teams/team-form.tsx b/apps/web/components/teams/team-form.tsx index 1fef9ba5..001ff4b3 100644 --- a/apps/web/components/teams/team-form.tsx +++ b/apps/web/components/teams/team-form.tsx @@ -348,9 +348,7 @@ export function TeamForm({ return; } // Access is stored separately; the parent commits it after the team itself is saved. - const commitAccess = async (id?: string) => { - await accessRef.current?.save(id); - }; + const commitAccess = async (id?: string) => (await accessRef.current?.save(id)) ?? true; await onSubmit(toInput(draft), commitAccess); } diff --git a/apps/web/lib/connections.ts b/apps/web/lib/connections.ts index e4ceb5df..201056d5 100644 --- a/apps/web/lib/connections.ts +++ b/apps/web/lib/connections.ts @@ -115,24 +115,4 @@ export function useConnection(id: string | undefined) { }; } -/** Kubernetes capabilities map to the resources the connection may touch. */ -const KUBERNETES_RESOURCES: Record = { - "namespaces.list": ["namespaces"], - "pods.list": ["pods"], - "pods.get": ["pods"], - "pods.logs": ["pods/log"], - "pods.delete": ["pods"], - "deployments.list": ["deployments"], - "deployments.get": ["deployments"], - "deployments.restart": ["deployments"], - "events.list": ["events"], -}; - -/** - * Resource-level allowlist for a Kubernetes connection. The connector still checks - * agent-level capability scopes; this list prevents a connection from ever touching a - * resource class it was not set up for. Advanced config can override it. - */ -export function kubernetesResources(scopes: string[]): string[] { - return [...new Set(scopes.flatMap((scope) => KUBERNETES_RESOURCES[scope] ?? []))].sort(); -} +export { allowedKubernetesResources, kubernetesResources } from "@/lib/kubernetes-resources"; diff --git a/apps/web/lib/kubernetes-resources.test.ts b/apps/web/lib/kubernetes-resources.test.ts new file mode 100644 index 00000000..b377ad8b --- /dev/null +++ b/apps/web/lib/kubernetes-resources.test.ts @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { allowedKubernetesResources, kubernetesResources } from "./kubernetes-resources.ts"; + +test("a new connection derives the list from its capabilities", () => { + assert.deepEqual(allowedKubernetesResources(undefined, ["pods.list", "events.list"], []), ["events", "pods"]); + assert.deepEqual(allowedKubernetesResources([], ["pods.list"], []), ["pods"]); +}); + +test("a derived list follows later capability changes", () => { + const created = kubernetesResources(["pods.list"]); + assert.deepEqual(created, ["pods"]); + // Edit: add pods.logs. + assert.deepEqual(allowedKubernetesResources(created, ["pods.list", "pods.logs"], ["pods.list"]), [ + "pods", + "pods/log", + ]); + // Edit: remove capabilities. + const wide = kubernetesResources(["pods.list", "deployments.list", "events.list"]); + assert.deepEqual( + allowedKubernetesResources(wide, ["pods.list"], ["pods.list", "deployments.list", "events.list"]), + ["pods"], + ); + // Order of the stored list does not make it an override. + assert.deepEqual( + allowedKubernetesResources(["pods", "events"], ["events.list"], ["pods.list", "events.list"]), + ["events"], + ); +}); + +test("an explicit override survives an edit", () => { + const override = ["pods", "pods/log", "configmaps"]; + assert.deepEqual(allowedKubernetesResources(override, ["pods.list", "events.list"], ["pods.list"]), override); +}); diff --git a/apps/web/lib/kubernetes-resources.ts b/apps/web/lib/kubernetes-resources.ts new file mode 100644 index 00000000..d903c6c1 --- /dev/null +++ b/apps/web/lib/kubernetes-resources.ts @@ -0,0 +1,46 @@ +/** Kubernetes capabilities map to the resources the connection may touch. */ +const KUBERNETES_RESOURCES: Record = { + "namespaces.list": ["namespaces"], + "pods.list": ["pods"], + "pods.get": ["pods"], + "pods.logs": ["pods/log"], + "pods.delete": ["pods"], + "deployments.list": ["deployments"], + "deployments.get": ["deployments"], + "deployments.restart": ["deployments"], + "events.list": ["events"], +}; + +/** + * Resource-level allowlist for a Kubernetes connection. The connector still checks + * agent-level capability scopes; this list prevents a connection from ever touching a + * resource class it was not set up for. Advanced config can override it. + */ +export function kubernetesResources(scopes: string[]): string[] { + return [...new Set(scopes.flatMap((scope) => KUBERNETES_RESOURCES[scope] ?? []))].sort(); +} + +function sameSet(a: string[], b: string[]): boolean { + const left = [...new Set(a)].sort(); + const right = [...new Set(b)].sort(); + return left.length === right.length && left.every((value, index) => value === right[index]); +} + +/** + * The `allowedResources` to save. The list follows the selected capabilities unless it is + * a deliberate override: a non-empty stored list that differs from what the capabilities + * the connection was loaded with would derive. A list that was itself derived (the usual + * case after the first save) is derived again, so adding `pods.logs` adds `pods/log` and + * removing capabilities shrinks the list. + */ +export function allowedKubernetesResources( + stored: unknown, + scopes: string[], + previousScopes: string[], +): string[] { + const derived = kubernetesResources(scopes); + if (!Array.isArray(stored) || stored.length === 0) return derived; + const list = stored.filter((value): value is string => typeof value === "string"); + if (sameSet(list, kubernetesResources(previousScopes))) return derived; + return list; +} diff --git a/apps/web/lib/resource-save.test.ts b/apps/web/lib/resource-save.test.ts new file mode 100644 index 00000000..7fc47e53 --- /dev/null +++ b/apps/web/lib/resource-save.test.ts @@ -0,0 +1,39 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { resourceSaveOutcome } from "./resource-save.ts"; + +test("a full save reports success and goes to the resource", () => { + assert.deepEqual( + resourceSaveOutcome({ mode: "edit", saved: "Agent saved", href: "/agents/a1", accessSaved: true }), + { tone: "success", message: "Agent saved", href: "/agents/a1" }, + ); + assert.equal( + resourceSaveOutcome({ mode: "create", saved: "Skill created", href: "/skills/s1", accessSaved: true }).href, + "/skills/s1", + ); +}); + +test("a refused access change on edit keeps the user on the form", () => { + const outcome = resourceSaveOutcome({ + mode: "edit", + saved: "Agent team saved", + href: "/agent-teams/t1", + accessSaved: false, + }); + assert.equal(outcome.tone, "warning"); + assert.equal(outcome.href, null); + assert.match(outcome.message, /^Agent team saved, but access could not be updated/); +}); + +test("a refused access change on create goes to the edit page, not the create form", () => { + const outcome = resourceSaveOutcome({ + mode: "create", + saved: "Connection created", + href: "/connections/c1", + accessSaved: false, + }); + assert.equal(outcome.tone, "warning"); + assert.equal(outcome.href, "/connections/c1/edit"); + assert.match(outcome.message, /access could not be updated/); +}); diff --git a/apps/web/lib/resource-save.ts b/apps/web/lib/resource-save.ts new file mode 100644 index 00000000..7bb3758b --- /dev/null +++ b/apps/web/lib/resource-save.ts @@ -0,0 +1,38 @@ +/** + * What a create or edit page does after the resource itself was saved, given whether the + * deferred access (sharing) change was saved too. A refused access change is never + * reported as a plain success: + * - edit: stay on the form, so the pending access edit is still there to fix or retry; + * - create: the resource exists now, so go to its edit page (creating again would + * duplicate it) and say that access still needs to be set. + */ +export type ResourceSaveOutcome = { + tone: "success" | "warning"; + message: string; + /** Where to go next; null keeps the user on the current form. */ + href: string | null; +}; + +export function resourceSaveOutcome(options: { + mode: "create" | "edit"; + /** Success text when everything saved, e.g. "Agent created". */ + saved: string; + /** The resource's page, e.g. `/agents/`. */ + href: string; + accessSaved: boolean; +}): ResourceSaveOutcome { + const { mode, saved, href, accessSaved } = options; + if (accessSaved) return { tone: "success", message: saved, href }; + if (mode === "edit") { + return { + tone: "warning", + message: `${saved}, but access could not be updated. Fix it and use Save access.`, + href: null, + }; + } + return { + tone: "warning", + message: `${saved}, but access could not be updated. Set it again here.`, + href: `${href}/edit`, + }; +} diff --git a/changelog.d/397.fixed.md b/changelog.d/397.fixed.md new file mode 100644 index 00000000..21a9ea8c --- /dev/null +++ b/changelog.d/397.fixed.md @@ -0,0 +1,8 @@ +- Web forms no longer report a refused access (sharing) change as a full save (#397). + Edit pages stay on the form with the access edit intact and say that the resource + was saved but access was not; create pages open the new resource's edit page to set + access again. +- A Kubernetes connection's `allowedResources` follows capability changes made in the + web form: adding `pods.logs` adds `pods/log`, and removing capabilities shrinks the + list. A deliberate override in Advanced config is kept, and the form shows the list + that will be saved (#397). See [the Kubernetes connector guide](docs/guide/kubernetes-connector.md). diff --git a/docs/guide/kubernetes-connector.md b/docs/guide/kubernetes-connector.md index fd490ea5..84f7af06 100644 --- a/docs/guide/kubernetes-connector.md +++ b/docs/guide/kubernetes-connector.md @@ -21,8 +21,11 @@ connection also stores `defaultNamespace`, an optional public `caCert`, `timeoutSeconds` (default 20) and `maxLogBytes` (default 200000). Allowlists are enforced by ORBIT as well as RBAC, and an empty list denies: -- `allowedResources` is normally derived from the selected capabilities; an explicit - non-empty value in Advanced config overrides that derivation. +- `allowedResources` is normally derived from the selected capabilities, and the web form + derives it again on every save, so adding `pods.logs` later adds `pods/log` and removing + capabilities shrinks the list. A non-empty value in Advanced config that differs from what + the saved capabilities derive is an explicit override and is kept. The form shows the + list that will be saved. - `namespaces` empty: only `defaultNamespace` may be used; with neither set, every namespaced tool is refused. Use `["*"]` to let the kubeconfig/RBAC decide which