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 <noreply@anthropic.com> Signed-off-by: Paul Faure <paul@faure.ca> * 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WpoeSoKYEdgQcWjEzTVKpT Signed-off-by: Paul Faure <paul@faure.ca> * 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WpoeSoKYEdgQcWjEzTVKpT Signed-off-by: Paul Faure <paul@faure.ca> --------- Signed-off-by: Paul Faure <paul@faure.ca> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
d90dfa7085
commit
be21f76688
9 files changed
+122
-1
No files matched your search
@@ -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
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -24,6 +24,8 @@ func RegisteredEvents() []events.Unmarshaller {
|
||||
events.ItemPurged{},
|
||||
events.ItemRestored{},
|
||||
events.FileVersionRestored{},
|
||||
events.TagsAdded{},
|
||||
events.TagsRemoved{},
|
||||
events.SpaceCreated{},
|
||||
events.SpaceRenamed{},
|
||||
events.SpaceEnabled{},
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user