diff --git a/services/ocm/pkg/revaconfig/config.go b/services/ocm/pkg/revaconfig/config.go index 4483d9c950..69a5e152d6 100644 --- a/services/ocm/pkg/revaconfig/config.go +++ b/services/ocm/pkg/revaconfig/config.go @@ -176,6 +176,15 @@ func OCMConfigFromStruct(cfg *config.Config, logger log.Logger) map[string]any { "file": cfg.OCMCore.Drivers.JSON.File, }, }, + "events": map[string]any{ + "natsaddress": cfg.Events.Endpoint, + "natsclusterid": cfg.Events.Cluster, + "enabletls": cfg.Events.EnableTLS, + "tlsinsecure": cfg.Events.TLSInsecure, + "tlsrootcacertificate": cfg.Events.TLSRootCACertificate, + "authusername": cfg.Events.AuthUsername, + "authpassword": cfg.Events.AuthPassword, + }, }, "storageprovider": map[string]any{ "driver": "ocmreceived", diff --git a/services/userlog/pkg/command/server.go b/services/userlog/pkg/command/server.go index f744786ec9..3181639cec 100644 --- a/services/userlog/pkg/command/server.go +++ b/services/userlog/pkg/command/server.go @@ -50,6 +50,10 @@ var _registeredEvents = []events.Unmarshaller{ events.ShareRemoved{}, events.ShareExpired{}, + // ocm share related + events.OCMCoreShareCreated{}, + events.OCMCoreShareDelete{}, + // misc ocEvents.ResourceMention{}, } diff --git a/services/userlog/pkg/service/consumer/filter.go b/services/userlog/pkg/service/consumer/filter.go index 8a83d5ac1b..248676ae9b 100644 --- a/services/userlog/pkg/service/consumer/filter.go +++ b/services/userlog/pkg/service/consumer/filter.go @@ -47,9 +47,9 @@ func (ulf userlogFilter) filterUsersBySettings(ctx context.Context, users []stri var settingId string // map type to settings key switch event.Event.(type) { - case events.ShareCreated: + case events.ShareCreated, events.OCMCoreShareCreated: settingId = defaults.SettingUUIDProfileEventShareCreated - case events.ShareRemoved: + case events.ShareRemoved, events.OCMCoreShareDelete: settingId = defaults.SettingUUIDProfileEventShareRemoved case events.ShareExpired: settingId = defaults.SettingUUIDProfileEventShareExpired diff --git a/services/userlog/pkg/service/consumer/service.go b/services/userlog/pkg/service/consumer/service.go index db376329da..65d265fb9c 100644 --- a/services/userlog/pkg/service/consumer/service.go +++ b/services/userlog/pkg/service/consumer/service.go @@ -160,6 +160,14 @@ func (s *Service) processEvent(event events.Event) { users, err = utils.ResolveID(ctx, e.GranteeUserID, e.GranteeGroupID, gwc) case events.ShareExpired: users, err = utils.ResolveID(ctx, e.GranteeUserID, e.GranteeGroupID, gwc) + + // ocm share related, the grantee is always a local user + case events.OCMCoreShareCreated: + executant = e.Sharer + users = append(users, e.GranteeUserID.GetOpaqueId()) + case events.OCMCoreShareDelete: + executant = e.Sharer + users = append(users, e.Grantee.GetOpaqueId()) } if err != nil { diff --git a/services/userlog/pkg/service/conversion.go b/services/userlog/pkg/service/conversion.go index 3fd2d8ebe0..ff556c77c6 100644 --- a/services/userlog/pkg/service/conversion.go +++ b/services/userlog/pkg/service/conversion.go @@ -12,9 +12,13 @@ import ( gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1" + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" + typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" "github.com/opencloud-eu/reva/v3/pkg/events" + ocmuser "github.com/opencloud-eu/reva/v3/pkg/ocm/user" "github.com/opencloud-eu/reva/v3/pkg/rgrpc/todo/pool" "github.com/opencloud-eu/reva/v3/pkg/storagespace" "github.com/opencloud-eu/reva/v3/pkg/utils" @@ -118,6 +122,12 @@ func (c *Converter) ConvertEvent(eventid string, event any) (OC10Notification, e case events.ShareRemoved: return c.shareMessage(eventid, ShareRemoved, ev.Executant, ev.ItemID, ev.ShareID, ev.Timestamp) + // ocm share related + case events.OCMCoreShareCreated: + return c.ocmShareMessage(eventid, ShareCreated, ShareCreatedByProvider, ev.Sharer, ev.GranteeUserID, ev.ShareID, ev.ResourceName, utils.TSToTime(ev.CTime)) + case events.OCMCoreShareDelete: + return c.ocmShareMessage(eventid, ShareRemoved, ShareRemovedByProvider, ev.Sharer, ev.Grantee, ev.ShareID, ev.ResourceName, utils.TSToTime(ev.CTime)) + // misc case ocEvents.ResourceMention: return c.resourceMention(eventid, Mention, ev.Executant, ev.Ref.GetResourceId(), ev.Timestamp) @@ -308,6 +318,85 @@ func (c *Converter) shareMessage(eventid string, nt NotificationTemplate, execut }, nil } +// ocmShareMessage converts an event about a share received from a federated instance. The +// resource lives on the remote instance, so we only know its name from the event. The resource +// details carry no id, clients would turn it into a link to a local resource. +// +// If the sharer cannot be resolved we use the providerNT template, which names the provider +// domain of the sharer instead of the user. +func (c *Converter) ocmShareMessage(eventid string, nt, providerNT NotificationTemplate, sharer *user.UserId, grantee *user.UserId, shareid string, resourcename string, ts time.Time) (OC10Notification, error) { + usr := c.getOCMUser(sharer, grantee) + provider := sharer.GetIdp() + if usr == nil { + nt = providerNT + } + + subj, subjraw, msg, msgraw, err := composeMessage(nt, c.locale, c.defaultLanguage, c.translationPath, map[string]any{ + "username": usr.GetDisplayName(), + "resourcename": resourcename, + "provider": provider, + }) + if err != nil { + return OC10Notification{}, err + } + // clients only know how to fill in the rich parameters, so we fill in the provider ourselves + msgraw = strings.ReplaceAll(msgraw, "{provider}", provider) + + details := generateDetails(usr, nil, nil, &collaboration.ShareId{OpaqueId: shareid}) + details["resource"] = map[string]string{"name": resourcename} + + return OC10Notification{ + EventID: eventid, + Service: c.serviceName, + UserName: usr.GetUsername(), + Timestamp: ts.Format(time.RFC3339Nano), + ResourceID: shareid, + ResourceType: _resourceTypeShare, + Subject: subj, + SubjectRaw: subjraw, + Message: msg, + MessageRaw: msgraw, + MessageDetails: details, + }, nil +} + +// getOCMUser looks up a federated user the grantee has accepted an invite from. It returns nil +// if the user cannot be resolved, e.g. because the federated connection was deleted. The +// federated id is no help then, it is the remote user id plus the issuer of the remote IdP, +// e.g. "@https://cloud.example.org". +func (c *Converter) getOCMUser(sharer *user.UserId, grantee *user.UserId) *user.User { + key := ocmuser.FormatOCMUser(sharer) + if u, ok := c.users[key]; ok { + return u + } + + gwc, err := c.gatewaySelector.Next() + if err != nil { + return nil + } + + // GetAcceptedUser uses the user from the context to find the accepted user. The service + // account would never match, so we call it without a token and pass the grantee as a filter. + filter, err := utils.MarshalProtoV1ToJSON(grantee) + if err != nil { + return nil + } + res, err := gwc.GetAcceptedUser(context.Background(), &invitepb.GetAcceptedUserRequest{ + RemoteUserId: sharer, + Opaque: &typespb.Opaque{ + Map: map[string]*typespb.OpaqueEntry{ + "user-filter": {Decoder: "json", Value: filter}, + }, + }, + }) + if err != nil || res.GetStatus().GetCode() != rpc.Code_CODE_OK || res.GetRemoteUser() == nil { + return nil + } + + c.users[key] = res.GetRemoteUser() + return res.GetRemoteUser() +} + func (c *Converter) virusMessage(eventid string, nt NotificationTemplate, executant *user.User, rid *storageprovider.ResourceId, filename string, virus string, ts time.Time) (OC10Notification, error) { subj, subjraw, msg, msgraw, err := composeMessage(nt, c.locale, c.defaultLanguage, c.translationPath, map[string]any{ "resourcename": filename, diff --git a/services/userlog/pkg/service/conversion_test.go b/services/userlog/pkg/service/conversion_test.go new file mode 100644 index 0000000000..f13afba099 --- /dev/null +++ b/services/userlog/pkg/service/conversion_test.go @@ -0,0 +1,103 @@ +package service_test + +import ( + "context" + + gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" + user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1" + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" + types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/opencloud-eu/reva/v3/pkg/events" + "github.com/opencloud-eu/reva/v3/pkg/rgrpc/todo/pool" + "github.com/opencloud-eu/reva/v3/pkg/utils" + cs3mocks "github.com/opencloud-eu/reva/v3/tests/cs3mocks/mocks" + "github.com/stretchr/testify/mock" + "google.golang.org/grpc" + + "github.com/opencloud-eu/opencloud/services/userlog/pkg/service" +) + +var _ = Describe("Converter", func() { + var ( + gatewayClient *cs3mocks.GatewayAPIClient + converter *service.Converter + + // what ocmd makes of the sender "@@" of an incoming share + sharer = &user.UserId{ + Type: user.UserType_USER_TYPE_FEDERATED, + OpaqueId: "4c510ada-c86b-4815-8820-42cdf82c3d51@https://remote.example.org", + Idp: "remote.example.org", + } + grantee = &user.UserId{OpaqueId: "f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c", Idp: "https://local.example.org"} + ctime = &types.Timestamp{Seconds: 1700000000} + ) + + BeforeEach(func() { + pool.RemoveSelector("GatewaySelector" + "eu.opencloud.api.gateway") + gatewayClient = &cs3mocks.GatewayAPIClient{} + selector := pool.GetSelector[gateway.GatewayAPIClient]( + "GatewaySelector", + "eu.opencloud.api.gateway", + func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient { + return gatewayClient + }, + ) + converter = service.NewConverter(context.Background(), "en", selector, "userlog", "", "en") + }) + + Describe("ocm shares", func() { + It("converts a received share and resolves the sharer for the grantee", func() { + gatewayClient.On("GetAcceptedUser", mock.Anything, mock.MatchedBy(func(req *invitepb.GetAcceptedUserRequest) bool { + var filter user.UserId + if err := utils.UnmarshalJSONToProtoV1(req.GetOpaque().GetMap()["user-filter"].GetValue(), &filter); err != nil { + return false + } + return req.GetRemoteUserId().GetOpaqueId() == sharer.GetOpaqueId() && filter.GetOpaqueId() == grantee.GetOpaqueId() + })).Return(&invitepb.GetAcceptedUserResponse{ + Status: &rpc.Status{Code: rpc.Code_CODE_OK}, + RemoteUser: &user.User{Id: sharer, Username: "einstein", DisplayName: "Albert Einstein"}, + }, nil) + + n, err := converter.ConvertEvent("eventid", events.OCMCoreShareCreated{ + ShareID: "shareid", + Sharer: sharer, + GranteeUserID: grantee, + ItemID: "remote-item-id", + ResourceName: "relativity", + CTime: ctime, + }) + Expect(err).ToNot(HaveOccurred()) + Expect(n.Subject).To(Equal("Resource shared")) + Expect(n.Message).To(Equal("Albert Einstein shared relativity with you")) + Expect(n.UserName).To(Equal("einstein")) + Expect(n.ResourceID).To(Equal("shareid")) + Expect(n.ResourceType).To(Equal("share")) + Expect(n.MessageDetails["share"]).To(Equal(map[string]string{"id": "shareid"})) + Expect(n.MessageDetails["resource"]).To(Equal(map[string]string{"name": "relativity"})) + }) + + It("names the provider domain when the sharer cannot be resolved", func() { + gatewayClient.On("GetAcceptedUser", mock.Anything, mock.Anything).Return(&invitepb.GetAcceptedUserResponse{ + Status: &rpc.Status{Code: rpc.Code_CODE_NOT_FOUND}, + }, nil) + + n, err := converter.ConvertEvent("eventid", events.OCMCoreShareDelete{ + ShareID: "shareid", + Sharer: sharer, + Grantee: grantee, + ResourceName: "relativity", + CTime: ctime, + }) + Expect(err).ToNot(HaveOccurred()) + Expect(n.Subject).To(Equal("Resource unshared")) + Expect(n.Message).To(Equal("Someone at remote.example.org unshared relativity with you")) + Expect(n.MessageRaw).To(Equal("Someone at remote.example.org unshared {resource} with you")) + Expect(n.ResourceID).To(Equal("shareid")) + Expect(n.MessageDetails).ToNot(HaveKey("user")) + Expect(n.MessageDetails["resource"]).To(Equal(map[string]string{"name": "relativity"})) + }) + }) +}) diff --git a/services/userlog/pkg/service/templates.go b/services/userlog/pkg/service/templates.go index 6b244f2da5..853f809ed7 100644 --- a/services/userlog/pkg/service/templates.go +++ b/services/userlog/pkg/service/templates.go @@ -49,6 +49,17 @@ var ( Message: l10n.Template("{user} unshared {resource} with you"), } + // used for federated shares when the sharer cannot be resolved + ShareCreatedByProvider = NotificationTemplate{ + Subject: l10n.Template("Resource shared"), + Message: l10n.Template("Someone at {provider} shared {resource} with you"), + } + + ShareRemovedByProvider = NotificationTemplate{ + Subject: l10n.Template("Resource unshared"), + Message: l10n.Template("Someone at {provider} unshared {resource} with you"), + } + Mention = NotificationTemplate{ Subject: l10n.Template("You have been mentioned"), Message: l10n.Template("{user} mentioned you in {resource}"), @@ -72,6 +83,7 @@ var _placeholders = map[string]string{ "{resource}": "{{ .resourcename }}", "{virus}": "{{ .virusdescription }}", "{date}": "{{ .date }}", + "{provider}": "{{ .provider }}", } // NotificationTemplate is the data structure for the notifications diff --git a/tests/acceptance/features/apiOcm/notification.feature b/tests/acceptance/features/apiOcm/notification.feature new file mode 100644 index 0000000000..08c7e2ce96 --- /dev/null +++ b/tests/acceptance/features/apiOcm/notification.feature @@ -0,0 +1,64 @@ +@ocm +Feature: federated users get in-app notifications about shares + As a federated user + I want to be notified when a user of another instance shares a resource with me or removes the share + So that I know which federated resources I have access to + + Background: + Given user "Alice" has been created with default attributes + And using server "REMOTE" + And user "Brian" has been created with default attributes + And "Brian" has created the federation share invitation + And using server "LOCAL" + And "Alice" has accepted invitation + And user "Alice" has created folder "folderToShare" + + + Scenario: federated user gets a notification when a resource is shared + When user "Alice" sends the following resource share invitation to federated user using the Graph API: + | resource | folderToShare | + | space | Personal | + | sharee | Brian | + | shareType | user | + | permissionsRole | Viewer | + Then the HTTP status code should be "200" + And using server "REMOTE" + And user "Brian" should get a notification with subject "Resource shared" and message: + | message | + | Alice Hansen shared folderToShare with you | + + + Scenario: federated user gets a notification when a share is removed + Given user "Alice" has sent the following resource share invitation to federated user: + | resource | folderToShare | + | space | Personal | + | sharee | Brian | + | shareType | user | + | permissionsRole | Viewer | + When user "Alice" removes the access of user "Brian" from resource "folderToShare" of space "Personal" using the Graph API + Then the HTTP status code should be "204" + And using server "REMOTE" + And user "Brian" should get a notification with subject "Resource unshared" and message: + | message | + | Alice Hansen unshared folderToShare with you | + + + Scenario: federated user does not get a notification for a removed share when the in-app notification is disabled + Given using server "REMOTE" + And user "Brian" has disabled notification for the following event using the settings API: + | event | Share Removed | + | notificationTypes | in-app | + And using server "LOCAL" + And user "Alice" has sent the following resource share invitation to federated user: + | resource | folderToShare | + | space | Personal | + | sharee | Brian | + | shareType | user | + | permissionsRole | Viewer | + When user "Alice" removes the access of user "Brian" from resource "folderToShare" of space "Personal" using the Graph API + Then the HTTP status code should be "204" + And using server "REMOTE" + And user "Brian" should get a notification with subject "Resource shared" and message: + | message | + | Alice Hansen shared folderToShare with you | + But user "Brian" should not have a notification related to resource "folderToShare" with subject "Resource unshared"