Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Gateway Panics #4953

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/fix-gateway-panic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Avoid gateway panics

The gateway would panic if there is a missing user in the context. Now it errors instead.

https://github.com/cs3org/reva/issues/4953
6 changes: 5 additions & 1 deletion internal/grpc/services/gateway/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ func buildOpenInAppRequest(ctx context.Context, ri *storageprovider.ResourceInfo
}

// build a fake user object for the token
currentuser := ctxpkg.ContextMustGetUser(ctx)
currentuser, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

scopedUser := &userpb.User{
Id: ri.GetOwner(), // the owner of the resource is always set, right?
DisplayName: "View Only user for " + currentuser.GetUsername(),
Expand Down
5 changes: 4 additions & 1 deletion internal/grpc/services/gateway/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,10 @@ func (s *svc) DeleteStorageSpace(ctx context.Context, req *provider.DeleteStorag
}

func (s *svc) GetHome(ctx context.Context, _ *provider.GetHomeRequest) (*provider.GetHomeResponse, error) {
currentUser := ctxpkg.ContextMustGetUser(ctx)
currentUser, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

srClient, err := s.getStorageRegistryClient(ctx, s.c.StorageRegistryEndpoint)
if err != nil {
Expand Down
22 changes: 19 additions & 3 deletions internal/grpc/services/gateway/storageprovidercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
sdk "github.com/cs3org/reva/v2/pkg/sdk/common"
"github.com/cs3org/reva/v2/pkg/storage/cache"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/pkg/errors"
"google.golang.org/grpc"
)

Expand All @@ -44,7 +45,12 @@ func (c *cachedRegistryClient) ListStorageProviders(ctx context.Context, in *reg

spaceID := sdk.DecodeOpaqueMap(in.Opaque)["space_id"]

key := c.cache.GetKey(ctxpkg.ContextMustGetUser(ctx).GetId(), spaceID)
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

key := c.cache.GetKey(u.GetId(), spaceID)
if key != "" {
s := &registry.ListStorageProvidersResponse{}
if err := c.cache.PullFromCache(key, s); err == nil {
Expand Down Expand Up @@ -89,7 +95,12 @@ type cachedSpacesAPIClient struct {
// CreateStorageSpace creates a storage space
func (c *cachedSpacesAPIClient) CreateStorageSpace(ctx context.Context, in *provider.CreateStorageSpaceRequest, opts ...grpc.CallOption) (*provider.CreateStorageSpaceResponse, error) {
if in.Type == "personal" {
key := c.createPersonalSpaceCache.GetKey(ctxpkg.ContextMustGetUser(ctx).GetId())
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

key := c.createPersonalSpaceCache.GetKey(u.GetId())
if key != "" {
s := &provider.CreateStorageSpaceResponse{}
if err := c.createPersonalSpaceCache.PullFromCache(key, s); err == nil {
Expand Down Expand Up @@ -132,7 +143,12 @@ type cachedAPIClient struct {

// CreateHome caches calls to CreateHome locally - anyways they only need to be called once per user
func (c *cachedAPIClient) CreateHome(ctx context.Context, in *provider.CreateHomeRequest, opts ...grpc.CallOption) (*provider.CreateHomeResponse, error) {
key := c.createPersonalSpaceCache.GetKey(ctxpkg.ContextMustGetUser(ctx).GetId())
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

key := c.createPersonalSpaceCache.GetKey(u.GetId())
if key != "" {
s := &provider.CreateHomeResponse{}
if err := c.createPersonalSpaceCache.PullFromCache(key, s); err == nil {
Expand Down
19 changes: 16 additions & 3 deletions internal/grpc/services/gateway/usershareprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ func (s *svc) updateShare(ctx context.Context, req *collaboration.UpdateShareReq
}

if s.c.CommitShareToStorageGrant {
creator := ctxpkg.ContextMustGetUser(ctx)
creator, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

grant := &provider.Grant{
Grantee: res.GetShare().GetGrantee(),
Permissions: res.GetShare().GetPermissions().GetPermissions(),
Expand Down Expand Up @@ -198,11 +202,16 @@ func (s *svc) updateSpaceShare(ctx context.Context, req *collaboration.UpdateSha
req.Share.Expiration = existsGrant.GetExpiration()
}

u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

grant := &provider.Grant{
Grantee: req.GetShare().GetGrantee(),
Permissions: req.GetShare().GetPermissions().GetPermissions(),
Expiration: req.GetShare().GetExpiration(),
Creator: ctxpkg.ContextMustGetUser(ctx).GetId(),
Creator: u.GetId(),
}

if grant.GetPermissions() == nil {
Expand Down Expand Up @@ -410,7 +419,11 @@ func (s *svc) addGrant(ctx context.Context, id *provider.ResourceId, g *provider
ResourceId: id,
}

creator := ctxpkg.ContextMustGetUser(ctx)
creator, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
return nil, errors.New("user not found in context")
}

grantReq := &provider.AddGrantRequest{
Ref: ref,
Grant: &provider.Grant{
Expand Down
Loading