|
| 1 | +// Copyright 2018-2024 CERN |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// In applying this license, CERN does not waive the privileges and immunities |
| 16 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 17 | +// or submit itself to any jurisdiction. |
| 18 | + |
| 19 | +package gateway |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" |
| 25 | + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" |
| 26 | + typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" |
| 27 | + "github.com/owncloud/reva/v2/pkg/conversions" |
| 28 | +) |
| 29 | + |
| 30 | +func userGrantee(id string) *provider.Grantee { |
| 31 | + return &provider.Grantee{ |
| 32 | + Type: provider.GranteeType_GRANTEE_TYPE_USER, |
| 33 | + Id: &provider.Grantee_UserId{UserId: &userpb.UserId{OpaqueId: id}}, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func managerGrant(grantee *provider.Grantee, exp *typesv1beta1.Timestamp) *provider.Grant { |
| 38 | + return &provider.Grant{ |
| 39 | + Grantee: grantee, |
| 40 | + Permissions: &provider.ResourcePermissions{RemoveGrant: true}, |
| 41 | + Expiration: exp, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func viewerGrant(grantee *provider.Grantee, exp *typesv1beta1.Timestamp) *provider.Grant { |
| 46 | + return &provider.Grant{ |
| 47 | + Grantee: grantee, |
| 48 | + Permissions: &provider.ResourcePermissions{RemoveGrant: false}, |
| 49 | + Expiration: exp, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// TestMayRemoveLastPermanentManager covers the guard that prevents orphaning a |
| 54 | +// space: a manager grant that gains an expiration must be treated the same as a |
| 55 | +// manager that loses the role, because an expiring manager is not a permanent |
| 56 | +// manager. |
| 57 | +func TestMayRemoveLastPermanentManager(t *testing.T) { |
| 58 | + alice := userGrantee("alice") |
| 59 | + exp := &typesv1beta1.Timestamp{Seconds: 1} |
| 60 | + |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + grant *provider.Grant |
| 64 | + want bool |
| 65 | + }{ |
| 66 | + {"permanent manager, unchanged", managerGrant(alice, nil), false}, |
| 67 | + {"manager gains an expiration", managerGrant(alice, exp), true}, |
| 68 | + {"manager role dropped", viewerGrant(alice, nil), true}, |
| 69 | + {"non-manager with expiration", viewerGrant(alice, exp), true}, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tt := range tests { |
| 73 | + t.Run(tt.name, func(t *testing.T) { |
| 74 | + if got := mayRemoveLastPermanentManager(tt.grant); got != tt.want { |
| 75 | + t.Errorf("mayRemoveLastPermanentManager() = %v, want %v", got, tt.want) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// TestIsSpaceManagerRemaining documents the invariant the guard relies on: only |
| 82 | +// another permanent (non-expiring), non-self manager keeps a space managed. |
| 83 | +func TestIsSpaceManagerRemaining(t *testing.T) { |
| 84 | + alice := userGrantee("alice") |
| 85 | + bob := userGrantee("bob") |
| 86 | + exp := &typesv1beta1.Timestamp{Seconds: 1} |
| 87 | + |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + grants []*provider.Grant |
| 91 | + updating *provider.Grantee |
| 92 | + want bool |
| 93 | + }{ |
| 94 | + {"another permanent manager remains", []*provider.Grant{managerGrant(alice, nil), managerGrant(bob, nil)}, alice, true}, |
| 95 | + {"only the updated grantee is manager", []*provider.Grant{managerGrant(alice, nil)}, alice, false}, |
| 96 | + {"the other manager is expiring", []*provider.Grant{managerGrant(alice, nil), managerGrant(bob, exp)}, alice, false}, |
| 97 | + {"the other grantee is only a viewer", []*provider.Grant{managerGrant(alice, nil), viewerGrant(bob, nil)}, alice, false}, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tt := range tests { |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + if got := isSpaceManagerRemaining(tt.grants, tt.updating); got != tt.want { |
| 103 | + t.Errorf("isSpaceManagerRemaining() = %v, want %v", got, tt.want) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// spaceRootGrantRejected mirrors the guard used in both addSpaceShare and updateSpaceShare. |
| 110 | +func spaceRootGrantRejected(perms *provider.ResourcePermissions) bool { |
| 111 | + return hasGrantManagementBits(perms) && !conversions.SufficientCS3Permissions(perms, managerPerms) |
| 112 | +} |
| 113 | + |
| 114 | +func TestSpaceShareRejectsPartialGrantManagement(t *testing.T) { |
| 115 | + // a single grant-management bit must be rejected |
| 116 | + if !spaceRootGrantRejected(&provider.ResourcePermissions{RemoveGrant: true}) { |
| 117 | + t.Error("single-bit RemoveGrant grant should be rejected on a space root") |
| 118 | + } |
| 119 | + // read bits plus a grant-management bit (the update-path stealth-manager payload) must be rejected |
| 120 | + if !spaceRootGrantRejected(&provider.ResourcePermissions{Stat: true, GetPath: true, ListGrants: true, RemoveGrant: true}) { |
| 121 | + t.Error("read+RemoveGrant grant should be rejected on a space root") |
| 122 | + } |
| 123 | + // a full manager grant must be accepted |
| 124 | + if spaceRootGrantRejected(conversions.NewManagerRole().CS3ResourcePermissions()) { |
| 125 | + t.Error("full manager grant should be accepted on a space root") |
| 126 | + } |
| 127 | + // viewer/editor grants carry no grant-management bits and must be accepted |
| 128 | + if spaceRootGrantRejected(conversions.NewSpaceViewerRole().CS3ResourcePermissions()) { |
| 129 | + t.Error("space viewer grant should be accepted on a space root") |
| 130 | + } |
| 131 | + if spaceRootGrantRejected(conversions.NewSpaceEditorRole().CS3ResourcePermissions()) { |
| 132 | + t.Error("space editor grant should be accepted on a space root") |
| 133 | + } |
| 134 | +} |
0 commit comments