Automatic Agent mTLS
Agents get a client certificate from the platform CA by themselves and talk to a dedicated mTLS endpoint with it. The private key never leaves the host, certificates last 90 days and are renewed automatically, and an admin can reset a lost certificate from the Agents page.
How It Works
The platform runs its own CA (created on first use: ECDSA P-384, valid ten years, common name "AsPeekIt Agent CA"; a CA created before the product was renamed keeps its original name). Every agent generates its private key locally (ECDSA P-256), sends only a certificate signing request (CSR) and receives a client certificate signed by that CA. From then on it sends its data to the mTLS endpoint, where the reverse proxy accepts only certificates of the platform CA, and the backend checks that the certificate is the one bound to that agent. Nothing has to be copied to the hosts.
- First certificate: requested with POST /api/v1/agent/certificate/ on backend_url, authenticated with the agent API key (X-Agent-ID and X-API-Key headers), or obtained in the same request as the enrollment when the agent enrolls with a token (the csr field of POST /api/v1/agent/enroll/).
- The backend ignores the CSR subject and signs the certificate with CN=<agent_id>, for client authentication only, valid AGENT_CERT_VALIDITY_DAYS days (default 90). Accepted CSR keys: ECDSA P-256 or P-384, RSA of at least 2048 bits. Its SHA-256 fingerprint and expiry are stored on the agent (cert_fingerprint, cert_expires_at).
- The 201 response carries certificate, ca_certificate, expires_at and mtls_url (the mTLS endpoint, empty when the platform has none: the agent keeps using backend_url and still stores the certificate).
- An agent that authenticated with a zone key receives its own API key (api_key) together with its first certificate: from then on the zone key no longer authenticates that agent.
- Every issuance and renewal is written to the audit log as agent_cert.issued (fingerprint, expiry, renewal flag); a reset as agent_cert.reset.
Note: The certificate endpoint is rate limited per agent (THROTTLE_AGENT_CERTIFICATE_RATE, default 10/minute). A backend without the feature answers 404: the agent logs it once and keeps working without mTLS.
On the Host
The automatic mode is on by default (mtls.auto: true) and needs no configuration. It does not run with -output or -dry-run, and setting tls_cert_file / tls_key_file by hand turns it off.
- Files in mtls.dir (directory 0700, files 0600; on Windows the same ACL as the rest of the data directory): agent.key (private key, generated once), agent.crt, ca.crt, state.json (the mTLS endpoint) and api-key (the own API key received with the first certificate, which then overrides api_key of the configuration).
- With a valid certificate the agent sends to mtls_url and presents the certificate; an mtls_url that is not https:// is ignored with a warning and backend_url is used.
- Renewal: when less than 1/3 of the lifetime is left (for a 90-day certificate, the last 30 days), checked at start and every 6 hours. The renewal goes through the mTLS endpoint with the current certificate and the new one is hot-reloaded, without restarting the agent.
- Errors never stop collection or sending: they are logged with the [mtls] prefix and retried at the next check. The private key is never logged nor sent.
mtls:
auto: true # IDP_MTLS_AUTO (true/1/false/0)
dir: "/var/lib/idp-agent/mtls" # IDP_MTLS_DIR; Windows: %ProgramData%\idp-agent\mtlsThe mTLS Endpoint
In the production compose file the endpoint is a dedicated host name, AGENT_MTLS_HOSTNAME (for example idp-agents.example.com), served by a Traefik router that only exposes /api/v1/agent/ and requires a client certificate signed by the platform CA (RequireAndVerifyClientCert). Traefik forwards the verified certificate to the backend (X-Forwarded-Tls-Client-Cert) together with a proof header (X-Idp-Mtls-Proof) carrying AGENT_MTLS_PROXY_SECRET; every other router strips both headers, so they cannot be forged through the main host.
- The backend returns AGENT_MTLS_URL (https://<AGENT_MTLS_HOSTNAME> in production) to the agents as mtls_url; leave it empty to issue certificates without an mTLS endpoint.
- AGENT_CA_ENCRYPTION_KEY: when empty, a key derived from SECRET_KEY is used, so rotating SECRET_KEY would make the CA unusable (certificate requests then answer 503 "The platform CA is unavailable").
- The agents host must also be in DJANGO_ALLOWED_HOSTS (the production compose file adds it).
# .env.prod
AGENT_MTLS_HOSTNAME=idp-agents.example.com
TRAEFIK_CERT_DOMAIN=example.com # wildcard certificate covering the agents host
AGENT_MTLS_PROXY_SECRET=... # python3 -c "import secrets; print(secrets.token_urlsafe(48))"
AGENT_CA_ENCRYPTION_KEY=... # Fernet key that encrypts the CA private key at rest
AGENT_REQUIRE_MTLS=false # true once every agent has its certificate
# In the backend container: Traefik TLS options (file provider) trusting the platform CA
python manage.py agent_ca --traefik-options
# The CA certificate alone (PEM)
python manage.py agent_caRequiring the Certificate
With AGENT_REQUIRE_MTLS=true every agent request must present a verified certificate whose fingerprint matches the one bound to the agent; anything else gets 401. Turn it on only after every agent has its certificate (the Agents page shows it on each card).
- An agent with its own API key and no bound certificate (new, or reset by an admin) may still call /api/v1/agent/certificate/ without a certificate to get its first one. Zone keys never can: with mTLS required a new agent enrolls with an enrollment token, which issues the certificate in the same request.
- A certificate that has expired cannot be presented any more: the agent falls back to backend_url and the backend refuses a new certificate (403) until an admin resets it.
Certificate on the Agents Page
Each agent card on the Agents page (Discovery section) shows the certificate state: "No client certificate", "mTLS certificate until <date>" for a certificate issued by the platform, or "mTLS certificate (manual)" for a fingerprint set by hand. Admins see a Reset button next to it.

Resetting a Certificate
Use Reset (confirmation "Reset certificate") when the agent private key is lost or compromised, or when the certificate expired. It clears the bound fingerprint and expiry, so the current certificate stops being accepted, and the agent can request a new one. Only admins can reset (POST /api/v1/agent/agents/{id}/reset-certificate/).
Warning: A renewal must present the current certificate. If the agent logs "Certificate request refused (403)", the backend still has a certificate bound to it: an admin has to reset it before the agent can get a new one. With AGENT_REQUIRE_MTLS=true the agent also needs an enrollment token for the new certificate.
# On the host: get a new certificate (keep api-key: the zone key no longer works for this agent)
sudo systemctl stop idp-agent
sudo rm /var/lib/idp-agent/mtls/agent.crt # also agent.key to rotate the key
# ... an admin clicks Reset on the agent card ...
sudo systemctl start idp-agent