Skip to content

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

TypeScript

fix(hcc): surface census AMBIGUOUS_PRESENT and zach88 nits

Alfredo Lopez4f39cc72cfe6
diff --git a/scripts/ops/hcc-netpol-orphan-census-clerum-dev.sh b/scripts/ops/hcc-netpol-orphan-census-clerum-dev.sh
index ba01ecf18..cedfeabbf 100755
--- a/scripts/ops/hcc-netpol-orphan-census-clerum-dev.sh
+++ b/scripts/ops/hcc-netpol-orphan-census-clerum-dev.sh
@@ -21,5 +21,24 @@ umask 077
CONTEXT=gke_your-gcp-project_us-central1-a_example-dev
export CONTEXT
-ROOT="$(cd "$(dirname "$0")" && pwd)"
+# Resolve this file, including PATH invocation and a symlink, so exec
+# finds the sibling generic script. dirname "$0" is "." when the
+# wrapper is invoked by name off PATH (R1-L4).
+_self="${BASH_SOURCE[0]:-$0}"
+if [[ "${_self}" != /* && "$(dirname "${_self}")" == "." ]]; then
+ _found="$(command -v -- "$(basename "${_self}")" 2>/dev/null || true)"
+ if [[ -n "${_found}" ]]; then
+ _self="${_found}"
+ fi
+fi
+while [[ -L "${_self}" ]]; do
+ _dir="$(cd "$(dirname "${_self}")" && pwd)"
+ _link="$(readlink "${_self}")"
+ if [[ "${_link}" == /* ]]; then
+ _self="${_link}"
+ else
+ _self="${_dir}/${_link}"
+ fi
+done
+ROOT="$(cd "$(dirname "${_self}")" && pwd)"
exec "${ROOT}/hcc-netpol-orphan-census.sh"
diff --git a/scripts/ops/hcc-netpol-orphan-census.sh b/scripts/ops/hcc-netpol-orphan-census.sh
index b7de79a90..2962f49c2 100755
--- a/scripts/ops/hcc-netpol-orphan-census.sh
+++ b/scripts/ops/hcc-netpol-orphan-census.sh
@@ -29,8 +29,13 @@
# compiled default. Do not declare the key in deploy/ to unblock the job.
#
# This census reflects on-cluster orphans by policy-type plus the
-# reserved-name repairable approximation, not the controller's current
-# pass authority gate.
+# reserved-name repairable approximation. It is not the full pass
+# authority gate, but one abort-shaped class is reported:
+# VERDICT=AMBIGUOUS_PRESENT (exit 5) when a foreign managed-by,
+# typed, reserved-name policy also carries an owner label. That
+# object stays out of listed/orphans (#484) and must not print CLEAN
+# — classifySafetyInventoryPolicy tags it ambiguous and the
+# controller aborts the pass. Other ambiguous arms stay out of scope.
#
# Cap formula must stay aligned with evaluateNetPolOrphanSweepCap in
# host-context-controller/src/networkPolicyReconciler.ts and the compiled
@@ -43,8 +48,8 @@
#
# Double-samples 90s apart. Adjudicates only when the desired Context +
# McpServer identity set is identical across samples; otherwise
-# INCONCLUSIVE_RERUN. A sample that listed zero managed policies is
-# INCONCLUSIVE_EMPTY, never CLEAN.
+# INCONCLUSIVE_RERUN. A sample that listed zero managed policies and
+# zero ambiguous-foreign objects is INCONCLUSIVE_EMPTY, never CLEAN.
#
# Usage:
# CONTEXT=<kube-context> ./scripts/ops/hcc-netpol-orphan-census.sh
@@ -151,6 +156,23 @@ CENSUS_MANAGED_JQ='
or (.metadata.name // "" | startswith("ext-egress-"))
);
def is_census_managed: is_typed or is_untyped_repairable;
+ def is_reserved:
+ (.metadata.name // "" | startswith("ctx-"))
+ or (.metadata.name // "" | startswith("rpc-egress-"))
+ or (.metadata.name // "" | startswith("ext-egress-"));
+ def has_owner:
+ (.metadata.labels["clerum.io/context"] != null)
+ or (.metadata.labels["clerum.io/mcpserver"] != null);
+ def is_ambiguous_foreign:
+ managed_by != null
+ and managed_by != "host-context-controller"
+ and (
+ policy_type == "context-allow"
+ or policy_type == "rpc-proxy-egress"
+ or policy_type == "external-egress"
+ )
+ and is_reserved
+ and has_owner;
def census_lane:
if policy_type == "external-egress"
or (policy_type == null and (.metadata.name // "" | startswith("ext-egress-")))
@@ -171,6 +193,7 @@ sample_state() {
)"
local desired_contexts desired_servers listed untyped orphans orphan_count
+ local ambiguous ambiguous_count
desired_contexts="$(printf "%s" "${contexts_json}" | jq -r '[.items[]?.spec.contextId // empty] | unique | sort | join(",")')"
desired_servers="$(printf "%s" "${servers_json}" | jq -r '[.items[]?.metadata.name // empty] | unique | sort | join(",")')"
listed="$(printf "%s" "${np_json}" | jq -r "${CENSUS_MANAGED_JQ} [.items[] | select(is_census_managed)] | length")"
@@ -200,14 +223,35 @@ sample_state() {
orphan_count="$(printf "%s\n" "${orphans}" | awk 'NF { n++ } END { print n+0 }')"
fi
+ ambiguous="$(printf "%s" "${np_json}" | jq -r "${CENSUS_MANAGED_JQ}"'
+ .items[]
+ | select(is_ambiguous_foreign)
+ | [
+ .metadata.namespace,
+ .metadata.name,
+ (.metadata.labels["clerum.io/policy-type"] // "none"),
+ (.metadata.labels["clerum.io/managed-by"] // "")
+ ]
+ | @tsv
+ ')"
+ if [[ -z "${ambiguous}" ]]; then
+ ambiguous_count=0
+ else
+ ambiguous_count="$(printf "%s\n" "${ambiguous}" | awk 'NF { n++ } END { print n+0 }')"
+ fi
+
printf "DESIRED_CONTEXTS=%s\n" "${desired_contexts}"
printf "DESIRED_SERVERS=%s\n" "${desired_servers}"
printf "LISTED_MANAGED=%s\n" "${listed}"
printf "REPAIRABLE_UNTYPED=%s\n" "${untyped}"
printf "ORPHAN_COUNT=%s\n" "${orphan_count}"
+ printf "AMBIGUOUS_COUNT=%s\n" "${ambiguous_count}"
printf "ORPHANS<<EOF\n"
printf "%s\n" "${orphans}"
printf "EOF\n"
+ printf "AMBIGUOUS<<EOF\n"
+ printf "%s\n" "${ambiguous}"
+ printf "EOF\n"
}
parse_field() {
@@ -217,14 +261,14 @@ parse_field() {
echo "[census] sample 1"
SAMPLE1="$(sample_state)"
-echo "${SAMPLE1}" | sed "/^ORPHANS<<EOF/,/^EOF/d"
+echo "${SAMPLE1}" | sed -e "/^ORPHANS<<EOF/,/^EOF/d" -e "/^AMBIGUOUS<<EOF/,/^EOF/d"
echo "[census] waiting ${SAMPLE_GAP_SEC}s for second sample"
sleep "${SAMPLE_GAP_SEC}"
echo "[census] sample 2"
SAMPLE2="$(sample_state)"
-echo "${SAMPLE2}" | sed "/^ORPHANS<<EOF/,/^EOF/d"
+echo "${SAMPLE2}" | sed -e "/^ORPHANS<<EOF/,/^EOF/d" -e "/^AMBIGUOUS<<EOF/,/^EOF/d"
CTX1="$(parse_field "${SAMPLE1}" DESIRED_CONTEXTS)"
SRV1="$(parse_field "${SAMPLE1}" DESIRED_SERVERS)"
@@ -244,10 +288,13 @@ fi
ORPHAN_COUNT="$(parse_field "${SAMPLE2}" ORPHAN_COUNT)"
LISTED="$(parse_field "${SAMPLE2}" LISTED_MANAGED)"
REPAIRABLE_UNTYPED="$(parse_field "${SAMPLE2}" REPAIRABLE_UNTYPED)"
+AMBIGUOUS_COUNT="$(parse_field "${SAMPLE2}" AMBIGUOUS_COUNT)"
echo "[census] desired set identical across samples"
-echo "[census] listed_managed=${LISTED} repairable_untyped=${REPAIRABLE_UNTYPED} orphan_count=${ORPHAN_COUNT}"
+echo "[census] listed_managed=${LISTED} repairable_untyped=${REPAIRABLE_UNTYPED} orphan_count=${ORPHAN_COUNT} ambiguous_count=${AMBIGUOUS_COUNT}"
echo "[census] orphans (namespace name policy-type):"
printf "%s" "${SAMPLE2}" | sed -n "/^ORPHANS<<EOF/,/^EOF/{ /^ORPHANS<<EOF/d; /^EOF/d; p; }"
+echo "[census] ambiguous (namespace name policy-type managed-by):"
+printf "%s" "${SAMPLE2}" | sed -n "/^AMBIGUOUS<<EOF/,/^EOF/{ /^AMBIGUOUS<<EOF/d; /^EOF/d; p; }"
# Controller compiled defaults (config.ts). Used only for
# controller_cap_would_trip, never as a substitute for live env.
@@ -321,12 +368,22 @@ CONTROLLER_CAP_REASON="$(cap_would_trip "${ORPHAN_COUNT}" "${LISTED}" "${CONTROL
echo "[census] live_cap_would_trip=${CAP_REASON}"
echo "[census] compiled_default_absolute=${COMPILED_ABS_DEFAULT} compiled_default_percent=${COMPILED_PCT_DEFAULT}"
echo "[census] controller_cap_would_trip=${CONTROLLER_CAP_REASON} (live env, else compiled defaults)"
-if [[ "${LISTED}" -eq 0 ]]; then
+if ! is_uint "${AMBIGUOUS_COUNT}"; then
+ echo "[census] VERDICT=INCONCLUSIVE_EMPTY"
+ echo "[census] ambiguous_count is not an unsigned integer; do not treat this as CLEAN" >&2
+ exit 4
+fi
+if [[ "${LISTED}" -eq 0 && "${AMBIGUOUS_COUNT}" -eq 0 ]]; then
echo "[census] VERDICT=INCONCLUSIVE_EMPTY"
echo "[census] listed zero managed NetworkPolicies; \"found nothing\" is not CLEAN"
echo "[census] hint: re-check CONTEXT, CONTEXT_MAPPER_RPC_PROXY_NAMESPACE, and kubectl connectivity"
exit 4
fi
+if [[ "${AMBIGUOUS_COUNT}" -gt 0 ]]; then
+ echo "[census] VERDICT=AMBIGUOUS_PRESENT"
+ echo "[census] foreign typed reserved+owner policy present; controller aborts the pass — not CLEAN"
+ exit 5
+fi
if [[ "${ORPHAN_COUNT}" -eq 0 ]]; then
echo "[census] VERDICT=CLEAN"
else
diff --git a/scripts/tests/test-hcc-netpol-orphan-census-guards.sh b/scripts/tests/test-hcc-netpol-orphan-census-guards.sh
index 6d98e1373..bed201ecc 100755
--- a/scripts/tests/test-hcc-netpol-orphan-census-guards.sh
+++ b/scripts/tests/test-hcc-netpol-orphan-census-guards.sh
@@ -212,16 +212,8 @@ fi
run_census "$GENERIC" CONTEXT="$TEST_CTX"
if ! grep -Fq 'UNSET (controller applies compiled default: rpc-proxy)' <<<"$OUT"; then
fail "UNSET rpc-proxy is two-tier — missing live label"
-elif ! grep -Fq 'get networkpolicy -n rpc-proxy' "$CASE_LOG" &&
- ! grep -Fq 'get networkpolicy --namespace rpc-proxy' "$CASE_LOG" &&
- ! grep -Eq -- '-n rpc-proxy get networkpolicy|get networkpolicy -n rpc-proxy' "$CASE_LOG"; then
- # kubectl is invoked as: --context X -n rpc-proxy get networkpolicy -o json
- # logged as the raw argv string from $*.
- if ! grep -Fq -- '-n rpc-proxy' "$CASE_LOG" || ! grep -Fq 'networkpolicy' "$CASE_LOG"; then
- fail "UNSET rpc-proxy is two-tier — did not sample rpc-proxy; got: $(tr '\n' '|' <"$CASE_LOG")"
- else
- pass "UNSET rpc-proxy namespace is labelled two-tier and sampled with the compiled default"
- fi
+elif ! grep -Fq -- '-n rpc-proxy' "$CASE_LOG" || ! grep -Fq 'networkpolicy' "$CASE_LOG"; then
+ fail "UNSET rpc-proxy is two-tier — did not sample rpc-proxy; got: $(tr '\n' '|' <"$CASE_LOG")"
else
pass "UNSET rpc-proxy namespace is labelled two-tier and sampled with the compiled default"
fi
@@ -240,10 +232,47 @@ fi
# G8
if grep -Fxq "CONTEXT=${PLACEHOLDER}" "$WRAPPER" &&
! grep -Eq 'CONTEXT=.*:-|\$\{CONTEXT:-' "$WRAPPER" &&
- grep -Eq 'exec .*hcc-netpol-orphan-census\.sh' "$WRAPPER"; then
+ grep -Eq 'exec .*hcc-netpol-orphan-census\.sh' "$WRAPPER" &&
+ grep -Fq 'BASH_SOURCE[0]' "$WRAPPER"; then
pass "wrapper source pins: unconditional assignment + exec of the generic census"
else
- fail "wrapper source pins — unconditional CONTEXT= or exec of the generic census is missing"
+ fail "wrapper source pins — unconditional CONTEXT=, BASH_SOURCE resolve, or exec of the generic census is missing"
+fi
+
+# G9: PATH + symlink invocation must still exec the sibling generic.
+ln -sf "$WRAPPER" "${STUB_BIN}/hcc-netpol-orphan-census-clerum-dev.sh"
+: >"$CASE_LOG"
+_g9_pwd="$(pwd)"
+cd "$WORKDIR"
+OUT="$(
+ env -i \
+ HOME="$HOME" \
+ TMPDIR="$WORKDIR" \
+ KUBECONFIG="$FAKE_KUBECONFIG" \
+ PATH="${STUB_BIN}:${PATH}" \
+ CENSUS_STUB_LOG="$CASE_LOG" \
+ SAMPLE_GAP_SEC=0 \
+ CONTEXT=gke_evil_inherited_ctx \
+ STUB_FAIL_ENV_READ=1 \
+ STUB_ENV_CONTEXT_MAPPER_NAMESPACE="$MAPPER_NS" \
+ STUB_ENV_CONTEXT_MAPPER_HOST_NAMESPACE="$HOST_NS" \
+ STUB_CONTEXTS_FIXTURE="${WORKDIR}/contexts.json" \
+ STUB_SERVERS_FIXTURE="${WORKDIR}/servers.json" \
+ STUB_NP_MAPPER_FIXTURE="${WORKDIR}/empty-np.json" \
+ STUB_NP_HOST_FIXTURE="${WORKDIR}/empty-np.json" \
+ STUB_NP_RPC_FIXTURE="${WORKDIR}/empty-np.json" \
+ bash -c 'hcc-netpol-orphan-census-clerum-dev.sh' 2>&1
+)"
+RC=$?
+cd "$_g9_pwd"
+cat "$CASE_LOG" >>"$ALL_LOG"
+if ! grep -Fq -- "--context ${PLACEHOLDER}" "$CASE_LOG" &&
+ ! grep -Fq -- "--context=${PLACEHOLDER}" "$CASE_LOG"; then
+ fail "wrapper PATH/symlink resolve — placeholder missing from argv; got: $(tr '\n' '|' <"$CASE_LOG")"
+elif grep -Fq 'gke_evil_inherited_ctx' "$CASE_LOG" || grep -Fq 'gke_evil_inherited_ctx' <<<"$OUT"; then
+ fail "wrapper PATH/symlink resolve — hostile inherited context leaked"
+else
+ pass "wrapper PATH/symlink invocation still execs the sibling generic and pins CONTEXT"
fi
if grep -Eq '(^|[[:space:]])(set|delete|apply|patch|rollout|scale|create|replace|annotate|label|exec|drain|edit|expose)([[:space:]]|$)' "$ALL_LOG"; then
diff --git a/scripts/tests/test-hcc-netpol-orphan-census-verdicts.sh b/scripts/tests/test-hcc-netpol-orphan-census-verdicts.sh
index 546451045..ca26209f0 100755
--- a/scripts/tests/test-hcc-netpol-orphan-census-verdicts.sh
+++ b/scripts/tests/test-hcc-netpol-orphan-census-verdicts.sh
@@ -221,7 +221,9 @@ jq -n '
# V7: member + typed reserved orphan without managed-by (controller
# repairable arm: type present, managed-by absent, reserved + owner).
-# Pre-fix is_typed required managed-by and printed CLEAN (listed=1).
+# Hidden only after the intra-PR #484 tightening (30070fdd), which
+# required managed-by on is_typed and printed CLEAN (listed=1).
+# Against merge-base/dev, type-only is_typed already listed this case.
jq -n '{
items: [
{
@@ -252,6 +254,26 @@ jq -n '{
]
}' >"${WORKDIR}/np-v7.json"
+# V8: only the foreign typed reserved+owner shape (no HCC member).
+# listed=0 must not collapse to INCONCLUSIVE_EMPTY / CLEAN.
+jq -n '{
+ items: [
+ {
+ metadata: {
+ uid: "uid-foreign-only",
+ namespace: "mcp-server",
+ name: "ctx-ghost-allow",
+ labels: {
+ "clerum.io/managed-by": "someone-else",
+ "clerum.io/policy-type": "context-allow",
+ "clerum.io/context": "ctx-ghost",
+ "clerum.io/mcpserver": "srv-ghost"
+ }
+ }
+ }
+ ]
+}' >"${WORKDIR}/np-v8.json"
+
run_census() {
: >"$CASE_LOG"
rm -f "${WORKDIR}/call-count-contexts"
@@ -281,14 +303,20 @@ run_census() {
# V1
run_census STUB_NP_MAPPER_FIXTURE="${WORKDIR}/np-v1.json"
-if [ "$RC" -ne 0 ]; then
- fail "foreign-owned typed policy is excluded — exit ${RC}; got: $(head -c 400 <<<"$OUT")"
+if [ "$RC" -ne 5 ]; then
+ fail "foreign-owned typed reserved+owner is AMBIGUOUS_PRESENT — exit ${RC}; got: $(head -c 400 <<<"$OUT")"
elif ! grep -Fq 'listed_managed=1' <<<"$OUT" || ! grep -Fq 'orphan_count=0' <<<"$OUT"; then
- fail "foreign-owned typed policy is excluded — listed/orphan mismatch; got: $(head -c 400 <<<"$OUT")"
-elif ! grep -Fq 'VERDICT=CLEAN' <<<"$OUT"; then
- fail "foreign-owned typed policy is excluded — missing CLEAN"
+ fail "foreign-owned typed reserved+owner is AMBIGUOUS_PRESENT — listed/orphan mismatch; got: $(head -c 400 <<<"$OUT")"
+elif ! grep -Fq 'ambiguous_count=1' <<<"$OUT"; then
+ fail "foreign-owned typed reserved+owner is AMBIGUOUS_PRESENT — ambiguous_count missing"
+elif ! grep -Fq 'VERDICT=AMBIGUOUS_PRESENT' <<<"$OUT"; then
+ fail "foreign-owned typed reserved+owner is AMBIGUOUS_PRESENT — missing verdict"
+elif grep -Fq 'VERDICT=CLEAN' <<<"$OUT"; then
+ fail "foreign-owned typed reserved+owner is AMBIGUOUS_PRESENT — printed CLEAN"
+elif ! grep -Fq $'mcp-server\tctx-ghost-allow\tcontext-allow\tsomeone-else' <<<"$OUT"; then
+ fail "foreign-owned typed reserved+owner is AMBIGUOUS_PRESENT — TSV missing; got: $(head -c 500 <<<"$OUT")"
else
- pass "foreign-owned typed policy is excluded from listed and orphans (#484)"
+ pass "foreign-owned typed reserved+owner stays out of listed/orphans and is AMBIGUOUS_PRESENT (#484 / R1-L1)"
fi
# V2
@@ -383,6 +411,20 @@ else
pass "typed reserved orphan without managed-by is listed and ORPHANS_PRESENT (RP-495-01)"
fi
+# V8
+run_census STUB_NP_MAPPER_FIXTURE="${WORKDIR}/np-v8.json"
+if [ "$RC" -ne 5 ]; then
+ fail "foreign-only ambiguous is not EMPTY — exit ${RC}; got: $(head -c 400 <<<"$OUT")"
+elif ! grep -Fq 'listed_managed=0' <<<"$OUT" || ! grep -Fq 'ambiguous_count=1' <<<"$OUT"; then
+ fail "foreign-only ambiguous is not EMPTY — counters mismatch; got: $(head -c 500 <<<"$OUT")"
+elif ! grep -Fq 'VERDICT=AMBIGUOUS_PRESENT' <<<"$OUT"; then
+ fail "foreign-only ambiguous is not EMPTY — missing AMBIGUOUS_PRESENT"
+elif grep -Fq 'VERDICT=INCONCLUSIVE_EMPTY' <<<"$OUT" || grep -Fq 'VERDICT=CLEAN' <<<"$OUT"; then
+ fail "foreign-only ambiguous is not EMPTY — printed EMPTY or CLEAN"
+else
+ pass "foreign-only typed reserved+owner is AMBIGUOUS_PRESENT, never EMPTY or CLEAN (R1-L1)"
+fi
+
if grep -Eq '(^|[[:space:]])(set|delete|apply|patch|rollout|scale|create|replace|annotate|label|exec|drain|edit|expose)([[:space:]]|$)' "$ALL_LOG"; then
fail "census stayed read-only — mutating verb recorded"
else