Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix: preserve auth key bytes during convergence
Alfredo Lopez41d60fa136ca
diff --git a/scripts/minikube/sync-auth-key.sh b/scripts/minikube/sync-auth-key.shindex d4514e386..47f0fec87 100755--- a/scripts/minikube/sync-auth-key.sh+++ b/scripts/minikube/sync-auth-key.sh@@ -102,11 +102,19 @@ rollout_restart_with_retry() {# Build a merge patch that sets one ConfigMap data key to a value.make_patch() {- SOURCE_VALUE="$1" TARGET_KEY="$2" python3 - <<'PY'+ SOURCE_VALUE_B64="$1" TARGET_KEY="$2" python3 - <<'PY'+import base64import jsonimport os+import sys-print(json.dumps({"data": {os.environ["TARGET_KEY"]: os.environ["SOURCE_VALUE"]}}))+try:+ source_value = base64.b64decode(os.environ["SOURCE_VALUE_B64"], validate=True).decode("utf-8")+except (ValueError, UnicodeDecodeError) as exc:+ print(f"invalid UTF-8 source auth key: {exc}", file=sys.stderr)+ raise SystemExit(1)++print(json.dumps({"data": {os.environ["TARGET_KEY"]: source_value}}))PY}@@ -137,6 +145,22 @@ mark_key_applied() {-p "$(make_applied_hash_patch "${source_hash}")" >/dev/null}+configmap_key_hash() {+ local namespace="$1" configmap="$2" key="$3"+ "${KCTL[@]}" get configmap "${configmap}" -n "${namespace}" \+ -o "jsonpath={.data.${key}}" | shasum -a 256 | awk '{print $1}'+}++write_source_key() {+ SOURCE_KEY_B64="${source_key_b64}" python3 - <<'PY'+import base64+import os+import sys++sys.stdout.buffer.write(base64.b64decode(os.environ["SOURCE_KEY_B64"], validate=True))+PY+}+consumer_pods_use_key() {local namespace="$1" selector="$2" container="$3" env_name="$4" consumer="$5"local rows pod ready deleting pod_count=0@@ -155,7 +179,7 @@ consumer_pods_use_key() {return 1fipod_count=$((pod_count + 1))- if ! printf '%s' "${source_key}" | "${KCTL[@]}" exec -i "${pod}" -n "${namespace}" -c "${container}" -- \+ if ! write_source_key | "${KCTL[@]}" exec -i "${pod}" -n "${namespace}" -c "${container}" -- \node -e 'const fs=require("fs");const expected=fs.readFileSync(0,"utf8");process.exit(process.env[process.argv[1]]===expected?0:42)' \"${env_name}" >/dev/null; thenlog "${consumer} pod ${namespace}/${pod} has not consumed the target auth key" >&2@@ -192,14 +216,35 @@ if ! source_probe="$("${KCTL[@]}" get secret "${SOURCE_SECRET}" -n "${SOURCE_NAMfidie "cannot inspect auth source ${SOURCE_NAMESPACE}/${SOURCE_SECRET}: ${source_probe}"fi-source_key="$("${KCTL[@]}" get secret "${SOURCE_SECRET}" -n "${SOURCE_NAMESPACE}" -o "jsonpath={.data.${SOURCE_KEY}}" | base64 -d)"-if [[ -z "${source_key}" ]]; then+source_key_b64="$("${KCTL[@]}" get secret "${SOURCE_SECRET}" -n "${SOURCE_NAMESPACE}" -o "jsonpath={.data.${SOURCE_KEY}}")"+if [[ -z "${source_key_b64}" ]]; thenif [[ "${REQUIRE_GFS}" == "true" ]]; thendie "required GFS auth source key ${SOURCE_KEY} is empty"fidie "auth source key ${SOURCE_KEY} is empty; refusing to mutate consumers"fi-source_hash="$(printf '%s' "${source_key}" | shasum -a 256 | awk '{print $1}')"+if ! source_hash="$(SOURCE_KEY_B64="${source_key_b64}" python3 - <<'PY'+import base64+import hashlib+import os+import sys++try:+ source_value = base64.b64decode(os.environ["SOURCE_KEY_B64"], validate=True)+ source_value.decode("utf-8")+except (ValueError, UnicodeDecodeError) as exc:+ print(f"invalid source auth key encoding: {exc}", file=sys.stderr)+ raise SystemExit(1)++if not source_value:+ print("source auth key decodes empty", file=sys.stderr)+ raise SystemExit(1)++print(hashlib.sha256(source_value).hexdigest())+PY+)"; then+ die "source auth key ${SOURCE_KEY} is not valid non-empty UTF-8 base64"+fiif [[ "${REQUIRE_GFS}" == "true" ]] && \! gfs_target_probe="$("${KCTL[@]}" get configmap "${GFS_CONFIGMAP}" -n "${GFS_NAMESPACE}" 2>&1)"; thendie "required GFS auth target ${GFS_NAMESPACE}/${GFS_CONFIGMAP} is missing or unreadable: ${gfs_target_probe}"@@ -222,17 +267,17 @@ sync_mcp_host() {fidie "cannot inspect auth target ${CONFIG_NAMESPACE}/${CONFIGMAP}: ${target_probe}"fi- local current_key applied_hash- if ! current_key="$("${KCTL[@]}" get configmap "${CONFIGMAP}" -n "${CONFIG_NAMESPACE}" -o "jsonpath={.data.${TARGET_KEY}}" 2>&1)"; then- die "cannot read auth target ${CONFIG_NAMESPACE}/${CONFIGMAP}: ${current_key}"+ local current_hash applied_hash+ if ! current_hash="$(configmap_key_hash "${CONFIG_NAMESPACE}" "${CONFIGMAP}" "${TARGET_KEY}" 2>&1)"; then+ die "cannot read auth target ${CONFIG_NAMESPACE}/${CONFIGMAP}: ${current_hash}"fiif ! applied_hash="$(read_applied_hash "${CONFIG_NAMESPACE}" "${CONFIGMAP}" 2>&1)"; thendie "cannot read auth convergence marker from ${CONFIG_NAMESPACE}/${CONFIGMAP}: ${applied_hash}"filocal force_rollout=false- if [[ "${source_key}" != "${current_key}" ]]; then+ if [[ "${source_hash}" != "${current_hash}" ]]; thenlog "Patching ${CONFIGMAP}.${TARGET_KEY}"- "${KCTL[@]}" patch configmap "${CONFIGMAP}" -n "${CONFIG_NAMESPACE}" --type=merge -p "$(make_patch "${source_key}" "${TARGET_KEY}")" >/dev/null+ "${KCTL[@]}" patch configmap "${CONFIGMAP}" -n "${CONFIG_NAMESPACE}" --type=merge -p "$(make_patch "${source_key_b64}" "${TARGET_KEY}")" >/dev/nullforce_rollout=trueelseif [[ "${applied_hash}" == "${source_hash}" ]]; then@@ -292,17 +337,17 @@ sync_gfs() {fidie "cannot inspect auth target ${GFS_NAMESPACE}/${GFS_CONFIGMAP}: ${target_probe}"fi- local current_key applied_hash- if ! current_key="$("${KCTL[@]}" get configmap "${GFS_CONFIGMAP}" -n "${GFS_NAMESPACE}" -o "jsonpath={.data.${GFS_TARGET_KEY}}" 2>&1)"; then- die "cannot read auth target ${GFS_NAMESPACE}/${GFS_CONFIGMAP}: ${current_key}"+ local current_hash applied_hash+ if ! current_hash="$(configmap_key_hash "${GFS_NAMESPACE}" "${GFS_CONFIGMAP}" "${GFS_TARGET_KEY}" 2>&1)"; then+ die "cannot read auth target ${GFS_NAMESPACE}/${GFS_CONFIGMAP}: ${current_hash}"fiif ! applied_hash="$(read_applied_hash "${GFS_NAMESPACE}" "${GFS_CONFIGMAP}" 2>&1)"; thendie "cannot read auth convergence marker from ${GFS_NAMESPACE}/${GFS_CONFIGMAP}: ${applied_hash}"filocal force_rollout=false- if [[ "${source_key}" != "${current_key}" ]]; then+ if [[ "${source_hash}" != "${current_hash}" ]]; thenlog "Patching ${GFS_CONFIGMAP}.${GFS_TARGET_KEY}"- "${KCTL[@]}" patch configmap "${GFS_CONFIGMAP}" -n "${GFS_NAMESPACE}" --type=merge -p "$(make_patch "${source_key}" "${GFS_TARGET_KEY}")" >/dev/null+ "${KCTL[@]}" patch configmap "${GFS_CONFIGMAP}" -n "${GFS_NAMESPACE}" --type=merge -p "$(make_patch "${source_key_b64}" "${GFS_TARGET_KEY}")" >/dev/nullforce_rollout=trueelseif [[ "${applied_hash}" == "${source_hash}" ]]; thendiff --git a/scripts/tests/test-minikube-sync-auth-key-durable.sh b/scripts/tests/test-minikube-sync-auth-key-durable.shindex 115ef791a..8a6c13cb8 100755--- a/scripts/tests/test-minikube-sync-auth-key-durable.sh+++ b/scripts/tests/test-minikube-sync-auth-key-durable.sh@@ -30,7 +30,7 @@ increment() {}if [[ "${1:-}" == get && "${2:-}" == secret && "${3:-}" == rpc-proxy-secrets ]]; then- [[ "$*" == *jsonpath* ]] && printf 'cHVibGljLWtleQ=='+ [[ "$*" == *jsonpath* ]] && printf '%s' "${TEST_SOURCE_B64:-cHVibGljLWtleQ==}"exit 0fi@@ -131,11 +131,14 @@ if [[ "${1:-}" == get && "${2:-}" == pods ]]; thenfiif [[ "${1:-}" == exec ]]; then- payload="$(cat)"+ input_file="${TEST_STATE_DIR}/exec-input"+ expected_file="${TEST_STATE_DIR}/exec-expected"+ cat >"${input_file}"expected_key="${TEST_RUNTIME_KEY}"[[ "$*" == *GFS_JWT_PUBLIC_KEY* ]] && expected_key="${TEST_RUNTIME_KEY_GFS:-${expected_key}}"[[ "$*" == *CLERUM_AUTH_JWT_PUBLIC_KEY* ]] && expected_key="${TEST_RUNTIME_KEY_MCP:-${expected_key}}"- [[ "${payload}" == "${expected_key}" ]]+ printf '%s' "${expected_key}" >"${expected_file}"+ cmp -s "${input_file}" "${expected_file}"exitfi@@ -145,8 +148,9 @@ SHchmod +x "${TMP_DIR}/kubectl"run_sync() {+ local source_hash="${TEST_SOURCE_HASH_OVERRIDE:-${SOURCE_HASH}}"TEST_KUBECTL_LOG="${LOG_FILE}" TEST_STATE_DIR="${STATE_DIR}" \- TEST_SOURCE_HASH="${SOURCE_HASH}" PATH="${TMP_DIR}:$PATH" \+ TEST_SOURCE_HASH="${source_hash}" PATH="${TMP_DIR}:$PATH" \"$@"}@@ -251,4 +255,26 @@ if grep -q 'deployment/gfs-controller' "${LOG_FILE}"; thenexit 1fi+# ConfigMap values commonly end in the PEM newline. The source must be+# compared and attested byte-for-byte; command substitution must not silently+# remove that newline and trigger a false rollout failure.+NEWLINE_SOURCE_HASH="$(printf 'public-key\n' | shasum -a 256 | awk '{print $1}')"+printf 'public-key\n' >"${STATE_DIR}/mcp.key"+rm -f "${STATE_DIR}/mcp.applied"+if ! TEST_SOURCE_HASH_OVERRIDE="${NEWLINE_SOURCE_HASH}" run_sync env \+ TEST_SOURCE_B64='cHVibGljLWtleQo=' TEST_RUNTIME_KEY=$'public-key\n' \+ bash "${ROOT}/scripts/minikube/sync-auth-key.sh" --context fake --require-mcp --skip-gfs \+ >"${OUT_FILE}" 2>&1; then+ cat "${OUT_FILE}" >&2+ cat "${LOG_FILE}" >&2+ echo "FAIL: auth-key sync rejected an exact PEM-style trailing newline" >&2+ exit 1+fi+[[ "$(cat "${STATE_DIR}/mcp.applied")" == "${NEWLINE_SOURCE_HASH}" ]]+if grep -q 'rollout restart deployment/' "${LOG_FILE}"; then+ echo "FAIL: exact trailing-newline convergence triggered an unnecessary rollout" >&2+ cat "${LOG_FILE}" >&2+ exit 1+fi+echo "PASS: auth-key convergence is durable, resumable, consumer-proven, and idempotent"