From be21f76688b1dcbf089f977746b3ea11e4e3d57c Mon Sep 17 00:00:00 2001 From: Paul Faure <31411417+paul43210@users.noreply.github.com> Date: Fri, 9 Oct 2026 03:54:17 -0400 Subject: [PATCH] feat(audit): record tag changes in the audit log (#13055) * feat(audit): record tag changes in the audit log Graph publishes TagsAdded and TagsRemoved on every tag assign and unassign, but only the search service listened, so a tag change left no trace in the audit log. The audit service now registers both events and logs them as file_tags_add and file_tags_remove, with the acting user (Executant), the space owner, the file id and path, and the comma-separated tags. Graph sets the Timestamp on both events, which it left unset. Two test cases use different owner and actor ids; the audit README documents the two actions. Fixes #13054 Co-authored-by: Claude Fable 5.1 Signed-off-by: Paul Faure * fix(audit): record only the tags that actually changed; skip no-op tag events Review follow-up for #13055: - graph: TagsAdded/TagsRemoved now carry the tags that were actually added or removed (requesting an existing tag, or removing an absent one, is not a change). Search only uses the event's Ref, so it is unaffected. - audit: skip tag events whose tag list is empty. Graph still publishes TagsRemoved for search when nothing was present; that is not an audit event. - audit: document why User is the executant for tag events; describe Tags as carried by the event; remove a stray copy-pasted doc comment. Co-authored-by: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WpoeSoKYEdgQcWjEzTVKpT Signed-off-by: Paul Faure * fix(audit): carry the requested tags in tag events, as before Per review: TagsAdded/TagsRemoved go back to carrying the tags as requested, and the audit log records every request, including tags that were already present (or absent, for a removal), so repeated requests stay visible. Computing the actual delta belongs in the tags package, not here. Removes the graph-side tagsDiff helper and its test, and the audit service's skip for empty tag lists. The other review fixes stay. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WpoeSoKYEdgQcWjEzTVKpT Signed-off-by: Paul Faure --------- Signed-off-by: Paul Faure Co-authored-by: Claude Fable 5.1 --- .../enhancement-audit-tag-changes.md | 12 +++++ services/audit/README.md | 2 + services/audit/pkg/service/service.go | 4 ++ services/audit/pkg/service/service_test.go | 46 +++++++++++++++++++ services/audit/pkg/types/conversion.go | 24 ++++++++++ services/audit/pkg/types/events.go | 2 + services/audit/pkg/types/messages.go | 14 +++++- services/audit/pkg/types/types.go | 14 ++++++ services/graph/pkg/service/v0/tags.go | 5 ++ 9 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/enhancement-audit-tag-changes.md diff --git a/changelog/unreleased/enhancement-audit-tag-changes.md b/changelog/unreleased/enhancement-audit-tag-changes.md new file mode 100644 index 00000000000..9495755b550 --- /dev/null +++ b/changelog/unreleased/enhancement-audit-tag-changes.md @@ -0,0 +1,12 @@ +Enhancement: Record tag changes in the audit log + +Adding or removing tags on a file or folder left no trace in the audit log. +The graph service already publishes `TagsAdded` and `TagsRemoved` events for +every change, but only the search service listened to them. + +The audit service now records both: `file_tags_add` and `file_tags_remove` +entries carry the acting user, the file id and path, the space owner and the +tags concerned. The graph service also sets the timestamp on the two events, +which it left empty before. + +https://github.com/owncloud/ocis/issues/13054 diff --git a/services/audit/README.md b/services/audit/README.md index 112a5d60ee8..7e172fc5239 100644 --- a/services/audit/README.md +++ b/services/audit/README.md @@ -2,6 +2,8 @@ The audit service logs all events of the system as an audit log. Per default, it will be logged to standard out, but can also be configured to a file output. Supported log formats are json or a minimal human-readable format. +Besides shares, links, files, spaces, users and groups, tag changes are recorded: adding tags to a file or folder is logged as `file_tags_add` and removing them as `file_tags_remove`, each with the acting user, the file id and the tags as requested. The request is recorded as made: a tag that was already present (or, for a removal, absent) is still listed, so repeated requests remain visible. + With audit logs, you are able to prove compliance with corporate guidelines as well as to enable reporting and auditing of operations. The audit service takes note of actions conducted by users and administrators. Example minimal format: diff --git a/services/audit/pkg/service/service.go b/services/audit/pkg/service/service.go index 0b7b74859fa..6678c2790dd 100644 --- a/services/audit/pkg/service/service.go +++ b/services/audit/pkg/service/service.go @@ -83,6 +83,10 @@ func StartAuditLogger(ctx context.Context, ch <-chan events.Event, log log.Logge auditEvent = types.ItemRestored(ev) case events.FileVersionRestored: auditEvent = types.FileVersionRestored(ev) + case events.TagsAdded: + auditEvent = types.TagsAdded(ev) + case events.TagsRemoved: + auditEvent = types.TagsRemoved(ev) case events.SpaceCreated: auditEvent = types.SpaceCreated(ev) case events.SpaceRenamed: diff --git a/services/audit/pkg/service/service_test.go b/services/audit/pkg/service/service_test.go index e244bf0cafa..93eefb6a2ad 100644 --- a/services/audit/pkg/service/service_test.go +++ b/services/audit/pkg/service/service_test.go @@ -587,6 +587,52 @@ var testCases = []struct { require.Equal(t, "http://ocis.test/invite", ev.InviteLink) }, }, + { + Alias: "Tags added", + SystemEvent: events.Event{ + Event: events.TagsAdded{ + Executant: userID("uid-123"), + SpaceOwner: userID("uid-owner"), + Ref: reference("pro-1", "sto-123", "iid-123", "./item"), + Tags: "holiday,family", + Timestamp: timestamp(10e8), + }, + }, + CheckAuditEvent: func(t *testing.T, b []byte) { + ev := types.AuditEventTagsAdded{} + require.NoError(t, json.Unmarshal(b, &ev)) + + // AuditEvent fields: User is the actor, Owner the space owner + checkBaseAuditEvent(t, ev.AuditEvent, "uid-123", "2001-09-09T01:46:40Z", "user 'uid-123' added tags 'holiday,family' to file 'pro-1$sto-123!iid-123/item'", "file_tags_add") + // AuditEventFiles fields + checkFilesAuditEvent(t, ev.AuditEventFiles, "pro-1$sto-123!iid-123/item", "uid-owner", "./item") + // AuditEventTagsAdded fields + require.Equal(t, "holiday,family", ev.Tags) + }, + }, + { + Alias: "Tags removed", + SystemEvent: events.Event{ + Event: events.TagsRemoved{ + Executant: userID("uid-123"), + SpaceOwner: userID("uid-owner"), + Ref: reference("pro-1", "sto-123", "iid-123", "./item"), + Tags: "holiday", + Timestamp: timestamp(10e8), + }, + }, + CheckAuditEvent: func(t *testing.T, b []byte) { + ev := types.AuditEventTagsRemoved{} + require.NoError(t, json.Unmarshal(b, &ev)) + + // AuditEvent fields: User is the actor, Owner the space owner + checkBaseAuditEvent(t, ev.AuditEvent, "uid-123", "2001-09-09T01:46:40Z", "user 'uid-123' removed tags 'holiday' from file 'pro-1$sto-123!iid-123/item'", "file_tags_remove") + // AuditEventFiles fields + checkFilesAuditEvent(t, ev.AuditEventFiles, "pro-1$sto-123!iid-123/item", "uid-owner", "./item") + // AuditEventTagsRemoved fields + require.Equal(t, "holiday", ev.Tags) + }, + }, } func TestAuditLogging(t *testing.T) { diff --git a/services/audit/pkg/types/conversion.go b/services/audit/pkg/types/conversion.go index e074628a8cf..fc3885bfdd3 100644 --- a/services/audit/pkg/types/conversion.go +++ b/services/audit/pkg/types/conversion.go @@ -331,6 +331,30 @@ func FileVersionRestored(ev events.FileVersionRestored) AuditEventFileVersionRes } } +// TagsAdded converts a TagsAdded event to an AuditEventTagsAdded. +// Unlike the other file events, User is the executant (who changed the tags), not the +// space owner: tags are usually changed by someone other than the owner, and the +// owner is still recorded in the Owner field. +func TagsAdded(ev events.TagsAdded) AuditEventTagsAdded { + iid, path, owner := extractFileDetails(ev.Ref, ev.SpaceOwner) + base := BasicAuditEvent(ev.Executant.GetOpaqueId(), formatTime(ev.Timestamp), MessageTagsAdded(ev.Executant.GetOpaqueId(), ev.Tags, iid), ActionTagsAdded) + return AuditEventTagsAdded{ + AuditEventFiles: FilesAuditEvent(base, iid, owner, path), + Tags: ev.Tags, + } +} + +// TagsRemoved converts a TagsRemoved event to an AuditEventTagsRemoved. +// User is the executant, as in TagsAdded. +func TagsRemoved(ev events.TagsRemoved) AuditEventTagsRemoved { + iid, path, owner := extractFileDetails(ev.Ref, ev.SpaceOwner) + base := BasicAuditEvent(ev.Executant.GetOpaqueId(), formatTime(ev.Timestamp), MessageTagsRemoved(ev.Executant.GetOpaqueId(), ev.Tags, iid), ActionTagsRemoved) + return AuditEventTagsRemoved{ + AuditEventFiles: FilesAuditEvent(base, iid, owner, path), + Tags: ev.Tags, + } +} + // SpacesAuditEvent creates an AuditEventSpaces from the given values func SpacesAuditEvent(base AuditEvent, spaceID string) AuditEventSpaces { return AuditEventSpaces{ diff --git a/services/audit/pkg/types/events.go b/services/audit/pkg/types/events.go index 1cf10307b2c..f43b86545aa 100644 --- a/services/audit/pkg/types/events.go +++ b/services/audit/pkg/types/events.go @@ -24,6 +24,8 @@ func RegisteredEvents() []events.Unmarshaller { events.ItemPurged{}, events.ItemRestored{}, events.FileVersionRestored{}, + events.TagsAdded{}, + events.TagsRemoved{}, events.SpaceCreated{}, events.SpaceRenamed{}, events.SpaceEnabled{}, diff --git a/services/audit/pkg/types/messages.go b/services/audit/pkg/types/messages.go index b2604632c77..52358a9ed26 100644 --- a/services/audit/pkg/types/messages.go +++ b/services/audit/pkg/types/messages.go @@ -32,6 +32,8 @@ const ( ActionFilePurged = "file_trash_delete" ActionFileRestored = "file_trash_restore" ActionFileVersionRestored = "file_version_restore" + ActionTagsAdded = "file_tags_add" + ActionTagsRemoved = "file_tags_remove" // Spaces ActionSpaceCreated = "space_created" @@ -124,7 +126,17 @@ func MessageFileRead(executant, item string) string { return fmt.Sprintf("user '%s' read file '%s'", executant, item) } -// MessageFileTrashed returns the human-readable string that describes the action +// MessageTagsAdded returns a human readable string that describes the action +func MessageTagsAdded(executant, tags, item string) string { + return fmt.Sprintf("user '%s' added tags '%s' to file '%s'", executant, tags, item) +} + +// MessageTagsRemoved returns a human readable string that describes the action +func MessageTagsRemoved(executant, tags, item string) string { + return fmt.Sprintf("user '%s' removed tags '%s' from file '%s'", executant, tags, item) +} + +// MessageFileTrashed returns a human readable string that describes the action func MessageFileTrashed(executant, item string) string { return fmt.Sprintf("user '%s' trashed file '%s'", executant, item) } diff --git a/services/audit/pkg/types/types.go b/services/audit/pkg/types/types.go index 28e21e3d1f7..660678a7f48 100644 --- a/services/audit/pkg/types/types.go +++ b/services/audit/pkg/types/types.go @@ -149,6 +149,20 @@ type AuditEventFileRestored struct { OldPath string } +// AuditEventTagsAdded is the event logged when tags are added to a file or folder +type AuditEventTagsAdded struct { + AuditEventFiles + + Tags string // the tags the request asked to add, as carried by the event (ev.Tags); some may already have been present +} + +// AuditEventTagsRemoved is the event logged when tags are removed from a file or folder +type AuditEventTagsRemoved struct { + AuditEventFiles + + Tags string // the tags the request asked to remove, as carried by the event (ev.Tags); some may not have been present +} + // AuditEventFileVersionRestored is the event logged when a file version is restored type AuditEventFileVersionRestored struct { AuditEventFiles diff --git a/services/graph/pkg/service/v0/tags.go b/services/graph/pkg/service/v0/tags.go index 97b7491b826..b3df717eb31 100644 --- a/services/graph/pkg/service/v0/tags.go +++ b/services/graph/pkg/service/v0/tags.go @@ -16,6 +16,7 @@ import ( "github.com/owncloud/reva/v2/pkg/events" "github.com/owncloud/reva/v2/pkg/storagespace" "github.com/owncloud/reva/v2/pkg/tags" + "github.com/owncloud/reva/v2/pkg/utils" "go-micro.dev/v4/metadata" ) @@ -141,6 +142,7 @@ func (g Graph) AssignTags(w http.ResponseWriter, r *http.Request) { if g.eventsPublisher != nil { ev := events.TagsAdded{ + // The tags as requested, including any that were already present. Tags: strings.Join(assignment.Tags, ","), Ref: &provider.Reference{ ResourceId: &rid, @@ -148,6 +150,7 @@ func (g Graph) AssignTags(w http.ResponseWriter, r *http.Request) { }, SpaceOwner: sres.Info.Owner, Executant: revaCtx.ContextMustGetUser(r.Context()).Id, + Timestamp: utils.TSNow(), } if err := events.Publish(r.Context(), g.eventsPublisher, ev); err != nil { g.logger.Error().Err(err).Msg("Failed to publish TagsAdded event") @@ -239,6 +242,7 @@ func (g Graph) UnassignTags(w http.ResponseWriter, r *http.Request) { // Always publish the event so the search index gets updated, // even if the tag was already absent from file metadata. ev := events.TagsRemoved{ + // The tags as requested, including any that were not present. Tags: strings.Join(unassignment.Tags, ","), Ref: &provider.Reference{ ResourceId: &rid, @@ -246,6 +250,7 @@ func (g Graph) UnassignTags(w http.ResponseWriter, r *http.Request) { }, SpaceOwner: sres.Info.Owner, Executant: revaCtx.ContextMustGetUser(ctx).Id, + Timestamp: utils.TSNow(), } if g.publishTagsRemoved(ctx, client, ev, tagsChanged, currentTags) != nil { w.WriteHeader(http.StatusInternalServerError)