Introducing a new way to review your PRs.

Read the announcement

bunkerity / bunkerweb

🛡️ Open-source and cloud-native Web Application Firewall (WAF)

View on GitHub

19 documents · Updated Nov 23, 2026

docs / management-api

Management API

The Management API is BunkerWeb's control-plane service. It gives trusted clients a single HTTP interface to read and change shared configuration, manage protected instances, and run management actions. It uses the shared database and shared API client so that changes made through the API can reach the rest of the management system.

Service boundary

Control plane

The API accepts management requests and coordinates two kinds of work: changes to shared state and calls to BunkerWeb instances. It is not the traffic proxy itself. The web user interface and the administration CLI are clients of the same management plane; see Management services, Web user interface, and Administration CLI.

The public surface is grouped by business area. It covers authentication, instances, services, global settings, custom configurations, bans, plugins, cache files, jobs, and health checks. The service returns one common error shape so clients can handle failures in the same way.

The following diagram shows how a management request moves through the service and its shared facilities.

flowchart LR
    C[UI, CLI, or trusted client] --> G[API security boundary]
    G --> R[Management routes]
    R --> D[Shared database]
    R --> I[Internal or instance API clients]
    D --> S[Shared configuration and management state]
    I --> W[BunkerWeb instances]

References: src/…/main.py• L43-57 src/…/main.py• L170-216 src/…/core.py• L6-52

Request protection

The API places authentication and authorization before protected route work. It supports an administrator's HTTP Basic credentials, an optional administrator Bearer token, and signed Biscuit tokens for users with scoped permissions. Health and documentation endpoints remain available for service checks and discovery.

Administrator authentication has two full-access paths. An administrator API user can use HTTP Basic credentials, or a configured API token can use Bearer authentication. Other Bearer requests follow the scoped Biscuit authorization path. This keeps administrator access separate from resource-level access while allowing both forms of client integration.

Authorization links an HTTP method and route to a resource and permission. A permission may apply to all resources of a type or to one resource. This lets an API user work on a narrow part of the management state without receiving full administrator access. IP allowlists, host checks, and rate limits add network and request controls around this model.

References: src/…/guard.py• L14-20 src/…/guard.py• L28-124 src/…/biscuit.py• L320-385 src/…/main.py• L59-134

Application structure

Application assembly

The application entry point is home/…/main.py. It builds the FastAPI application, attaches the common middleware and error handling, and mounts the category routes through the core router. This creates one service boundary while allowing each management area to keep its own request and domain flow.

The route layer depends on shared helpers rather than creating its own database or instance clients for each request. The dependency layer selects the local API endpoint or builds callers for the registered instances. This keeps route behavior focused on the management action and leaves endpoint discovery in one place.

References: src/…/main.py• L23-27 src/…/main.py• L116-167 src/…/core.py• L7-52 src/…/deps.py• L9-35

Management resources

The API presents management state as related resource groups rather than as separate products. Global settings shape the shared configuration. Services and custom configurations describe what BunkerWeb should protect. Instances identify where that state is applied. Bans, plugins, cache files, and jobs support the operating cycle around that configuration.

Custom configurations can belong to the global scope or to a service. The API supports their normal management life cycle, including text or file-based changes and draft state, while protecting configurations that another source owns. These rules let the API and web interface manage shared configuration without overwriting generated or externally managed content.

Some actions stay local to the management database. Others use the shared HTTP client to contact one instance or many registered instances. The database therefore supplies both durable management state and the instance details needed to perform distributed actions.

References: src/…/core.py• L8-15 src/…/configs.py• L22-24 src/…/configs.py• L61-95 src/…/configs.py• L98-166 src/…/configs.py• L229-288 src/…/configs.py• L380-430 src/…/deps.py• L14-35 src/…/API.py• L103-125 src/…/Database.py• L5996-6044

Startup container

Container entrypoint

The container entrypoint is home/…/entrypoint.sh. It prepares the shared data area, loads container secrets, records the deployment mode, and starts the API server. It also owns signal handling so a stop request reaches the server process and the container exits with the server's result.

The entrypoint removes stale process state before launch and handles stop requests both before and after the server process starts. This closes the gap between container lifecycle events and the API process, so an interrupted start does not leave a misleading process state.

The image exposes the API on port 8888 by default and runs as a dedicated api user. A health check calls the API health path. The data volume keeps service state outside the short-lived container, while runtime files hold the process identity and readiness signal.

References: src/…/entrypoint.sh• L3-5 src/…/entrypoint.sh• L7-44 src/…/entrypoint.sh• L46-82 src/…/entrypoint.sh• L84-97 src/…/Dockerfile• L41-44 src/…/Dockerfile• L56-83

Server readiness

The Gunicorn configuration starts the FastAPI application with asynchronous workers and the API logger. Before it marks the service ready, it prepares runtime paths, establishes the authentication key material, waits for an initialized database, and ensures that an API authentication path exists.

This startup gate prevents the service from accepting normal work while its shared state or security basis is missing. The process writes a readiness file only after these checks pass. A controlled shutdown removes that signal and closes pooled database connections.

References: src/…/gunicorn.conf.py• L100-132 src/…/gunicorn.conf.py• L155-177 src/…/gunicorn.conf.py• L281-317 src/…/gunicorn.conf.py• L556-579 src/…/worker.py• L5-11

Configuration model

Layered settings

The API runtime settings are defined by ApiConfig in home/…/config.py. The shared settings source combines environment variables, secret files, YAML, dotenv values, and defaults. Higher-priority sources can therefore change a deployed service without changing the application image.

The API uses this model for its documentation paths, root path, administrator token, network allowlists, rate limits, and Biscuit lifetime. It also reads the shared database for the internal API port and host name. This creates a link between API runtime behavior and global management settings.

References: src/…/config.py• L19-69 src/…/config.py• L134-160 src/…/yaml_base_settings.py• L146-180

Shared endpoint resolution

The API has two endpoint roles. The server listens for external management clients. The internal endpoint points back to the local BunkerWeb API, while registered instance records provide the targets for distributed actions.

The dependency layer resolves these targets from configuration and database state. If instance lookup fails, it can fall back to the local endpoint for the control-plane path. This makes the database's instance records part of the API's ability to reach the wider BunkerWeb system.

References: src/…/config.py• L134-157 src/…/deps.py• L9-35 src/…/API.py• L103-125

Persistence model

Shared database

The API uses the shared Database service for configuration, services, instances, jobs, plugins, cache records, and other management state. That service owns the connection pool and session life cycle, so the API reuses a pooled database handle instead of defining a separate persistence system.

The database can work with the supported deployment database choices and can enter read-only mode when the main connection cannot accept writes. API changes then report that they cannot be saved, while reads can continue when the shared database permits them.

References: src/…/utils.py• L20-45 src/…/Database.py• L322-489 src/…/Database.py• L500-512

API identity store

API users and their permissions are stored in API-specific tables within the same shared database. APIDatabase provides the API-facing view of those records. It keeps user identity, administrator status, and resource grants together so authentication and route authorization use the same source of truth.

Resource grants support a global scope or a resource-specific scope. Administrator users bypass those grants. The API startup process can build a protected local ACL cache and can bootstrap users and grants from a supplied file, but the durable authority remains the shared database.

References: src/…/api_database.py• L19-49 src/…/api_database.py• L138-202 src/…/api_database.py• L247-321 src/…/model.py• L512-595 src/…/gunicorn.conf.py• L378-406

Utility layer

Shared service handles

The utility layer in home/…/utils.py provides the shared logger, database handles, configuration reporting, and password helpers used across the application. Its database accessors create a handle on first use and reuse it for later requests. The application closes those handles during controlled shutdown.

This layer also keeps password rules and Biscuit key locations in one place. Authentication code can then use the same password checks and key files as startup without copying security rules across route modules.

References: src/…/utils.py• L15-45 src/…/utils.py• L48-65 src/…/utils.py• L68-118 src/…/main.py• L32-41

Logging and client helpers

The API logger aligns Gunicorn and Uvicorn records under the API service's log names. The shared API client centralizes calls to local and remote BunkerWeb endpoints. Together they give the service one view of request activity and one way to perform instance actions.

These helpers are cross-cutting contracts. A change to the shared client can affect API calls made to every registered instance. A change to logging setup can affect both server errors and access records.

References: src/…/logger.py• L21-43 src/…/logger.py• L71-112 src/…/API.py• L103-140

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep route names, resource names, permission names, and the database enums aligned. A mismatch can deny valid scoped users without affecting administrators.
  • Keep API configuration precedence and secret-file handling aligned with deployment files. A new setting is not usable if it is defined in code but cannot arrive through the supported sources.
  • Keep the internal host and port settings aligned with the server listener and the instance records. A mismatch can leave local control-plane calls unable to reach the API.
  • Keep startup readiness, the health check, and runtime file paths aligned. A stale or missing readiness signal can make a healthy process appear unavailable.
  • Keep API user renames and permission ownership changes atomic. Otherwise a renamed user can lose its grants or leave grants attached to the old name.

Important failure modes

TriggerConsequenceGuard
The database is not initialized or cannot become ready during startupThe API does not become ready and exits after its wait periodStartup checks database metadata before readiness
No valid Biscuit keys, administrator user, or API_TOKEN existsThe service exits instead of starting without an authentication pathStartup requires at least one valid authentication basis
A non-admin user lacks the route's resource permissionThe request is rejected even when the user has a valid tokenRoute-to-permission mapping and stored grants must match
The shared database enters read-only mode during a writeThe requested management change is not savedThe shared database reports read-only state and the API returns an error
Biscuit key material changes between restartsTokens signed with the old key fail verificationPersist key files and treat key rotation as a token boundary

Key files

FileWhy to read it
src/…/main.pyShows application assembly, middleware, route mounting, and common response behavior.
src/…/core.pyShows the management route groups and the health boundary.
src/…/configs.pyShows the custom configuration life cycle and ownership rules exposed by the API.
src/…/entrypoint.shShows container preparation, signal handling, and server startup.
src/…/gunicorn.conf.pyShows server workers, readiness gates, authentication bootstrap, and runtime paths.
src/…/config.pyShows API settings and the link from runtime settings to shared database values.
src/…/utils.pyShows shared database handles, password rules, and service-wide API helpers.
src/…/api_database.pyShows the API identity and permission view over the shared database.
src/…/Database.pyShows the shared persistence service used by the API and other management components.
src/…/API.pyShows the shared client used to reach the local API and registered instances.