|
|
@@ -0,0 +1,164 @@
|
|
|
+import { useEffect, useState } from 'react'
|
|
|
+import { useQuery, useQueryClient } from '@tanstack/react-query'
|
|
|
+import { api } from '@/api/client'
|
|
|
+import { Badge, FormRow, inputCls, SaveButton } from '@/components/form'
|
|
|
+import { useAuthStore } from '@/stores/authStore'
|
|
|
+import { useToastStore } from '@/stores/toastStore'
|
|
|
+import { ApiError } from '@/types'
|
|
|
+import type { ProjectFunctionSettingsPatch } from '@/types'
|
|
|
+
|
|
|
+/// A project's function limits and its network egress.
|
|
|
+///
|
|
|
+/// ADMIN ONLY, matching the route, which is requireAdmin rather than
|
|
|
+/// requireProjectManage: these constrain what one tenant may take from a
|
|
|
+/// runner every other project shares, so the party they constrain must not be
|
|
|
+/// the party who sets them.
|
|
|
+///
|
|
|
+/// Until this existed both settings were reachable only by curl - a control
|
|
|
+/// nobody can see is a control nobody knows exists, and that was especially
|
|
|
+/// true of the egress switch, where an operator could not tell whether a
|
|
|
+/// project's functions were allowed outbound access at all.
|
|
|
+
|
|
|
+/// `''` means "no project cap", which is NOT a cap of zero: the runner's own
|
|
|
+/// ceiling for that kind applies instead. Kept as a string so an empty input
|
|
|
+/// and a zero stay distinguishable all the way to the request.
|
|
|
+function capToInput(ms: number | null): string {
|
|
|
+ return ms === null ? '' : String(ms)
|
|
|
+}
|
|
|
+
|
|
|
+export default function ProjectFunctionsTab() {
|
|
|
+ const admin = useAuthStore((s) => s.admin)
|
|
|
+ const project = useAuthStore((s) => s.currentProject)
|
|
|
+ const toast = useToastStore((s) => s.push)
|
|
|
+ const qc = useQueryClient()
|
|
|
+
|
|
|
+ const { data, isLoading, error } = useQuery({
|
|
|
+ queryKey: ['project-function-settings', project],
|
|
|
+ queryFn: () => api.getProjectFunctionSettings(project!),
|
|
|
+ enabled: admin && !!project,
|
|
|
+ })
|
|
|
+
|
|
|
+ const [http, setHttp] = useState('')
|
|
|
+ const [job, setJob] = useState('')
|
|
|
+ // Three states, not two. `network_egress` is the EFFECTIVE value and is
|
|
|
+ // never null, while `network_egress_set` says whether it was chosen here or
|
|
|
+ // inherited - and "allow (inherited)" is a different statement from
|
|
|
+ // "allow (set for this project)".
|
|
|
+ const [egress, setEgress] = useState<'inherit' | 'allow' | 'deny'>('inherit')
|
|
|
+ const [saving, setSaving] = useState(false)
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (!data) return
|
|
|
+ setHttp(capToInput(data.max_wall_time_ms.http))
|
|
|
+ setJob(capToInput(data.max_wall_time_ms.job))
|
|
|
+ setEgress(data.network_egress_set ? data.network_egress : 'inherit')
|
|
|
+ }, [data])
|
|
|
+
|
|
|
+ if (!admin) {
|
|
|
+ return (
|
|
|
+ <p className="text-sm text-slate-400">
|
|
|
+ These settings are admin-only: they bound what one project may take from
|
|
|
+ a runner shared with every other project.
|
|
|
+ </p>
|
|
|
+ )
|
|
|
+ }
|
|
|
+ if (!project) return <p className="text-sm text-slate-400">Select a project first.</p>
|
|
|
+ if (isLoading) return <p className="text-sm text-slate-400">Loading…</p>
|
|
|
+ if (error) return <p className="text-sm text-rose-400">{String(error)}</p>
|
|
|
+
|
|
|
+ async function onSubmit(e: React.FormEvent) {
|
|
|
+ e.preventDefault()
|
|
|
+ if (!project) return
|
|
|
+ const patch: ProjectFunctionSettingsPatch = {
|
|
|
+ max_wall_time_ms: {
|
|
|
+ // An empty field means "clear the cap", which the route spells as null.
|
|
|
+ http: http.trim() === '' ? null : Number(http),
|
|
|
+ job: job.trim() === '' ? null : Number(job),
|
|
|
+ },
|
|
|
+ network_egress: egress === 'inherit' ? null : egress,
|
|
|
+ }
|
|
|
+ setSaving(true)
|
|
|
+ try {
|
|
|
+ await api.updateProjectFunctionSettings(project, patch)
|
|
|
+ await qc.invalidateQueries({ queryKey: ['project-function-settings', project] })
|
|
|
+ toast('Function settings saved', 'success')
|
|
|
+ } catch (e) {
|
|
|
+ toast(e instanceof ApiError ? e.message : String(e), 'error')
|
|
|
+ } finally {
|
|
|
+ setSaving(false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const effective = data?.network_egress ?? 'allow'
|
|
|
+ const inherited = data ? !data.network_egress_set : true
|
|
|
+
|
|
|
+ return (
|
|
|
+ <form onSubmit={onSubmit} className="flex flex-col gap-5 max-w-2xl">
|
|
|
+ <FormRow
|
|
|
+ label="Network egress"
|
|
|
+ hint="Whether this project's functions may make outbound calls at all. Covers BOTH ctx.fetch and ctx.mail - a project denied the network is denied both, refused before any name is resolved, so a denied project does not even emit a DNS query."
|
|
|
+ badge={inherited ? <Badge>inherited</Badge> : <Badge tone="indigo">set here</Badge>}
|
|
|
+ >
|
|
|
+ <div className="flex items-center gap-3">
|
|
|
+ <select
|
|
|
+ value={egress}
|
|
|
+ onChange={(e) => setEgress(e.target.value as 'inherit' | 'allow' | 'deny')}
|
|
|
+ className={inputCls}
|
|
|
+ data-testid="egress-select"
|
|
|
+ >
|
|
|
+ <option value="inherit">inherit (allow)</option>
|
|
|
+ <option value="allow">allow</option>
|
|
|
+ <option value="deny">deny</option>
|
|
|
+ </select>
|
|
|
+ <span className="text-xs text-slate-400">
|
|
|
+ effective: <strong className={effective === 'deny' ? 'text-rose-400' : 'text-slate-200'}>{effective}</strong>
|
|
|
+ </span>
|
|
|
+ </div>
|
|
|
+ </FormRow>
|
|
|
+
|
|
|
+ {egress === 'deny' && (
|
|
|
+ <p className="text-xs text-amber-400/90" data-testid="egress-warning">
|
|
|
+ Every outbound call from this project's functions will be refused with
|
|
|
+ <code className="mx-1">egress_blocked</code>, including mail. Functions
|
|
|
+ that fetch or send will fail, not degrade.
|
|
|
+ </p>
|
|
|
+ )}
|
|
|
+
|
|
|
+ <FormRow
|
|
|
+ label="Max wall time, HTTP runs"
|
|
|
+ hint="Milliseconds. Empty means no project cap, which is not the same as zero - the runner's own ceiling for HTTP runs applies instead. Narrowing only: this cannot raise a ceiling."
|
|
|
+ >
|
|
|
+ <input
|
|
|
+ type="number"
|
|
|
+ min={1}
|
|
|
+ value={http}
|
|
|
+ placeholder="no project cap"
|
|
|
+ onChange={(e) => setHttp(e.target.value)}
|
|
|
+ className={`${inputCls} w-40`}
|
|
|
+ data-testid="wall-http"
|
|
|
+ />
|
|
|
+ </FormRow>
|
|
|
+
|
|
|
+ <FormRow
|
|
|
+ label="Max wall time, JOB runs"
|
|
|
+ hint="Milliseconds. Separate from the HTTP cap on purpose: one number could not express 'short for routes, long for cron', so capping a project's routes used to cut its cron work too."
|
|
|
+ >
|
|
|
+ <input
|
|
|
+ type="number"
|
|
|
+ min={1}
|
|
|
+ value={job}
|
|
|
+ placeholder="no project cap"
|
|
|
+ onChange={(e) => setJob(e.target.value)}
|
|
|
+ className={`${inputCls} w-40`}
|
|
|
+ data-testid="wall-job"
|
|
|
+ />
|
|
|
+ </FormRow>
|
|
|
+
|
|
|
+ {/* SaveButton IS the button, with type="submit" - wrapping it in another
|
|
|
+ button nests one inside the other and only works by accident. */}
|
|
|
+ <div className="flex items-center gap-3">
|
|
|
+ <SaveButton saving={saving} />
|
|
|
+ </div>
|
|
|
+ </form>
|
|
|
+ )
|
|
|
+}
|