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)