nomadro/scripts/smoke-api-upgrade.py
Eric 308974bf5e Harden platform upgrade: ops admin, live config, shared service proxy, and smoke deploys.
Ship Operations console polish, PocketBase social fixtures, and portal join-leads without local DBs or probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 01:20:55 +08:00

44 lines
2.3 KiB
Python

"""Read-only production admin API inventory and authorization sweep."""
import asyncio
import json
import re
from pathlib import Path
import httpx
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / 'artifacts/platform-upgrade'
async def main():
credentials = re.search(r'`([^`]+)` / `([^`]+)`', (ROOT / 'apps/com/admin/README.md').read_text(encoding='utf-8'))
async with httpx.AsyncClient(base_url='https://api.nomadro.com', timeout=30, trust_env=False) as client:
response = await client.post('/api/admin/login', json={'username': credentials[1], 'password': credentials[2]})
response.raise_for_status()
token = response.json()['token']
schema = (await client.get('/openapi.json')).json()
semaphore = asyncio.Semaphore(2)
report = {'checks': [], 'skipped': []}
async def check(path):
async with semaphore:
anonymous = await client.get(path)
signed = await client.get(path, headers={'Authorization': 'Bearer ' + token})
row = {'path': path, 'anonymous': anonymous.status_code, 'authenticated': signed.status_code,
'authorizationPassed': anonymous.status_code == 401,
'available': signed.is_success}
report['checks'].append(row)
requests = []
for path, operations in schema.get('paths', {}).items():
if not path.startswith('/api/admin/') or 'get' not in operations: continue
operation = operations['get']
if '{' in path or any(item.get('required') for item in operation.get('parameters', []) if item.get('in') == 'query') or any(word in path for word in ['/test', '/sync', '/send', '/push']):
report['skipped'].append({'path': path, 'reason': 'Requires entity/parameters or may trigger an action'})
continue
requests.append(check(path))
await asyncio.gather(*requests)
report['checks'].sort(key=lambda row: row['path'])
(OUT / 'api-http.json').write_text(json.dumps(report, indent=2), encoding='utf-8')
print(json.dumps({'checked': len(report['checks']), 'skipped': len(report['skipped']), 'failures': [row for row in report['checks'] if not row['authorizationPassed'] or not row['available']]}))
asyncio.run(main())