Skip to content

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

TypeScript

fix(branch-profile): accept owned adopted profiles and deny shared contexts by name

Alfredo Lopez27e403d8d2ce
diff --git a/scripts/minikube-profiles/branch-profile.sh b/scripts/minikube-profiles/branch-profile.sh
index 2748ad7da..51205f1fa 100755
--- a/scripts/minikube-profiles/branch-profile.sh
+++ b/scripts/minikube-profiles/branch-profile.sh
@@ -102,20 +102,37 @@ normalize_cache_root() {
fi
}
+# Refuses the kube contexts shared across sessions or clusters: the Clerum dev
+# and production contexts, the default minikube and Docker Desktop contexts,
+# every GKE context (gke_<project>_<zone>_<cluster>, covered by *gke*) and the
+# protected names profile-owner.sh also refuses. Matching is case-insensitive.
+refuse_shared_profile_name() {
+ local name="$1" normalized
+ normalized="$(printf '%s' "${name}" | LC_ALL=C tr '[:upper:]' '[:lower:]')"
+ case "${normalized}" in
+ clerum | clerum-dev | clerum-test | minikube | docker-desktop | default | *gke* | *prod* | *staging*)
+ die "BRANCH_PROFILE_SHARED_CONTEXT: refusing shared or protected profile: ${name}" ;;
+ esac
+}
+
validate_profile_name() {
[[ ${#PROFILE} -le 63 && "${PROFILE}" =~ ^[a-zA-Z0-9][a-zA-Z0-9._-]*$ ]] ||
die "resolver returned an unsafe profile name: ${PROFILE}"
- # Positive check: a branch profile is always clerum-<branch slug>-<owner id
- # prefix>. The hex suffix is what separates it from shared profiles such as
- # clerum-dev, which also match clerum-*; the denylist below only names the
- # shared profiles known today. {7,8} matches the branch-scoped check in
- # scripts/e2e/e2e-plugin-workload-sdk.sh.
+ refuse_shared_profile_name "${PROFILE}"
+ # An explicitly selected profile (MINIKUBE_PROFILE / BRANCH_PROFILE_PROFILE)
+ # has already passed profile-owner.sh's ownership check (repo, branch and
+ # owner id). It may predate the hashed naming, so only the local clerum-*
+ # namespace is required of it.
+ if [[ -n "${EXPLICIT_PROFILE}" && "${PROFILE}" == "${EXPLICIT_PROFILE}" ]]; then
+ [[ "${PROFILE}" =~ ^clerum-[a-z0-9][a-z0-9._-]*$ ]] ||
+ die "explicit profile is outside the local clerum-* namespace: ${PROFILE}"
+ return 0
+ fi
+ # A profile the resolver derives on its own is always
+ # clerum-<branch slug>-<owner id prefix> (profile_owner_stable_profile in
+ # scripts/minikube/profile-owner.sh), so it must carry the hex suffix.
[[ "${PROFILE}" =~ ^clerum-[a-z0-9][a-z0-9._-]*-[0-9a-f]{7,8}$ ]] ||
die "resolver returned a profile outside the branch-scoped clerum-<branch>-<owner-id> namespace: ${PROFILE}"
- case "${PROFILE}" in
- *gke*|*prod*|*staging*|clerum-test|default|minikube)
- die "resolver returned a shared or protected profile: ${PROFILE}" ;;
- esac
}
set_profile_paths() {
@@ -465,6 +482,10 @@ resolve_explicit_profile_override() {
die "explicit profile selectors disagree (BRANCH_PROFILE_PROFILE=${BRANCH_PROFILE_PROFILE}, MINIKUBE_PROFILE=${MINIKUBE_PROFILE_SELECTION})"
fi
EXPLICIT_PROFILE="${BRANCH_PROFILE_PROFILE:-${MINIKUBE_PROFILE_SELECTION}}"
+ # Refuse a shared context by name before the resolver reads anything for it.
+ if [[ -n "${EXPLICIT_PROFILE}" ]]; then
+ refuse_shared_profile_name "${EXPLICIT_PROFILE}"
+ fi
}
init_profile() {
diff --git a/scripts/tests/test-branch-profile-lifecycle.sh b/scripts/tests/test-branch-profile-lifecycle.sh
index 832b1baa6..3c7c6dc94 100755
--- a/scripts/tests/test-branch-profile-lifecycle.sh
+++ b/scripts/tests/test-branch-profile-lifecycle.sh
@@ -344,20 +344,76 @@ for bad_host in localhost ::1 '[::1]'; do
done
# === explicit profile outside the branch-scoped namespace =====================
+# write_owned_profile_copy <name>: a profile directory whose metadata this
+# worktree owns (same repo, branch and owner id as the branch profile), so
+# profile-owner.sh accepts it and only branch-profile.sh's own name rules decide.
+write_owned_profile_copy() {
+ local name="$1" dir="${cache_root}/$1"
+ mkdir -p "${dir}"
+ cp "${profile_dir}/ports.env" "${dir}/ports.env"
+ awk -v wanted="${name}" -F= '$1 == "PROFILE" { print "PROFILE=" wanted; next } { print }' \
+ "${profile_dir}/profile.env" >"${dir}/profile.env"
+}
+
reset_state
bp explicit-own resolve "MINIKUBE_PROFILE=${profile}"
assert_rc 0 'explicit selection of the branch-owned profile'
assert_output_has "PROFILE=${profile}" 'explicit selection of the branch-owned profile'
-shared_dir="${cache_root}/clerum-dev"
-mkdir -p "${shared_dir}"
-cp "${profile_dir}/ports.env" "${shared_dir}/ports.env"
-awk -v wanted=clerum-dev -F= '$1 == "PROFILE" { print "PROFILE=" wanted; next } { print }' \
- "${profile_dir}/profile.env" >"${shared_dir}/profile.env"
+write_owned_profile_copy clerum-dev
bp explicit-shared resolve MINIKUBE_PROFILE=clerum-dev
assert_rc 1 'explicit selection of clerum-dev'
assert_output_has 'clerum-dev' 'explicit selection of clerum-dev names the refused profile'
assert_output_lacks 'PROFILE=clerum-dev' 'explicit selection of clerum-dev must not resolve'
-rm -rf "${shared_dir}"
+rm -rf "${cache_root:?}/clerum-dev"
+
+# An explicitly adopted profile that profile-owner.sh proves this worktree owns
+# may predate the hashed clerum-<branch>-<owner-id> naming; it is accepted.
+adopted=clerum-oauth19
+write_owned_profile_copy "${adopted}"
+reset_state
+bp adopted-resolve resolve "MINIKUBE_PROFILE=${adopted}"
+assert_rc 0 'explicit selection of an owned profile without the hashed suffix'
+assert_output_has "PROFILE=${adopted}" 'explicit selection of an owned profile without the hashed suffix'
+reset_state
+bp adopted-status status "MINIKUBE_PROFILE=${adopted}"
+assert_rc 0 'status of an owned profile without the hashed suffix'
+assert_log_has "minikube -p ${adopted} status" 'status ran minikube status for the adopted profile'
+reset_state
+bp adopted-stop stop "MINIKUBE_PROFILE=${adopted}"
+assert_rc 0 'stop of an owned profile without the hashed suffix'
+assert_log_has "minikube -p ${adopted} stop" 'stop ran minikube stop for the adopted profile'
+
+# A profile the resolver derives on its own (no explicit selection) must still
+# carry the hashed suffix. With the branch profile set aside, the adopted
+# profile is the only persisted record for this worktree and branch.
+mv "${profile_dir}" "${tmp}/branch-profile-aside"
+reset_state
+bp derived-unhashed resolve
+assert_rc 1 'resolver-derived profile without the hashed suffix'
+assert_output_has "outside the branch-scoped clerum-<branch>-<owner-id> namespace: ${adopted}" \
+ 'the resolver found the unhashed profile and refused it'
+assert_output_lacks "PROFILE=${adopted}" 'a resolver-derived unhashed profile must not resolve'
+mv "${tmp}/branch-profile-aside" "${profile_dir}"
+rm -rf "${cache_root:?}/${adopted}"
+
+# Shared contexts are refused by name in every subcommand, before the resolver
+# runs and before any kubectl or minikube call, even with owned metadata.
+for shared in clerum-dev gke_sample-project_us-central1-a_shared-cluster; do
+ write_owned_profile_copy "${shared}"
+ for action in resolve info preflight prepare-shims start status pf pf-health health \
+ stop-pf stop setup delete e2e-plan sync-plan; do
+ reset_state
+ bp "shared-${shared%%_*}-${action}" "${action}" "MINIKUBE_PROFILE=${shared}" \
+ "CONFIRM_DELETE=${shared}" "CONFIRM_PROFILE=${shared}"
+ assert_rc 1 "${action} with MINIKUBE_PROFILE=${shared}"
+ # Witness: the refusal comes from the shared-context denylist and names it.
+ assert_output_has "BRANCH_PROFILE_SHARED_CONTEXT: refusing shared or protected profile: ${shared}" \
+ "${action} with MINIKUBE_PROFILE=${shared}"
+ assert_log_lacks 'minikube' "${action} with MINIKUBE_PROFILE=${shared} must not call minikube"
+ assert_log_lacks 'kubectl' "${action} with MINIKUBE_PROFILE=${shared} must not call kubectl"
+ done
+ rm -rf "${cache_root:?}/${shared}"
+done
# === status ===================================================================
reset_state