Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(np-08): harden rendered authorization contract
Alfredo Lopez91887816d5f1
diff --git a/.github/workflows/ci-public.yml b/.github/workflows/ci-public.ymlindex f480a5818..8d8de320f 100644--- a/.github/workflows/ci-public.yml+++ b/.github/workflows/ci-public.yml@@ -547,6 +547,15 @@ jobs:kubectl kustomize deploy/overlays/minikube > /tmp/clerum-minikube.yamlkubectl kustomize deploy/overlays/minikube-no-uis > /tmp/clerum-minikube-no-uis.yaml+ - name: Validate NP-08 caller-bound HCC contract+ run: |+ bash scripts/tests/test-np08-render-contract.sh \+ /tmp/clerum-minikube.yaml \+ /tmp/clerum-minikube-no-uis.yaml++ - name: Test NP-08 render contract rejection fixtures+ run: bash scripts/tests/test-np08-render-contract-fixtures.sh /tmp/clerum-minikube.yaml+- name: cloudflared must not render into the minikube overlaysrun: |for f in /tmp/clerum-minikube.yaml /tmp/clerum-minikube-no-uis.yaml; dodiff --git a/scripts/tests/test-np08-render-contract-fixtures.sh b/scripts/tests/test-np08-render-contract-fixtures.shnew file mode 100755index 000000000..f3bc2757f--- /dev/null+++ b/scripts/tests/test-np08-render-contract-fixtures.sh@@ -0,0 +1,99 @@+#!/usr/bin/env bash+set -euo pipefail++if [[ "$#" -ne 1 || ! -f "$1" ]]; then+ echo "usage: $0 <valid-rendered-yaml>" >&2+ exit 2+fi++ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"+CONTRACT="${ROOT}/scripts/tests/test-np08-render-contract.sh"+SOURCE_RENDER="$1"+tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/np08-render-contract.XXXXXX")"+trap 'rm -rf "${tmpdir}"' EXIT++mutate_render() {+ local mutation="$1"+ local destination="$2"+ RUBYOPT=--disable=gems ruby -ryaml - "${mutation}" "${SOURCE_RENDER}" >"${destination}" <<'RUBY'+mutation, source = ARGV+documents = YAML.load_stream(File.read(source)).select { |document| document.is_a?(Hash) }++case mutation+when 'broad-egress'+ policy = documents.find do |document|+ document['kind'] == 'NetworkPolicy' &&+ document.dig('metadata', 'namespace') == 'mcp-host' &&+ document.dig('metadata', 'name') == 'mcp-host'+ end+ abort('fixture source is missing mcp-host NetworkPolicy') unless policy+ policy.fetch('spec').fetch('egress') << {+ 'to' => [+ {+ 'namespaceSelector' => {+ 'matchLabels' => { 'kubernetes.io/metadata.name' => 'mcp-server' },+ },+ },+ ],+ }+when 'host-secret-grant'+ documents << {+ 'apiVersion' => 'rbac.authorization.k8s.io/v1',+ 'kind' => 'Role',+ 'metadata' => { 'name' => 'fixture-host-secret-reader', 'namespace' => 'mcp-server' },+ 'rules' => [+ { 'apiGroups' => [''], 'resources' => ['secrets'], 'verbs' => ['get'] },+ ],+ }+ documents << {+ 'apiVersion' => 'rbac.authorization.k8s.io/v1',+ 'kind' => 'RoleBinding',+ 'metadata' => { 'name' => 'fixture-host-secret-reader', 'namespace' => 'mcp-server' },+ 'roleRef' => {+ 'apiGroup' => 'rbac.authorization.k8s.io',+ 'kind' => 'Role',+ 'name' => 'fixture-host-secret-reader',+ },+ 'subjects' => [+ { 'kind' => 'ServiceAccount', 'name' => 'host-fixture-sa', 'namespace' => 'mcp-host' },+ ],+ }+else+ abort("unknown fixture mutation: #{mutation}")+end++puts documents.map { |document| YAML.dump(document) }.join("---\n")+RUBY+}++assert_rejected() {+ local rendered="$1"+ local expected="$2"+ local label="$3"+ local output="${tmpdir}/${label}.out"++ if bash "${CONTRACT}" "${rendered}" >"${output}" 2>&1; then+ echo "FAIL: NP-08 render contract accepted ${label}" >&2+ exit 1+ fi+ if ! grep -Fq -- "${expected}" "${output}"; then+ echo "FAIL: NP-08 render contract rejected ${label} for an unexpected reason" >&2+ sed -n '1,80p' "${output}" >&2+ exit 1+ fi+ echo "PASS: NP-08 render contract rejects ${label}"+}++broad_egress="${tmpdir}/broad-egress.yaml"+mutate_render broad-egress "${broad_egress}"+assert_rejected \+ "${broad_egress}" \+ 'mcp-host must not gain broad destination or all-port egress' \+ 'broad mcp-host egress'++host_secret_grant="${tmpdir}/host-secret-grant.yaml"+mutate_render host-secret-grant "${host_secret_grant}"+assert_rejected \+ "${host_secret_grant}" \+ 'mcp-host ServiceAccounts must not receive MCP Secret read grants' \+ 'mcp-host MCP Secret grant'diff --git a/scripts/tests/test-np08-render-contract.sh b/scripts/tests/test-np08-render-contract.shindex d814ea3d9..9960a15f3 100755--- a/scripts/tests/test-np08-render-contract.sh+++ b/scripts/tests/test-np08-render-contract.sh@@ -107,6 +107,14 @@ for rendered in "$@"; dopolicies = documents.select do |document|document["kind"] == "NetworkPolicy" && document.dig("metadata", "namespace") == "mcp-host"end+ broad_host_egress = policies.any? do |policy|+ Array(policy.dig("spec", "egress")).any? do |rule|+ !rule.key?("to") || Array(rule["to"]).empty? ||+ !rule.key?("ports") || Array(rule["ports"]).empty?+ end+ end+ abort("mcp-host must not gain broad destination or all-port egress") if broad_host_egress+hcc_lane = policies.any? do |policy|Array(policy.dig("spec", "egress")).any? do |rule|ports = Array(rule["ports"])@@ -120,12 +128,61 @@ for rendered in "$@"; doendabort("mcp-host to HCC TCP 8081 lane is absent") unless hcc_lane+ allows_tcp_port = lambda do |rule, expected_port|+ ports = rule["ports"]+ next true if ports.nil? || Array(ports).empty?++ Array(ports).any? do |port|+ next false unless port.fetch("protocol", "TCP") == "TCP"++ first = port["port"]+ last = port.fetch("endPort", first)+ if first.is_a?(Integer) && last.is_a?(Integer)+ first <= expected_port && expected_port <= last+ else+ first.to_s == expected_port.to_s+ end+ end+ endhost_proxy_lane = policies.any? do |policy|Array(policy.dig("spec", "egress")).any? do |rule|- Array(rule["ports"]).any? { |port| port["port"] == 8083 }+ allows_tcp_port.call(rule, 8083)endendabort("mcp-host must not gain an egress lane to mcp-proxy TCP 8083") if host_proxy_lane++ secret_reading_role = lambda do |role|+ Array(role["rules"]).any? do |rule|+ api_groups = Array(rule["apiGroups"])+ resources = Array(rule["resources"])+ verbs = Array(rule["verbs"])+ (api_groups.include?("") || api_groups.include?("*")) &&+ (resources.include?("secrets") || resources.include?("*")) &&+ !(verbs & %w[get list watch *]).empty?+ end+ end+ roles = documents.each_with_object({}) do |document, index|+ next unless %w[Role ClusterRole].include?(document["kind"])++ namespace = document["kind"] == "Role" ? document.dig("metadata", "namespace") : nil+ index[[document["kind"], namespace, document.dig("metadata", "name")]] = document+ end+ host_mcp_secret_grant = documents.any? do |binding|+ next false unless %w[RoleBinding ClusterRoleBinding].include?(binding["kind"])++ binding_namespace = binding.dig("metadata", "namespace")+ host_subject = Array(binding["subjects"]).any? do |subject|+ subject["kind"] == "ServiceAccount" && subject["namespace"] == "mcp-host"+ end+ next false unless host_subject+ next false unless binding["kind"] == "ClusterRoleBinding" || binding_namespace == "mcp-server"++ role_kind = binding.dig("roleRef", "kind")+ role_namespace = role_kind == "Role" ? binding_namespace : nil+ role = roles[[role_kind, role_namespace, binding.dig("roleRef", "name")]]+ role && secret_reading_role.call(role)+ end+ abort("mcp-host ServiceAccounts must not receive MCP Secret read grants") if host_mcp_secret_grant' "${rendered}"echo "PASS: NP-08 rendered contract (${rendered##*/})"