Skip to main content

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.txt for all dependencies

Installation

  1. Clone the repository.
  2. Navigate to the backend directory:
    cd Topcoder/backend
    
  3. (Recommended) Create a virtual environment:
    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  4. 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_KEY only if you are running locally and want AI review without using the settings UI/CLI.

Guardrails Backend

FastAPI-based service that analyzes code, applies policy/rulepacks, and reports results for PRs, commits, and local scans. It ships with a settings UI, audit logging, and a lightweight CLI for scanning any repository.

What it does

  • Analyzes code via REST endpoints and returns structured findings.
  • Enforces security, coding standards, license/IP, sector rules, and AI review policy.
  • Stores audit logs and provides summaries.
  • Supports rulepacks and repo-level overrides.
  • Integrates with a GitHub App for PR and push scans.

Challenge requirement coverage

  • Secure coding guardrails with OWASP/CWE mappings.
  • Copilot-aware flagging and stricter handling for AI-generated code.
  • Configurable coding standards (YAML/JSON in repo config).
  • AI-assisted review with explainable findings.
  • License/IP checks with restricted license detection.
  • Policy-based enforcement (advisory/warning/blocking) with override label.
  • PR/commit scanning via GitHub App with inline comments and summaries.
  • Traceability with audit IDs, export, and resolution events.
  • Async scan flow for large PRs.
  • Pluggable rulepacks per industry (finance, healthcare, public sector, telecom, government).

How people use it

Website-only:

  1. Open /settings/ui on your deployment.
  2. Save your OpenAI API key.
  3. Set AI mode and auto-fix defaults.
  4. Use the GitHub App integration or call the API directly.

CLI (most common):

  1. pip install guardrails-cli
  2. Open /settings/ui once and save your API key.
  3. Copy guardrails_user_token from browser local storage if SETTINGS_SCOPE=user.
  4. guardrails scan <repo-path> --user <token>

Project layout

  • backend/ — FastAPI service and rule engine
  • github-app/ — GitHub App integration
  • src/guardrails_cli/ — Published CLI package
  • docs/ — Architecture notes

Requirements

  • Python 3.9+
  • Dependencies in backend/requirements.txt

Local setup

cd Topcoder/backend
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install -r requirements.txt

Run the server

uvicorn main:app --reload --host 127.0.0.1 --port 8000

Configuration (environment variables)

Core settings:

  • OPENAI_API_KEY (optional) — used when no per-user key exists
  • SETTINGS_SCOPE (default: global) — global | user | ip
  • SETTINGS_TOKEN (optional) — protects settings endpoints
  • SETTINGS_ENC_KEY (recommended) — encrypts persisted settings
  • SETTINGS_STORE_PATH (default: settings.enc) — encrypted settings file
  • REQUIRE_AI_REVIEW_DEFAULT (default: false)

Audit logging:

  • AUDIT_LOG_ENABLED (default: true)
  • AUDIT_LOG_PATH (default: audit_log.jsonl)
  • AUDIT_LOG_STORE_OUTPUT (default: true)

Data residency:

  • DATA_RESIDENCY (optional)

GitHub App integration

The GitHub App scans PRs and pushes, posts comments/checks, and reads repo overrides from .guardrails/config.yml|yaml|json.

Environment variables:

  • BACKEND_URL (required)
  • OVERRIDE_LABEL (optional, default: guardrails-override)
  • MAX_FILES (optional, default: 100)
  • MAX_FILE_BYTES (optional, default: 200000)
  • USE_ASYNC_SCAN (optional, default: false)

CLI usage

Install:

pip install guardrails-cli

Scan:

guardrails scan <repo-path> --user <token>

Notes:

CLI settings (no UI required)

python guardrails.py settings --generate-local-key
python guardrails.py settings --set-api-key <key>
python guardrails.py settings --ai-mode require|allow
python guardrails.py settings --autofix-mode on|off
python guardrails.py settings --verify

API endpoints

  • GET /health
  • GET /
  • GET /dashboard
  • GET /settings
  • POST /settings/token
  • GET /settings/ui
  • POST /settings/api-key
  • POST /settings/ai-mode
  • POST /settings/autofix-mode
  • POST /analyze
  • POST /analyze-batch
  • POST /scan/async
  • GET /scan/status/{job_id}
  • GET /report/summary
  • GET /report/trends
  • GET /rulepacks
  • POST /rulepacks
  • GET /audit/export
  • POST /audit/resolve
  • GET /docs

Testing

pytest
  • GET /report/summary — Audit summary counts

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

guardrails_cli-0.1.6-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file guardrails_cli-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: guardrails_cli-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for guardrails_cli-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 34135bb9c13b785a3f4d8da422daa1553b7ca1cf89c8f15dfed7d092223877f2
MD5 0ac8a462b8c37ce86cd840850d44c2d4
BLAKE2b-256 036733a113e4f7819b6d3ab6d1f5d9a6ba89aaaea98ffbd3eda3e95f0a81301f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page