Skip to content

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

TypeScript

fix(e2e): invoke port-forward ownership through a fixed CLI

Alfredo Lopez09d6f4ffde39
diff --git a/scripts/e2e/codex-approved-tools-pf-owner.sh b/scripts/e2e/codex-approved-tools-pf-owner.sh
new file mode 100755
index 000000000..27a92140b
--- /dev/null
+++ b/scripts/e2e/codex-approved-tools-pf-owner.sh
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+# Fixed CLI surface for the existing ownership library. No source path or
+# command body comes from caller arguments; each operation has a literal call.
+set -euo pipefail
+
+fail() { printf '%s\n' 'Invalid approved-tools port-forward ownership request' >&2; exit 2; }
+SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
+WORKTREE="$(cd -- "${SCRIPT_DIR}/../.." && pwd -P)"
+operation="${1:-}"
+[[ $# -gt 0 ]] || fail
+shift
+case "$operation" in
+ record) [[ $# -eq 9 ]] || fail ;;
+ matches|cleanup) [[ $# -eq 8 ]] || fail ;;
+ *) fail ;;
+esac
+
+# Preserve the original argument vector for the ownership library after checking
+# each semantic binding. Paths are data, never sourced or evaluated.
+args=("$@")
+record="$1"
+shift
+if [[ "$operation" == record ]]; then
+ [[ "$1" =~ ^[1-9][0-9]{0,9}$ ]] || fail
+ shift
+fi
+profile="$1" context="$2" worktree="$3" namespace="$4" service="$5"
+local_port="$6" remote_port="$7"
+[[ "$profile" =~ ^clerum-[a-z0-9-]+-[a-f0-9]{7,8}$ && ${#profile} -le 100 ]] || fail
+[[ "$profile" == "${MINIKUBE_PROFILE:-}" && "$context" == "$profile" &&
+ "$context" == "${CONTROL_API_REAL_PG_CONTEXT:-}" && "$worktree" == "$WORKTREE" ]] || fail
+case "$namespace" in
+ control-plane) [[ "$service" == codex-llm-proxy && "$remote_port" == 9090 ]] || fail ;;
+ mcp-server) [[ "$service" =~ ^approved-tools-[a-f0-9]{12}-mcp-(83|150|250)$ && "$remote_port" == 8080 ]] || fail ;;
+ *) fail ;;
+esac
+[[ "$local_port" =~ ^[0-9]{4,5}$ ]] || fail
+(( 10#$local_port >= 1024 && 10#$local_port <= 65535 )) || fail
+[[ "${T2_PROFILE_ROOT:-}" == /* ]] || fail
+pid_directory="$(cd -- "${T2_PROFILE_ROOT}/${profile}/pids" && pwd -P)" || fail
+[[ "${record%/*}" == "$pid_directory" ]] || fail
+filename="${record##*/}"
+[[ "$filename" =~ ^approved-tools-[a-f0-9]{12}-(codex-llm-proxy|approved-tools-[a-f0-9]{12}-mcp-(83|150|250))\.pid$ ]] || fail
+[[ "$filename" == *"-${service}.pid" ]] || fail
+
+# shellcheck source=scripts/minikube/port-forward-owner.sh
+source "${SCRIPT_DIR}/../minikube/port-forward-owner.sh"
+case "$operation" in
+ record) pf_owner_record_process "${args[@]}" ;;
+ matches) pf_owner_record_process_matches "${args[@]}" ;;
+ cleanup) pf_owner_cleanup_record "${args[@]}" ;;
+esac
diff --git a/scripts/e2e/prepare-codex-approved-tools.mjs b/scripts/e2e/prepare-codex-approved-tools.mjs
index b44bade89..1ae699e32 100644
--- a/scripts/e2e/prepare-codex-approved-tools.mjs
+++ b/scripts/e2e/prepare-codex-approved-tools.mjs
@@ -396,36 +396,21 @@ function command(operation, args, { input, timeout = 60_000, inherit = false } =
case 'pf_owner_record_process':
result = spawnSync(
'bash',
- [
- '-c',
- 'source scripts/minikube/port-forward-owner.sh; pf_owner_record_process "$@"',
- 'pf-owner',
- ...args,
- ],
+ ['scripts/e2e/codex-approved-tools-pf-owner.sh', 'record', ...args],
options
)
break
case 'pf_owner_record_process_matches':
result = spawnSync(
'bash',
- [
- '-c',
- 'source scripts/minikube/port-forward-owner.sh; pf_owner_record_process_matches "$@"',
- 'pf-owner',
- ...args,
- ],
+ ['scripts/e2e/codex-approved-tools-pf-owner.sh', 'matches', ...args],
options
)
break
case 'pf_owner_cleanup_record':
result = spawnSync(
'bash',
- [
- '-c',
- 'source scripts/minikube/port-forward-owner.sh; pf_owner_cleanup_record "$@"',
- 'pf-owner',
- ...args,
- ],
+ ['scripts/e2e/codex-approved-tools-pf-owner.sh', 'cleanup', ...args],
options
)
break
diff --git a/scripts/e2e/prepare-codex-approved-tools.test.mjs b/scripts/e2e/prepare-codex-approved-tools.test.mjs
index 269ad12e3..ba950feca 100644
--- a/scripts/e2e/prepare-codex-approved-tools.test.mjs
+++ b/scripts/e2e/prepare-codex-approved-tools.test.mjs
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict'
+import { spawnSync } from 'node:child_process'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
@@ -228,3 +229,70 @@ test('rejects hostile profile, kubectl operation and owner arguments before subp
validateOwnerArgs([record, '1;echo', ...args.slice(1)], true, profile, pidDirectory)
)
})
+
+test('fixed ownership CLI rejects invalid operations and bindings without touching unrelated files', () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'approved-tools-owner-cli-'))
+ const profile = 'clerum-fixture-owner-1234abcd'
+ const worktree = path.resolve(path.dirname(new URL(import.meta.url).pathname), '../..')
+ const pidDirectory = path.join(root, profile, 'pids')
+ fs.mkdirSync(pidDirectory, { recursive: true })
+ const canonicalPidDirectory = fs.realpathSync(pidDirectory)
+ const record = path.join(canonicalPidDirectory, 'approved-tools-a1b2c3d4e5f6-codex-llm-proxy.pid')
+ const sentinel = path.join(root, 'unrelated')
+ fs.writeFileSync(sentinel, 'untouched')
+ const args = [
+ record,
+ profile,
+ profile,
+ worktree,
+ 'control-plane',
+ 'codex-llm-proxy',
+ '43101',
+ '9090',
+ ]
+ const env = {
+ PATH: process.env.PATH,
+ MINIKUBE_PROFILE: profile,
+ CONTROL_API_REAL_PG_CONTEXT: profile,
+ T2_PROFILE_ROOT: root,
+ }
+ const run = (operation, values = args) =>
+ spawnSync('bash', ['scripts/e2e/codex-approved-tools-pf-owner.sh', operation, ...values], {
+ cwd: worktree,
+ env,
+ encoding: 'utf8',
+ timeout: 5000,
+ })
+ try {
+ // The canonical library's valid missing-record cleanup is a harmless no-op.
+ assert.equal(run('cleanup').status, 0)
+ for (const operation of ['unknown', 'cleanup;echo', '--help', 'pf_owner_cleanup_record'])
+ assert.equal(run(operation).status, 2)
+ for (const [index, value] of [
+ [0, sentinel],
+ [1, 'clerum-dev'],
+ [2, 'other-context'],
+ [3, root],
+ [4, 'default'],
+ [5, '--service'],
+ [6, '80'],
+ [7, '8080'],
+ ]) {
+ const invalid = [...args]
+ invalid[index] = value
+ assert.equal(run('cleanup', invalid).status, 2)
+ }
+ assert.equal(run('record', [record, 'bad-pid', ...args.slice(1)]).status, 2)
+ assert.equal(run('cleanup', args.slice(1)).status, 2)
+ const mismatch = [...args]
+ mismatch[0] = path.join(
+ canonicalPidDirectory,
+ 'approved-tools-a1b2c3d4e5f6-approved-tools-a1b2c3d4e5f6-mcp-83.pid'
+ )
+ assert.equal(run('cleanup', mismatch).status, 2)
+ assert.equal(fs.readFileSync(sentinel, 'utf8'), 'untouched')
+ assert.deepEqual(fs.readdirSync(pidDirectory), [])
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+})