# Audit The audit service writes the audit log of OpenCloud: who did what to which object, when, from where and with which outcome. It records successful actions as well as failed and denied ones, for example logins with a wrong password or a user trying to give themselves the admin role. The services emit audit events into the JetStream stream `audit`. The audit service reads that stream and writes every event as one line of JSON. Shipping the records to a SIEM or a log store is done by a log agent or the OpenTelemetry Collector, not by OpenCloud. The design is described in [ADR 0006](../../docs/adr/0006-audit-logging.md). ## Enabling the Audit Log Two things are needed: - Set `OC_AUDIT_ENABLED=true`. It makes the proxy, graph, frontend, sharing and storage-users services emit audit events, and switches the audit service from the [legacy audit log](#legacy-audit-log) to the audit stream. Each service also has its own variable, e.g. `PROXY_AUDIT_ENABLED`, and `AUDIT_STREAM_ENABLED` for the audit service. If you use those instead, set `AUDIT_STREAM_ENABLED=true` as well, otherwise the audit service keeps running the legacy audit log. - The audit service must run. It is not started automatically when running as single binary via `opencloud server` or as docker container. Add it with `OC_ADD_RUN_SERVICES=audit`. Events emitted while the audit service is not running are kept in the stream and written when it starts, see `AUDIT_STREAM_MAX_AGE`. ## Audit Records Every record is one line of JSON. Field names follow the OpenTelemetry semantic conventions where they exist. ```json {"event.id":"019a3c1e-7b2f-7c4e-9a10-5d2f0e6b8a41","event.name":"opencloud.auth.login","event.code":"A001F","schema_version":"1.0","time":"2026-10-28T09:14:03.512Z","outcome":"failure","outcome.reason":"invalid_credentials","severity":"warning","tenant.id":"","actor.type":"user","actor.name":"alice","auth.method":"basic","client.address":"203.0.113.7","user_agent.original":"Mozilla/5.0 ...","target.type":"user","target.id":"alice","target.name":"alice","service.name":"proxy","service.version":"5.1.0","ocsf.class_uid":3002,"ocsf.activity_id":1} ``` | Field | Content | |---|---| | `event.id` | UUIDv7, unique per event | | `event.name` | `opencloud..`, see the event catalog | | `event.code` | stable code per event and outcome: a letter for the category, a number and `S`, `F` or `D` for success, failure or denied | | `schema_version` | `1.0`, changes are additive only | | `time` | RFC 3339, UTC, milliseconds | | `outcome`, `outcome.reason` | `success`, `failure` or `denied`, with a machine readable reason on failures | | `severity` | `info`, `notice`, `warning` or `alert`; failures and denials are at least `warning` | | `tenant.id` | empty on single-tenant installations | | `actor.type`, `actor.id`, `actor.name` | type is `user`, `service`, `public_link`, `guest`, `anonymous`, `system` or `cli` | | `auth.method`, `auth.client_id`, `session.id` | `oidc`, `basic`, `app_token`, `public_link`, `guest_link` or `signed_url`; the OIDC client and session | | `client.address`, `user_agent.original` | address of the client, see [Client Address](#client-address) | | `target.type`, `target.id`, `target.space.id`, `target.name`, `target.path` | the object acted on, as it was before the action; files and folders are identified by their ID, which stays the same across renames, moves and the trash bin. `target.path` is the path in the space of `target.space.id`, also for actions through a share or a public link | | `change.before`, `change.after` | changed attributes of an update, never secrets | | `trace_id`, `span_id` | OpenTelemetry trace correlation | | `service.name`, `service.version`, `service.instance.id` | the service that emitted the event | | `ocsf.class_uid`, `ocsf.activity_id` | mapping to OCSF 1.9.0, for SIEMs that normalize to OCSF | Records contain no human readable message. Passwords, tokens and file contents are never written. Public links are identified by their share ID; when a link lookup fails there is no share ID, then a hash of the token is used. ## Event Catalog | Category | Object | Actions | |---|---|---| | Authentication | `auth` | login, backchannel_logout | | Credentials | `app_token` | create, delete | | | `guest_token` | create, renew | | | `user.password` | change, reset | | Files | `file` | upload, download, preview, open_in_app, rename, move, copy, delete, restore, purge, lock, unlock | | | `file.version` | restore, delete | | | `file.malware` | detected | | | `folder` | create, download, rename, move, copy, delete, restore, purge | | | `trash` | purge_all | | Sharing | `share` | create, update, delete, accept, decline | | | `public_link` | create, update, delete, access | | | `ocm.share` | create, update, delete | | | `ocm.invite` | create, accept | | Spaces | `space` | create, update, enable, disable, delete | | | `space.member` | add, update, remove | | | `space.quota` | change | | Identity | `user` | create, update, enable, disable, soft_delete, delete | | | `group` | create, update, delete | | | `group.member` | add, remove | | | `app_role` | assign, unassign | | Administration | `app_role.definition` | change | | | `sharing_role` | enable, disable | | | `settings` | change | | | `config` | change | | | `cli` | command | | | `personal_data` | export | | Security | `access` | denied | Shares, space members and public links carry what was granted in `change.after`, a deleted one in `change.before`: | Key | Content | |---|---| | `recipient_type`, `recipient` | `user` or `group` and its ID; on every share and space member record | | `role` | the role of the permissions, e.g. `viewer`, `editor`, `file-editor`, `spaceviewer`, `spaceeditor`, `manager`, `uploader`, `denied`; a link without permissions is `internal`. The unified roles are built from these roles. Permissions that match no role are `custom`, with the permission names in `permissions` | | `expiration` | RFC 3339, empty when it was removed | | `password` | public links: whether the link has a password, never the password | An update only lists what it changed, e.g. only `role`. `display_name`, `description`, `notify_uploads` and `notify_uploads_extra_recipients` are link updates; the latter only says whether there are extra recipients, not their addresses. `space.update` lists what changed in `change.after`: `name`, `description`, `alias`, and `image` and `readme` with the ID of the file. An empty value means the attribute was removed. The catalog is defined in reva's `pkg/audit`. Some of its events are not emitted yet: app and guest tokens, previews, open in app, copies, deleted versions, the malware finding, OCM, settings, configuration and CLI commands, app role and sharing role definitions, and `access.denied`. Some actions are left out on purpose: - Thumbnails are not audited. `file.preview` is opening a file in the preview app. - Uploads and downloads are recorded when the transfer is done. Uploads and downloads by OpenCloud itself, e.g. for a copy, thumbnails, search or an archive, are not recorded. - A download that is resumed or seeked makes many range requests, only the one starting at the first byte counts. - `auth.login` is recorded for a new OIDC session. When the access token has a session ID (`sid`), as with Keycloak, only the first token of a session and OIDC client is a login; renewed tokens are not. The proxy keeps the sessions it recorded for a day, in the database `audit-logins` of the store of the userinfo cache (`PROXY_OIDC_USERINFO_CACHE_STORE`). With the `memory` store every proxy replica records the login once, use `nats-js-kv` with more than one proxy. Without a session ID, e.g. with the built-in identity provider, every new access token is recorded as a login, also the ones a client gets by renewing its token. Basic auth and app tokens authenticate every single request, for them only failed logins are recorded. Expired OIDC tokens are part of the normal token renewal and are not recorded as failed logins. - A `PATCH` of a group that renames it and then fails on the members records a successful `group.update` for the rename and a failed one for the request: the rename did happen. - `public_link.access` is recorded once per link and client within 30 minutes, every failed attempt is recorded. The web client asking whether a link needs a password is not an attempt. - Shares accepted automatically, see `SHARING_AUTO_ACCEPT_SHARES`, are not recorded as `share.accept`. The `share.create` of the sharer is the record. ## Outputs The audit service writes to stdout, one record per line and nothing else, see `AUDIT_OUTPUT_STDOUT`. Any log agent can pick it up. When the audit service runs with other services in one process or container, their logs share the same stdout; run it in its own container to keep the audit log separate. With `AUDIT_OUTPUT_OTLP_ENDPOINT` the audit service also sends the records to an OpenTelemetry Collector as OTLP logs over gRPC. The endpoint is `host:port` without a scheme, the audit service does not start with an invalid value. Use `AUDIT_OUTPUT_OTLP_INSECURE` for a Collector without TLS. Every record is an OpenTelemetry event: the event name is `event.name`, the fields are attributes and the body is the record as JSON. Both on purpose: Collector processors filter and route on the attributes, exporters like syslog or Splunk HEC send the body. A record is about 1 KB, sending it twice costs little. The resource names the service that emitted the event, `service.name` and `service.version` are therefore no attributes; the instrumentation scope is the audit service. The Collector routes the records to the SIEM, e.g. with its Splunk HEC, Elasticsearch, OpenSearch or syslog exporters. Every record is sent on its own and acknowledged only after the Collector accepted it, so a Collector outage delays the records but loses none. At least one output must be enabled. ## Delivery The services publish the events to the JetStream stream `audit`. The stream is a buffer, not the archive: it keeps the events for `AUDIT_STREAM_MAX_AGE` (default 7 days). Use `AUDIT_STREAM_REPLICAS=3` in a clustered NATS. The audit service acknowledges an event only after all outputs wrote it. If an output fails, the audit service keeps the event and writes it to that output again every 5 seconds, the other outputs get it once. The next events wait, so the records stay in order. If the audit service stops before an event is written everywhere, the event is delivered again; a record can therefore appear twice after a restart, with the same `event.id`. `OC_AUDIT_MODE` decides what a service does when it can't store an event. `best_effort`, the default and for now the only mode, publishes in the background and never slows down a request. Events that can't be stored are dropped and counted in `opencloud_audit_events_dropped_total`. A service with any other mode doesn't start. ## Client Address The proxy determines the client address. It only uses the `X-Forwarded-For` header, or the header set in `PROXY_CLIENT_IP_HEADER`, when the request comes from a trusted reverse proxy, see `PROXY_TRUSTED_PROXIES`. The default trusts the loopback and the private network ranges. Check the setting if your reverse proxy is not in a private network: - Your reverse proxy reaches OpenCloud from a public address, e.g. Cloudflare, a load balancer of a cloud provider or a proxy on another server. Its headers are ignored with the default, every request then has the address of the proxy and a SIEM sees all logins coming from one address. Add the addresses of your proxy to `PROXY_TRUSTED_PROXIES`, for Cloudflare its published IP ranges. - Clients can reach OpenCloud from a private network without passing your reverse proxy. With the default they can set their address themselves. Set `PROXY_TRUSTED_PROXIES` to the addresses of your reverse proxies only. `PROXY_TRUSTED_PROXIES` replaces the default, list the loopback and private ranges again if you still need them. See [Trusted Reverse Proxies](../proxy/README.md#trusted-reverse-proxies) in the proxy service. The proxy passes the address and the other request details to the services in the `X-Audit-Context` header and drops any value a client sent. The services trust this header like the other internal headers, so the services must not be reachable directly from outside. ## Metrics | Metric | Content | |---|---| | `opencloud_audit_events_emitted_total` | events handed to the emitter, by service, event and outcome, including the ones dropped later | | `opencloud_audit_events_dropped_total` | events not stored, by service, event and reason | | `opencloud_audit_publish_duration_seconds` | time to store an event in the stream | | `opencloud_audit_records_total` | records handled by the audit service, by result | | `opencloud_audit_sink_errors_total` | failed writes to an output | | `opencloud_audit_consumer_lag` | events in the stream the audit service has not written yet, also the ones waiting for a failed output | The events stored in the stream are `emitted_total - dropped_total{reason=~"closed|buffer_full|publish"}`. There is no label per tenant, the number of tenants has no upper bound. Every record has `tenant.id` and the stream subject contains the tenant, so count per tenant there. Alert on dropped events and on a growing consumer lag. ## Legacy Audit Log As long as `OC_AUDIT_ENABLED` is not `true`, the audit service runs the audit log of earlier versions, so an existing audit log keeps working after an upgrade. It is deprecated and will be removed in a later version. It reads the domain events instead of the audit stream and uses the `AUDIT_LOG_TO_CONSOLE`, `AUDIT_LOG_TO_FILE`, `AUDIT_FILEPATH` and `AUDIT_FORMAT` settings. Its records have a different format: ```json {"RemoteAddr":"","User":"user_id","URL":"","Method":"","UserAgent":"","Time":"","App":"admin_audit","Message":"user 'user_id' trashed file 'item_id'","Action":"file_delete","CLI":false,"Level":1,"Path":"path","Owner":"user_id","FileID":"item_id"} ``` To move to the audit stream, set `OC_AUDIT_ENABLED=true` for all services. The records then have the format described above, and there is no output to a file: point a log agent at stdout or set `AUDIT_OUTPUT_OTLP_ENDPOINT` before you switch. With `OC_AUDIT_ENABLED=true` the audit service ignores `AUDIT_LOG_TO_FILE` and `AUDIT_FILEPATH` and logs an error at startup while they are set. The release that removes the legacy audit log will refuse to start the audit service without `OC_AUDIT_ENABLED=true`, so an audit log never stops without notice.