From 3e361cd03c6187b08164a8c239414127620c55fd Mon Sep 17 00:00:00 2001 From: Pallav Vasa Date: Sat, 17 May 2025 14:38:56 +0000 Subject: [PATCH] feat: separate access validation logic for gitops commands into a separate file --- gitops_router.sh | 67 +++--------------------------------------------- 1 file changed, 3 insertions(+), 64 deletions(-) diff --git a/gitops_router.sh b/gitops_router.sh index 7296134..4702198 100644 --- a/gitops_router.sh +++ b/gitops_router.sh @@ -127,69 +127,8 @@ remove_containers() { } # ───────────────────────────────────────────── -# validate_command [ …] -validate_command() { - local cmd="$1" - shift - local tokens=("$@") - local yaml="$HOME/access.yml" - - # 1) Is command allowed at all? - if [[ "$(yq e ".\"$PERSON\".commands | has(\"$cmd\")" "$yaml")" != "true" ]]; then - log ERROR "Unauthorized command: '$cmd'" - exit 1 - fi - - # 2) Load allowed args for this cmd (may be empty array) - mapfile -t allowed < <(yq e ".\"$PERSON\".commands.${cmd}[]" "$yaml") - - if [[ "${#allowed[@]}" -eq 0 ]]; then - log ERROR "No allowed arguments for command '$cmd' in $yaml" - exit 1 - fi - - # 3) Extract just the non-flag tokens - local args=() - for tok in "${tokens[@]}"; do - [[ "$tok" == -* ]] && continue - args+=("$tok") - done - - if [[ "$cmd" == "remove" ]]; then - # ─ remove: must have at least one arg - if ((${#args[@]} == 0)); then - log ERROR "Command '$cmd' requires at least one argument: ${allowed[*]}" - exit 1 - fi - # Validate each against allowed[] - for a in "${args[@]}"; do - local ok=false - for want in "${allowed[@]}"; do - [[ "$a" == "$want" ]] && ok=true && break - done - if ! $ok; then - log ERROR "Invalid argument '$a' for '$cmd'; allowed: ${allowed[*]}" - exit 1 - fi - done - else - # ─ all other cmds: must have exactly one arg - if ((${#args[@]} != 1)); then - log ERROR "Command '$cmd' requires exactly one argument: ${allowed[*]}" - exit 1 - fi - # And that single arg must be allowed - local a="${args[0]}" - local ok=false - for want in "${allowed[@]}"; do - [[ "$a" == "$want" ]] && ok=true && break - done - if ! $ok; then - log ERROR "Invalid argument '$a' for '$cmd'; allowed: ${allowed[*]}" - exit 1 - fi - fi -} +# validate_command [ …] +source "$HOME"/.local/bin/validate_command_access.sh # ───────────────────────────────────────────── # Entry & command parsing @@ -203,7 +142,7 @@ read -ra parts <<<"$SSH_ORIGINAL_COMMAND" cmd="${parts[0]}" args=("${parts[@]:1}") -validate_command "$cmd" "${args[@]}" +validate_command "$PERSON" "$cmd" "${args[@]}" # ───────────────────────────────────────────── # Dispatch