Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Fornax Cutouts User Documentation

Auth Providers Overview

POST /cutouts/async can limit how many cutouts a single identity requests within a rolling time window. The feature is off by default (CUTOUTS__CUTOUT_LIMIT__ENABLED=false); enable it and tune the settings below before limits take effect.

When enabled, anonymous requests are bucketed by a hashed client IP. An auth provider lets you plug in your own authentication/authorization logic — resolved from a request header, cookie, or token — to raise or remove that limit for known users.


Configuration

Nested under CUTOUTS__CUTOUT_LIMIT__, plus one top-level setting for reverse-proxy trust.

Environment VariableTypeDefaultDescription
CUTOUTS__CUTOUT_LIMIT__ENABLEDboolfalseMaster switch. When false, CutoutLimiter.reserve / reconcile are no-ops and no 429s are returned.
CUTOUTS__CUTOUT_LIMIT__ANON_CUTOUT_LIMITint10Rolling-window cutout budget for anonymous (IP-derived) principals.
CUTOUTS__CUTOUT_LIMIT__WINDOW_SECONDSint60Rolling window length in seconds. Overridable per principal via Principal.window_seconds.
CUTOUTS__CUTOUT_LIMIT__PRINCIPAL_RESOLVERpath—Optional path to a .py file or directory of auth provider modules, loaded in addition to CUTOUTS__SOURCE_PATH.
CUTOUTS__NUM_TRUSTED_PROXIESint0Number of trusted reverse proxies in front of the API. Used when deriving the client IP from X-Forwarded-For (see Anonymous principals).

See Configuration for all other service settings.


Anonymous principals

When no auth provider is registered, the provider returns None, or resolution fails in a controlled way (see below), the registry builds an anonymous Principal:

Client IP is resolved by client_ip_from_request():

  1. X-Forwarded-For: split on commas and take the entry at index -num_trusted_proxies — the rightmost client IP after stripping trusted proxy hops appended at the end of the list. This prevents clients from spoofing their IP by prepending values to X-Forwarded-For.

  2. X-Real-IP: used when X-Forwarded-For is absent.

  3. TCP peer address: request.client.host as a last resort.

Set CUTOUTS__NUM_TRUSTED_PROXIES to the number of proxies you control between the internet and the API (for example 1 behind a single load balancer).


The Registry

The AuthRegistry is a singleton that holds at most one registered auth provider. It is created once at import time and shared across the API process.

from fornax_cutouts.auth import auth_registry

Discovery

Auth providers are discovered by the same discover_sources() call that loads mission sources at startup. Decorate a class with @auth_registry.register_provider and put it either under CUTOUTS__SOURCE_PATH alongside the mission sources, or in a dedicated location pointed at by CUTOUTS__CUTOUT_LIMIT__PRINCIPAL_RESOLVER (a single .py file or a directory that is globbed recursively). A file that lives under both paths is executed only once.

A CUTOUTS__CUTOUT_LIMIT__PRINCIPAL_RESOLVER path that does not exist is logged as a warning and skipped; the service starts and falls back to anonymous principals.

Registry Methods

MethodDescription
register_providerDecorator that instantiates and registers the sole auth provider. Raises RuntimeError if one is already registered.
resolve_principal(request)FastAPI dependency. Consults the registered provider and returns its Principal when non-None. Falls back to an IP-derived anonymous Principal when no provider is registered, the provider returns None, or it raises PrincipalResolutionError. Other exceptions propagate.
limit_hit_info(is_anonymous)Help text appended to a cutout-limit error. Returns the registered provider’s anon_limit_hit_info or authenticated_limit_hit_info, or "" when no provider is registered or that field is unset.

Request Flow (API)

At submission time the limiter reserves len(position) as a placeholder — the number of sky positions in the request, not the eventual per-file cutout count. This gives immediate feedback (a 429) before the worker runs, but the placeholder is replaced during reconcile (see below).

The 429 body is the CutoutLimitExceededError message, and the response includes a Retry-After header. That body gains a second line from the provider’s anon_limit_hit_info or authenticated_limit_hit_info, when the chosen string is non-empty. The help text is not stored on the job.

The Principal’s identity, cutout_limit, and window_seconds are snapshotted on the job at creation, since the worker has no HTTP request to re-resolve auth against:

Redis keyDescription
jobs:{job_id}:cutout_limit_identityIdentity string used for limiter bucketing
jobs:{job_id}:cutout_limit_maxSnapshotted cutout_limit (int); omitted when unlimited
jobs:{job_id}:cutout_limit_window_secondsSnapshotted window_seconds; omitted when the config default applies

Per-identity rolling-window state lives under {redis_prefix}:cutout_limit:{identity}:events (ZSET) and :counts (HASH). See CutoutLimitKeys in fornax_cutouts/jobs/redis.py.


Worker Reconcile Flow

Once schedule_job resolves the actual source files for each position, it reconciles the placeholder reservation to the true cutout count.

Key behaviors:


AbstractAuthProvider

Every provider must subclass AbstractAuthProvider and provide:

  1. A class-level name attribute, used as the registry key

  2. An implementation of async resolve(request) -> Principal | None

Optionally set class-level anon_limit_hit_info and authenticated_limit_hit_info strings. They are appended to anonymous and authenticated cutout-limit errors (the submit 429 body and, if reconcile fails the job, the UWS error summary). Each defaults to "", which adds nothing.

from starlette.requests import Request
from fornax_cutouts.auth import AbstractAuthProvider, Principal


class MyAuthProvider(AbstractAuthProvider):
    name = "my_auth"
    anon_limit_hit_info = "Send an Authorization: Bearer token to raise this limit."
    authenticated_limit_hit_info = "Retry after the window resets, or request a higher limit."

    async def resolve(self, request: Request) -> Principal | None: ...

Return None when the provider doesn’t recognize the request (e.g. no auth header present) — this lets the registry fall back to the anonymous default rather than treating the request as an error.

Raise PrincipalResolutionError when the request clearly targets your provider but resolution fails (e.g. identity backend unreachable). The registry logs a warning and falls back to the anonymous limit instead of returning HTTP 500. Any other exception propagates to FastAPI as an unhandled error.

Principal Fields

FieldTypeDescription
identitystrOpaque key the cutout limiter buckets requests by. Should not be reversible to raw PII.
is_anonymousboolWhether this identity is the IP-derived anonymous fallback. Defaults to False.
cutout_limitint | NoneMaximum cutouts per window for this identity. None means unlimited (no budget enforced).
window_secondsint | NoneOverrides CUTOUTS__CUTOUT_LIMIT__WINDOW_SECONDS for this identity. None uses the config default.
extrasdictFree-form metadata for your own use (e.g. plan tier). Not used by the framework.

A provider fully controls the budget through cutout_limit: return a higher number to raise it, or None to remove it entirely.


Next Steps

See Building an Auth Provider for a complete step-by-step tutorial.