Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(hcc): preserve recreated ownership and strengthen recovery evidence

Alfredo Lopez51c9e9f191e5
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index 81b65874d..56d1ed569 100644
--- a/host-context-controller/package-lock.json
+++ b/host-context-controller/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-host-context-controller",
- "version": "0.1.317",
+ "version": "0.1.318",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.317",
+ "version": "0.1.318",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@clerum/codex-catalog-projection": "file:../packages/codex-catalog-projection",
diff --git a/host-context-controller/package.json b/host-context-controller/package.json
index 331c8e952..e4729c558 100644
--- a/host-context-controller/package.json
+++ b/host-context-controller/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-host-context-controller",
- "version": "0.1.317",
+ "version": "0.1.318",
"description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API",
"main": "dist/main.js",
"engines": {
diff --git a/host-context-controller/src/k8sClient.policyEligibility.integration.test.ts b/host-context-controller/src/k8sClient.policyEligibility.integration.test.ts
index 5c744aced..ec0f23aa3 100644
--- a/host-context-controller/src/k8sClient.policyEligibility.integration.test.ts
+++ b/host-context-controller/src/k8sClient.policyEligibility.integration.test.ts
@@ -828,6 +828,7 @@ describe('WATCH and recovered LIST policy effects', () => {
it('A-T6 regression: interrupted additive policy work cannot be marked complete after identical recovery', async () => {
const state = await settled()
+ const log = vi.spyOn(hccLogger, 'info')
const owner = (state as any).netPolReconciler
const desired = [...fixture.policies.entries()].find(([, policy]) =>
Object.values(policy.metadata.labels ?? {}).includes('context-allow')
@@ -874,6 +875,12 @@ describe('WATCH and recovered LIST policy effects', () => {
finish.resolve()
await pass
await drain(state)
+ const results = log.mock.calls
+ .map(([, fields]) => fields as { event?: string; result?: string })
+ .filter(fields => fields?.event === 'networkpolicy-pass-result')
+ expect(results).toContainEqual(expect.objectContaining({ result: 'aborted-authority' }))
+ expect(results).not.toContainEqual(expect.objectContaining({ result: 'failed' }))
+ log.mockRestore()
expect(state.initialConvergenceRetryTimers.has('NetworkPolicy')).toBe(true)
expect(watcher.getReadinessInventoryDetail().safetyInventoryCertified).toBe(false)
await vi.advanceTimersByTimeAsync(5000)
diff --git a/host-context-controller/src/k8sClient.test.ts b/host-context-controller/src/k8sClient.test.ts
index 772965505..698bf3834 100644
--- a/host-context-controller/src/k8sClient.test.ts
+++ b/host-context-controller/src/k8sClient.test.ts
@@ -5544,9 +5544,10 @@ describe('McpServerWatcher startup', () => {
const watcher = new McpServerWatcher()
;(watcher as any).contextCacheSynced = true
;(watcher as any).mcpServerCacheSynced = true
- mocks.netPolFullReconcile.mockRejectedValueOnce(
- new Error(DESIRED_NETWORKPOLICY_INVENTORY_CHANGED_MESSAGE)
- )
+ mocks.netPolFullReconcile.mockImplementationOnce(async () => {
+ ;(watcher as any).contextDesiredRevision += 1
+ throw new Error(DESIRED_NETWORKPOLICY_INVENTORY_CHANGED_MESSAGE)
+ })
const bumpBefore = await readLabeledConvergenceMetric(
'clerum_hcc_initial_convergence_pass_results_total',
{ lane: 'NetworkPolicy', result: 'aborted-bump' }
@@ -5564,7 +5565,7 @@ describe('McpServerWatcher startup', () => {
expect(warnSpy).toHaveBeenCalledWith(
'[K8s] pass ended without certifying: desired inventory changed',
expect.objectContaining({
- contextMoved: false,
+ contextMoved: true,
serverMoved: false,
contextCacheSynced: true,
mcpServerCacheSynced: true,
@@ -5575,6 +5576,83 @@ describe('McpServerWatcher startup', () => {
await watcher.stop()
})
+ it('names a nested all-inventory-changed AggregateError aborted-bump', async () => {
+ const errorSpy = vi.spyOn(hccLogger, 'error').mockImplementation(() => {})
+ const watcher = new McpServerWatcher()
+ ;(watcher as any).contextCacheSynced = true
+ ;(watcher as any).mcpServerCacheSynced = true
+ mocks.netPolFullReconcile.mockImplementationOnce(async () => {
+ ;(watcher as any).mcpServerDesiredRevision += 1
+ throw new AggregateError(
+ [
+ new Error(DESIRED_NETWORKPOLICY_INVENTORY_CHANGED_MESSAGE),
+ new AggregateError([new Error(DESIRED_NETWORKPOLICY_INVENTORY_CHANGED_MESSAGE)]),
+ ],
+ 'nested inventory interruption'
+ )
+ })
+ const bumpBefore = await readLabeledConvergenceMetric(
+ 'clerum_hcc_initial_convergence_pass_results_total',
+ { lane: 'NetworkPolicy', result: 'aborted-bump' }
+ )
+
+ await (watcher as any).runInitialNetworkPolicyConvergence({ cause: 'startup' })
+
+ expect(
+ await readLabeledConvergenceMetric('clerum_hcc_initial_convergence_pass_results_total', {
+ lane: 'NetworkPolicy',
+ result: 'aborted-bump',
+ })
+ ).toBe(bumpBefore + 1)
+ expect((watcher as any).initialConvergenceRetryAttempts.get('NetworkPolicy')).toBe(1)
+ errorSpy.mockRestore()
+ await watcher.stop()
+ })
+
+ it.each([
+ [
+ 'a mixed AggregateError',
+ () =>
+ new AggregateError([
+ new Error(DESIRED_NETWORKPOLICY_INVENTORY_CHANGED_MESSAGE),
+ new Error('apiserver 5xx'),
+ ]),
+ ],
+ ['an empty AggregateError', () => new AggregateError([])],
+ ])('names %s failed instead of aborted-bump', async (_name, createError) => {
+ const errorSpy = vi.spyOn(hccLogger, 'error').mockImplementation(() => {})
+ const watcher = new McpServerWatcher()
+ ;(watcher as any).contextCacheSynced = true
+ ;(watcher as any).mcpServerCacheSynced = true
+ mocks.netPolFullReconcile.mockRejectedValueOnce(createError())
+ const failedBefore = await readLabeledConvergenceMetric(
+ 'clerum_hcc_initial_convergence_pass_results_total',
+ { lane: 'NetworkPolicy', result: 'failed' }
+ )
+ const bumpBefore = await readLabeledConvergenceMetric(
+ 'clerum_hcc_initial_convergence_pass_results_total',
+ { lane: 'NetworkPolicy', result: 'aborted-bump' }
+ )
+
+ await (watcher as any).runInitialNetworkPolicyConvergence({ cause: 'startup' })
+
+ expect(
+ await readLabeledConvergenceMetric('clerum_hcc_initial_convergence_pass_results_total', {
+ lane: 'NetworkPolicy',
+ result: 'failed',
+ })
+ ).toBe(failedBefore + 1)
+ expect(
+ await readLabeledConvergenceMetric('clerum_hcc_initial_convergence_pass_results_total', {
+ lane: 'NetworkPolicy',
+ result: 'aborted-bump',
+ })
+ ).toBe(bumpBefore)
+ expect((watcher as any).initialConvergenceRetryAttempts.get('NetworkPolicy')).toBe(1)
+ errorSpy.mockRestore()
+ await watcher.stop()
+ })
+
it('fences NetworkPolicy orphan cleanup to the desired revisions captured by the pass', async () => {
const watcher = new McpServerWatcher()
;(watcher as any).contextCacheSynced = true
diff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.ts
index 47f15463c..e180352f1 100644
--- a/host-context-controller/src/k8sClient.ts
+++ b/host-context-controller/src/k8sClient.ts
@@ -261,6 +261,15 @@ function observeInitialNetworkPolicyPass(
initialConvergencePassDurationSeconds.observe({ lane: 'NetworkPolicy', result }, seconds)
}
+function isDesiredNetworkPolicyInventoryInterruption(error: unknown): boolean {
+ if (error instanceof AggregateError) {
+ return (
+ error.errors.length > 0 && error.errors.every(isDesiredNetworkPolicyInventoryInterruption)
+ )
+ }
+ return error instanceof Error && error.message === DESIRED_NETWORKPOLICY_INVENTORY_CHANGED_MESSAGE
+}
+
type NetworkPolicySafetyCertificate = {
// VESTIGIAL — retained for record shape only, never read in any decision.
// The certificate is identified SOLELY by content revision (the PR #382 fix);
@@ -3482,15 +3491,23 @@ export class McpServerWatcher implements McpServerProvider {
: '[K8s] Initial NetworkPolicy background reconciliation failed:',
{ err: error }
)
- const abortedBump =
- error instanceof Error && error.message === DESIRED_NETWORKPOLICY_INVENTORY_CHANGED_MESSAGE
- if (abortedBump && safetyCertificate) {
- hccLogger.warn(
- '[K8s] pass ended without certifying: desired inventory changed',
- this.networkPolicyInventoryMovement(safetyCertificate)
- )
+ const inventoryInterruption =
+ safetyCertificate !== undefined && isDesiredNetworkPolicyInventoryInterruption(error)
+ const movement = safetyCertificate
+ ? this.networkPolicyInventoryMovement(safetyCertificate)
+ : undefined
+ const result: InitialConvergencePassResult =
+ inventoryInterruption && movement
+ ? movement.contextMoved || movement.serverMoved
+ ? 'aborted-bump'
+ : 'aborted-authority'
+ : 'failed'
+ if (result === 'aborted-bump') {
+ hccLogger.warn('[K8s] pass ended without certifying: desired inventory changed', movement)
+ } else if (result === 'aborted-authority') {
+ hccLogger.warn('[K8s] pass ended without certifying: inventory authority lost', movement)
}
- observePass(abortedBump ? 'aborted-bump' : 'failed')
+ observePass(result)
this.networkPolicyRepairPending = true
this.scheduleInitialConvergenceRetry('NetworkPolicy')
}
diff --git a/host-context-controller/src/reconciler.managedRecreate.integration.test.ts b/host-context-controller/src/reconciler.managedRecreate.integration.test.ts
new file mode 100644
index 000000000..aa65c9052
--- /dev/null
+++ b/host-context-controller/src/reconciler.managedRecreate.integration.test.ts
@@ -0,0 +1,180 @@
+import { describe, expect, it, vi } from 'vitest'
+import { McpServerReconciler } from './reconciler'
+import type { McpServerCRD } from './types'
+
+function deferred<T>() {
+ let resolve!: (value: T) => void
+ const promise = new Promise<T>(r => {
+ resolve = r
+ })
+ return { promise, resolve }
+}
+
+function notFound(): Error & { code: number } {
+ return Object.assign(new Error('not found'), { code: 404 })
+}
+
+function server(uid: string, managed: boolean, enabled = true): McpServerCRD {
+ return {
+ name: 'ownership-recreate',
+ namespace: 'mcp-server',
+ uid,
+ generation: 1,
+ spec: {
+ contextRef: 'ctx',
+ image: 'example.invalid/runtime:v1',
+ enabled,
+ managed,
+ transport: {
+ type: 'streamableHttp',
+ url: 'http://ownership-recreate.mcp-server.svc.cluster.local:3000/mcp',
+ },
+ },
+ }
+}
+
+function hccDeployment() {
+ return {
+ metadata: {
+ name: 'ownership-recreate',
+ namespace: 'mcp-server',
+ generation: 1,
+ labels: {
+ 'clerum.io/managed-by': 'host-context-controller',
+ 'clerum.io/mcpserver': 'ownership-recreate',
+ },
+ },
+ spec: { replicas: 1 },
+ status: {
+ observedGeneration: 1,
+ replicas: 1,
+ updatedReplicas: 1,
+ readyReplicas: 1,
+ availableReplicas: 1,
+ unavailableReplicas: 0,
+ },
+ }
+}
+
+function fixture() {
+ const current = new Map<string, McpServerCRD>()
+ const deleteReadEntered = deferred<void>()
+ const releaseDeleteRead = deferred<void>()
+ let phase: 'seed' | 'delete' | 'replacement' = 'seed'
+ let replacementDeploymentReads = 0
+ const appsApi = {
+ readNamespacedDeployment: vi.fn(async () => {
+ if (phase === 'delete') {
+ deleteReadEntered.resolve()
+ await releaseDeleteRead.promise
+ return hccDeployment()
+ }
+ if (phase === 'replacement') {
+ replacementDeploymentReads += 1
+ if (replacementDeploymentReads === 1) throw notFound()
+ return hccDeployment()
+ }
+ throw notFound()
+ }),
+ createNamespacedDeployment: vi.fn(async () => hccDeployment()),
+ replaceNamespacedDeployment: vi.fn(async () => hccDeployment()),
+ deleteNamespacedDeployment: vi.fn(async () => ({})),
+ listNamespacedDeployment: vi.fn(async () => ({ items: [] })),
+ }
+ const coreApi = {
+ readNamespacedService: vi.fn(async () => {
+ throw notFound()
+ }),
+ createNamespacedService: vi.fn(async () => ({})),
+ replaceNamespacedService: vi.fn(async () => ({})),
+ deleteNamespacedService: vi.fn(async () => ({})),
+ readNamespacedConfigMap: vi.fn(async () => {
+ throw notFound()
+ }),
+ deleteNamespacedConfigMap: vi.fn(async () => ({})),
+ readNamespacedSecret: vi.fn(async () => {
+ throw notFound()
+ }),
+ }
+ const customApi = {
+ getNamespacedCustomObjectStatus: vi.fn(async () => ({
+ metadata: { resourceVersion: 'status-rv' },
+ status: { conditions: [] },
+ })),
+ patchNamespacedCustomObjectStatus: vi.fn(async () => ({})),
+ patchNamespacedCustomObject: vi.fn(async () => ({})),
+ getNamespacedCustomObject: vi.fn(async () => {
+ const live = current.get('ownership-recreate')
+ if (live) return live
+ throw notFound()
+ }),
+ }
+ const reconciler = new McpServerReconciler({} as never, {
+ appsApi: appsApi as never,
+ coreApi: coreApi as never,
+ customApi: customApi as never,
+ })
+ reconciler.setInventoryAuthority(() => ({ known: true, generation: 1 }))
+ reconciler.setResolveCurrentServer(name => current.get(name))
+ return {
+ appsApi,
+ current,
+ deleteReadEntered,
+ releaseDeleteRead,
+ reconciler,
+ setPhase(value: typeof phase) {
+ phase = value
+ },
+ }
+}
+
+async function replaceAcrossBlockedDelete(oldManaged: boolean, newManaged: boolean) {
+ const f = fixture()
+ const oldServer = server('old-uid', oldManaged, oldManaged ? false : true)
+ f.current.set(oldServer.name, oldServer)
+ await f.reconciler.reconcile(oldServer)
+ f.current.delete(oldServer.name)
+ f.setPhase('delete')
+ const deletion = f.reconciler.reconcileDelete(oldServer.name, oldServer.namespace)
+ await f.deleteReadEntered.promise
+ const replacement = server('new-uid', newManaged)
+ f.current.set(replacement.name, replacement)
+ f.releaseDeleteRead.resolve()
+ await deletion
+ f.setPhase('replacement')
+ await f.reconciler.reconcile(replacement)
+ return { ...f, replacement }
+}
+
+describe('managed ownership across same-name CRD recreation', () => {
+ it('allows managed:true to managed:false after a new UID replaces a guarded delete', async () => {
+ const f = await replaceAcrossBlockedDelete(true, false)
+ expect(f.appsApi.deleteNamespacedDeployment).not.toHaveBeenCalled()
+ expect(f.reconciler.hasIncompleteReconciliation()).toBe(false)
+ expect(f.reconciler.getStatus(f.replacement)).toMatchObject({
+ deployed: true,
+ ready: true,
+ message: 'WRC-owned runtime registered',
+ })
+ })
+
+ it('allows managed:false to managed:true after a new UID replaces a guarded delete', async () => {
+ const f = await replaceAcrossBlockedDelete(false, true)
+ expect(f.appsApi.createNamespacedDeployment).toHaveBeenCalledTimes(1)
+ expect(f.reconciler.hasIncompleteReconciliation()).toBe(false)
+ expect(f.reconciler.getStatus(f.replacement)).toMatchObject({ deployed: true, ready: true })
+ })
+
+ it('still rejects an in-place managed change for the same UID', async () => {
+ const f = fixture()
+ const oldServer = server('same-uid', false)
+ f.current.set(oldServer.name, oldServer)
+ await f.reconciler.reconcile(oldServer)
+ const edited = server('same-uid', true)
+ edited.generation = 2
+ f.current.set(edited.name, edited)
+ await f.reconciler.reconcile(edited)
+ expect(f.appsApi.createNamespacedDeployment).not.toHaveBeenCalled()
+ expect(f.reconciler.getStatus(edited).message).toContain('managed field is immutable')
+ })
+})
diff --git a/host-context-controller/src/reconciler.ts b/host-context-controller/src/reconciler.ts
index e5a52b7a9..d47b16c29 100644
--- a/host-context-controller/src/reconciler.ts
+++ b/host-context-controller/src/reconciler.ts
@@ -440,6 +440,7 @@ export class McpServerReconciler {
}
private beginStatusTracking(server: McpServerCRD): void {
+ const previousIdentity = this.statusIdentities.get(server.name)
if (!this.statusIdentityMatches(server)) {
this.statusMap.delete(server.name)
// A new CRD identity (uid/generation) retires any poll window armed for
@@ -451,6 +452,9 @@ export class McpServerReconciler {
if (poll.timer !== undefined) clearTimeout(poll.timer)
this.readinessPolls.delete(server.name)
}
+ if (previousIdentity !== undefined && previousIdentity.uid !== server.uid) {
+ this.managedSnapshot.delete(server.name)
+ }
}
this.statusIdentities.set(server.name, {
uid: server.uid,
diff --git a/scripts/e2e/_lib/hcc-watch-api-proxy.mjs b/scripts/e2e/_lib/hcc-watch-api-proxy.mjs
index 46b2b2daf..270f7bdd5 100644
--- a/scripts/e2e/_lib/hcc-watch-api-proxy.mjs
+++ b/scripts/e2e/_lib/hcc-watch-api-proxy.mjs
@@ -1,6 +1,7 @@
// Development-only transport fixture. No fabricated upstream success responses.
import fs from 'node:fs'
import https from 'node:https'
+import { createSecureContext } from 'node:tls'
import { createBookmarkObservation } from './hcc-watch-bookmarks.mjs'
export function proxyUpstreamErrorRecord(error) {
@@ -60,6 +61,8 @@ export function createProxy({
periodMs,
minAgeMs,
}) {
+ // CA bytes configure TLS trust only; they never enter the request or destination.
+ const upstreamSecureContext = createSecureContext({ ca: upstreamCa })
const streams = new Set()
const bookmarks = createBookmarkObservation()
let pause = null
@@ -118,7 +121,7 @@ export function createProxy({
port: upstreamPort,
method: request.method,
path: request.url,
- ca: upstreamCa,
+ secureContext: upstreamSecureContext,
rejectUnauthorized: true,
headers: { ...request.headers, host: upstreamHost },
agent: false,
diff --git a/scripts/e2e/_lib/hcc-watch-config.mjs b/scripts/e2e/_lib/hcc-watch-config.mjs
index 468a11d1b..8cbcf61c5 100644
--- a/scripts/e2e/_lib/hcc-watch-config.mjs
+++ b/scripts/e2e/_lib/hcc-watch-config.mjs
@@ -8,6 +8,14 @@ export const changedEnvironment = [
'KUBECONFIG',
]
const own = (object, key) => Object.prototype.hasOwnProperty.call(object, key)
+
+function environmentNames(env) {
+ const names = env.map(item => item?.name)
+ if (names.some(name => typeof name !== 'string' || !name) || new Set(names).size !== names.length)
+ throw new Error('environment_name_conflict')
+ return names
+}
+
export function snapshot(deployment) {
const pod = deployment.spec.template.spec
const container = pod.containers.find(item => item.name === 'host-context-controller')
@@ -17,10 +25,14 @@ export function snapshot(deployment) {
(container.volumeMounts ?? []).some(item => item.name === 'hcc-pr-a-config')
)
throw new Error('fixture_mount_collision')
+ const env = container.env ?? []
return {
uid: deployment.metadata.uid,
envPresent: own(container, 'env'),
- env: (container.env ?? []).filter(item => changedEnvironment.includes(item.name)),
+ // The name sequence is public structure. Values remain private unless
+ // this fixture owns the entry.
+ envNames: environmentNames(env),
+ env: env.filter(item => changedEnvironment.includes(item.name)),
volumesPresent: own(pod, 'volumes'),
volumes: pod.volumes ?? [],
mountsPresent: own(container, 'volumeMounts'),
@@ -33,6 +45,25 @@ export function restorePatch(original, deployment) {
const index = pod.containers.findIndex(item => item.name === 'host-context-controller')
if (index < 0) throw new Error('deployment_container_missing')
const container = pod.containers[index]
+ const currentEnv = container.env ?? []
+ const currentEnvNames = environmentNames(currentEnv)
+ const originalEnvNames = original.envNames
+ if (
+ !Array.isArray(originalEnvNames) ||
+ originalEnvNames.some(name => typeof name !== 'string' || !name) ||
+ new Set(originalEnvNames).size !== originalEnvNames.length
+ )
+ throw new Error('environment_snapshot_invalid')
+ const originalUnrelatedNames = originalEnvNames.filter(name => !changedEnvironment.includes(name))
+ const currentUnrelatedNames = currentEnvNames.filter(name => !changedEnvironment.includes(name))
+ if (JSON.stringify(currentUnrelatedNames) !== JSON.stringify(originalUnrelatedNames))
+ throw new Error('unrelated_environment_changed')
+ const originalOwnedNames = originalEnvNames.filter(name => changedEnvironment.includes(name))
+ if (
+ !Array.isArray(original.env) ||
+ JSON.stringify(environmentNames(original.env)) !== JSON.stringify(originalOwnedNames)
+ )
+ throw new Error('environment_snapshot_invalid')
for (const [current, previous] of [
[pod.volumes ?? [], original.volumes],
[container.volumeMounts ?? [], original.mounts],
@@ -58,10 +89,13 @@ export function restorePatch(original, deployment) {
if (present) patches.push({ op: 'add', path, value })
else if (existed) patches.push({ op: 'remove', path })
}
- const env = [
- ...(container.env ?? []).filter(item => !changedEnvironment.includes(item.name)),
- ...original.env,
- ]
+ const originalEnvironment = new Map(original.env.map(item => [item.name, item]))
+ const currentEnvironment = new Map(currentEnv.map(item => [item.name, item]))
+ const env = originalEnvNames.map(name =>
+ changedEnvironment.includes(name)
+ ? originalEnvironment.get(name)
+ : currentEnvironment.get(name)
+ )
set(
`/spec/template/spec/containers/${index}/env`,
original.envPresent || env.length > 0,
@@ -88,8 +122,6 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
else if (action === 'verify') {
const original = JSON.parse(fs.readFileSync(originalPath, 'utf8'))
const current = snapshot(deployment)
- // Environment ordering has no semantic meaning; each exact entry does.
- for (const value of [original, current]) value.env.sort((a, b) => a.name.localeCompare(b.name))
if (JSON.stringify(current) !== JSON.stringify(original))
throw new Error('configuration_not_restored')
} else throw new Error('unknown_configuration_action')
diff --git a/scripts/e2e/_lib/hcc-watch-pr-a.sh b/scripts/e2e/_lib/hcc-watch-pr-a.sh
index 7ba1585a5..22a3511c0 100755
--- a/scripts/e2e/_lib/hcc-watch-pr-a.sh
+++ b/scripts/e2e/_lib/hcc-watch-pr-a.sh
@@ -205,8 +205,8 @@ hcc_pr_a_run() {
np604_invoke "$NP604_SERVER" || die 'PR A interrupted runtime business result failed'
printf 'PR_A_INTERRUPTED_RUNTIME=PASS\n'
- affected_policy="$(np604_snapshot "$NP604_SERVER" | jq -er '[.[]|select(.type=="external-egress")]|if length==1 then .[0].name else error("egress identity ambiguous") end')"
- hcc_pr_a_command arm "/apis/networking.k8s.io/v1/namespaces/${MCP_NS}/networkpolicies/${affected_policy}"
+ affected_policy="$(hcc_pr_a_capture_egress_identity)" || die 'PR A egress identity ambiguous'
+ hcc_pr_a_command arm "/apis/networking.k8s.io/v1/namespaces/${MCP_NS}/networkpolicies/$(jq -r '.name' <<< "$affected_policy")"
pause_id=$HCC_PR_A_COMMAND_ID
wait_until 5 'egress pause armed' hcc_pr_a_ack armed || die 'PR A egress pause not armed'
np604_kctl patch mcpserver "$NP604_SERVER" -n "$MCP_NS" --type=merge \
@@ -221,7 +221,7 @@ hcc_pr_a_run() {
hcc_pr_a_command release "$pause_id"
wait_until 5 'egress pause released' hcc_pr_a_ack released || die 'PR A egress release not acknowledged'
wait_until 25 'protected API recertifies without revoked server' hcc_pr_a_gate 200 false || die 'PR A protected gate did not reopen with revoked inventory'
- np604_snapshot "$NP604_SERVER" | jq -e 'all(.[]; .type=="external-egress")' >/dev/null || die 'PR A stale Context allows remained'
+ hcc_pr_a_only_captured_egress_remains "$affected_policy" || die 'PR A revoked policy set differs from captured egress identity'
np604_kctl patch context "$NP604_CONTEXT" -n "$MCP_NS" --type=merge \
-p "$(jq -cn --argjson names "$context_servers" '{spec:{mcpServers:$names}}')" >/dev/null
wait_until 20 'protected API restores desired binding' hcc_pr_a_gate 200 || die 'PR A desired binding not restored'
@@ -301,6 +301,38 @@ hcc_pr_a_service_restored() {
'.metadata.uid!=$uid and .metadata.labels["clerum.io/mcpserver"]==$name and any(.spec.ports[]; .port==3000)' >/dev/null
}
+hcc_pr_a_affected_policy_identities() {
+ np604_kctl get networkpolicy -A \
+ -l "clerum.io/mcpserver=${NP604_SERVER},clerum.io/managed-by=host-context-controller" -o json |
+ jq -Sce '
+ [.items[] | {
+ namespace:.metadata.namespace,
+ name:.metadata.name,
+ type:.metadata.labels["clerum.io/policy-type"],
+ uid:.metadata.uid,
+ spec
+ }] |
+ if all(.[]; (.namespace|type)=="string" and (.namespace|length)>0 and
+ (.name|type)=="string" and (.name|length)>0 and
+ (.type|type)=="string" and (.type|length)>0 and
+ (.uid|type)=="string" and (.uid|length)>0 and
+ (.spec|type)=="object")
+ then sort_by(.namespace,.name)
+ else error("affected policy identity missing")
+ end'
+}
+
+hcc_pr_a_capture_egress_identity() {
+ hcc_pr_a_affected_policy_identities | jq -Sce '
+ [.[] | select(.type=="external-egress")] |
+ if length==1 then .[0] else error("egress identity ambiguous") end'
+}
+
+hcc_pr_a_only_captured_egress_remains() {
+ local captured=$1
+ hcc_pr_a_affected_policy_identities | jq -Sce --argjson captured "$captured" '. == [$captured]' >/dev/null
+}
+
hcc_pr_a_remove_service() {
HCC_PR_A_SERVICE_UID="$(np604_kctl get service "$NP604_SERVER" -n "$MCP_NS" -o json |
jq -er --arg name "$NP604_SERVER" 'select(.metadata.labels["clerum.io/mcpserver"]==$name)|.metadata.uid')" || die 'PR A Service ownership missing'
diff --git a/scripts/e2e/_lib/hcc-watch-tls-manifest.mjs b/scripts/e2e/_lib/hcc-watch-tls-manifest.mjs
index c2657c20c..cd88faab6 100644
--- a/scripts/e2e/_lib/hcc-watch-tls-manifest.mjs
+++ b/scripts/e2e/_lib/hcc-watch-tls-manifest.mjs
@@ -1,47 +1,13 @@
// Public configuration and an ephemeral test key go directly to kubectl stdin.
// Never redirect this program's output to an artifact or log.
-import { spawnSync } from 'node:child_process'
-import { generateKeyPairSync } from 'node:crypto'
+import { createFixtureTls } from './hcc-watch-tls.mjs'
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
const [name, namespace, run, proxySource] = process.argv.slice(2)
for (const value of [name, namespace, run])
if (!/^[a-z0-9][a-z0-9.-]{0,62}$/.test(value ?? '')) throw new Error('invalid_fixture_identity')
-const { privateKey } = generateKeyPairSync('rsa', {
- modulusLength: 2048,
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
- publicKeyEncoding: { type: 'spki', format: 'pem' },
-})
-// Linux cannot reopen Node's socket-backed stdin through /dev/stdin. A POSIX
-// pipe keeps the ephemeral key in memory and lets OpenSSL read it portably.
-const signed = spawnSync(
- '/bin/sh',
- [
- '-c',
- "exec 3<&0; trap 'kill \"$signer\" 2>/dev/null; wait \"$signer\" 2>/dev/null; exit 143' TERM; cat <&3 | openssl \"$@\" & signer=$!; exec 3<&-; wait \"$signer\"",
- 'openssl',
- 'req',
- '-new',
- '-x509',
- '-key',
- '/dev/stdin',
- '-days',
- '1',
- '-subj',
- `/CN=${name}.${namespace}.svc`,
- '-addext',
- `subjectAltName=DNS:${name}.${namespace}.svc`,
- '-addext',
- 'basicConstraints=critical,CA:TRUE',
- '-addext',
- 'keyUsage=critical,digitalSignature,keyEncipherment,keyCertSign',
- ],
- { input: privateKey, encoding: 'utf8', timeout: 15000, maxBuffer: 1048576 }
-)
-if (signed.status !== 0 || !signed.stdout.includes('BEGIN CERTIFICATE'))
- throw new Error('fixture_certificate_generation_failed')
-const cert = signed.stdout
+const { key: privateKey, cert } = createFixtureTls(`${name}.${namespace}.svc`)
const labels = { 'e2e.clerum.io/suite': 'hcc-watch-churn', 'e2e.clerum.io/run': run }
const metadata = { name, namespace, labels }
const config = {
diff --git a/scripts/e2e/_lib/hcc-watch-tls.mjs b/scripts/e2e/_lib/hcc-watch-tls.mjs
new file mode 100644
index 000000000..5fba6a413
--- /dev/null
+++ b/scripts/e2e/_lib/hcc-watch-tls.mjs
@@ -0,0 +1,37 @@
+// Development certificates stay in memory; never log or persist this result.
+import { spawnSync } from 'node:child_process'
+import { createPrivateKey, X509Certificate } from 'node:crypto'
+import { isIP } from 'node:net'
+
+export function createFixtureTls(hostname) {
+ const ip = typeof hostname === 'string' && isIP(hostname)
+ if (!ip && (typeof hostname !== 'string' ||
+ !/^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(hostname))) {
+ throw new Error('invalid_fixture_hostname')
+ }
+ // Direct argv avoids a shell and Node's socket-backed stdin on Linux.
+ // OpenSSL writes both PEM blocks to captured stdout; no key file is created.
+ const generated = spawnSync('openssl', [
+ 'req', '-new', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', '-',
+ '-days', '1', '-subj', `/CN=${hostname}`,
+ '-addext', `subjectAltName=${ip ? 'IP' : 'DNS'}:${hostname}`,
+ '-addext', 'basicConstraints=critical,CA:TRUE',
+ '-addext', 'keyUsage=critical,digitalSignature,keyEncipherment,keyCertSign',
+ ], { encoding: 'utf8', timeout: 15000, maxBuffer: 1048576, shell: false })
+ try {
+ if (generated.status !== 0) throw new Error()
+ const pem = /-----BEGIN (PRIVATE KEY|RSA PRIVATE KEY|CERTIFICATE)-----[\s\S]+?-----END \1-----/g
+ const blocks = generated.stdout.match(pem) ?? []
+ if (blocks.length !== 2 || generated.stdout.replace(pem, '').trim()) throw new Error()
+ const cert = blocks.find(block => block.startsWith('-----BEGIN CERTIFICATE-----'))
+ const key = blocks.find(block => block !== cert)
+ if (!key || !cert) throw new Error()
+ const certificate = new X509Certificate(cert)
+ if (!certificate.checkPrivateKey(createPrivateKey(key)) ||
+ !(ip ? certificate.checkIP(hostname) : certificate.checkHost(hostname))) throw new Error()
+ return { key, cert }
+ } catch {
+ // Do not expose captured output or private material in error diagnostics.
+ throw new Error('fixture_certificate_generation_failed')
+ }
+}
diff --git a/scripts/tests/hcc-watch-api-proxy.test.mjs b/scripts/tests/hcc-watch-api-proxy.test.mjs
index acdd8d356..a4bfe0b0b 100644
--- a/scripts/tests/hcc-watch-api-proxy.test.mjs
+++ b/scripts/tests/hcc-watch-api-proxy.test.mjs
@@ -1,6 +1,7 @@
+import { createFixtureTls } from '../e2e/_lib/hcc-watch-tls.mjs'
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
-import { X509Certificate, generateKeyPairSync } from 'node:crypto'
+import { X509Certificate } from 'node:crypto'
import { once } from 'node:events'
import {
existsSync,
@@ -301,40 +302,7 @@ test('projected-file symlink CLI enters startup and fails loud without fixture m
})
function certificate() {
- const { privateKey } = generateKeyPairSync('rsa', {
- modulusLength: 2048,
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
- publicKeyEncoding: { type: 'spki', format: 'pem' },
- })
- const result = spawnSync(
- '/bin/sh',
- [
- '-c',
- "exec 3<&0; trap 'kill \"$signer\" 2>/dev/null; wait \"$signer\" 2>/dev/null; exit 143' TERM; cat <&3 | openssl \"$@\" & signer=$!; exec 3<&-; wait \"$signer\"",
- 'openssl',
- 'req',
- '-new',
- '-x509',
- '-key',
- '/dev/stdin',
- '-days',
- '1',
- '-subj',
- '/CN=localhost',
- '-addext',
- 'subjectAltName=DNS:localhost,IP:127.0.0.1',
- '-addext',
- 'basicConstraints=critical,CA:TRUE',
- ],
- {
- input: privateKey,
- encoding: 'utf8',
- timeout: 15000,
- maxBuffer: 1048576,
- }
- )
- assert.equal(result.status, 0, 'in-memory certificate generation succeeds')
- return { key: privateKey, cert: result.stdout }
+ return createFixtureTls('127.0.0.1')
}
before(() => {
@@ -423,11 +391,12 @@ function ack(controlDir, id, state, filename = 'ack.json') {
read()
})
}
-async function fixture(run, { wrongCa = false } = {}) {
+async function fixture(run, { wrongCa = false, wrongHostname = false } = {}) {
+ const upstreamCredentials = wrongHostname ? createFixtureTls('other.invalid') : upstreamTls
const controlDir = mkdtempSync(join(tmpdir(), 'hcc-proxy-test-'))
const requests = []
const responses = new Map()
- const upstream = https.createServer(upstreamTls, (request, response) => {
+ const upstream = https.createServer(upstreamCredentials, (request, response) => {
requests.push({
path: request.url,
method: request.method,
@@ -450,7 +419,7 @@ async function fixture(run, { wrongCa = false } = {}) {
await new Promise(resolveListen => upstream.listen(0, '127.0.0.1', resolveListen))
const proxy = createProxy({
...front,
- upstreamCa: wrongCa ? front.cert : upstreamTls.cert,
+ upstreamCa: wrongCa ? front.cert : upstreamCredentials.cert,
upstreamHost: '127.0.0.1',
upstreamPort: upstream.address().port,
allowedPaths: [allowedPath],
@@ -605,6 +574,18 @@ test('wrong upstream CA cannot produce a successful response', { timeout: 10000
{ wrongCa: true }
)
})
+test('trusted CA with wrong upstream hostname cannot forward a request', { timeout: 10000 }, async () => {
+ await fixture(
+ async ({ request, requests }) => {
+ await assert.rejects(
+ bounded(request('/status').completed, 'untrusted response did not settle'),
+ { code: 'ECONNRESET' }
+ )
+ assert.equal(requests.length, 0)
+ },
+ { wrongHostname: true }
+ )
+})
test(
'exact GET pause acknowledges interception and release while watches keep flowing',
{ timeout: 10000 },
diff --git a/scripts/tests/hcc-watch-bookmarks.test.mjs b/scripts/tests/hcc-watch-bookmarks.test.mjs
index caf3490b4..85ad3513e 100644
--- a/scripts/tests/hcc-watch-bookmarks.test.mjs
+++ b/scripts/tests/hcc-watch-bookmarks.test.mjs
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
-import { generateKeyPairSync } from 'node:crypto'
+import { createFixtureTls } from '../e2e/_lib/hcc-watch-tls.mjs'
import { once } from 'node:events'
import { mkdtempSync, readFileSync, rmSync, watch, writeFileSync } from 'node:fs'
import https from 'node:https'
@@ -121,35 +121,7 @@ test('observation preserves streamed bytes including malformed frames and backpr
})
test('proxy observes the actual verified HTTPS response and publishes only a bounded summary', async () => {
- const { privateKey } = generateKeyPairSync('rsa', {
- modulusLength: 2048,
- privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
- publicKeyEncoding: { type: 'spki', format: 'pem' },
- })
- const generated = spawnSync(
- '/bin/sh',
- [
- '-c',
- "exec 3<&0; trap 'kill \"$signer\" 2>/dev/null; wait \"$signer\" 2>/dev/null; exit 143' TERM; cat <&3 | openssl \"$@\" & signer=$!; exec 3<&-; wait \"$signer\"",
- 'openssl',
- 'req',
- '-new',
- '-x509',
- '-key',
- '/dev/stdin',
- '-days',
- '1',
- '-subj',
- '/CN=localhost',
- '-addext',
- 'subjectAltName=DNS:localhost,IP:127.0.0.1',
- '-addext',
- 'basicConstraints=critical,CA:TRUE',
- ],
- { input: privateKey, encoding: 'utf8', timeout: 15000 }
- )
- assert.equal(generated.status, 0, 'in-memory TLS fixture generation')
- const credentials = { key: privateKey, cert: generated.stdout }
+ const credentials = createFixtureTls('127.0.0.1')
const controlDir = mkdtempSync(join(tmpdir(), 'hcc-bookmark-observation-'))
const upstream = https.createServer(credentials, (_request, response) => {
response.writeHead(200, headers)
diff --git a/scripts/tests/hcc-watch-config.test.mjs b/scripts/tests/hcc-watch-config.test.mjs
new file mode 100644
index 000000000..132f7316b
--- /dev/null
+++ b/scripts/tests/hcc-watch-config.test.mjs
@@ -0,0 +1,181 @@
+import assert from 'node:assert/strict'
+import { spawnSync } from 'node:child_process'
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { dirname, join, resolve } from 'node:path'
+import { test } from 'node:test'
+import { fileURLToPath } from 'node:url'
+import { restorePatch, snapshot } from '../e2e/_lib/hcc-watch-config.mjs'
+
+const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..')
+const config = join(root, 'scripts/e2e/_lib/hcc-watch-config.mjs')
+
+function deployment(env) {
+ return {
+ metadata: { uid: 'deployment-uid', resourceVersion: '1' },
+ spec: {
+ template: {
+ spec: { containers: [{ name: 'host-context-controller', env }] },
+ },
+ },
+ }
+}
+
+function restoredEnvironment(original, current) {
+ const patch = restorePatch(original, current).find(patch => patch.path.endsWith('/env'))
+ assert.ok(patch, 'restore must write the environment')
+ return patch.value
+}
+
+test('config snapshot records public env order but values only for fixture-owned variables', () => {
+ const saved = snapshot(
+ deployment([
+ { name: 'DEPENDENCY_URL', value: 'private-dependency-url' },
+ { name: 'KUBECONFIG', value: '/private/kubeconfig' },
+ { name: 'AFTER_DEPENDENCY', valueFrom: { secretKeyRef: { name: 'private', key: 'value' } } },
+ ])
+ )
+ assert.deepEqual(saved.envNames, ['DEPENDENCY_URL', 'KUBECONFIG', 'AFTER_DEPENDENCY'])
+ assert.deepEqual(saved.env, [{ name: 'KUBECONFIG', value: '/private/kubeconfig' }])
+ const serialized = JSON.stringify(saved)
+ assert.equal(serialized.includes('private-dependency-url'), false)
+ assert.equal(serialized.includes('secretKeyRef'), false)
+})
+
+test('restore rebuilds the original dependency order and preserves current unrelated values', () => {
+ const initial = deployment([
+ { name: 'DEPENDENCY_URL', value: 'initial-url' },
+ { name: 'KUBECONFIG', value: '/initial/config' },
+ { name: 'AFTER_DEPENDENCY', value: '$(KUBECONFIG)/initial-after' },
+ { name: 'CONTEXT_MAPPER_NETPOL_RESYNC_SEC', value: '30' },
+ ])
+ const saved = snapshot(initial)
+ const current = deployment([
+ { name: 'DEPENDENCY_URL', value: 'concurrently-edited-url' },
+ { name: 'KUBECONFIG', value: '/fixture/config' },
+ { name: 'AFTER_DEPENDENCY', value: '$(KUBECONFIG)/concurrently-edited-after' },
+ { name: 'CONTEXT_MAPPER_NETPOL_RESYNC_SEC', value: '60' },
+ { name: 'CONTEXT_MAPPER_K8S_API_CIDRS', value: '1.2.3.4/32' },
+ ])
+ const env = restoredEnvironment(saved, current)
+ assert.deepEqual(
+ env.map(item => item.name),
+ ['DEPENDENCY_URL', 'KUBECONFIG', 'AFTER_DEPENDENCY', 'CONTEXT_MAPPER_NETPOL_RESYNC_SEC']
+ )
+ assert.equal(env[0].value, 'concurrently-edited-url')
+ assert.equal(env[2].value, '$(KUBECONFIG)/concurrently-edited-after')
+ assert.equal(env[1].value, '/initial/config')
+ assert.equal(env[3].value, '30')
+})
+
+test('restore removes fixture-only variables that were absent from the original environment', () => {
+ const saved = snapshot(deployment([{ name: 'DEPENDENCY_URL', value: 'initial' }]))
+ const current = deployment([
+ { name: 'DEPENDENCY_URL', value: 'edited' },
+ { name: 'KUBECONFIG', value: '/fixture/config' },
+ { name: 'CONTEXT_MAPPER_NETPOL_RESYNC_SEC', value: '60' },
+ { name: 'CONTEXT_MAPPER_K8S_API_CIDRS', value: '1.2.3.4/32' },
+ ])
+ assert.deepEqual(restoredEnvironment(saved, current), [{ name: 'DEPENDENCY_URL', value: 'edited' }])
+})
+
+test('restore rejects unrelated name additions, removals, reordering and duplicates', () => {
+ const saved = snapshot(
+ deployment([
+ { name: 'FIRST', value: 'one' },
+ { name: 'KUBECONFIG', value: '/original' },
+ { name: 'SECOND', value: 'two' },
+ ])
+ )
+ const variants = [
+ [{ name: 'FIRST', value: 'one' }, { name: 'KUBECONFIG', value: '/fixture' }],
+ [
+ { name: 'FIRST', value: 'one' },
+ { name: 'KUBECONFIG', value: '/fixture' },
+ { name: 'SECOND', value: 'two' },
+ { name: 'NEW', value: 'three' },
+ ],
+ [
+ { name: 'SECOND', value: 'two' },
+ { name: 'KUBECONFIG', value: '/fixture' },
+ { name: 'FIRST', value: 'one' },
+ ],
+ [
+ { name: 'FIRST', value: 'one' },
+ { name: 'KUBECONFIG', value: '/fixture' },
+ { name: 'SECOND', value: 'two' },
+ { name: 'SECOND', value: 'duplicate' },
+ ],
+ ]
+ for (const env of variants)
+ assert.throws(() => restorePatch(saved, deployment(env)), /environment_(name_conflict|changed)/)
+})
+
+test('snapshot rejects duplicate environment names', () => {
+ assert.throws(
+ () => snapshot(deployment([{ name: 'DUPLICATE', value: 'one' }, { name: 'DUPLICATE', value: 'two' }])),
+ /environment_name_conflict/
+ )
+})
+
+test('restore rejects a malformed snapshot that omits an originally owned variable', () => {
+ const saved = snapshot(
+ deployment([{ name: 'KUBECONFIG', value: '/original' }, { name: 'DEPENDENCY', value: 'value' }])
+ )
+ saved.env = []
+ assert.throws(
+ () => restorePatch(saved, deployment([{ name: 'KUBECONFIG', value: '/fixture' }, { name: 'DEPENDENCY', value: 'value' }])),
+ /environment_snapshot_invalid/
+ )
+})
+
+test('verify CLI rejects a restored environment with reordered names', () => {
+ const directory = mkdtempSync(join(tmpdir(), 'hcc-watch-config-'))
+ try {
+ const original = deployment([
+ { name: 'FIRST', value: 'one' },
+ { name: 'KUBECONFIG', value: '/original' },
+ { name: 'SECOND', value: 'two' },
+ ])
+ const snapshotPath = join(directory, 'snapshot.json')
+ writeFileSync(snapshotPath, JSON.stringify(snapshot(original)))
+ const reordered = deployment([
+ { name: 'SECOND', value: 'two' },
+ { name: 'KUBECONFIG', value: '/original' },
+ { name: 'FIRST', value: 'one' },
+ ])
+ const result = spawnSync(process.execPath, [config, 'verify', snapshotPath], {
+ input: JSON.stringify(reordered),
+ encoding: 'utf8',
+ })
+ assert.equal(result.status, 1)
+ assert.match(result.stderr, /configuration_not_restored/)
+ } finally {
+ rmSync(directory, { recursive: true, force: true })
+ }
+})
+
+test('verify CLI accepts unchanged names with concurrent unrelated value edits', () => {
+ const directory = mkdtempSync(join(tmpdir(), 'hcc-watch-config-'))
+ try {
+ const original = deployment([
+ { name: 'DEPENDENCY_URL', value: 'initial-url' },
+ { name: 'KUBECONFIG', value: '/original' },
+ { name: 'AFTER_DEPENDENCY', value: '$(KUBECONFIG)/initial-after' },
+ ])
+ const snapshotPath = join(directory, 'snapshot.json')
+ writeFileSync(snapshotPath, JSON.stringify(snapshot(original)))
+ const current = deployment([
+ { name: 'DEPENDENCY_URL', value: 'concurrently-edited-url' },
+ { name: 'KUBECONFIG', value: '/original' },
+ { name: 'AFTER_DEPENDENCY', value: '$(KUBECONFIG)/concurrently-edited-after' },
+ ])
+ const result = spawnSync(process.execPath, [config, 'verify', snapshotPath], {
+ input: JSON.stringify(current),
+ encoding: 'utf8',
+ })
+ assert.equal(result.status, 0, result.stderr)
+ } finally {
+ rmSync(directory, { recursive: true, force: true })
+ }
+})
diff --git a/scripts/tests/hcc-watch-tls.test.mjs b/scripts/tests/hcc-watch-tls.test.mjs
new file mode 100644
index 000000000..6855cffa8
--- /dev/null
+++ b/scripts/tests/hcc-watch-tls.test.mjs
@@ -0,0 +1,22 @@
+import assert from 'node:assert/strict'
+import { createPrivateKey, X509Certificate } from 'node:crypto'
+import test from 'node:test'
+import { createFixtureTls } from '../e2e/_lib/hcc-watch-tls.mjs'
+
+for (const hostname of ['proxy-fixture.test-fixture.svc', '127.0.0.1', '::1']) {
+ test(`direct fixture signing validates its key and exact identity: ${hostname}`, () => {
+ const result = createFixtureTls(hostname)
+ const cert = new X509Certificate(result.cert)
+ assert.equal(cert.checkPrivateKey(createPrivateKey(result.key)), true)
+ assert.equal(cert.checkHost('unrelated.invalid'), undefined)
+ assert.equal(result.cert.includes('PRIVATE KEY'), false)
+ assert.equal(result.key.includes('CERTIFICATE'), false)
+ })
+}
+
+for (const hostname of [undefined, null, true, '', '-flag', 'valid;echo injected',
+ 'valid/../../path', 'a..b', 'a\nb', 'a'.repeat(64) + '.svc', 'x.'.repeat(127) + 'x']) {
+ test(`invalid TLS identity is rejected before signing: ${JSON.stringify(hostname)}`, () => {
+ assert.throws(() => createFixtureTls(hostname), /invalid_fixture_hostname/)
+ })
+}
diff --git a/scripts/tests/test-hcc-watch-api-proxy.sh b/scripts/tests/test-hcc-watch-api-proxy.sh
index 1d667f3f2..cb357a366 100755
--- a/scripts/tests/test-hcc-watch-api-proxy.sh
+++ b/scripts/tests/test-hcc-watch-api-proxy.sh
@@ -5,4 +5,4 @@ command -v openssl >/dev/null || { echo 'FAIL: openssl is required' >&2; exit 1;
node -e 'if (Number(process.versions.node.split(".")[0]) !== 24) { console.error("FAIL: Node 24 is required"); process.exit(1) }'
# Test certificates and the generator output stay in Node memory; never tee or
# redirect the generator itself to any artifact. The reporter prints test results.
-exec node --test --test-timeout=15000 "$SCRIPT_DIR/hcc-watch-api-proxy.test.mjs" "$SCRIPT_DIR/hcc-watch-bookmarks.test.mjs"
+exec node --test --test-timeout=15000 "$SCRIPT_DIR/hcc-watch-api-proxy.test.mjs" "$SCRIPT_DIR/hcc-watch-bookmarks.test.mjs" "$SCRIPT_DIR/hcc-watch-tls.test.mjs" "$SCRIPT_DIR/hcc-watch-config.test.mjs"
diff --git a/scripts/tests/test-hcc-watch-pr-a.sh b/scripts/tests/test-hcc-watch-pr-a.sh
index 31bcecf65..8f82a516a 100755
--- a/scripts/tests/test-hcc-watch-pr-a.sh
+++ b/scripts/tests/test-hcc-watch-pr-a.sh
@@ -65,6 +65,37 @@ if hcc_pr_a_service_absent; then echo 'FAIL: delete request replaced absence pro
hcc_pr_a_service_absent
printf 'PASS: actual absence and new correctly scoped Service are required\n'
+# The revocation witness must retain exactly the captured egress object. The
+# fixture invokes the production helpers against API-shaped NetworkPolicy JSON.
+NP604_SERVER=fixture-affected
+policy() {
+ local name=$1 type=$2 uid=$3 cidr=${4:-1.2.3.4/32}
+ jq -cn --arg name "$name" --arg type "$type" --arg uid "$uid" --arg cidr "$cidr" \
+ '{metadata:{namespace:"mcp-server",name:$name,uid:$uid,labels:{"clerum.io/policy-type":$type}},spec:{podSelector:{matchLabels:{"clerum.io/mcpserver":"fixture-affected"}},egress:[{to:[{ipBlock:{cidr:$cidr}}]}]}}'
+}
+egress="$(policy fixture-egress external-egress uid-egress)"
+other="$(policy fixture-deny deny-all uid-deny)"
+np604_kctl() { cat "$tmp/policies"; }
+jq -cn --argjson egress "$egress" --argjson other "$other" '{items:[$egress,$other]}' > "$tmp/policies"
+captured="$(hcc_pr_a_capture_egress_identity)"
+jq -e --argjson egress "$egress" '. == {namespace:"mcp-server",name:"fixture-egress",type:"external-egress",uid:"uid-egress",spec:$egress.spec}' <<< "$captured" >/dev/null
+for variant in exact empty replaced-uid spec-drift stale-allows duplicate; do
+ case "$variant" in
+ exact) jq -cn --argjson egress "$egress" '{items:[$egress]}' ;;
+ empty) jq -cn '{items:[]}' ;;
+ replaced-uid) jq -cn --argjson egress "$(policy fixture-egress external-egress replacement)" '{items:[$egress]}' ;;
+ spec-drift) jq -cn --argjson egress "$(policy fixture-egress external-egress uid-egress 9.9.9.9/32)" '{items:[$egress]}' ;;
+ stale-allows) jq -cn --argjson egress "$egress" --argjson other "$other" '{items:[$egress,$other]}' ;;
+ duplicate) jq -cn --argjson egress "$egress" '{items:[$egress,$egress]}' ;;
+ esac > "$tmp/policies"
+ if hcc_pr_a_only_captured_egress_remains "$captured"; then
+ [[ "$variant" = exact ]] || { echo "FAIL: revocation witness accepted $variant" >&2; exit 1; }
+ else
+ [[ "$variant" != exact ]] || { echo 'FAIL: revocation witness rejected exact egress' >&2; exit 1; }
+ fi
+done
+printf 'PASS: revocation retains exactly one captured egress identity and rejects empty, replacement, drift, stale allows and duplicates\n'
+
# Execute the scenario until its first arm command with harmless boundary
# doubles. This proves the selected read is before Service repair, rather
# than searching a comment or passing through an unexecuted branch.