From 53dcd93818894afe146e1136380fada33c878312 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Mon, 19 Jun 2023 07:29:04 +0200 Subject: [PATCH 1/4] add storage id to audit log for spaces Signed-off-by: Christian Richter --- .../unreleased/add-storage-id-to-audit-log.md | 6 +++++ services/audit/pkg/types/helpers.go | 8 ++++++ .../pkg/types/{constants.go => messages.go} | 26 ++++++++++++------- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 changelog/unreleased/add-storage-id-to-audit-log.md create mode 100644 services/audit/pkg/types/helpers.go rename services/audit/pkg/types/{constants.go => messages.go} (89%) diff --git a/changelog/unreleased/add-storage-id-to-audit-log.md b/changelog/unreleased/add-storage-id-to-audit-log.md new file mode 100644 index 0000000000..dd9eb887a9 --- /dev/null +++ b/changelog/unreleased/add-storage-id-to-audit-log.md @@ -0,0 +1,6 @@ +Enhancement: We added the storage id to the audit log for spaces + +We added the storage id to the audit log for spaces + +https://github.com/owncloud/ocis/pull/6548 +https://github.com/owncloud/ocis/issues/3753 \ No newline at end of file diff --git a/services/audit/pkg/types/helpers.go b/services/audit/pkg/types/helpers.go new file mode 100644 index 0000000000..f1aad63836 --- /dev/null +++ b/services/audit/pkg/types/helpers.go @@ -0,0 +1,8 @@ +package types + +import "strings" + +func SplitId(id string) (string, string) { + ids := strings.Split(id, "$") + return ids[0], ids[1] +} diff --git a/services/audit/pkg/types/constants.go b/services/audit/pkg/types/messages.go similarity index 89% rename from services/audit/pkg/types/constants.go rename to services/audit/pkg/types/messages.go index e41f53ba6c..8ef8e3fe7f 100644 --- a/services/audit/pkg/types/constants.go +++ b/services/audit/pkg/types/messages.go @@ -139,43 +139,51 @@ func MessageFileVersionRestored(executant, item, version string) string { // MessageSpaceCreated returns the human readable string that describes the action func MessageSpaceCreated(executant, spaceID, name string) string { - return fmt.Sprintf("user '%s' created a space '%s' with name '%s'", executant, spaceID, name) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' created a space '%s' with name '%s' (storage: '%s')", executant, spaceID, name, storagId) } // MessageSpaceRenamed returns the human readable string that describes the action func MessageSpaceRenamed(executant, spaceID, name string) string { - return fmt.Sprintf("user '%s' renamed space '%s' to '%s'", executant, spaceID, name) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' renamed space '%s' to '%s' (storage: '%s')", executant, spaceID, name, storagId) } // MessageSpaceDisabled returns the human readable string that describes the action func MessageSpaceDisabled(executant, spaceID string) string { - return fmt.Sprintf("user '%s' disabled the space '%s'", executant, spaceID) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' disabled the space '%s' (storage: '%s')", executant, spaceID, storagId) } // MessageSpaceEnabled returns the human readable string that describes the action func MessageSpaceEnabled(executant, spaceID string) string { - return fmt.Sprintf("user '%s' (re-) enabled the space '%s'", executant, spaceID) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' (re-) enabled the space '%s' (storage: '%s')", executant, spaceID, storagId) } // MessageSpaceDeleted returns the human readable string that describes the action func MessageSpaceDeleted(executant, spaceID string) string { - return fmt.Sprintf("user '%s' deleted the space '%s'", executant, spaceID) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' deleted the space '%s' (storage: '%s')", executant, spaceID, storagId) } // MessageSpaceShared returns the human readable string that describes the action func MessageSpaceShared(executant, spaceID, grantee string) string { - return fmt.Sprintf("user '%s' shared the space '%s' with '%s'", executant, spaceID, grantee) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' shared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagId) } // MessageSpaceUnshared returns the human readable string that describes the action func MessageSpaceUnshared(executant, spaceID, grantee string) string { - return fmt.Sprintf("user '%s' unshared the space '%s' with '%s'", executant, spaceID, grantee) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' unshared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagId) } // MessageSpaceUpdated returns the human readable string that describes the action func MessageSpaceUpdated(executant, spaceID, name string, quota uint64, opaque map[string]string) string { - return fmt.Sprintf("user '%s' updated space '%s'. name: '%s', quota: '%d', opaque: '%s'", - executant, spaceID, name, quota, opaque) + storagId, spaceID := SplitId(spaceID) + return fmt.Sprintf("user '%s' updated space '%s'. name: '%s', quota: '%d', opaque: '%s' (storage: '%s')", + executant, spaceID, name, quota, opaque, storagId) } // MessageUserCreated returns the human readable string that describes the action From a3b8a7742a7a3375bd70005a35b8a316c6a8db6c Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Mon, 19 Jun 2023 07:45:04 +0200 Subject: [PATCH 2/4] fix tests Signed-off-by: Christian Richter --- services/audit/pkg/service/service_test.go | 30 +++++++++++----------- services/audit/pkg/types/helpers.go | 3 +++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/services/audit/pkg/service/service_test.go b/services/audit/pkg/service/service_test.go index d612783c11..2cc3585140 100644 --- a/services/audit/pkg/service/service_test.go +++ b/services/audit/pkg/service/service_test.go @@ -462,7 +462,7 @@ var testCases = []struct { SystemEvent: events.Event{ Event: events.SpaceCreated{ Executant: userID("uid-123"), - ID: &provider.StorageSpaceId{OpaqueId: "space-123"}, + ID: &provider.StorageSpaceId{OpaqueId: "storage-1$space-123"}, Owner: userID("uid-123"), Root: resourceID("pro-1", "sto-123", "iid-123"), Name: "test-space", @@ -476,9 +476,9 @@ var testCases = []struct { require.NoError(t, json.Unmarshal(b, &ev)) // AuditEvent fields - checkBaseAuditEvent(t, ev.AuditEvent, "", "2286-11-20T17:46:40Z", "user 'uid-123' created a space 'space-123' with name 'test-space'", "space_created") + checkBaseAuditEvent(t, ev.AuditEvent, "", "2286-11-20T17:46:40Z", "user 'uid-123' created a space 'space-123' with name 'test-space' (storage: 'storage-1')", "space_created") // AuditEventSpaces fields - checkSpacesAuditEvent(t, ev.AuditEventSpaces, "space-123") + checkSpacesAuditEvent(t, ev.AuditEventSpaces, "storage-1$space-123") // AuditEventFileRestored fields require.Equal(t, "uid-123", ev.Owner) require.Equal(t, "pro-1$sto-123!iid-123", ev.RootItem) @@ -490,7 +490,7 @@ var testCases = []struct { SystemEvent: events.Event{ Event: events.SpaceRenamed{ Executant: userID("uid-123"), - ID: &provider.StorageSpaceId{OpaqueId: "space-123"}, + ID: &provider.StorageSpaceId{OpaqueId: "storage-1$space-123"}, Owner: userID("uid-123"), Name: "new-name", }, @@ -500,9 +500,9 @@ var testCases = []struct { require.NoError(t, json.Unmarshal(b, &ev)) // AuditEvent fields - checkBaseAuditEvent(t, ev.AuditEvent, "", "", "user 'uid-123' renamed space 'space-123' to 'new-name'", "space_renamed") + checkBaseAuditEvent(t, ev.AuditEvent, "", "", "user 'uid-123' renamed space 'space-123' to 'new-name' (storage: 'storage-1')", "space_renamed") // AuditEventSpaces fields - checkSpacesAuditEvent(t, ev.AuditEventSpaces, "space-123") + checkSpacesAuditEvent(t, ev.AuditEventSpaces, "storage-1$space-123") // AuditEventSpaceRenamed fields require.Equal(t, "new-name", ev.NewName) }, @@ -511,7 +511,7 @@ var testCases = []struct { SystemEvent: events.Event{ Event: events.SpaceDisabled{ Executant: userID("uid-123"), - ID: &provider.StorageSpaceId{OpaqueId: "space-123"}, + ID: &provider.StorageSpaceId{OpaqueId: "storage-1$space-123"}, }, }, CheckAuditEvent: func(t *testing.T, b []byte) { @@ -519,16 +519,16 @@ var testCases = []struct { require.NoError(t, json.Unmarshal(b, &ev)) // AuditEvent fields - checkBaseAuditEvent(t, ev.AuditEvent, "", "0001-01-01T00:00:00Z", "user 'uid-123' disabled the space 'space-123'", "space_disabled") + checkBaseAuditEvent(t, ev.AuditEvent, "", "0001-01-01T00:00:00Z", "user 'uid-123' disabled the space 'space-123' (storage: 'storage-1')", "space_disabled") // AuditEventSpaces fields - checkSpacesAuditEvent(t, ev.AuditEventSpaces, "space-123") + checkSpacesAuditEvent(t, ev.AuditEventSpaces, "storage-1$space-123") }, }, { Alias: "Space enabled", SystemEvent: events.Event{ Event: events.SpaceEnabled{ Executant: userID("uid-123"), - ID: &provider.StorageSpaceId{OpaqueId: "space-123"}, + ID: &provider.StorageSpaceId{OpaqueId: "storage-1$space-123"}, }, }, CheckAuditEvent: func(t *testing.T, b []byte) { @@ -536,16 +536,16 @@ var testCases = []struct { require.NoError(t, json.Unmarshal(b, &ev)) // AuditEvent fields - checkBaseAuditEvent(t, ev.AuditEvent, "", "", "user 'uid-123' (re-) enabled the space 'space-123'", "space_enabled") + checkBaseAuditEvent(t, ev.AuditEvent, "", "", "user 'uid-123' (re-) enabled the space 'space-123' (storage: 'storage-1')", "space_enabled") // AuditEventSpaces fields - checkSpacesAuditEvent(t, ev.AuditEventSpaces, "space-123") + checkSpacesAuditEvent(t, ev.AuditEventSpaces, "storage-1$space-123") }, }, { Alias: "Space deleted", SystemEvent: events.Event{ Event: events.SpaceDeleted{ Executant: userID("uid-123"), - ID: &provider.StorageSpaceId{OpaqueId: "space-123"}, + ID: &provider.StorageSpaceId{OpaqueId: "storage-1$space-123"}, }, }, CheckAuditEvent: func(t *testing.T, b []byte) { @@ -553,9 +553,9 @@ var testCases = []struct { require.NoError(t, json.Unmarshal(b, &ev)) // AuditEvent fields - checkBaseAuditEvent(t, ev.AuditEvent, "", "0001-01-01T00:00:00Z", "user 'uid-123' deleted the space 'space-123'", "space_deleted") + checkBaseAuditEvent(t, ev.AuditEvent, "", "0001-01-01T00:00:00Z", "user 'uid-123' deleted the space 'space-123' (storage: 'storage-1')", "space_deleted") // AuditEventSpaces fields - checkSpacesAuditEvent(t, ev.AuditEventSpaces, "space-123") + checkSpacesAuditEvent(t, ev.AuditEventSpaces, "storage-1$space-123") }, }, } diff --git a/services/audit/pkg/types/helpers.go b/services/audit/pkg/types/helpers.go index f1aad63836..cc76e7cb37 100644 --- a/services/audit/pkg/types/helpers.go +++ b/services/audit/pkg/types/helpers.go @@ -4,5 +4,8 @@ import "strings" func SplitId(id string) (string, string) { ids := strings.Split(id, "$") + if len(ids) != 2 { + return id, "" + } return ids[0], ids[1] } From a8934c3655531b7d674c84b9b16dd34a5b75b515 Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Mon, 19 Jun 2023 07:54:55 +0200 Subject: [PATCH 3/4] make linter happy Signed-off-by: Christian Richter --- services/audit/pkg/types/helpers.go | 3 ++- services/audit/pkg/types/messages.go | 32 ++++++++++++++-------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/services/audit/pkg/types/helpers.go b/services/audit/pkg/types/helpers.go index cc76e7cb37..989cb66c20 100644 --- a/services/audit/pkg/types/helpers.go +++ b/services/audit/pkg/types/helpers.go @@ -2,7 +2,8 @@ package types import "strings" -func SplitId(id string) (string, string) { +// SplitStorageIDFromSpaceID splits the storage- and spaceid- from the given string +func SplitStorageIDFromSpaceID(id string) (string, string) { ids := strings.Split(id, "$") if len(ids) != 2 { return id, "" diff --git a/services/audit/pkg/types/messages.go b/services/audit/pkg/types/messages.go index 8ef8e3fe7f..ce28ae52f0 100644 --- a/services/audit/pkg/types/messages.go +++ b/services/audit/pkg/types/messages.go @@ -139,51 +139,51 @@ func MessageFileVersionRestored(executant, item, version string) string { // MessageSpaceCreated returns the human readable string that describes the action func MessageSpaceCreated(executant, spaceID, name string) string { - storagId, spaceID := SplitId(spaceID) - return fmt.Sprintf("user '%s' created a space '%s' with name '%s' (storage: '%s')", executant, spaceID, name, storagId) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + return fmt.Sprintf("user '%s' created a space '%s' with name '%s' (storage: '%s')", executant, spaceID, name, storagID) } // MessageSpaceRenamed returns the human readable string that describes the action func MessageSpaceRenamed(executant, spaceID, name string) string { - storagId, spaceID := SplitId(spaceID) - return fmt.Sprintf("user '%s' renamed space '%s' to '%s' (storage: '%s')", executant, spaceID, name, storagId) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + return fmt.Sprintf("user '%s' renamed space '%s' to '%s' (storage: '%s')", executant, spaceID, name, storagID) } // MessageSpaceDisabled returns the human readable string that describes the action func MessageSpaceDisabled(executant, spaceID string) string { - storagId, spaceID := SplitId(spaceID) - return fmt.Sprintf("user '%s' disabled the space '%s' (storage: '%s')", executant, spaceID, storagId) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + return fmt.Sprintf("user '%s' disabled the space '%s' (storage: '%s')", executant, spaceID, storagID) } // MessageSpaceEnabled returns the human readable string that describes the action func MessageSpaceEnabled(executant, spaceID string) string { - storagId, spaceID := SplitId(spaceID) - return fmt.Sprintf("user '%s' (re-) enabled the space '%s' (storage: '%s')", executant, spaceID, storagId) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + return fmt.Sprintf("user '%s' (re-) enabled the space '%s' (storage: '%s')", executant, spaceID, storagID) } // MessageSpaceDeleted returns the human readable string that describes the action func MessageSpaceDeleted(executant, spaceID string) string { - storagId, spaceID := SplitId(spaceID) - return fmt.Sprintf("user '%s' deleted the space '%s' (storage: '%s')", executant, spaceID, storagId) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + return fmt.Sprintf("user '%s' deleted the space '%s' (storage: '%s')", executant, spaceID, storagID) } // MessageSpaceShared returns the human readable string that describes the action func MessageSpaceShared(executant, spaceID, grantee string) string { - storagId, spaceID := SplitId(spaceID) - return fmt.Sprintf("user '%s' shared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagId) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + return fmt.Sprintf("user '%s' shared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagID) } // MessageSpaceUnshared returns the human readable string that describes the action func MessageSpaceUnshared(executant, spaceID, grantee string) string { - storagId, spaceID := SplitId(spaceID) - return fmt.Sprintf("user '%s' unshared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagId) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + return fmt.Sprintf("user '%s' unshared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagID) } // MessageSpaceUpdated returns the human readable string that describes the action func MessageSpaceUpdated(executant, spaceID, name string, quota uint64, opaque map[string]string) string { - storagId, spaceID := SplitId(spaceID) + storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) return fmt.Sprintf("user '%s' updated space '%s'. name: '%s', quota: '%d', opaque: '%s' (storage: '%s')", - executant, spaceID, name, quota, opaque, storagId) + executant, spaceID, name, quota, opaque, storagID) } // MessageUserCreated returns the human readable string that describes the action From 2d60212e1268aa51c6509caab529a9059b7dd16d Mon Sep 17 00:00:00 2001 From: Christian Richter Date: Mon, 19 Jun 2023 11:16:44 +0200 Subject: [PATCH 4/4] incorporate requested changes Signed-off-by: Christian Richter --- services/audit/pkg/types/helpers.go | 12 ------------ services/audit/pkg/types/messages.go | 17 +++++++++-------- 2 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 services/audit/pkg/types/helpers.go diff --git a/services/audit/pkg/types/helpers.go b/services/audit/pkg/types/helpers.go deleted file mode 100644 index 989cb66c20..0000000000 --- a/services/audit/pkg/types/helpers.go +++ /dev/null @@ -1,12 +0,0 @@ -package types - -import "strings" - -// SplitStorageIDFromSpaceID splits the storage- and spaceid- from the given string -func SplitStorageIDFromSpaceID(id string) (string, string) { - ids := strings.Split(id, "$") - if len(ids) != 2 { - return id, "" - } - return ids[0], ids[1] -} diff --git a/services/audit/pkg/types/messages.go b/services/audit/pkg/types/messages.go index ce28ae52f0..049e08f5bf 100644 --- a/services/audit/pkg/types/messages.go +++ b/services/audit/pkg/types/messages.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/cs3org/reva/v2/pkg/events" + "github.com/cs3org/reva/v2/pkg/storagespace" ) // short identifiers for audit actions @@ -139,49 +140,49 @@ func MessageFileVersionRestored(executant, item, version string) string { // MessageSpaceCreated returns the human readable string that describes the action func MessageSpaceCreated(executant, spaceID, name string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' created a space '%s' with name '%s' (storage: '%s')", executant, spaceID, name, storagID) } // MessageSpaceRenamed returns the human readable string that describes the action func MessageSpaceRenamed(executant, spaceID, name string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' renamed space '%s' to '%s' (storage: '%s')", executant, spaceID, name, storagID) } // MessageSpaceDisabled returns the human readable string that describes the action func MessageSpaceDisabled(executant, spaceID string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' disabled the space '%s' (storage: '%s')", executant, spaceID, storagID) } // MessageSpaceEnabled returns the human readable string that describes the action func MessageSpaceEnabled(executant, spaceID string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' (re-) enabled the space '%s' (storage: '%s')", executant, spaceID, storagID) } // MessageSpaceDeleted returns the human readable string that describes the action func MessageSpaceDeleted(executant, spaceID string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' deleted the space '%s' (storage: '%s')", executant, spaceID, storagID) } // MessageSpaceShared returns the human readable string that describes the action func MessageSpaceShared(executant, spaceID, grantee string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' shared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagID) } // MessageSpaceUnshared returns the human readable string that describes the action func MessageSpaceUnshared(executant, spaceID, grantee string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' unshared the space '%s' with '%s' (storage: '%s')", executant, spaceID, grantee, storagID) } // MessageSpaceUpdated returns the human readable string that describes the action func MessageSpaceUpdated(executant, spaceID, name string, quota uint64, opaque map[string]string) string { - storagID, spaceID := SplitStorageIDFromSpaceID(spaceID) + storagID, spaceID := storagespace.SplitStorageID(spaceID) return fmt.Sprintf("user '%s' updated space '%s'. name: '%s', quota: '%d', opaque: '%s' (storage: '%s')", executant, spaceID, name, quota, opaque, storagID) }