Guardrails CLI for scanning repositories
Project description
Topcoder Guardrails Backend
Overview
This project is a secure, modular backend for code analysis, policy enforcement, and audit logging. It uses FastAPI and includes endpoints for code analysis, health checks, and a dashboard for audit logs.
Features
- REST API for code analysis (
/analyze) - Health check endpoint (
/health) - Audit dashboard (
/dashboard) - Modular rulepacks for different sectors
- Security, coding standards, license/IP, and AI review checks
Requirements
- Python 3.9+
- See
backend/requirements.txtfor all dependencies
Installation
- Clone the repository.
- Navigate to the backend directory:
cd Topcoder/backend
- (Recommended) Create a virtual environment:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
Configuration
This app is per-user, not global. Settings are scoped by user token when SETTINGS_SCOPE=user.
Local-only (optional):
- Set
OPENAI_API_KEYonly if you are running locally and want AI review without using the settings UI/CLI. - Optional: set
REQUIRE_AI_REVIEW=falsefor local/dev to allow non-AI runs.
Audit logging:
AUDIT_LOG_ENABLED(default: true)AUDIT_LOG_PATH(default: audit_log.jsonl)AUDIT_LOG_STORE_OUTPUT(default: true) — store full results in audit log
Data residency:
DATA_RESIDENCY(optional) — reject requests when repo config requires a different residency
Persistent settings (encrypted):
SETTINGS_ENC_KEY(required for persistence) — a Fernet key used to encrypt stored settingsSETTINGS_STORE_PATH(optional) — file path for encrypted storage (default:settings.enc)SETTINGS_SCOPE(recommended:user) —userkeeps settings per user tokenREQUIRE_AI_REVIEW_DEFAULT(optional) — default AI mode when user settings are not set (default: false)
Running the Server
From the Topcoder/backend directory:
uvicorn main:app --reload --host 127.0.0.1 --port 8000
GitHub App Integration
The GitHub app scans PRs and posts a Guardrails report plus a check run, with inline PR review comments for findings in the diff. It also scans push events and reports results on the latest commit.
Environment variables:
BACKEND_URL(required): Guardrails API base URLOVERRIDE_LABEL(optional): label name that allows blocking overrides (default:guardrails-override)MAX_FILES(optional): maximum files per PR to scan (default: 100)MAX_FILE_BYTES(optional): max file size in bytes (default: 200000)USE_ASYNC_SCAN(optional): enable async scan flow (default: false)
The app reads .guardrails/config.yml|yaml|json from the repo when available to apply sector and policy overrides.
Hosted usage (no repo cloning)
If you deploy the backend as a hosted service, users can supply their own OpenAI key without cloning or installing anything.
Per-user flow (required when SETTINGS_SCOPE=user):
- The UI generates a User Token and stores it locally in the browser
- All settings are saved per token
Optional protection:
- Set
SETTINGS_TOKENto protect the settings endpoints
This enables scanning any connected GitHub repo via the app, or calling the API directly from external tools.
Settings UI:
- Website (main app): https://topcoder-production.up.railway.app
- Open
/settings/uion the website to store the key in the main app. - Auto-fix default and AI mode can be set in the UI and used by the CLI.
- When
SETTINGS_SCOPE=user, the UI generates a per-user token and stores it locally. - You can also paste an existing token (from CLI) and click “Use User Token”.
Step-by-step (UI only):
- Open
/settings/ui. - Click “Generate User Token” (or paste one and click “Use User Token”).
- Paste your OpenAI API key and click “Save Key”.
- Choose AI mode and Auto-fix defaults.
- Use the CLI with the same token to scan any repo.
CLI usage (scan any local repo)
Use the lightweight CLI wrapper to scan any repo from its root:
- Run the CLI in this repo via
python guardrails.py scan <repo-path> - Provide the hosted API URL and API key via flags or environment
- Default hosted URL is
https://topcoder-production.up.railway.app - Set
GUARDRAILS_API_URLto override the default and avoid passing--apievery time - Optional
--autofixapplies safe local fixes and stores backups in.guardrails_backup - If no API key is detected, the CLI prompts for a key or lets you run in non-AI mode
- Use
--no-aito explicitly disable AI review for that run - If
SETTINGS_ENC_KEYis set locally, the CLI saves the entered key to encrypted storage - For
SETTINGS_SCOPE=user, pass--user <token>or setGUARDRAILS_USERto match the UI token - If no token is provided, the CLI prompts to paste one or generates a new token
Install without cloning (PyPI):
pip install guardrails-cli- Then run
guardrails scan <repo-path>from any folder
Quick start (CLI):
pip install guardrails-cliguardrails scan . --user <token>- When prompted, paste the token from the UI (or press Enter to generate a new one)
Local install from repo (optional):
- Run
python install-cli.pyfrom this repo to addguardrailsto your PATH - After that, run
guardrails scan <repo-path>from any folder
Publish automation (PyPI):
- Run
python publish_cli.pyto bump patch version, build, and upload - Run
python publish_cli.py --watchto auto-publish on changes Docker auto-publish: - Set
PUBLISH_CLI=trueplusTWINE_USERNAME=__token__andTWINE_PASSWORD=<pypi-token>
CLI settings (no website required)
You can manage the same settings from the CLI:
- Generate a server key:
python guardrails.py settings --generate-key - Generate a local key:
python guardrails.py settings --generate-local-key - Set API key:
python guardrails.py settings --set-api-key <key> - Set AI mode:
python guardrails.py settings --ai-mode require|allow - Set auto-fix default:
python guardrails.py settings --autofix-mode on|off - Verify settings sync:
python guardrails.py settings --verify
Optional flags:
--apito target a different backend URL--tokenif SETTINGS_TOKEN is enabled on the server--userto setX-Guardrails-Userfor scoped settings (useful behind shared IPs)
GitHub App endpoint
Use the same deployment for the webhook URL:
- Webhook URL: https://topcoder-production.up.railway.app
Scan an entire repository
From the Topcoder/backend directory, run:
python scan_repo.py .. --api https://topcoder-production.up.railway.app --sector finance --output scan_results.json
Notes:
- The scan writes a JSON report to
scan_results.json. - Adjust
--apifor local (http://127.0.0.1:8000) or hosted usage. - Use
--max-filesto limit large repos.
API Endpoints
GET /health— Health checkPOST /analyze— Analyze code (JSON:{ "code": "..." })POST /analyze-batch— Analyze multiple filesPOST /scan/async— Queue an asynchronous scanGET /scan/status/{job_id}— Check async scan statusGET /dashboard— View audit dashboardGET /audit/export— Export audit logGET /report/summary— Audit summary countsGET /rulepacks— List rulepacksPOST /rulepacks— Upload a rulepackGET /docs— Interactive API docs (Swagger UI)
Testing
Run all backend tests:
pytest
Manual end-to-end usage (what you actually do):
- Open
/settings/uiand generate a User Token (required whenSETTINGS_SCOPE=user). - Paste your OpenAI API key and set AI/Auto-fix defaults.
- From any repo, run
guardrails scan . --user <token>. - Confirm the scan results in
scan_results.jsonand audit entries in the dashboard.
CLI-only test (no UI):
guardrails settings --generate-local-key(optional)guardrails settings --set-api-key <key> --user <token>guardrails settings --ai-mode require --user <token>guardrails scan . --user <token>
Notes
- All dependencies are listed in
requirements.txt. - The dashboard reads from
audit_log.jsonl. - For production, set environment variables securely and use HTTPS.
Changelog
- 2026-01-26: Full test and endpoint verification, requirements and documentation updated.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guardrails_cli-0.1.0.tar.gz.
File metadata
- Download URL: guardrails_cli-0.1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5377e04027dcb18829edc648d08468911f8542b6e7413a5d39d3be5d6b89a048
|
|
| MD5 |
d2593aee78203465a5626522522ec800
|
|
| BLAKE2b-256 |
549793b13f19304ea77caeb6269644fd9b91fe8c88599ecf04fdcd834b07b394
|
File details
Details for the file guardrails_cli-0.1.0-py3-none-any.whl.
File metadata
- Download URL: guardrails_cli-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
386647f36f21e781658fe561e5740db5b375c3cdf5f033b451a494ad25780204
|
|
| MD5 |
a8e367c12150b409ca09ed6215322836
|
|
| BLAKE2b-256 |
0506f17995ffe677018e92d9f4f0b3ea8dcf6a871fc6abadc362d87ea041849a
|