Skip to content

feat: new audit log - #3864

Open
micbar wants to merge 7 commits into
mainfrom
auditing-ng
Open

micbar wants to merge 7 commits into
mainfrom
auditing-ng

Conversation

@micbar

@micbar micbar commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Description

First part of the audit log from ADR 0006. The services emit audit events into the JetStream stream audit, the audit service writes them as JSON lines to stdout and, if configured, to an OpenTelemetry Collector.

Off by default. Enable it with OC_AUDIT_ENABLED=true and OC_ADD_RUN_SERVICES=audit. The old audit log stays available with AUDIT_LEGACY_ENABLED.

Needs opencloud-eu/reva#956. The reva bump is not in the PR yet, so the build fails until it is merged.

flowchart LR
    client([client]) --> proxy
    proxy -- X-Audit-Context --> services[graph, frontend,<br/>sharing, storage-users]
    proxy -- events --> stream[(JetStream<br/>audit)]
    services -- events --> stream
    stream --> auditsvc[audit service]
    auditsvc --> stdout[stdout]
    auditsvc --> otlp[OTLP Collector]
Loading

Implementation

One commit per part, best reviewed commit by commit.

  1. feat(audit): write audit records from the audit stream
    • the audit service acks a record only when every output has it, a failing output is retried on its own, order is kept
    • OTLP output: endpoint checked at startup, the resource names the emitting service
    • opencloud_audit_consumer_lag also counts records waiting for a failed output
  2. read the audit request context in the HTTP services
    • pkg/middleware.AuditContext puts X-Audit-Context into the request context, so it reaches reva
  3. feat(proxy): logins, public link access and backchannel logouts
    • builds the audit context: client address, user agent, auth method, session, OIDC client
    • drops an X-Audit-Context sent by a client
    • one login per OIDC session when the token has a sid
  4. feat(graph): user, group and role changes
    • also group members, denied space creation and personal data exports
  5. pass the audit settings to the reva services
    • frontend, sharing and storage-users, the file, share and space events come from reva
  6. docs(adr): the decisions from the first implementation, see below
  7. refactor(audit): build the audit emitter in one place
    • pkg/auditemitter for graph and proxy, from the review

Not in this part: syslog output, the policy, pseudonymization and the events the README lists as not emitted yet.

ADR 0006

The first implementation added these decisions:

  • target is the object before the action, new values go into change.after
  • target.path is the path in the space, also through a share or a link
  • whatever is done through a public link has the link as actor
  • uploads and downloads are recorded when the transfer is done, only for requests a client sent
  • one login per OIDC session and client, not per access token
  • public_link.access once per link and client within 30 minutes, every failed attempt
  • an output that fails holds back the records behind it, so none is lost or written twice
  • best_effort is the only mode, strict comes back when it can refuse a request before it acts
  • actor.impersonated_by becomes an ID and a name, not filled yet

What changes for developers

  • a new event starts in the catalog in reva pkg/audit
  • a Go service that emits audit events builds its emitter with auditemitter.New, see graph and proxy
  • a reva based service gets audit_enabled, audit_mode, audit_service and audit_service_version in its revaconfig, see sharing and storage-users
  • an HTTP service behind the proxy reads the audit context with middleware.AuditContext. Only the proxy sets it
  • graph: domain events become audit events in auditEvents (services/graph/pkg/service/v0/audit.go). Routes without a domain event use auditOnError, auditOnDenied or auditAlways
  • a new output of the audit service implements writer.Sink. Return the error, the writer retries the record on this output only
  • the audit service README is the reference for fields, catalog and delivery

Related Issue

Motivation and Context

The old audit log is built from the domain events. It only sees successful actions, has no client address and its records have no stable format for a SIEM.

How Has This Been Tested?

  • test environment: opencloud server with OC_AUDIT_ENABLED=true, the audit service in its own process, built-in IdP
  • test case 1: unit tests in the audit service, proxy, graph, pkg/auditemitter and reva pkg/audit
  • test case 2: web client: uploads, shares, links, spaces, trash bin, versions, upload through a public link in a private window
  • test case 3: curl: TUS uploads, shares and space members with roles, links, actions through links and as share recipient, failed logins
  • test case 4: OIDC logins and backchannel logouts with tokens signed by the built-in IdP key, not yet with a real Keycloak
  • test case 5: OTLP output to a local Collector, also with the Collector stopped for a minute: no record lost, none written twice

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation added

@codacy-production

codacy-production Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 335 complexity

Metric Results
Complexity 335

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Comment thread services/audit/README.md Resolved
micbar added a commit to maki5/opencloud that referenced this pull request Oct 30, 2026
  chi's ClientIPFrom* header middlewares trust the header from anyone who can
  reach us. Pick the middleware per request instead: headers are only read
  when the peer is a trusted proxy, everyone else gets their network address.

  - PROXY_TRUSTED_PROXIES, defaults to loopback and private ranges
  - PROXY_CLIENT_IP_HEADER, defaults to X-Forwarded-For, e.g. CF-Connecting-IP
  - drop the strategy setting and xff_trusted_hops
  - pass the client address to the services in X-Forwarded-For instead of
    X-Client-Ip, reva reads it e.g. for OCM
  - same check in collaboration, traefik routes to it directly
  - OC_TRUSTED_PROXIES and OC_CLIENT_IP_HEADER set both services
  - the other services only drop RealIP, none of them reads the address

  Taken from opencloud-eu#3864 and opencloud-eu#3464 (comment)
micbar added a commit to maki5/opencloud that referenced this pull request Oct 30, 2026
  chi's ClientIPFrom* header middlewares trust the header from anyone who can
  reach us. Pick the middleware per request instead: headers are only read
  when the peer is a trusted proxy, everyone else gets their network address.

  - PROXY_TRUSTED_PROXIES, defaults to loopback and private ranges
  - PROXY_CLIENT_IP_HEADER, defaults to X-Forwarded-For, e.g. CF-Connecting-IP
  - drop the strategy setting and xff_trusted_hops
  - pass the client address to the services in X-Forwarded-For instead of
    X-Client-Ip, reva reads it e.g. for OCM
  - same check in collaboration, traefik routes to it directly
  - OC_TRUSTED_PROXIES and OC_CLIENT_IP_HEADER set both services
  - the other services only drop RealIP, none of them reads the address

  Taken from opencloud-eu#3864 and opencloud-eu#3464 (comment)

# Conflicts:
#	services/proxy/README.md
@micbar

micbar commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

@butonic The RealIP changes are now in the PR from @maki5 so this has become much smaller.

micbar added a commit that referenced this pull request Oct 30, 2026
  chi's ClientIPFrom* header middlewares trust the header from anyone who can
  reach us. Pick the middleware per request instead: headers are only read
  when the peer is a trusted proxy, everyone else gets their network address.

  - PROXY_TRUSTED_PROXIES, defaults to loopback and private ranges
  - PROXY_CLIENT_IP_HEADER, defaults to X-Forwarded-For, e.g. CF-Connecting-IP
  - drop the strategy setting and xff_trusted_hops
  - pass the client address to the services in X-Forwarded-For instead of
    X-Client-Ip, reva reads it e.g. for OCM
  - same check in collaboration, traefik routes to it directly
  - OC_TRUSTED_PROXIES and OC_CLIENT_IP_HEADER set both services
  - the other services only drop RealIP, none of them reads the address

  Taken from #3864 and #3464 (comment)

# Conflicts:
#	services/proxy/README.md
@micbar
micbar force-pushed the auditing-ng branch 3 times, most recently from 9da196a to a72ba7b Compare November 3, 2026 10:51
@micbar

micbar commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

rebased, the realIP middleware changes have been merged to main

@micbar
micbar force-pushed the auditing-ng branch 2 times, most recently from 422fe98 to fdd3127 Compare November 6, 2026 14:08
@micbar micbar changed the title Auditing ng - First Part feat: new audit log Nov 19, 2026
@pbleser-oc

Copy link
Copy Markdown
Member

Hi Michael, I reviewed the reva part in opencloud-eu/reva#956 and looked at how the services here use it. I know this one is still a draft, so take these as early notes, mostly about how a service gets its emitter:

1. Creating an emitter takes a lot of code in each service

graph and proxy each build the emitter by hand, with the same ~20 lines: the NATS connection name from generators.GenerateConnectionName plus -audit, a raw.Config copied field by field from the service's Events config, the service name, version.GetString() and the mode. frontend, sharing and storage-users pass the same values as reva config maps instead, with 12 keys for the archiver.

The Audit config struct (OC_AUDIT_ENABLED and OC_AUDIT_MODE with the service's own override) and its best_effort default are also repeated in all five services.

Keeping the per-service env var overrides is in line with how the other settings work, but the struct, its defaults and building the emitter could live in one place, e.g. a shared Audit config type and a helper in pkg/ that takes the service name, the Audit and the Events config and returns the emitter. It would build the connection name and the NATS config, add the version, validate the mode, and return audit.Discard{} when audit is off. A service would then only need something like:

emitter, err := audit.NewEmitter(ctx, cfg.Service.Name, cfg.Audit, cfg.Events, logger)
if err != nil {
	return err
}
defer emitter.Close()

Adding audit events to another service later would then be a few lines instead of copying the setup again.

2. Audit without an events endpoint is silently off in graph and proxy

Both only create the emitter when cfg.Audit.Enabled && cfg.Events.Endpoint != "", so with audit enabled and no endpoint they run without auditing, while reva's EmitterFromConfig and the data provider return an error in that case. Since this is an audit log, failing at startup seems like the safer choice, and the helper from point 1 would do the same in every service.

3. The mode isn't validated

An unknown OC_AUDIT_MODE, e.g. a typo, isn't rejected anywhere. In reva it currently makes the emitter drop every record, see point 3 of my review on opencloud-eu/reva#956. Validating it once, in the config parsing or in the helper, would catch that at startup.

4. defer emitter.Close() in graph and proxy

With the current reva code, a request that still emits while the service shuts down panics after Close, see point 5 of my review on opencloud-eu/reva#956. That's a fix in reva, I'm only mentioning it here because these two services are the ones calling Close.

I only looked at the parts that touch the reva API, not the whole PR. One thing I checked and liked: the proxy drops an X-Audit-Context sent by a client, so the services can trust it.

The audit service reads the JetStream stream `audit` and writes every record as one line of JSON to stdout and, if configured, to an OTLP endpoint, with the emitting service in the resource. A record is acknowledged only when all outputs have it, so an output outage keeps the records in the stream, in order and without duplicates. The old audit log stays available with AUDIT_LEGACY_ENABLED.

Adds the OpenTelemetry log packages for the OTLP output.
The HTTP services put the X-Audit-Context header of the proxy into the request context, so it reaches reva with the next call.
The proxy builds the audit request context: client address (PROXY_CLIENT_IP_HEADER behind trusted proxies), user agent, auth method, session and OIDC client. A value a client sent in X-Audit-Context is dropped.

A login is recorded once per OIDC session and client, not per access token. Public link access is recorded with the client address, failed attempts every time.
Also records denied space creation and data exports of other users.
frontend, sharing and storage-users pass the audit settings and the service version to the reva audit emitters. `best_effort` is the only mode for now, any other value stops the service. The frontend keeps the events endpoint the archiver needs for its records.
target and path semantics, the public link as actor, roles and recipients in share records, one login per OIDC session, the link access dedupe, the output ordering and why strict comes later.
@micbar

micbar commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

@pbleser-oc Thanks! 2. to 4. are done with the fixes from the reva review: graph and proxy use EmitterFromConfig, a missing endpoint or an unknown mode stops the service, and Close is safe now.

  1. There is a helper now, pkg/auditemitter. It builds the connection name, the version and the NATS config, graph and proxy only map their events config. The Audit struct stays in every service because of the service specific env vars. frontend, sharing and storage-users pass the settings to reva, the emitter is built there.

@micbar
micbar requested a review from butonic November 24, 2026 16:07
@micbar
micbar marked this pull request as ready for review November 24, 2026 16:08
@pbleser-oc

Copy link
Copy Markdown
Member

Thanks Michael, pkg/auditemitter looks good to me, that's what I had in mind. Mapping the events config to raw.Config in graph and proxy is fine, since their Events structs are different types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants