No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

168 líneas
15KB

  1. #!/usr/bin/env bash
  2. # Management API checks (spec section 9 "Management", section 16 least privilege; docs/decisions.md ADR-011):
  3. # namespaces, principals, API keys and grants. Covers the happy paths, validation (422), bad input (400), unknown ids
  4. # (404), wrong method (405), privilege escalation attempts (keys for principals with wider access, inheriting grants
  5. # from a non-inheriting admin, grants outside the caller's scope), tenant isolation (AC-06), key revocation and
  6. # principal disabling, UTF-8 round trip and a cold first request.
  7. #
  8. # Needs the container from docker-compose.yml running. Provisions two throw-away tenants and purges them at the end.
  9. # bash tests/admin-api.sh
  10. set -u
  11. BASE=${BASE:-http://localhost:8801/api/v1}
  12. PASS=0; FAIL=0; FAILED=(); TENANTS=()
  13. TMP=$(mktemp -d)
  14. prov() {
  15. MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
  16. -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \
  17. axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015'
  18. }
  19. field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
  20. cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; rm -rf "$TMP"; }
  21. trap cleanup EXIT
  22. check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
  23. 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"; }
  24. body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
  25. jq_() { python -I -c "import sys,json;sys.stdin.reconfigure(encoding='utf-8');d=json.load(sys.stdin);print($1)"; }
  26. mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
  27. grant_body() { python -I -c "import json,sys;d={'principal_id':sys.argv[1],'namespace_id':sys.argv[2],'permissions':sys.argv[3].split(',')};
  28. if sys.argv[4]!='': d['inherit']=(sys.argv[4]=='true')
  29. print(json.dumps(d))" "$1" "$2" "$3" "${4:-}"; }
  30. # ---- setup ------------------------------------------------------------------------------------------------------
  31. TA=$(prov tenant "adm-A" | field tenant_id); TB=$(prov tenant "adm-B" | field tenant_id); TENANTS=("$TA" "$TB")
  32. mk "$TA" owner human; OWN=$P_ID; OK=$P_KEY
  33. mk "$TA" subadmin agent; SUB=$P_ID; SK=$P_KEY
  34. mk "$TA" narrow agent; NAR=$P_ID; NK=$P_KEY
  35. mk "$TA" plain agent; PLA=$P_ID; PK=$P_KEY
  36. mk "$TB" adminB human; BAD=$P_ID; BK=$P_KEY
  37. prov grant "$OWN" /org admin 1 >/dev/null
  38. prov grant "$SUB" /org/team-a admin 1 >/dev/null
  39. prov grant "$NAR" /org/team-b admin 0 >/dev/null
  40. prov grant "$PLA" /org/team-a read 1 >/dev/null
  41. prov grant "$BAD" /org admin 1 >/dev/null
  42. NSA=$(prov namespace "$TA" /org/team-a | field namespace_id); NSB=$(prov namespace "$TA" /org/team-b | field namespace_id)
  43. NSBB=$(prov namespace "$TB" /org | field namespace_id)
  44. [ -n "$NSA" ] && [ -n "$NSB" ] && [ -n "$OK" ] && [ -n "$BK" ] || { echo "setup failed"; exit 2; }
  45. # ---- cold start: first request after a restart is a write -------------------------------------------------------
  46. docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
  47. R=$(body "$SK" POST /namespaces -d '{"path":"/org/team-a/x/y"}')
  48. check "cold: sub-admin creates nested namespace" "2 /org/team-a/x/y" "$(echo "$R" | jq_ "str(d['namespaces_created'])+' '+d['path']")"
  49. NSX=$(echo "$R" | jq_ "d['id']")
  50. # ---- me and namespaces -------------------------------------------------------------------------------------------
  51. check "me -> own principal" "$PLA plain agent" "$(body "$PK" GET /me | jq_ "d['principal_id']+' '+d['name']+' '+d['type']")"
  52. check "me lists own grants" "/org/team-a read" "$(body "$PK" GET /me | jq_ "' '.join([d['grants'][0]['path']]+d['grants'][0]['permissions'])")"
  53. check "me no auth -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' "$BASE/me")"
  54. 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'])")"
  55. check "namespaces: reader permissions" "['read']" "$(body "$PK" GET /namespaces | jq_ "d['data'][0]['permissions']")"
  56. 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'")"
  57. check "namespaces: tenant B sees only its own" "/org" "$(body "$BK" GET /namespaces | jq_ "' '.join(n['path'] for n in d['data'])")"
  58. # ---- create namespace ----------------------------------------------------------------------------------------------
  59. check "create outside admin scope -> 404" "404" "$(code "$SK" POST /namespaces -d '{"path":"/org/team-b/y"}')"
  60. check "non-inheriting admin cannot create below -> 404" "404" "$(code "$NK" POST /namespaces -d '{"path":"/org/team-b/z"}')"
  61. check "reader cannot create -> 404" "404" "$(code "$PK" POST /namespaces -d '{"path":"/org/team-a/r"}')"
  62. check "top-level namespace -> 404" "404" "$(code "$OK" POST /namespaces -d '{"path":"/elsewhere"}')"
  63. check "existing namespace -> 409" "409" "$(code "$SK" POST /namespaces -d '{"path":"/org/team-a/x"}')"
  64. 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'")"
  65. check "bad path -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":"/org/has space"}')"
  66. check "path without slash -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":"org/x"}')"
  67. check "trailing slash -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":"/org/x/"}')"
  68. check "missing path -> 422" "422" "$(code "$OK" POST /namespaces -d '{}')"
  69. check "path not a string -> 422" "422" "$(code "$OK" POST /namespaces -d '{"path":5}')"
  70. check "invalid JSON -> 400" "400" "$(code "$OK" POST /namespaces -d '{"path":')"
  71. check "empty body -> 400" "400" "$(code "$OK" POST /namespaces)"
  72. check "namespaces wrong method -> 405" "405" "$(code "$OK" PUT /namespaces -d '{}')"
  73. 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']")"
  74. # ---- principals ----------------------------------------------------------------------------------------------------
  75. check "reader lists principals -> 404" "404" "$(code "$PK" GET /principals)"
  76. check "sub-admin lists tenant principals" "4" "$(body "$SK" GET /principals | jq_ "len(d['data'])")"
  77. check "principals list has no keys" "no" "$(body "$SK" GET /principals | grep -q 'amk_' && echo yes || echo no)"
  78. 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"
  79. curl -s -D "$TMP/h.txt" -o "$TMP/r.json" -X POST -H "Authorization: Bearer $SK" --data-binary @"$TMP/p.json" "$BASE/principals"
  80. check "create principal -> 201" "201" "$(sed -n '1s/^HTTP[^ ]* \([0-9]*\).*/\1/p' "$TMP/h.txt")"
  81. check "create principal: no-store" "yes" "$(grep -qi '^cache-control: no-store' "$TMP/h.txt" && echo yes || echo no)"
  82. 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")
  83. check "new key has prefix" "amk_" "${NEWK:0:4}"
  84. 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'")"
  85. check "new principal has no access yet" "0" "$(body "$NEWK" GET /namespaces | jq_ "len(d['data'])")"
  86. check "create principal bad type -> 422" "422" "$(code "$SK" POST /principals -d '{"name":"x","type":"robot"}')"
  87. check "create principal missing name -> 422" "422" "$(code "$SK" POST /principals -d '{"type":"agent"}')"
  88. check "reader cannot create principals -> 404" "404" "$(code "$PK" POST /principals -d '{"name":"x","type":"agent"}')"
  89. check "principals wrong method -> 405" "405" "$(code "$SK" DELETE /principals)"
  90. 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')")"
  91. check "sub-admin cannot see owner's keys or grants" "None []" "$(body "$SK" GET /principals/$OWN | jq_ "str(d['keys'])+' '+str(d['grants'])")"
  92. check "reader sees itself" "$PLA" "$(body "$PK" GET /principals/$PLA | jq_ "d['id']")"
  93. check "reader cannot see others -> 404" "404" "$(code "$PK" GET /principals/$OWN)"
  94. check "tenant B cannot see A's principal -> 404" "404" "$(code "$BK" GET /principals/$NEW)"
  95. check "unknown principal -> 404" "404" "$(code "$OK" GET /principals/00000000-0000-4000-8000-000000000000)"
  96. check "malformed principal id -> 404" "404" "$(code "$OK" GET /principals/abc)"
  97. # ---- grants --------------------------------------------------------------------------------------------------------
  98. R=$(body "$SK" POST /grants -d "$(grant_body "$NEW" "$NSA" "read,write,read")")
  99. check "grant -> permissions de-duplicated, inherit default" "['read', 'write'] True" "$(echo "$R" | jq_ "str(d['permissions'])+' '+str(d['inherit'])")"
  100. G1=$(echo "$R" | jq_ "d['id']")
  101. 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'")"
  102. check "duplicate grant -> 409" "409" "$(code "$SK" POST /grants -d "$(grant_body "$NEW" "$NSA" "read")")"
  103. check "grant outside scope -> 404" "404" "$(code "$SK" POST /grants -d "$(grant_body "$NEW" "$NSB" "read")")"
  104. 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']")"
  105. R=$(body "$NK" POST /grants -d "$(grant_body "$NEW" "$NSB" "read" false)")
  106. check "non-inheriting admin: inherit false -> 201" "False" "$(echo "$R" | jq_ "d['inherit']")"
  107. G2=$(echo "$R" | jq_ "d['id']")
  108. check "unknown permission -> 422" "422" "$(code "$SK" POST /grants -d "$(grant_body "$PLA" "$NSX" "superuser")")"
  109. check "permissions not an array -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":\"read\"}")"
  110. check "empty permissions -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[]}")"
  111. check "inherit not boolean -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[\"read\"],\"inherit\":\"yes\"}")"
  112. 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\"}")"
  113. check "expires_at bad format -> 422" "422" "$(code "$SK" POST /grants -d "{\"principal_id\":\"$PLA\",\"namespace_id\":\"$NSX\",\"permissions\":[\"read\"],\"expires_at\":\"tomorrow\"}")"
  114. check "malformed ids -> 422" "422" "$(code "$SK" POST /grants -d '{"principal_id":"x","namespace_id":"y","permissions":["read"]}')"
  115. 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']")"
  116. check "grant on other tenant's namespace -> 404" "404" "$(code "$SK" POST /grants -d "$(grant_body "$PLA" "$NSBB" "read")")"
  117. 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']")"
  118. 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')")"
  119. 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'])))")"
  120. 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'])))")"
  121. check "grants bad filter -> 422" "422" "$(code "$SK" GET "/grants?principal_id=abc")"
  122. check "grants list by reader -> 404" "404" "$(code "$PK" GET /grants)"
  123. check "grants wrong method -> 405" "405" "$(code "$SK" PUT /grants -d '{}')"
  124. # ---- keys and principal status: no escalation ----------------------------------------------------------------------
  125. check "sub-admin cannot issue key for principal with access outside its scope -> 404" "404" "$(code "$SK" POST /principals/$NEW/keys)"
  126. check "sub-admin cannot issue key for the owner -> 404" "404" "$(code "$SK" POST /principals/$OWN/keys)"
  127. check "non-inheriting admin cannot issue key (inheriting grant elsewhere) -> 404" "404" "$(code "$NK" POST /principals/$NEW/keys)"
  128. R=$(body "$OK" POST /principals/$NEW/keys)
  129. check "owner issues key -> new key works" "$NEW" "$(body "$(echo "$R" | jq_ "d['api_key']['key']")" GET /me | jq_ "d['principal_id']")"
  130. K2=$(echo "$R" | jq_ "d['api_key']['key']")
  131. check "reader issues own key -> 201" "201" "$(code "$PK" POST /principals/$PLA/keys)"
  132. check "tenant B issues key for A principal -> 404" "404" "$(code "$BK" POST /principals/$NEW/keys)"
  133. check "revoke by tenant B -> 404" "404" "$(code "$BK" DELETE /keys/$NEWKID)"
  134. check "revoke by sub-admin (not manageable) -> 404" "404" "$(code "$SK" DELETE /keys/$NEWKID)"
  135. check "owner revokes first key -> 200" "revoked" "$(body "$OK" DELETE /keys/$NEWKID | jq_ "d['status']")"
  136. check "revoked key -> 401" "401" "$(code "$NEWK" GET /me)"
  137. check "second key still works" "200" "$(code "$K2" GET /me)"
  138. check "revoke again -> 200" "200" "$(code "$OK" DELETE /keys/$NEWKID)"
  139. check "revoke unknown key -> 404" "404" "$(code "$OK" DELETE /keys/00000000-0000-4000-8000-000000000000)"
  140. check "disable principal -> 200" "disabled" "$(body "$OK" PATCH /principals/$NEW -d '{"status":"disabled"}' | jq_ "d['status']")"
  141. check "disabled principal's key -> 401" "401" "$(code "$K2" GET /me)"
  142. check "key for disabled principal -> 409" "409" "$(code "$OK" POST /principals/$NEW/keys)"
  143. check "enable principal -> key works again" "200" "$(code "$OK" PATCH /principals/$NEW -d '{"status":"active"}' >/dev/null; code "$K2" GET /me)"
  144. check "change own status -> 422" "422" "$(code "$OK" PATCH /principals/$OWN -d '{"status":"disabled"}')"
  145. check "bad status -> 422" "422" "$(code "$OK" PATCH /principals/$NEW -d '{"status":"gone"}')"
  146. check "sub-admin disables owner -> 404" "404" "$(code "$SK" PATCH /principals/$OWN -d '{"status":"disabled"}')"
  147. # ---- delete grants -------------------------------------------------------------------------------------------------
  148. check "delete own grant -> 422" "422" "$(code "$SK" DELETE /grants/$(body "$SK" GET /me | jq_ "d['grants'][0]['id']"))"
  149. check "delete grant outside scope -> 404" "404" "$(code "$SK" DELETE /grants/$G2)"
  150. check "tenant B deletes A grant -> 404" "404" "$(code "$BK" DELETE /grants/$G1)"
  151. check "owner deletes team-b grant -> 200" "True" "$(body "$OK" DELETE /grants/$G2 | jq_ "d['deleted']")"
  152. check "now sub-admin may issue a key -> 201" "201" "$(code "$SK" POST /principals/$NEW/keys)"
  153. check "delete grant again -> 404" "404" "$(code "$OK" DELETE /grants/$G2)"
  154. check "sub-admin deletes G1 -> access gone" "0" "$(code "$SK" DELETE /grants/$G1 >/dev/null; body "$K2" GET /namespaces | jq_ "len(d['data'])")"
  155. echo "passed: $PASS failed: $FAIL"
  156. for f in "${FAILED[@]}"; do echo " FAIL: $f"; done
  157. [ "$FAIL" -eq 0 ]

Powered by TurnKey Linux.