Skip to content

Commit f5234d2

Browse files
authored
Simplify admin actions calculations on list buckets (#1233)
* Simplify admin actions calculations on list buckets * adding license to file
1 parent f6acb88 commit f5234d2

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

portal-ui/src/common/Copyright.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// This file is part of MinIO Console Server
2+
// Copyright (c) 2021 MinIO, Inc.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
117
import React from "react";
218
import Typography from "@mui/material/Typography";
319
import Link from "@mui/material/Link";

portal-ui/src/screens/Console/Buckets/ListBuckets/Objects/ObjectDetails/ShareFile.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ const ShareFile = ({
141141
const slDate = new Date(`${selectedDate}`);
142142
const currDate = new Date();
143143

144-
const diffDate = Math.ceil((slDate.getTime() - currDate.getTime()) / 1000);
144+
const diffDate = Math.ceil(
145+
(slDate.getTime() - currDate.getTime()) / 1000
146+
);
145147

146148
if (diffDate > 0) {
147149
api

restapi/admin_remote_buckets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ func listExternalBucketsResponse(params user_api.ListExternalBucketsParams) (*mo
458458
// create a minioClient interface implementation
459459
// defining the client to be used
460460
remoteClient := AdminClient{Client: remoteAdmin}
461-
buckets, err := getAccountBuckets(ctx, remoteClient)
461+
buckets, err := getAccountBuckets(ctx, remoteClient, *params.Body.AccessKey)
462462
if err != nil {
463463
return nil, prepareError(err)
464464
}

restapi/user_buckets.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -290,43 +290,25 @@ func getBucketVersionedResponse(session *models.Principal, bucketName string) (*
290290
}
291291

292292
// getAccountBuckets fetches a list of all buckets allowed to that particular client from MinIO Servers
293-
func getAccountBuckets(ctx context.Context, client MinioAdmin) ([]*models.Bucket, error) {
293+
func getAccountBuckets(ctx context.Context, client MinioAdmin, accessKey string) ([]*models.Bucket, error) {
294294
info, err := client.AccountInfo(ctx)
295295
if err != nil {
296296
return []*models.Bucket{}, err
297297
}
298-
299298
policyInfo, err := getAccountPolicy(ctx, client)
300299
if err != nil {
301300
return nil, err
302301
}
303-
304-
bucketsPolicies := map[string]minioIAMPolicy.ActionSet{}
305-
for _, statement := range policyInfo.Statements {
306-
if statement.Effect == "Allow" {
307-
for _, resource := range statement.Resources.ToSlice() {
308-
resourceName := resource.String()
309-
if actions, ok := bucketsPolicies[resourceName]; ok {
310-
mergedActions := append(actions.ToSlice(), statement.Actions.ToSlice()...)
311-
bucketsPolicies[resourceName] = minioIAMPolicy.NewActionSet(mergedActions...)
312-
} else {
313-
bucketsPolicies[resourceName] = statement.Actions
314-
}
315-
}
316-
}
317-
}
318302
var bucketInfos []*models.Bucket
319303
for _, bucket := range info.Buckets {
320304
var bucketAdminRole bool
321-
bucketNameARN := fmt.Sprintf("arn:aws:s3:::%s/*", bucket.Name)
322-
// match bucket name against policy that allows admin actions
323-
if bucketPolicyActions, ok := bucketsPolicies[bucketNameARN]; ok {
324-
bucketAdminRoleActions := bucketPolicyActions.Intersection(acl.BucketAdminRole)
325-
bucketAdminRole = len(bucketAdminRoleActions) > 0
326-
} else if bucketPolicyActions, ok := bucketsPolicies["arn:aws:s3:::*"]; ok {
327-
bucketAdminRoleActions := bucketPolicyActions.Intersection(acl.BucketAdminRole)
328-
bucketAdminRole = len(bucketAdminRoleActions) > 0
305+
conditionValues := map[string][]string{
306+
condition.AWSUsername.Name(): {accessKey},
329307
}
308+
bucketActions := policyInfo.IsAllowedActions(bucket.Name, "", conditionValues)
309+
bucketAdminRoleActions := bucketActions.Intersection(acl.BucketAdminRole)
310+
bucketAdminRole = len(bucketAdminRoleActions) > 0
311+
330312
bucketElem := &models.Bucket{
331313
CreationDate: bucket.Created.Format(time.RFC3339),
332314
Details: &models.BucketDetails{
@@ -376,7 +358,7 @@ func getListBucketsResponse(session *models.Principal) (*models.ListBucketsRespo
376358
// create a minioClient interface implementation
377359
// defining the client to be used
378360
adminClient := AdminClient{Client: mAdmin}
379-
buckets, err := getAccountBuckets(ctx, adminClient)
361+
buckets, err := getAccountBuckets(ctx, adminClient, session.AccountAccessKey)
380362
if err != nil {
381363
return nil, prepareError(err)
382364
}
@@ -486,7 +468,7 @@ func setBucketAccessPolicy(ctx context.Context, client MinioClient, bucketName s
486468

487469
bucketAccessPolicy := policy.BucketAccessPolicy{Version: minioIAMPolicy.DefaultVersion}
488470
bucketAccessPolicy.Statements = policy.SetPolicy(bucketAccessPolicy.Statements,
489-
policy.BucketPolicy(bucketPolicy), bucketName, "")
471+
bucketPolicy, bucketName, "")
490472
// implemented like minio/mc/ s3Client.SetAccess()
491473
if len(bucketAccessPolicy.Statements) == 0 {
492474
return client.setBucketPolicyWithContext(ctx, bucketName, "")

restapi/user_buckets_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func TestListBucket(t *testing.T) {
180180
// get list buckets response this response should have Name, CreationDate, Size and Access
181181
// as part of of each bucket
182182
function := "getaAcountUsageInfo()"
183-
bucketList, err := getAccountBuckets(ctx, adminClient)
183+
bucketList, err := getAccountBuckets(ctx, adminClient, "")
184184
if err != nil {
185185
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
186186
}
@@ -197,7 +197,7 @@ func TestListBucket(t *testing.T) {
197197
minioAccountInfoMock = func(ctx context.Context) (madmin.AccountInfo, error) {
198198
return madmin.AccountInfo{}, errors.New("error")
199199
}
200-
_, err = getAccountBuckets(ctx, adminClient)
200+
_, err = getAccountBuckets(ctx, adminClient, "")
201201
if assert.Error(err) {
202202
assert.Equal("error", err.Error())
203203
}

0 commit comments

Comments
 (0)