Repository navigation
Conversation
1bd747f to
e944f14
Compare
|
I restructured my commits into logical independent chunks. NOTE: The audit context is created in the proxy in opencloud, so there is an important part outside the scope of this PR. From my POV, we should merge the reva part first. I added the audit pkg to reva because adding it to opencloud would create a circular go mod import pattern. |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 503 |
| Duplication | 10 |
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.
pbleser-oc
left a comment
There was a problem hiding this comment.
Thanks Michael, splitting it into these six commits made it much easier to follow. I read it commit by commit, built it and ran the tests of the touched packages locally (with -race), and they pass. Thanks also for the note on the proxy part: I checked that the proxy in opencloud-eu/opencloud#3864 drops an X-Audit-Context sent by a client, so the services can trust it.
A few points, the first three look like bugs to me:
1. Hiding an accepted share records a share.accept (audit_share.go, UpdateReceivedShareResponse)
The event is picked from the state in the response. Hiding or unhiding an accepted share, or renaming its mount point, sends an UpdateReceivedShare with the mask hidden or mount_point, and the response still has the state ACCEPTED, so each of them is recorded as an accept. Maybe check that the update mask contains state first?
2. The dedupe of failed link accesses can grow without bound (audit_link.go, firstLinkAccess / onceWithin)
Failed accesses are keyed by the trace ID, so every attempt is a new key, and it is kept for the full 30 minutes. Someone trying passwords on a link (no login needed) adds one entry per attempt. Above 10,000 entries, every new one also scans the whole map under the mutex, and removes only the expired ones, so the map keeps growing and each request gets slower. Since the failure keys are only there to merge the proxy's and ocdav's check of the same request, a window of a few seconds for them, or a bounded LRU, would be enough.
3. An unknown audit_mode drops every record (emitter.go, NewEmitter)
The queue is only created for best_effort. With any other value than strict, e.g. a typo, Emit sends to a nil channel, the select falls through to default, and every record is counted as buffer_full and dropped, silently apart from the log lines. Could NewEmitter (or EmitterFromConfig) reject an unknown mode with an error, like a missing endpoint?
4. What should strict do in the middleware? (question)
In the events middleware, the data provider and the archiver, the event is emitted after the call, and an error is only logged ("failing it now would hide that it happened"), which makes sense. But then strict doesn't refuse anything there, it only makes the call wait for the JetStream ack, up to the 5 s timeout when NATS is slow. Is that what ADR 0006 means by strict, or should these call sites always publish asynchronously? Either way, a sentence on it in the doc comment of ModeStrict would help.
5. Nothing closes the emitter
Close flushes the best_effort buffer, but nothing calls it yet, so up to 1000 records are lost on a shutdown. When something does call it, a request still running at that moment panics in Emit, because a send on a closed channel panics, also inside a select with default. Guarding the send (a closed flag under an RWMutex, or a done channel) would make Close safe to call.
6. Three config shapes for the same emitter
The middleware reads audit_* keys plus the events keys, the data provider audit_* plus the nats_* keys, and the archiver an audit block with EmitterConfig. EmitterFromConfig is only used by the archiver. Using EmitterConfig and EmitterFromConfig everywhere would make them the one way to build an emitter from a config, with the validation (endpoint, and the mode from point 3) in one place. I'd understand if you'd rather keep the existing nats_* keys of the data provider, though.
7. Suggestion: pass the emitter to the data transfer handlers instead of the context
This one is a suggested improvement, not something that has to change. The data provider puts the emitter into the request context, and the download handler and the tus hook take it out with audit.ContextGetEmitter. When it isn't there, they get Discard{}, so if a future handler or a refactoring loses it on the way, the records silently stop, and neither the compiler nor a test would notice.
The events publisher already takes the route the emitter would need: dataprovider.New passes it to datatxregistry.NewFuncs[t], and the simple, spaces and tus managers keep it in their structs. Passing the emitter next to it would touch the NewFunc signature, the three New functions and their structs, and one parameter of download.GetOrHeadFile (two callers), all within pkg/rhttp/datatx. A missing emitter would then be a compile error, and the tus hook would no longer depend on ev.Context still holding the values of the last request.
Nitpicks:
storagespace.ContextSetItemTypeisn't used anywhere, andItemTypeSlotnow carries the ID, the name and the path as well, soItemSlot/ContextRegisterItemSlotmight read better.- In
Decomposedfs.Move,Deleteand the others, the item is set before the permission check, so a denied call has the path of the node in the record. I think that's what we want for denied calls, just mentioning it so it's a conscious choice.
Apart from these, it reads very well. The comments on why each piece is there are really helpful.
|
One more thing I forgot in my review, about the metrics in
And a question: would a count per tenant be useful for operators? As a label on |
|
@pbleser-oc Thanks for the thorough review! All points are in, one commit each, so you only need to look at the changes. I will squash them into the six commits before merging.
Nits: renamed to |
|
@pbleser-oc Both done in one commit.
No tenant label, I agree with you. The number of tenants has no upper bound, and every record has |
The record schema of ADR 0006, the event catalog with its codes and an emitter that writes the records into the JetStream stream `audit`. It publishes in the background and never blocks a call, a record that can't be stored is dropped and counted by event and reason. `best_effort` is the only mode for now. Nothing uses it yet.
The proxy sends client address, user agent, auth method, session and OIDC client in `X-Audit-Context`. A gRPC interceptor and the http auth interceptor put it into the context, ocdav passes it on to the data provider. Without the header nothing changes.
decomposedfs and posix report type, node ID, name and the path in the space through a slot in the context. The slot only exists with audit on, without it the drivers look nothing up. Trashed nodes get their type when they are read, Type() can't read it at their trash path.
Files, folders, trash bin, versions, locks, shares, space members, public links and spaces, also failed and denied calls. One file per area. Shares, members and links carry recipient, role, expiration and whether a password is set, never the password. The share providers add the permissions of a removed share to the response, for its role in the record. The user share provider adds the state before an update, so hiding or renaming a received share isn't recorded as an accept.
The data provider reports PUT and TUS uploads and downloads a client sent, marked by the audit header, with the path in the owner's space. A TUS upload keeps the request context and the public link of the request that created it in its session, the data requests carry only a transfer token.
One download record per file or folder in the archive.
|
Thanks Michael, I looked at the changes and they all look good to me. For the flush of the events middleware's emitter on shutdown, I opened #1070 as a follow-up, so it doesn't hold up this PR. |
|
A note from trying to use this for something it doesn't cover yet: the audit entries for the versions the revision cleanup deletes (opencloud-eu/opencloud#4068). The cleanup deletes them inside the storage driver, in a background worker, after a change on disk, or from a CLI command, so the events middleware never sees it, and the driver doesn't get the audit settings. Details and a few questions in opencloud-eu/opencloud#4068 (comment): opencloud-eu/opencloud#4068 (comment) Nothing that needs to change in this PR, as far as I can tell, but it might affect how storage-users passes the audit settings on, so I thought you'd want to know before merging. AI assistance: Claude Code (claude-opus-5-5) wrote the first draft of this comment; I reviewed it. |
|
@pbleser-oc Thanks! Can you approve the PR then? I will take care of #1070 after the merge. |
pbleser-oc
left a comment
There was a problem hiding this comment.
Approving as discussed. The open questions on opencloud-eu/opencloud#4068 are about adding to this, not about changing it.
butonic
left a comment
There was a problem hiding this comment.
Read it commit by commit, on top of what @pbleser-oc already found. Three things look like bugs to me (archiver Stat when audit is off, archiver records with a guessed type, quota change hiding other space changes). The rest is not a blocker IMO.
The X-Audit-Context trust is fine, the proxy in opencloud-eu/opencloud#3864 drops what the client sends. AFAICT nothing sets ImpersonatedBy / imp yet though ... is that for a later part, or can it go?
One more, older than this PR so I can't comment inline: in pkg/rhttp/datatx/utils/download/download.go the parse ID error has no return. The 400 is written and we keep going with an empty ResourceId, so we write a second response, and with this PR the new audit code can emit a denied record for an empty target.
rid, err := storagespace.ParseID(spaceID)
if err != nil {
handleError(w, &sublog, err, "parse ID")
return
}|
@butonic Thanks for the review!
The |
New audit system
Reva part of the audit log from ADR 0006 in opencloud.
The services
auditPlease review commit by commit, each one builds and passes its tests.
1. Audit package
pkg/audit: record schema, event catalog, emitter inbest_effortorstrictmodepkg/audit/catalog.go. Codes never change, the schema only growstargetis the object before the action, new values go intochange.afteraudit.Discard{}or a recording emitter2. Request context between the services
flowchart LR client[Client] --> proxy["OpenCloud proxy<br/>sets X-Audit-Context"] proxy -- "HTTP header" --> http["ocdav and other<br/>HTTP services"] http -- "gRPC metadata" --> grpc["gateway, storage<br/>and share providers"] http -- "HTTP header,<br/>client transfers only" --> dp[data provider]X-Audit-Contextaudit.Header), requests without it come from OpenCloud itself3. Storage drivers report the item of a call
storagespace.ContextSetItem/ContextSetItemPathContextWantsItembefore an extra lookup4. Events middleware
fileEvent,shareEvent,linkEventorspaceEventtarget.pathis the path in the owner's space, also through shares and linksaudit_enabled,audit_mode,audit_service,audit_service_version. Audit on without an events endpoint is an error5. Uploads and downloads
audit.ContextGetEmitter(ctx), a no-op without audit6. Archive downloads
auditblockTested with unit tests and with opencloud-eu/opencloud#3864 against a local server.
Needed by opencloud-eu/opencloud#3864.