Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.ht
### Production readiness fixes

- Route live HTTP/RBAC authorization through the compiled policy evaluator pinned to the admitted snapshot (part of #422). Preserve direct-rule, host-bound route, MCP HTTP alias, shadow and audit behavior; block incomplete or inconsistent evaluation inputs before downstream work. Tool admission, rate selection and egress retain their existing authorities.
- Report missing and truncated policy evidence instead of zeros (part of #243). `GET /v1{ADMIN_PREFIX}/policy/rules/hits` and `GET /v1{ADMIN_PREFIX}/policy/rules/shadow-review` now answer `503` with `"reason":"audit_evidence_unavailable"` when no audit query store is configured -- including every cluster-mode deployment, which does not serve these aggregates yet -- rather than `200` with zero counts that read as "never matched" or "safe to promote"; rule preview's existing `503` carries the same reason. Rule preview scans at most 100,000 observations and adds `scan_truncated` and `complete` to its response. The admin UI shows evidence unavailable, truncated, and a counted zero as distinct text.

- Bound gRPC response cleanup independently of downstream polling, own listener connection tasks through shutdown, and bound WebSocket writes while allowing both directions to progress.
- Acquire tool-scoped concurrency before global capacity; classify MCP application errors as failures while preserving their protocol response; count HTTP passive-health success only after a complete body.
Expand Down
65 changes: 65 additions & 0 deletions admin-ui/src/lib/policy.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { afterEach, describe, expect, it, vi } from 'vitest';

import { AdminApiError } from './api';
import {
createPolicyRule,
fetchPolicy,
fetchPolicyRuleHits,
fetchPolicyRuleShadowReview,
deletePolicyRule,
patchPolicyRule,
reorderPolicyRules,
Expand Down Expand Up @@ -56,6 +59,68 @@ describe('policy writes', () => {
);
});

describe('policy evidence', () => {
const unavailableBody = {
error: 'policy rule hit counts require an audit query store',
reason: 'audit_evidence_unavailable',
};

it('returns counted hits, including a counted zero, as available evidence', async () => {
vi.stubGlobal(
'fetch',
vi.fn(() =>
Promise.resolve(
jsonResponse(200, {
rules: [
{ rule_id: 'busy', hits: 3 },
{ rule_id: 'quiet', hits: 0 },
],
}),
),
),
);

expect(await fetchPolicyRuleHits()).toEqual({
status: 'available',
value: { busy: 3, quiet: 0 },
});
});

it.each([
['hit counts', fetchPolicyRuleHits],
['shadow review', fetchPolicyRuleShadowReview],
])(
'reports %s without an audit store as unavailable, not empty',
async (_name, fetchEvidence) => {
vi.stubGlobal(
'fetch',
vi.fn(() => Promise.resolve(jsonResponse(503, unavailableBody))),
);

expect(await fetchEvidence()).toEqual({
status: 'unavailable',
message: unavailableBody.error,
});
},
);

it.each([
['a 503 without the evidence reason', 503, { error: 'policy control plane unavailable' }],
['a 500', 500, { error: 'policy rule hit count query failed' }],
['a 403', 403, { error: 'forbidden', reason: 'audit_evidence_unavailable' }],
])('rejects %s instead of reporting an evidence state', async (_name, status, body) => {
vi.stubGlobal(
'fetch',
vi.fn(() => Promise.resolve(jsonResponse(status, body))),
);

await expect(fetchPolicyRuleHits()).rejects.toBeInstanceOf(AdminApiError);
await expect(fetchPolicyRuleShadowReview()).rejects.toBeInstanceOf(
AdminApiError,
);
});
});

function jsonResponse(status: number, body: unknown): Response {
return new Response(JSON.stringify(body), {
status,
Expand Down
81 changes: 69 additions & 12 deletions admin-ui/src/lib/policy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { adminFetchJson, adminFetchJsonResponse } from './api';
import { AdminApiError, adminFetchJson, adminFetchJsonResponse } from './api';
import { adminApiUrl } from './config';

// The gateway's stable reason when a policy evidence endpoint has no audit
// store to read. It answers 503 instead of zeros, so the UI can say the
// evidence is missing rather than imply a rule never matched.
export const POLICY_EVIDENCE_UNAVAILABLE_REASON = 'audit_evidence_unavailable';

export type PolicyDefaultAction = 'allow' | 'deny';
export type PolicyRuleAction = 'allow' | 'deny' | 'shadow';
export type AuthMethodName =
Expand Down Expand Up @@ -92,6 +97,10 @@ export type PolicyRulePreviewRequest = {
export type PolicyRulePreviewResponse = {
match_count: number;
scanned_event_count: number;
// True when the window held more observations than one preview scans;
// match_count then covers only the newest scanned_event_count of them.
scan_truncated: boolean;
complete: boolean;
sample_strategy: string;
samples: PolicyRulePreviewSample[];
};
Expand Down Expand Up @@ -130,6 +139,12 @@ export type PolicyRuleShadowReviewResponse = {
rules: PolicyRuleShadowReviewSummary[];
};

// Evidence read from the audit store, or an explicit statement that there was
// none to read. Never collapse `unavailable` into an empty or zero `value`.
export type PolicyEvidence<T> =
| { status: 'available'; value: T }
| { status: 'unavailable'; message: string };

type PolicyRuleHitsResponse =
| {
rules: Array<{
Expand Down Expand Up @@ -236,26 +251,68 @@ export async function reorderPolicyRules(
);
}

export async function fetchPolicyRuleHits(): Promise<Record<string, number>> {
const response = await adminFetchJson<PolicyRuleHitsResponse>(
adminApiUrl('/policy/rules/hits'),
);
export async function fetchPolicyRuleHits(): Promise<
PolicyEvidence<Record<string, number>>
> {
let response: PolicyRuleHitsResponse;
try {
response = await adminFetchJson<PolicyRuleHitsResponse>(
adminApiUrl('/policy/rules/hits'),
);
} catch (error) {
return unavailablePolicyEvidence(error);
}

if (isPolicyRuleHitsListResponse(response)) {
return Object.fromEntries(
response.rules.map((rule) => [rule.rule_id, rule.hits]),
);
return {
status: 'available',
value: Object.fromEntries(
response.rules.map((rule) => [rule.rule_id, rule.hits]),
),
};
}

return response;
return { status: 'available', value: response };
}

export async function fetchPolicyRuleShadowReview(): Promise<PolicyRuleShadowReviewResponse> {
return adminFetchJson<PolicyRuleShadowReviewResponse>(
adminApiUrl('/policy/rules/shadow-review'),
export async function fetchPolicyRuleShadowReview(): Promise<
PolicyEvidence<PolicyRuleShadowReviewResponse>
> {
try {
return {
status: 'available',
value: await adminFetchJson<PolicyRuleShadowReviewResponse>(
adminApiUrl('/policy/rules/shadow-review'),
),
};
} catch (error) {
return unavailablePolicyEvidence(error);
}
}

export function isPolicyEvidenceUnavailableError(
error: unknown,
): error is AdminApiError {
return (
error instanceof AdminApiError &&
error.status === 503 &&
error.code === POLICY_EVIDENCE_UNAVAILABLE_REASON
);
}

// Only the gateway's explicit "no audit store" answer becomes unavailable
// evidence. Every other failure, including other 503s, still rejects so the
// caller reports it as an error instead of an evidence state.
function unavailablePolicyEvidence(error: unknown): {
status: 'unavailable';
message: string;
} {
if (isPolicyEvidenceUnavailableError(error)) {
return { status: 'unavailable', message: error.message };
}
throw error;
}

export function policyRuleId(rule: PolicyRule, index: number): string {
return rule.id ?? String(index);
}
Expand Down
71 changes: 71 additions & 0 deletions admin-ui/src/views/RuleEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,77 @@ describe('RuleEditor', () => {
expect(screen.getByText('/api/users/123')).toBeTruthy();
});

it('presents a truncated preview count as a lower bound', async () => {
const fetchMock = policyBackedFetch(policyFixture(), 'W/"policy-1"', {
preview: {
...previewResponseFixture(3, '/api/users/123'),
scanned_event_count: 900,
scan_truncated: true,
complete: false,
},
});
vi.stubGlobal('fetch', fetchMock);

renderRuleEditor();

await screen.findByLabelText('Path pattern');
vi.useFakeTimers();
fireEvent.change(screen.getByLabelText('Path pattern'), {
target: { value: '/api/users/{id}' },
});
await advanceTimersByTime(RULE_PREVIEW_DEBOUNCE_MS);
vi.useRealTimers();

expect(await screen.findByText('Scan truncated')).toBeTruthy();
expect(
screen.getByText((_, node) =>
Boolean(
node instanceof HTMLElement &&
node.classList.contains('body-copy') &&
node.textContent ===
'This rule would have matched at least 3 requests in the last 24 hours; only the newest 900 events in that window were scanned.',
),
),
).toBeTruthy();
});

it('presents a complete preview count without a truncation notice', async () => {
const fetchMock = policyBackedFetch(policyFixture(), 'W/"policy-1"', {
preview: {
...previewResponseFixture(0, '/api/users/123'),
samples: [],
scan_truncated: false,
complete: true,
},
});
vi.stubGlobal('fetch', fetchMock);

renderRuleEditor();

await screen.findByLabelText('Path pattern');
vi.useFakeTimers();
fireEvent.change(screen.getByLabelText('Path pattern'), {
target: { value: '/api/users/{id}' },
});
await advanceTimersByTime(RULE_PREVIEW_DEBOUNCE_MS);
vi.useRealTimers();

expect(
await screen.findByText('No matched request samples returned.'),
).toBeTruthy();
expect(screen.queryByText('Scan truncated')).toBeNull();
expect(
screen.getByText((_, node) =>
Boolean(
node instanceof HTMLElement &&
node.classList.contains('body-copy') &&
node.textContent ===
'This rule would have matched 0 requests in the last 24 hours.',
),
),
).toBeTruthy();
});

it('keeps the newest preview when an older preview resolves last', async () => {
const previewCalls: Deferred<Response>[] = [];
const fetchMock = policyBackedFetch(policyFixture(), 'W/"policy-1"', {
Expand Down
22 changes: 20 additions & 2 deletions admin-ui/src/views/RuleEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -955,20 +955,38 @@ function PreviewResult({
response: PolicyRulePreviewResponse;
windowHours: number;
}) {
// A truncated scan counted only the newest events, so its match count is a
// lower bound for the window, never the window's count.
const truncated =
response.scan_truncated === true || response.complete === false;

return (
<div className="rule-preview-result">
<div className="rule-preview-stat">
<span className="stat-label">Matched requests</span>
<span className="stat-value">{formatCount(response.match_count)}</span>
<span className="body-copy">
This rule would have matched {formatCount(response.match_count)}{' '}
requests in the last {formatWindowHours(windowHours)}.
{truncated ? (
<>
This rule would have matched at least{' '}
{formatCount(response.match_count)} requests in the last{' '}
{formatWindowHours(windowHours)}; only the newest{' '}
{formatCount(response.scanned_event_count)} events in that window
were scanned.
</>
) : (
<>
This rule would have matched {formatCount(response.match_count)}{' '}
requests in the last {formatWindowHours(windowHours)}.
</>
)}
</span>
</div>
<div className="rule-preview-meta">
<span className="badge neutral">
{formatCount(response.scanned_event_count)} scanned
</span>
{truncated ? <span className="badge warning">Scan truncated</span> : null}
<span className="badge neutral">{response.sample_strategy}</span>
</div>
{response.samples.length === 0 ? (
Expand Down
Loading
Loading