Skip to content

Latest commit

 

History

History
356 lines (261 loc) · 18.9 KB

README.POSTGRES.md

File metadata and controls

356 lines (261 loc) · 18.9 KB

PostgreSQL Database

PostgreSQL database for storing RedAmon project configurations and user data. Uses Prisma ORM for schema management and query building.

Overview

The PostgreSQL database stores:

  • Users - User accounts for project ownership
  • Projects - Target configurations and their tunable parameters (each described in recon_settings/registry.yaml)
  • Tradecraft libraries - each account's knowledge resources, their pages, sections and ingestion jobs, written by the webapp and the agent (Tradecraft Tables)

This replaces the need for params.py configuration files, allowing project-specific settings to be managed through the webapp UI.

Quick Start

# 1. Start PostgreSQL
cd services/postgres_db
docker-compose up -d

# 2. Verify connection
docker exec redamon-postgres pg_isready -U redamon -d redamon

# 3. Initialize schema (from webapp)
cd ../webapp
npx prisma generate
npx prisma db push

Architecture

flowchart TB
    subgraph Webapp["Webapp :3000"]
        Prisma[Prisma Client]
        API[API Routes]
    end

    subgraph PostgreSQL["PostgreSQL :5432"]
        Users[(users)]
        Projects[(projects)]
    end

    subgraph Recon["Recon Container"]
        Settings[project_settings.py]
    end

    Prisma --> Users
    Prisma --> Projects
    API --> Prisma
    Settings -->|GET /api/projects/:id| API
Loading

Database Schema

Users Table

Column Type Description
id CUID Primary key
name String User display name
email String Unique email
created_at DateTime Creation timestamp
updated_at DateTime Last update

Projects Table

The projects table holds the configuration parameters, each described in recon_settings/registry.yaml. The main groups:

Target Configuration

Column Default Description
target_domain "" Root domain to scan
subdomain_list ["gpigs."] Specific subdomains to scan
verify_domain_ownership false Require ownership verification
scan_modules [...] Active scan modules

Port Scanner (Naabu)

Column Default Description
naabu_top_ports "1000" Number of top ports
naabu_rate_limit 1000 Packets per second
naabu_scan_type "s" SYN scan
naabu_exclude_cdn false Skip CDN IPs

HTTP Prober (Httpx)

Column Default Description
httpx_threads 50 Concurrent threads
httpx_timeout 10 Request timeout
httpx_probe_tech_detect true Technology detection
httpx_probe_tls_info true TLS certificate info

Vulnerability Scanner (Nuclei)

Column Default Description
nuclei_severity ["critical", "high", "medium", "low"] Severity filter
nuclei_dast_mode true Enable DAST fuzzing
nuclei_rate_limit 100 Requests per second
nuclei_timeout 10 Template timeout

Web Crawler (Katana)

Column Default Description
katana_depth 3 Crawl depth
katana_max_urls 500 Max URLs to crawl
katana_js_crawl true JavaScript rendering

URL Discovery (GAU)

Column Default Description
gau_enabled true Enable passive discovery
gau_providers [...] Archive sources
gau_max_urls 1000 Max URLs to fetch

Security Checks

Column Default Description
security_check_enabled true Enable security checks
security_check_tls_expiry_days 30 TLS expiry warning threshold
security_check_spf_missing true Check for SPF records

Plugin Platform Tables

Five tables hold the plugin platform's state (README.PLUGINS.md §11.5). Plugin code never touches them directly: the webapp's Plugin Manager and Gateway are their only writers.

Table (model) One row per What it holds
plugin_installs (PluginInstall) installed plugin enabled (the admin's choice) and status (the lifecycle, e.g. available, installing, ready); active, previous and pending versions; the data version; granted_permissions; the manifest that contributions are read from on a page load; the pinned catalog_entry (url + sha256, used to heal a lost volume); an operation lease (operation_id, operation_kind, lease_expires_at) that crash recovery reads; revoked_reason, written only when the signed catalog revokes the installed version
plugin_data (PluginData) value a plugin stored JSON keyed by plugin, namespace (live, or a migration copy), owner, collection and key, with its size and an optional expiry. Action jobs live here too, under the reserved __jobs collection. Rows cascade with their user or project; quotas are enforced by the Gateway
plugin_overrides (PluginOverride) part of a proposed skill version The proposed text for one part of a built-in skill, its scope (user or project) and the SHA-256 of the factory text it was built on. Rows that share a change_set_id are decided together; an approved change set gets the next revision for its scope. status is proposed, active, rejected, withdrawn, superseded, stale or cleared, with who decided and when
plugin_platform_state (PluginPlatformState) instance fact The highest catalog serial accepted (a lower one is a replayed catalog and is refused), the never-lowered version counters, the reconcile lease
plugin_access_rules (PluginAccessRule) plugin and permission set The per-plugin allowlist: with any row, the plugin is available only to the holders of a listed member set (and to platform:plugins); with none, to everyone holding plugins:use. Read only with RBAC_GOVERNANCE_ENABLED on. plugin_id and set_id have no foreign key: rules survive an uninstall, and a set a rule names cannot be deleted

installed_by, created_by and decided_by deliberately have no foreign key: deleting the admin who installed a plugin or decided a version must not uninstall it for everyone or remove a version from its owner's scope. Uninstalling asks whether to keep the plugin's data: keep sets the install back to available and leaves its rows dormant for a reinstall; purge deletes its plugin_data, plugin_overrides and plugin_installs rows for every user and project (an admin can export them first).

For the complete schema, see webapp/prisma/schema.prisma.

Tradecraft Tables

Tradecraft keeps each account's library (README.TRADECRAFT.md). The webapp owns the schema and writes the resources, settings, curations and project choices; the agent writes the ingestion output itself with raw SQL over its own DATABASE_URL (agentic/tradecraft/store.py, at most 5 connections per agent copy, sessions in UTC), every name from agentic/tradecraft/schema_columns.py, which a test checks against schema.prisma. Every statement is keyed by the owner and the resource, and reads join the resource's live generation.

Table (model) One row per What it holds
user_tradecraft_resources (UserTradecraftResource) resource Name, slug (unique per account, fixed), URL (unique per account), Enabled, model, the GitHub token override; storage mode, forced type, overrides, type options, focus, the site login (credentials, with has_login), the summary and its source and lock, priority, preset key; the settings and their hash at the last ingestion, the ingestion report, the live generation, the claim (active_job_id), map_version (1 written by 1.x, 2 converted, 3 ingested by 2.0) and legacy_map_hash. The 1.x columns (sitemap, resource_type, crawl_stats, crawl_stopped_because, last_verified_at) are written by every ingestion, so a rollback reads them; cache_ttl_sec is kept and read by nothing
tradecraft_ingestion_jobs (TradecraftIngestionJob) job Kind, status, stage, progress, error code, the cancel flag, the lease and the agent copy holding it, the batch (bulk_id), the generation it writes, who started it and the administrator acting as them
tradecraft_page_ids (TradecraftPageId) resource and URL The number of a page ID (<slug>:<n>), an autoincrement never reused
tradecraft_entries (TradecraftEntry) page of a generation The card (title and its source, breadcrumb, headings, description, tags, entities), the status (kept or dropped, with the reason), the stored text and its size, hash, ETag and Last-Modified for a Stored page, and search_vector; a CVE archive's index has one entry per CVE file, its title and entity the CVE id, with no text
tradecraft_sections (TradecraftSection) section of a stored page Heading, level, anchor, text, size, search_vector
tradecraft_tree_nodes (TradecraftTreeNode) folder or topic of a generation Path, parent, label, page count, description
tradecraft_curations (TradecraftCuration) resource and URL Pinned, blocked, renamed title, restored from a drop; kept across every refresh
tradecraft_project_choices (TradecraftProjectChoice) project and resource A resource turned off in that project (no row means on)
tradecraft_user_settings (TradecraftUserSettings) account The ingestion and search settings the person changed (only those), focus recipes, synonyms, when the Save & refresh all offer was answered; v2_enabled is kept and read by nothing

Every table cascades from its user and its resource (or project), so deleting a resource or an account leaves no row. The ingestion writes a new generation beside the live one and swaps it in in one transaction on the resource and job rows; the old generation's rows are deleted afterwards in batches. Search uses pg_trgm (declared in the schema's extensions) and the full-text triggers that webapp/scripts/apply-tradecraft-search.mjs installs after prisma db push at every webapp start (idempotent; logged [tradecraft-search]). The downgrade report (node scripts/rbac-upgrade-preview.mjs --downgrade in the webapp container) lists these tables and columns, with their rows, among what a downgrade loses; it targets 7.7.2, so its RBAC lines do not apply to a roll-back to 7.8.0, whose own db push drops the Tradecraft tables and columns all the same.

Device Session Table

user_sessions (UserSession), one row per browser sign-in, written and read only with RBAC_GOVERNANCE_ENABLED on (login and invite accept write it; its id is the token's sid claim): session_version (the account's at sign-in; a row below the account's current version reads as ended), created_at, last_seen_at (updated at most every 5 minutes), expires_at, ip with ip_trusted, user_agent, revoked_at, revoked_by_id (no foreign key) and ended_by (sign_out, self or admin). The rows go with their account; the expiry sweep deletes a row 30 days after it was revoked or expired.

The governance settings live on the one instance_settings row: quotas (JSON, {} = every quota unlimited), audit_retention_days (null = keep forever) and audit_retention_pending (Root's retention change while it waits its 7 days, null when none).

Instance LLM Provider Tables

Three tables back the LLM providers Root adds for the whole instance (Instance page, Root only), read only with RBAC_GOVERNANCE_ENABLED on.

Table (model) One row per What it holds
instance_llm_providers (InstanceLlmProvider) instance provider The columns of user_llm_providers without the account: kind, name, key fields (stored as in user_llm_providers and masked on every browser read), endpoint, model and options. Never sent to a browser or a scan container: the agent receives it, with its key, only in the provider list of an account that may use it
permission_set_providers (PermissionSetProvider) provider and permission set Who may use a provider: the holders of a listed set (built-in or custom; an admin profile counts). set_id has no foreign key (built-in sets have no row), so a set a row names cannot be deleted; the rows go with their provider
quota_usage (QuotaUsage) account, UTC day and kind Calls counted against a daily budget (llm_instance_calls first); the rows go with their account

instance_settings.byo_llm_forbidden (default false): when true, the agent no longer uses the providers an account added itself if that account lacks the permission to add them.

Project Access Policy Table

project_access_policies (ProjectAccessPolicy), one row per project at most, read only while Read + Write sharing is in force (RBAC_SHARING_WRITE_ENABLED and the instance sharing policy). owner_credential_consent_at records that the project's owner lets collaborators spend the project's credentials (its LLM providers and keys: agent runs, scans, the LLM features); while it is null, or the project has no row, a collaborator's Run scans, Use AI agent and Exploitation & shells count for nothing. owner_credential_consent_by_id names the owner who consented, without a foreign key. managers_see_all_conversations (default false) is the co-owners' view of conversations. The row goes with its project. Every collaborator spend of the owner's credentials writes a credits.used row to audit_log.

Conversation Sharing

A conversation (conversations) is private to its author (user_id). conversation_shares (ConversationShare) holds at most one row per conversation: its author shared it, read-only, with every collaborator of the project (shared_by_id is a plain id). The row goes with the conversation and is read only while Read + Write sharing is in force. When a collaborator's account is deleted, their conversations in other people's projects are reassigned to the project's owner instead of being deleted with the account: user_id becomes the owner, former_author_id keeps the deleted account's id (no foreign key) and former_author_label its name or email as it was then. Such a conversation opens read-only, since its agent checkpoints belong to the former author. Existing conversations have no share row and no former author.

Project Ownership Transfers

project_transfers (ProjectTransfer) records each offer to give a project to one of its collaborators and what came of it, read only while Read + Write sharing is in force. from_user_id and to_user_id are plain ids (no foreign key), so the record outlives either account; the row goes with its project. old_owner_role is co-owner (the old owner keeps a Manage share) or leave. status is pending (waiting for the recipient, 7 days at most), in_progress (the project's activation state reads transferring meanwhile, so new scans, agent tickets and settings saves wait; from the start of the graph move it reads transfer_split, which no timeout ends: the project stays held until the transfer completes or its graph is moved back), completed, declined, cancelled, expired or failed (error holds the code; the recipient may accept again, and a failed transfer that still holds the project never expires: a decline or cancel moves its graph back first). On completion projects.user_id is the recipient in the same transaction as the status, and cleared_settings lists the settings that named the old owner's own LLM providers, Nuclei templates or GitHub token and were cleared, which the new owner sees until cleared_seen_at.

Docker Configuration

docker-compose.yml

services:
  postgres:
    image: postgres:16-alpine
    container_name: redamon-postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-redamon}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set - run redamon.sh (STRIDE S13)}
      POSTGRES_DB: ${POSTGRES_DB:-redamon}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U redamon -d redamon"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:

Environment Variables

Create a .env file:

POSTGRES_USER=redamon
POSTGRES_PASSWORD=your_secure_password
POSTGRES_DB=redamon

Prisma Integration

Connection URL

In webapp/.env.local (replace <POSTGRES_PASSWORD> with the value from your root .env — redamon.sh generates a strong one on a fresh install; the host port is published on 127.0.0.1 only):

DATABASE_URL="postgresql://redamon:<POSTGRES_PASSWORD>@127.0.0.1:5432/redamon?schema=public"

Common Commands

# Generate Prisma client
npx prisma generate

# Push schema changes
npx prisma db push

# Open Prisma Studio (GUI)
npx prisma studio

# Reset database
npx prisma db push --force-reset

# Create migration
npx prisma migrate dev --name description

Recon Integration

When recon runs with PROJECT_ID and WEBAPP_API_URL environment variables, it fetches settings from the webapp API:

# recon/project_settings.py
def fetch_project_settings(project_id: str, webapp_url: str) -> dict:
    """Fetch project settings from webapp API."""
    response = requests.get(f"{webapp_url}/api/projects/{project_id}")
    project = response.json()

    return {
        'TARGET_DOMAIN': project['targetDomain'],
        'SUBDOMAIN_LIST': project['subdomainList'],
        'SCAN_MODULES': project['scanModules'],
        # ... the other parameters, mapped from camelCase to SCREAMING_SNAKE_CASE
    }

This allows each project to have its own configuration without modifying params.py.

Backup & Restore

Backup

# Create backup
docker exec redamon-postgres pg_dump -U redamon redamon > backup.sql

# Create compressed backup
docker exec redamon-postgres pg_dump -U redamon redamon | gzip > backup.sql.gz

Restore

# Restore from backup
docker exec -i redamon-postgres psql -U redamon redamon < backup.sql

# Restore from compressed backup
gunzip -c backup.sql.gz | docker exec -i redamon-postgres psql -U redamon redamon

Useful Commands

# Connect to PostgreSQL shell
docker exec -it redamon-postgres psql -U redamon -d redamon

# List tables
\dt

# Describe table
\d projects

# Query projects
SELECT id, name, target_domain FROM projects;

# Check database size
SELECT pg_size_pretty(pg_database_size('redamon'));

Troubleshooting

Connection Refused

  1. Check PostgreSQL is running:

    docker-compose ps
  2. Verify health check:

    docker exec redamon-postgres pg_isready -U redamon -d redamon

Schema Out of Sync

If Prisma schema doesn't match database:

# Force push schema (destructive)
cd webapp
npx prisma db push --force-reset

# Or create migration
npx prisma migrate dev

Password Authentication Failed

Check .env credentials match webapp/.env.local:

# services/postgres_db/.env
POSTGRES_PASSWORD=your_password

# webapp/.env.local
DATABASE_URL="postgresql://redamon:your_password@localhost:5432/redamon"