Add function to fetch events containing userID.

This PR adds a function to fetch events containing a userID. This is to
be able to offer this functionality for the GDPR requirement.
This commit is contained in:
Daniël Franke
2023-04-03 11:49:52 +02:00
parent 5c2a011519
commit cfb65cd289
6 changed files with 269 additions and 51 deletions

View File

@@ -3,6 +3,7 @@ package service
import (
"context"
"fmt"
"regexp"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
@@ -75,3 +76,39 @@ func (eh *EventHistoryService) GetEvents(ctx context.Context, req *ehsvc.GetEven
return nil
}
// GetEventsForUser allows to retrieve events from the eventstore by userID
// This function will match all events that contains the user ID between two non-word characters.
// The reasoning behind this is that events put the userID in many different fields, which can differ
// per event type. This function will match all events that contain the userID by using a regex.
// This should also cover future events that might contain the userID in a different field.
func (eh *EventHistoryService) GetEventsForUser(ctx context.Context, req *ehsvc.GetEventsForUserRequest, resp *ehsvc.GetEventsResponse) error {
idx, err := eh.store.List(store.ListPrefix(""))
if err != nil {
eh.log.Error().Err(err).Msg("could not list events")
return err
}
// Match all events that contains the user ID between two non-word characters.
userID := regexp.MustCompile(fmt.Sprintf(`\W%s\W`, req.UserID))
for _, i := range idx {
e, err := eh.store.Read(i)
if err != nil {
if err != store.ErrNotFound {
eh.log.Error().Err(err).Str("eventid", i).Msg("could not read event")
}
continue
}
if userID.Match(e[0].Value) {
resp.Events = append(resp.Events, &ehmsg.Event{
Id: i,
Event: e[0].Value,
Type: e[0].Metadata["type"].(string),
})
}
}
return nil
}

View File

@@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"reflect"
"sort"
"time"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
@@ -34,7 +36,6 @@ var _ = Describe("EventHistoryService", func() {
bus = testBus(make(chan events.Event))
eh, err = service.NewEventHistoryService(cfg, bus, sto, log.Logger{})
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
@@ -54,7 +55,49 @@ var _ = Describe("EventHistoryService", func() {
Expect(len(resp.Events)).To(Equal(1))
Expect(resp.Events[0].Id).To(Equal(id))
})
It("Gets all events", func() {
ids := make([]string, 3)
ids[0] = bus.Publish(events.UploadReady{
ExecutingUser: &userv1beta1.User{
Id: &userv1beta1.UserId{
OpaqueId: "test-id",
},
},
Failed: false,
Timestamp: time.Time{},
})
ids[1] = bus.Publish(events.UserCreated{
UserID: "another-id",
})
ids[2] = bus.Publish(events.UserDeleted{
Executant: &userv1beta1.UserId{
OpaqueId: "another-id",
},
UserID: "test-id",
})
time.Sleep(500 * time.Millisecond)
resp := &ehsvc.GetEventsResponse{}
err := eh.GetEventsForUser(context.Background(), &ehsvc.GetEventsForUserRequest{UserID: "test-id"}, resp)
Expect(err).ToNot(HaveOccurred())
Expect(resp).ToNot(BeNil())
// Events don't always come back in the same order as they were sent, so we need to sort them and
// do the same for the expcted IDs as well.
expectedIDs := []string{ids[0], ids[2]}
sort.Strings(expectedIDs)
var gotIDs []string
for _, ev := range resp.Events {
gotIDs = append(gotIDs, ev.Id)
}
sort.Strings(gotIDs)
Expect(len(gotIDs)).To(Equal(len(expectedIDs)))
Expect(gotIDs[0]).To(Equal(expectedIDs[0]))
Expect(gotIDs[1]).To(Equal(expectedIDs[1]))
})
})

View File

@@ -1,4 +1,4 @@
// Code generated by mockery v2.14.1. DO NOT EDIT.
// Code generated by mockery v2.22.1. DO NOT EDIT.
package mocks
@@ -29,6 +29,10 @@ func (_m *EventHistoryService) GetEvents(ctx context.Context, in *v0.GetEventsRe
ret := _m.Called(_ca...)
var r0 *v0.GetEventsResponse
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *v0.GetEventsRequest, ...client.CallOption) (*v0.GetEventsResponse, error)); ok {
return rf(ctx, in, opts...)
}
if rf, ok := ret.Get(0).(func(context.Context, *v0.GetEventsRequest, ...client.CallOption) *v0.GetEventsResponse); ok {
r0 = rf(ctx, in, opts...)
} else {
@@ -37,7 +41,6 @@ func (_m *EventHistoryService) GetEvents(ctx context.Context, in *v0.GetEventsRe
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *v0.GetEventsRequest, ...client.CallOption) error); ok {
r1 = rf(ctx, in, opts...)
} else {
@@ -47,6 +50,39 @@ func (_m *EventHistoryService) GetEvents(ctx context.Context, in *v0.GetEventsRe
return r0, r1
}
// GetEventsForUser provides a mock function with given fields: ctx, in, opts
func (_m *EventHistoryService) GetEventsForUser(ctx context.Context, in *v0.GetEventsForUserRequest, opts ...client.CallOption) (*v0.GetEventsResponse, error) {
_va := make([]interface{}, len(opts))
for _i := range opts {
_va[_i] = opts[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, in)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
var r0 *v0.GetEventsResponse
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *v0.GetEventsForUserRequest, ...client.CallOption) (*v0.GetEventsResponse, error)); ok {
return rf(ctx, in, opts...)
}
if rf, ok := ret.Get(0).(func(context.Context, *v0.GetEventsForUserRequest, ...client.CallOption) *v0.GetEventsResponse); ok {
r0 = rf(ctx, in, opts...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*v0.GetEventsResponse)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *v0.GetEventsForUserRequest, ...client.CallOption) error); ok {
r1 = rf(ctx, in, opts...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
type mockConstructorTestingTNewEventHistoryService interface {
mock.TestingT
Cleanup(func())