|
- #!/usr/bin/env bash
- # Management API checks (spec section 9 "Management", section 16 least privilege; docs/decisions.md ADR-011):
- # namespaces, principals, API keys and grants. Covers the happy paths, validation (422), bad input (400), unknown ids
- # (404), wrong method (405), privilege escalation attempts (keys for principals with wider access, inheriting grants
- # from a non-inheriting admin, grants outside the caller's scope), tenant isolation (AC-06), key revocation and
- # principal disabling, UTF-8 round trip and a cold first request.
- #
- # Needs the container from docker-compose.yml running. Provisions two throw-away tenants and purges them at the end.
- # bash tests/admin-api.sh
- set -u
- BASE=${BASE:-http://localhost:8801/api/v1}
- PASS=0; FAIL=0; FAILED=(); TENANTS=()
- TMP=$(mktemp -d)
- prov() {
- MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
- -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \
- axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015'
- }
- field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
- cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; rm -rf "$TMP"; }
- trap cleanup EXIT
- check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
- code() { local key=$1 m=$2 p=$3; shift 3; curl -s -o /dev/null -w '%{http_code}' -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
- body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
- jq_() { python -I -c "import sys,json;sys.stdin.reconfigure(encoding='utf-8');d=json.load(sys.stdin);print($1)"; }
- mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
- grant_body() { python -I -c "import json,sys;d={'principal_id':sys.argv[1],'namespace_id':sys.argv[2],'permissions':sys.argv[3].split(',')};
- if sys.argv[4]!='': d['inherit']=(sys.argv[4]=='true')
- print(json.dumps(d))" "$1" "$2" "$3" "${4:-}"; }
-
- # ---- setup ------------------------------------------------------------------------------------------------------
- TA=$(prov tenant "adm-A" | field tenant_id); TB=$(prov tenant "adm-B" | field tenant_id); TENANTS=("$TA" "$TB")
- mk "$TA" owner human; OWN=$P_ID; OK=$P_KEY
- mk "$TA" subadmin agent; SUB=$P_ID; SK=$P_KEY
- mk "$TA" narrow agent; NAR=$P_ID; NK=$P_KEY
- mk "$TA" plain agent; PLA=$P_ID; PK=$P_KEY
- mk "$TB" adminB human; BAD=$P_ID; BK=$P_KEY
- prov grant "$OWN" /org admin 1 >/dev/null
- prov grant "$SUB" /org/team-a admin 1 >/dev/null
- prov grant "$NAR" /org/team-b admin 0 >/dev/null
- prov grant "$PLA" /org/team-a read 1 >/dev/null
- prov grant "$BAD" /org admin 1 >/dev/null
- NSA=$(prov namespace "$TA" /org/team-a | field namespace_id); NSB=$(prov namespace "$TA" /org/team-b | field namespace_id)
- NSBB=$(prov namespace "$TB" /org | field namespace_id)
- [ -n "$NSA" ] && [ -n "$NSB" ] && [ -n "$OK" ] && [ -n "$BK" ] || { echo "setup failed"; exit 2; }
-
- # ---- cold start: first request after a restart is a write -------------------------------------------------------
- docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
- R=$(body "$SK" POST /namespaces -d '{"path":"/org/team-a/x/y"}')
- check "cold: sub-admin creates nested namespace" "2 /org/team-a/x/y" "$(echo "$R" | jq_ "str(d['namespaces_created'])+' '+d['path']")"
- NSX=$(echo "$R" | jq_ "d['id']")
-
- # ---- me and namespaces -------------------------------------------------------------------------------------------
- check "me -> own principal" "$PLA plain agent" "$(body "$PK" GET /me | jq_ "d['principal_id']+' '+d['name']+' '+d['type']")"
- check "me lists own grants" "/org/team-a read" "$(body "$PK" GET /me | jq_ "' '.join([d['grants'][0]['path']]+d['grants'][0]['permissions'])")"
- check "me no auth -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' "$BASE/me")"
- check "namespaces: reader sees team-a subtree only" "/org/team-a /org/team-a/x /org/team-a/x/y" "$(body "$PK" GET /namespaces | jq_ "' '.join(n['path'] for n in d['data'])")"
- check "namespaces: reader permissions" "['read']" "$(body "$PK" GET /namespaces | jq_ "d['data'][0]['permissions']")"
- check "namespaces: owner has admin on team-b" "yes" "$(body "$OK" GET /namespaces | jq_ "'yes' if any(n['path']=='/org/team-b' and 'admin' in n['permissions'] for n in d['data']) else 'no'")"
- check "namespaces: tenant B sees only its own" "/org" "$(body "$BK" GET /namespaces | jq_ "' '.join(n['path'] for n in d['data'])")"
-
- # ---- create namespace ----------------------------------------------------------------------------------------------
- check "create outside admin scope -> 404" "404" "$(code "$SK" POST /namespaces -d '{"path":"/org/team-b/y"}')"
- check "non-inheriting admin cannot create below -> 404" "404" "$(code "$NK" POST /namespaces -d '{"path":"/org/team-b/z"}')"
- check "reader cannot create -> 404" "404" "$(code "$PK" POST /namespaces -d '{"path":"/org/team-a/r"}')"
- check "top-level namespace -> 404" "404" "$(code "$OK" POST /namespaces -d '{"path":"/elsewhere"}')"
- check "existing namespace -> 409" "409" "$(code "$SK" POST /namespaces -d '{"path":"/org/team-a/x"}')"
- check "upper-case path is normalised" "201 " "$(code "$OK" POST /namespaces -d '{"path":"/org/Team-C"}') $(body "$OK" GET /namespaces | jq_ "'' if any(n['path']=='/org/team-c' for n in d['data']) else 'missing'")"
- check "bad path -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":"/org/has space"}')"
- check "path without slash -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":"org/x"}')"
- check "trailing slash -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":"/org/x/"}')"
- check "missing path -> 422" "422" "$(code "$OK" POST /namespaces -d '{}')"
- check "path not a string -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":5}')"
- check "invalid JSON -> 400" "400" "$(code "$OK" POST /namespaces -d '{"path":')"
- check "empty body -> 400" "400" "$(code "$OK" POST /namespaces)"
- check "namespaces wrong method -> 405" "405" "$(code "$OK" PUT /namespaces -d '{}')"
- check "tenant B cannot create in A's tree (own /org instead)" "/org/team-a" "$(body "$BK" POST /namespaces -d '{"path":"/org/team-a"}' | jq_ "d['path']")"
-
- # ---- principals ----------------------------------------------------------------------------------------------------
- check "reader lists principals -> 404" "404" "$(code "$PK" GET /principals)"
- check "sub-admin lists tenant principals" "4" "$(body "$SK" GET /principals | jq_ "len(d['data'])")"
- check "principals list has no keys" "no" "$(body "$SK" GET /principals | grep -q 'amk_' && echo yes || echo no)"
- python -I -c "import json,sys;open(sys.argv[1],'w',encoding='utf-8').write(json.dumps({'name':'Bot <b>'+chr(252)+chr(223)+chr(0x65e5)+'</b>','type':'agent'},ensure_ascii=False))" "$TMP/p.json"
- curl -s -D "$TMP/h.txt" -o "$TMP/r.json" -X POST -H "Authorization: Bearer $SK" --data-binary @"$TMP/p.json" "$BASE/principals"
- check "create principal -> 201" "201" "$(sed -n '1s/^HTTP[^ ]* \([0-9]*\).*/\1/p' "$TMP/h.txt")"
- check "create principal: no-store" "yes" "$(grep -qi '^cache-control: no-store' "$TMP/h.txt" && echo yes || echo no)"
- NEW=$(jq_ "d['principal']['id']" < "$TMP/r.json"); NEWK=$(jq_ "d['api_key']['key']" < "$TMP/r.json"); NEWKID=$(jq_ "d['api_key']['id']" < "$TMP/r.json")
- check "new key has prefix" "amk_" "${NEWK:0:4}"
- check "new key authenticates; UTF-8 name round trip" "yes" "$(body "$NEWK" GET /me | jq_ "'yes' if d['name']=='Bot <b>'+chr(252)+chr(223)+chr(0x65e5)+'</b>' and d['principal_id']=='$NEW' else 'no'")"
- check "new principal has no access yet" "0" "$(body "$NEWK" GET /namespaces | jq_ "len(d['data'])")"
- check "create principal bad type -> 422" "422" "$(code "$SK" POST /principals -d '{"name":"x","type":"robot"}')"
- check "create principal missing name -> 422" "422" "$(code "$SK" POST /principals -d '{"type":"agent"}')"
- check "reader cannot create principals -> 404" "404" "$(code "$PK" POST /principals -d '{"name":"x","type":"agent"}')"
- check "principals wrong method -> 405" "405" "$(code "$SK" DELETE /principals)"
-
- check "sub-admin sees keys of a manageable principal" "1 no" "$(body "$SK" GET /principals/$NEW | jq_ "str(len(d['keys']))+' '+('yes' if 'key' in d['keys'][0] else 'no')")"
- check "sub-admin cannot see owner's keys or grants" "None []" "$(body "$SK" GET /principals/$OWN | jq_ "str(d['keys'])+' '+str(d['grants'])")"
- check "reader sees itself" "$PLA" "$(body "$PK" GET /principals/$PLA | jq_ "d['id']")"
- check "reader cannot see others -> 404" "404" "$(code "$PK" GET /principals/$OWN)"
- check "tenant B cannot see A's principal -> 404" "404" "$(code "$BK" GET /principals/$NEW)"
- check "unknown principal -> 404" "404" "$(code "$OK" GET /principals/00000000-0000-4000-8000-000000000000)"
- check "malformed principal id -> 404" "404" "$(code "$OK" GET /principals/abc)"
-
- # ---- grants --------------------------------------------------------------------------------------------------------
- R=$(body "$SK" POST /grants -d "$(grant_body "$NEW" "$NSA" "read,write,read")")
- check "grant -> permissions de-duplicated, inherit default" "['read', 'write'] True" "$(echo "$R" | jq_ "str(d['permissions'])+' '+str(d['inherit'])")"
- G1=$(echo "$R" | jq_ "d['id']")
- check "new principal can now read team-a" "yes" "$(body "$NEWK" GET /namespaces | jq_ "'yes' if any(n['path']=='/org/team-a' for n in d['data']) else 'no'")"
- check "duplicate grant -> 409" "409" "$(code "$SK" POST /grants -d "$(grant_body "$NEW" "$NSA" "read")")"
- check "grant outside scope -> 404" "404" "$(code "$SK" POST /grants -d "$(grant_body "$NEW" "$NSB" "read")")"
- check "non-inheriting admin: inherit grant -> 422" "inherit_not_allowed" "$(body "$NK" POST /grants -d "$(grant_body "$NEW" "$NSB" "read" true)" | jq_ "d['error']['code']")"
- R=$(body "$NK" POST /grants -d "$(grant_body "$NEW" "$NSB" "read" false)")
- check "non-inheriting admin: inherit false -> 201" "False" "$(echo "$R" | jq_ "d['inherit']")"
- G2=$(echo "$R" | jq_ "d['id']")
- check "unknown permission -> 422" "422" "$(code "$SK" POST /grants -d "$(grant_body "$PLA" "$NSX" "superuser")")"
- check "permissions not an array -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":\"read\"}")"
- check "empty permissions -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[]}")"
- check "inherit not boolean -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[\"read\"],\"inherit\":\"yes\"}")"
- check "expires_at in the past -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[\"read\"],\"expires_at\":\"2020-01-01 00:00:00\"}")"
- check "expires_at bad format -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[\"read\"],\"expires_at\":\"tomorrow\"}")"
- check "malformed ids -> 422" "422" "$(code "$SK" POST /grants -d '{"principal_id":"x","namespace_id":"y","permissions":["read"]}')"
- check "grant to other tenant's principal -> 422" "principal_not_found" "$(body "$SK" POST /grants -d "$(grant_body "$BAD" "$NSX" "read")" | jq_ "d['error']['code']")"
- check "grant on other tenant's namespace -> 404" "404" "$(code "$SK" POST /grants -d "$(grant_body "$PLA" "$NSBB" "read")")"
- check "expiring grant -> 201 with expires_at" "2099-01-01 00:00:00" "$(body "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[\"write\"],\"expires_at\":\"2099-01-01 00:00:00\"}" | jq_ "d['expires_at']")"
- check "grants list (sub-admin) has G1, not team-b" "yes no" "$(body "$SK" GET /grants | jq_ "('yes' if any(g['id']=='$G1' for g in d['data']) else 'no')+' '+('yes' if any(g['id']=='$G2' for g in d['data']) else 'no')")"
- check "grants filter by principal" "$NEW" "$(body "$SK" GET "/grants?principal_id=$NEW" | jq_ "' '.join(sorted(set(g['principal_id'] for g in d['data'])))")"
- check "grants filter by namespace" "/org/team-a" "$(body "$SK" GET "/grants?namespace_id=$NSA" | jq_ "' '.join(sorted(set(g['path'] for g in d['data'])))")"
- check "grants bad filter -> 422" "422" "$(code "$SK" GET "/grants?principal_id=abc")"
- check "grants list by reader -> 404" "404" "$(code "$PK" GET /grants)"
- check "grants wrong method -> 405" "405" "$(code "$SK" PUT /grants -d '{}')"
-
- # ---- keys and principal status: no escalation ----------------------------------------------------------------------
- check "sub-admin cannot issue key for principal with access outside its scope -> 404" "404" "$(code "$SK" POST /principals/$NEW/keys)"
- check "sub-admin cannot issue key for the owner -> 404" "404" "$(code "$SK" POST /principals/$OWN/keys)"
- check "non-inheriting admin cannot issue key (inheriting grant elsewhere) -> 404" "404" "$(code "$NK" POST /principals/$NEW/keys)"
- R=$(body "$OK" POST /principals/$NEW/keys)
- check "owner issues key -> new key works" "$NEW" "$(body "$(echo "$R" | jq_ "d['api_key']['key']")" GET /me | jq_ "d['principal_id']")"
- K2=$(echo "$R" | jq_ "d['api_key']['key']")
- check "reader issues own key -> 201" "201" "$(code "$PK" POST /principals/$PLA/keys)"
- check "tenant B issues key for A principal -> 404" "404" "$(code "$BK" POST /principals/$NEW/keys)"
- check "revoke by tenant B -> 404" "404" "$(code "$BK" DELETE /keys/$NEWKID)"
- check "revoke by sub-admin (not manageable) -> 404" "404" "$(code "$SK" DELETE /keys/$NEWKID)"
- check "owner revokes first key -> 200" "revoked" "$(body "$OK" DELETE /keys/$NEWKID | jq_ "d['status']")"
- check "revoked key -> 401" "401" "$(code "$NEWK" GET /me)"
- check "second key still works" "200" "$(code "$K2" GET /me)"
- check "revoke again -> 200" "200" "$(code "$OK" DELETE /keys/$NEWKID)"
- check "revoke unknown key -> 404" "404" "$(code "$OK" DELETE /keys/00000000-0000-4000-8000-000000000000)"
- check "disable principal -> 200" "disabled" "$(body "$OK" PATCH /principals/$NEW -d '{"status":"disabled"}' | jq_ "d['status']")"
- check "disabled principal's key -> 401" "401" "$(code "$K2" GET /me)"
- check "key for disabled principal -> 409" "409" "$(code "$OK" POST /principals/$NEW/keys)"
- check "enable principal -> key works again" "200" "$(code "$OK" PATCH /principals/$NEW -d '{"status":"active"}' >/dev/null; code "$K2" GET /me)"
- check "change own status -> 422" "422" "$(code "$OK" PATCH /principals/$OWN -d '{"status":"disabled"}')"
- check "bad status -> 422" "422" "$(code "$OK" PATCH /principals/$NEW -d '{"status":"gone"}')"
- check "sub-admin disables owner -> 404" "404" "$(code "$SK" PATCH /principals/$OWN -d '{"status":"disabled"}')"
-
- # ---- delete grants -------------------------------------------------------------------------------------------------
- check "delete own grant -> 422" "422" "$(code "$SK" DELETE /grants/$(body "$SK" GET /me | jq_ "d['grants'][0]['id']"))"
- check "delete grant outside scope -> 404" "404" "$(code "$SK" DELETE /grants/$G2)"
- check "tenant B deletes A grant -> 404" "404" "$(code "$BK" DELETE /grants/$G1)"
- check "owner deletes team-b grant -> 200" "True" "$(body "$OK" DELETE /grants/$G2 | jq_ "d['deleted']")"
- check "now sub-admin may issue a key -> 201" "201" "$(code "$SK" POST /principals/$NEW/keys)"
- check "delete grant again -> 404" "404" "$(code "$OK" DELETE /grants/$G2)"
- check "sub-admin deletes G1 -> access gone" "0" "$(code "$SK" DELETE /grants/$G1 >/dev/null; body "$K2" GET /namespaces | jq_ "len(d['data'])")"
-
- echo "passed: $PASS failed: $FAIL"
- for f in "${FAILED[@]}"; do echo " FAIL: $f"; done
- [ "$FAIL" -eq 0 ]
|