pub const APERION_SHIELD_SHIM_MARKER: &str = "# APERION-SHIELD-SHIM v1 -- managed by `aperion-shield --install-shims`";
pub fn shim_script(command_name: &str, real_binary_path: &str) -> String {
format!(
r#"#!/bin/sh
{marker}
#
# What this does:
# * Routes every invocation of `{cmd}` through `aperion-shield --check-cmd`
# before letting it reach the real binary.
# * Blocks (with the banner Shield emits) on destructive operations
# that trip a rule in your active shieldset.
# * Falls back to exec-ing the real binary directly when Shield isn't
# available, so this never hard-breaks for teammates without Shield
# installed (e.g. on a fresh laptop pulling shared dotfiles).
#
# Bypass for a single invocation:
# SHIELD_SHIMS_DISABLE=1 {cmd} <args...>
#
# To remove every shim Shield has installed:
# aperion-shield --uninstall-shims
set -e
if [ "${{SHIELD_SHIMS_DISABLE:-}}" = "1" ]; then
exec "{real}" "$@"
fi
if ! command -v aperion-shield >/dev/null 2>&1; then
echo "[aperion-shield] binary not on \$PATH; skipping shim guardrail for `{cmd}`" >&2
echo "[aperion-shield] install: brew install AperionAI/tap/aperion-shield" >&2
exec "{real}" "$@"
fi
if [ ! -x "{real}" ]; then
echo "[aperion-shield] real `{cmd}` binary not found at {real}" >&2
echo "[aperion-shield] re-run `aperion-shield --install-shims` to refresh, or uninstall the shim" >&2
exit 127
fi
aperion-shield --check-cmd -- "{cmd}" "$@"
exit_code=$?
if [ "$exit_code" -ne 0 ]; then
# Shield refused. Banner is already on stderr; propagate the exit code.
exit "$exit_code"
fi
exec "{real}" "$@"
"#,
marker = APERION_SHIELD_SHIM_MARKER,
cmd = command_name,
real = real_binary_path,
)
}
pub const DEFAULT_SHIMMED_COMMANDS: &[&str] = &[
"aws",
"gcloud",
"az",
"kubectl",
"helm",
"terraform",
"psql",
"mongosh",
"redis-cli",
"rm",
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shim_script_contains_marker_and_command() {
let s = shim_script("aws", "/usr/local/bin/aws");
assert!(s.contains(APERION_SHIELD_SHIM_MARKER));
assert!(s.contains("aperion-shield --check-cmd -- \"aws\""));
assert!(s.contains("exec \"/usr/local/bin/aws\""));
}
#[test]
fn shim_script_uses_real_path_in_bypass_branch_too() {
let s = shim_script("kubectl", "/opt/homebrew/bin/kubectl");
let bypass_idx = s.find("SHIELD_SHIMS_DISABLE").expect("bypass branch");
let after_bypass = &s[bypass_idx..];
assert!(after_bypass.contains("exec \"/opt/homebrew/bin/kubectl\""));
}
#[test]
fn default_commands_contains_the_ten_we_announce() {
assert_eq!(DEFAULT_SHIMMED_COMMANDS.len(), 10);
for required in ["aws", "kubectl", "terraform", "rm", "psql"] {
assert!(
DEFAULT_SHIMMED_COMMANDS.contains(&required),
"missing required command in DEFAULT_SHIMMED_COMMANDS: {}",
required
);
}
}
}