feat(api): unify policy handling with default action updates
- Updated `evofw-firewall.sh` and related scripts to replace `policy_mode` with `default_action`, enhancing clarity and consistency in policy management. - Adjusted agent routes and evaluation logic to accommodate the new default action structure, ensuring backward compatibility with legacy modes. - Enhanced tests to validate the new default action behavior and its integration within the agent policy framework. - Refactored related components in the web interface to align with the updated policy handling, improving user experience and reducing confusion around policy modes.
This commit is contained in:
1 parent
a6eb21a10d
commit
1f7273f38d
30 files changed
+1469
-621
No files matched your search
@@ -0,0 +1,115 @@
|
||||
import type { AgentPolicyPreview } from '@evofw/shared'
|
||||
import {
|
||||
Frame,
|
||||
FrameDescription,
|
||||
FrameHeader,
|
||||
FramePanel,
|
||||
FrameTitle,
|
||||
} from '@/components/reui/frame'
|
||||
import { Badge } from '@/components/reui/badge'
|
||||
import { EmptyState } from '@/components/empty-state'
|
||||
import {
|
||||
Tabs,
|
||||
TabsContent,
|
||||
TabsList,
|
||||
TabsTrigger,
|
||||
} from '@evofw/ui/components/tabs'
|
||||
import { Skeleton } from '@evofw/ui/components/skeleton'
|
||||
|
||||
/**
|
||||
* Effective CIDR bags — tabs Блок / Accept.
|
||||
* Preview: https://reui.io/preview/base/components/c-tabs-2
|
||||
* · https://reui.io/preview/base/data-grid-filtering-2
|
||||
*/
|
||||
|
||||
type AgentEffectiveCidrsProps = {
|
||||
preview?: AgentPolicyPreview
|
||||
isLoading?: boolean
|
||||
}
|
||||
|
||||
function CidrList({
|
||||
items,
|
||||
total,
|
||||
emptyTitle,
|
||||
}: {
|
||||
items: string[]
|
||||
total: number
|
||||
emptyTitle: string
|
||||
}) {
|
||||
if (items.length === 0) {
|
||||
return (
|
||||
<EmptyState
|
||||
title={emptyTitle}
|
||||
description={total === 0 ? undefined : `Всего ${total} (обрезано)`}
|
||||
centered={false}
|
||||
className="py-8"
|
||||
/>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<ul className="flex max-h-64 flex-col gap-1 overflow-y-auto font-mono text-xs">
|
||||
{items.map((c) => (
|
||||
<li key={c} className="bg-muted/40 rounded px-2 py-1">
|
||||
{c}
|
||||
</li>
|
||||
))}
|
||||
{total > items.length ? (
|
||||
<li className="text-muted-foreground px-2 py-1">
|
||||
… и ещё {total - items.length}
|
||||
</li>
|
||||
) : null}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
export function AgentEffectiveCidrs({
|
||||
preview,
|
||||
isLoading,
|
||||
}: AgentEffectiveCidrsProps) {
|
||||
if (isLoading) {
|
||||
return <Skeleton className="h-48 w-full rounded-xl" />
|
||||
}
|
||||
|
||||
const deny = preview?.deny_cidrs ?? []
|
||||
const allow = preview?.allow_cidrs ?? []
|
||||
const denyTotal = preview?.deny_cidrs_total ?? deny.length
|
||||
const allowTotal = preview?.allow_cidrs_total ?? allow.length
|
||||
|
||||
return (
|
||||
<Frame dense spacing="sm">
|
||||
<FrameHeader>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<FrameTitle>Effective CIDR</FrameTitle>
|
||||
<Badge variant="secondary" size="sm">
|
||||
apply v{preview?.apply_version ?? 2}
|
||||
</Badge>
|
||||
</div>
|
||||
<FrameDescription>
|
||||
После deny-wins · hash {preview?.hash?.slice(0, 18) ?? '—'}…
|
||||
</FrameDescription>
|
||||
</FrameHeader>
|
||||
<FramePanel>
|
||||
<Tabs defaultValue="deny">
|
||||
<TabsList>
|
||||
<TabsTrigger value="deny">Блок ({denyTotal})</TabsTrigger>
|
||||
<TabsTrigger value="allow">Accept ({allowTotal})</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value="deny" className="mt-3">
|
||||
<CidrList
|
||||
items={deny}
|
||||
total={denyTotal}
|
||||
emptyTitle="Нет deny CIDR"
|
||||
/>
|
||||
</TabsContent>
|
||||
<TabsContent value="allow" className="mt-3">
|
||||
<CidrList
|
||||
items={allow}
|
||||
total={allowTotal}
|
||||
emptyTitle="Нет allow CIDR"
|
||||
/>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</FramePanel>
|
||||
</Frame>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { toast } from 'sonner'
|
||||
import type { Agent, DefaultAction } from '@evofw/shared'
|
||||
import {
|
||||
Frame,
|
||||
FrameDescription,
|
||||
FrameHeader,
|
||||
FramePanel,
|
||||
FrameTitle,
|
||||
} from '@/components/reui/frame'
|
||||
import { apiFetch } from '@/lib/api'
|
||||
import { Field, FieldLabel } from '@evofw/ui/components/field'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@evofw/ui/components/select'
|
||||
import { Separator } from '@evofw/ui/components/separator'
|
||||
|
||||
/**
|
||||
* Agent facts panel — SA3 RunFacts DNA (editable default_action).
|
||||
* Preview: https://reui.io/preview/base/solution-agents-3
|
||||
* · https://reui.io/preview/base/settings-3
|
||||
*/
|
||||
|
||||
type AgentFactsPanelProps = {
|
||||
agent: Agent
|
||||
}
|
||||
|
||||
function formatWhen(iso?: string | null): string {
|
||||
if (!iso) return '—'
|
||||
const d = new Date(iso)
|
||||
if (Number.isNaN(d.getTime())) return iso
|
||||
return d.toLocaleString('ru-RU')
|
||||
}
|
||||
|
||||
export function AgentFactsPanel({ agent }: AgentFactsPanelProps) {
|
||||
const qc = useQueryClient()
|
||||
const defaultAction: DefaultAction =
|
||||
agent.default_action === 'drop' ? 'drop' : 'accept'
|
||||
|
||||
const patch = useMutation({
|
||||
mutationFn: (default_action: DefaultAction) =>
|
||||
apiFetch<Agent>(`/api/v1/agents/${agent.id}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify({ default_action }),
|
||||
}),
|
||||
onSuccess: () => {
|
||||
toast.success('Default action обновлён')
|
||||
void qc.invalidateQueries({ queryKey: ['agents'] })
|
||||
void qc.invalidateQueries({ queryKey: ['agents', agent.id, 'preview'] })
|
||||
},
|
||||
onError: (e: Error) => toast.error(e.message),
|
||||
})
|
||||
|
||||
return (
|
||||
<Frame dense spacing="sm">
|
||||
<FrameHeader>
|
||||
<FrameTitle>Параметры</FrameTitle>
|
||||
<FrameDescription>Default + identity</FrameDescription>
|
||||
</FrameHeader>
|
||||
<FramePanel className="flex flex-col gap-4">
|
||||
<Field>
|
||||
<FieldLabel>Если не совпало</FieldLabel>
|
||||
<Select
|
||||
value={defaultAction}
|
||||
onValueChange={(v) => {
|
||||
if (v === 'accept' || v === 'drop') patch.mutate(v)
|
||||
}}
|
||||
disabled={patch.isPending}
|
||||
items={[
|
||||
{ value: 'accept', label: 'Accept' },
|
||||
{ value: 'drop', label: 'Drop' },
|
||||
]}
|
||||
>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="accept">Accept</SelectItem>
|
||||
<SelectItem value="drop">Drop</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
Пакет вне deny/allow → {defaultAction === 'drop' ? 'DROP' : 'ACCEPT'}
|
||||
</p>
|
||||
</Field>
|
||||
|
||||
<Separator />
|
||||
|
||||
<dl className="grid gap-2 text-sm">
|
||||
<div className="flex justify-between gap-3">
|
||||
<dt className="text-muted-foreground">Hostname</dt>
|
||||
<dd className="truncate font-medium">{agent.hostname ?? '—'}</dd>
|
||||
</div>
|
||||
<div className="flex justify-between gap-3">
|
||||
<dt className="text-muted-foreground">Token</dt>
|
||||
<dd className="font-mono text-xs">{agent.token_prefix}</dd>
|
||||
</div>
|
||||
<div className="flex justify-between gap-3">
|
||||
<dt className="text-muted-foreground">Client</dt>
|
||||
<dd>{agent.client_version ?? '—'}</dd>
|
||||
</div>
|
||||
<div className="flex justify-between gap-3">
|
||||
<dt className="text-muted-foreground">Last seen IP</dt>
|
||||
<dd>{agent.last_seen_ip ?? '—'}</dd>
|
||||
</div>
|
||||
<div className="flex justify-between gap-3">
|
||||
<dt className="text-muted-foreground">Created</dt>
|
||||
<dd className="text-right text-xs">{formatWhen(agent.created_at)}</dd>
|
||||
</div>
|
||||
<div className="flex justify-between gap-3">
|
||||
<dt className="text-muted-foreground">Approved</dt>
|
||||
<dd className="text-right text-xs">
|
||||
{formatWhen(agent.approved_at)}
|
||||
</dd>
|
||||
</div>
|
||||
<div className="flex justify-between gap-3">
|
||||
<dt className="text-muted-foreground">Generation</dt>
|
||||
<dd className="tabular-nums">{agent.policy_generation}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</FramePanel>
|
||||
</Frame>
|
||||
)
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type { PolicySet } from '@evofw/shared'
|
||||
import {
|
||||
Sortable,
|
||||
SortableItem,
|
||||
@@ -42,7 +41,6 @@ import {
|
||||
/**
|
||||
* Agent-assigned policy sets — ReUI Sortable (c-sortable-5 DNA).
|
||||
* Preview: https://reui.io/preview/base/components/c-sortable-5
|
||||
* · https://reui.io/preview/base/settings-8
|
||||
* Docs: https://reui.io/docs/components/base/sortable
|
||||
*/
|
||||
|
||||
@@ -52,7 +50,6 @@ export type AgentPolicySetRow = {
|
||||
name: string
|
||||
description?: string | null
|
||||
enabled: boolean
|
||||
policy_mode: 'blacklist' | 'whitelist'
|
||||
}
|
||||
|
||||
type AgentPolicySetsSortableProps = {
|
||||
@@ -72,24 +69,10 @@ export function AgentPolicySetsSortable({
|
||||
setItems(assignedQ.data?.items ?? [])
|
||||
}, [assignedQ.data])
|
||||
|
||||
const assignedMode = items[0]?.policy_mode
|
||||
|
||||
const availableSets = useMemo(() => {
|
||||
const assigned = new Set(items.map((i) => i.set_id))
|
||||
return (catalogQ.data?.items ?? []).filter((s) => {
|
||||
if (assigned.has(s.id)) return false
|
||||
if (assignedMode && s.policy_mode !== assignedMode) return false
|
||||
return true
|
||||
})
|
||||
}, [catalogQ.data?.items, items, assignedMode])
|
||||
|
||||
const conflictSets = useMemo(() => {
|
||||
if (!assignedMode) return [] as PolicySet[]
|
||||
const assigned = new Set(items.map((i) => i.set_id))
|
||||
return (catalogQ.data?.items ?? []).filter(
|
||||
(s) => !assigned.has(s.id) && s.policy_mode !== assignedMode,
|
||||
)
|
||||
}, [catalogQ.data?.items, items, assignedMode])
|
||||
return (catalogQ.data?.items ?? []).filter((s) => !assigned.has(s.id))
|
||||
}, [catalogQ.data?.items, items])
|
||||
|
||||
const persist = useMutation({
|
||||
mutationFn: (set_ids: string[]) =>
|
||||
@@ -103,6 +86,7 @@ export function AgentPolicySetsSortable({
|
||||
onSuccess: (res) => {
|
||||
setItems(res.items)
|
||||
void qc.invalidateQueries({ queryKey: ['agents', agentId] })
|
||||
void qc.invalidateQueries({ queryKey: ['agents', agentId, 'preview'] })
|
||||
void qc.invalidateQueries({ queryKey: ['policy-sets'] })
|
||||
},
|
||||
onError: (e: Error) => {
|
||||
@@ -127,12 +111,6 @@ export function AgentPolicySetsSortable({
|
||||
if (!addId) return
|
||||
const set = (catalogQ.data?.items ?? []).find((s) => s.id === addId)
|
||||
if (!set) return
|
||||
if (assignedMode && set.policy_mode !== assignedMode) {
|
||||
toast.error(
|
||||
`Режим набора (${set.policy_mode}) не совпадает с текущим (${assignedMode})`,
|
||||
)
|
||||
return
|
||||
}
|
||||
const next: AgentPolicySetRow[] = [
|
||||
...items,
|
||||
{
|
||||
@@ -141,7 +119,6 @@ export function AgentPolicySetsSortable({
|
||||
name: set.name,
|
||||
description: set.description,
|
||||
enabled: set.enabled,
|
||||
policy_mode: set.policy_mode,
|
||||
},
|
||||
]
|
||||
const prev = items
|
||||
@@ -168,7 +145,7 @@ export function AgentPolicySetsSortable({
|
||||
</Badge>
|
||||
</div>
|
||||
<FrameDescription>
|
||||
Перетащите для приоритета · один режим на агента
|
||||
Порядок = приоритет merge · deny → allow → default
|
||||
</FrameDescription>
|
||||
</div>
|
||||
<div className="flex shrink-0 flex-wrap items-center gap-2">
|
||||
@@ -191,9 +168,7 @@ export function AgentPolicySetsSortable({
|
||||
))}
|
||||
{availableSets.length === 0 ? (
|
||||
<div className="text-muted-foreground px-2 py-1.5 text-xs">
|
||||
{conflictSets.length > 0
|
||||
? 'Нет совместимых наборов'
|
||||
: 'Все наборы уже назначены'}
|
||||
Все наборы уже назначены
|
||||
</div>
|
||||
) : null}
|
||||
</SelectContent>
|
||||
@@ -242,23 +217,13 @@ export function AgentPolicySetsSortable({
|
||||
<GripVerticalIcon className="size-4" />
|
||||
</SortableItemHandle>
|
||||
|
||||
<PolicySetIcon mode={row.policy_mode} className="size-9" />
|
||||
<PolicySetIcon className="size-9" />
|
||||
|
||||
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className="truncate text-sm font-medium">
|
||||
{row.name}
|
||||
</span>
|
||||
<Badge
|
||||
variant={
|
||||
row.policy_mode === 'whitelist'
|
||||
? 'warning-light'
|
||||
: 'secondary'
|
||||
}
|
||||
size="xs"
|
||||
>
|
||||
{row.policy_mode}
|
||||
</Badge>
|
||||
<StatusBadge
|
||||
status={row.enabled ? 'enabled' : 'disabled'}
|
||||
/>
|
||||
@@ -300,13 +265,6 @@ export function AgentPolicySetsSortable({
|
||||
</FramePanel>
|
||||
)}
|
||||
</Frame>
|
||||
|
||||
{conflictSets.length > 0 && items.length > 0 ? (
|
||||
<p className="text-muted-foreground text-xs">
|
||||
{conflictSets.length} набор(ов) скрыты из‑за другого режима (
|
||||
{assignedMode}).
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import { BanIcon, ShieldCheckIcon } from 'lucide-react'
|
||||
import type { AgentPolicyPreview } from '@evofw/shared'
|
||||
import {
|
||||
Timeline,
|
||||
TimelineContent,
|
||||
TimelineHeader,
|
||||
TimelineIndicator,
|
||||
TimelineItem,
|
||||
TimelineSeparator,
|
||||
TimelineTitle,
|
||||
} from '@/components/reui/timeline'
|
||||
import {
|
||||
Frame,
|
||||
FrameDescription,
|
||||
FrameHeader,
|
||||
FramePanel,
|
||||
FrameTitle,
|
||||
} from '@/components/reui/frame'
|
||||
import {
|
||||
Alert,
|
||||
AlertDescription,
|
||||
AlertTitle,
|
||||
} from '@/components/reui/alert'
|
||||
import { Badge } from '@/components/reui/badge'
|
||||
import { EmptyState } from '@/components/empty-state'
|
||||
import { Skeleton } from '@evofw/ui/components/skeleton'
|
||||
|
||||
/**
|
||||
* Policy chain trace — SA3 Timeline DNA.
|
||||
* Preview: https://reui.io/preview/base/solution-agents-3
|
||||
* · https://reui.io/preview/base/components/c-timeline-6
|
||||
* Docs: https://reui.io/docs/components/base/timeline
|
||||
*/
|
||||
|
||||
type AgentPolicyTraceProps = {
|
||||
preview?: AgentPolicyPreview
|
||||
isLoading?: boolean
|
||||
}
|
||||
|
||||
export function AgentPolicyTrace({
|
||||
preview,
|
||||
isLoading,
|
||||
}: AgentPolicyTraceProps) {
|
||||
if (isLoading) {
|
||||
return <Skeleton className="h-64 w-full rounded-xl" />
|
||||
}
|
||||
|
||||
const chain = preview?.chain ?? []
|
||||
const conflicts = preview?.summary.conflicts_dropped ?? 0
|
||||
|
||||
return (
|
||||
<Frame dense spacing="sm">
|
||||
<FrameHeader>
|
||||
<FrameTitle>Цепочка политики</FrameTitle>
|
||||
<FrameDescription>
|
||||
deny → allow → default (
|
||||
{preview?.default_action === 'drop' ? 'Drop' : 'Accept'})
|
||||
</FrameDescription>
|
||||
</FrameHeader>
|
||||
<FramePanel className="flex flex-col gap-3">
|
||||
{conflicts > 0 ? (
|
||||
<Alert variant="warning">
|
||||
<BanIcon />
|
||||
<AlertTitle>Конфликты</AlertTitle>
|
||||
<AlertDescription>
|
||||
{conflicts} CIDR исключены из allow (deny wins, exact match)
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
) : null}
|
||||
|
||||
{chain.length === 0 ? (
|
||||
<EmptyState
|
||||
title="Нет правил"
|
||||
description="Назначьте наборы или добавьте overrides."
|
||||
centered={false}
|
||||
className="py-8"
|
||||
/>
|
||||
) : (
|
||||
<Timeline defaultValue={chain.length} className="px-1">
|
||||
{chain.map((step, i) => {
|
||||
const isDeny = step.action === 'deny'
|
||||
return (
|
||||
<TimelineItem key={`${step.rule_id ?? 'ov'}-${i}`} step={i + 1}>
|
||||
<TimelineHeader>
|
||||
<TimelineSeparator />
|
||||
<TimelineIndicator />
|
||||
<TimelineTitle className="flex flex-wrap items-center gap-2 text-sm">
|
||||
{isDeny ? (
|
||||
<BanIcon className="text-destructive size-3.5" />
|
||||
) : (
|
||||
<ShieldCheckIcon className="text-success size-3.5" />
|
||||
)}
|
||||
<Badge
|
||||
variant={isDeny ? 'destructive-light' : 'success-light'}
|
||||
size="xs"
|
||||
>
|
||||
{isDeny ? 'Блок' : 'Accept'}
|
||||
</Badge>
|
||||
<span className="font-medium">{step.source_label}</span>
|
||||
</TimelineTitle>
|
||||
</TimelineHeader>
|
||||
<TimelineContent className="text-muted-foreground text-xs">
|
||||
{[
|
||||
step.set_name,
|
||||
step.source_kind,
|
||||
`${step.cidr_count} CIDR`,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' · ')}
|
||||
</TimelineContent>
|
||||
</TimelineItem>
|
||||
)
|
||||
})}
|
||||
</Timeline>
|
||||
)}
|
||||
|
||||
{preview ? (
|
||||
<p className="text-muted-foreground text-xs">
|
||||
Блок: {preview.deny_cidrs_total} · Accept:{' '}
|
||||
{preview.allow_cidrs_total} · gen {preview.generation}
|
||||
</p>
|
||||
) : null}
|
||||
</FramePanel>
|
||||
</Frame>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user