From addf12f43d335367b2ffbe4710a7b40c257edae6 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Sat, 26 Dec 2020 17:41:04 +0800 Subject: [PATCH] optimized(region): qcloud bucket policy set and get operation --- pkg/apis/compute/bucket.go | 3 +++ pkg/cloudprovider/objectstore.go | 2 ++ pkg/compute/models/buckets.go | 19 +++++++++-------- pkg/multicloud/qcloud/bucket.go | 36 ++++++++++++++++++++++---------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/pkg/apis/compute/bucket.go b/pkg/apis/compute/bucket.go index 9296741580..cb7ec12349 100644 --- a/pkg/apis/compute/bucket.go +++ b/pkg/apis/compute/bucket.go @@ -257,6 +257,9 @@ type BucketPolicyStatement struct { // 解析字段,主账号id:子账号id PrincipalId []string + // map[主账号id:子账号id]子账号name + PrincipalNames map[string]string + // Read|ReadWrite|FullControl CannedAction string // 资源路径 diff --git a/pkg/cloudprovider/objectstore.go b/pkg/cloudprovider/objectstore.go index 71427b4ae5..4806479ca1 100644 --- a/pkg/cloudprovider/objectstore.go +++ b/pkg/cloudprovider/objectstore.go @@ -132,6 +132,8 @@ type SBucketPolicyStatement struct { // 解析字段,主账号id:子账号id PrincipalId []string + // map[主账号id:子账号id]子账号名称 + PrincipalNames map[string]string // Read|ReadWrite|FullControl CannedAction string // 资源路径 diff --git a/pkg/compute/models/buckets.go b/pkg/compute/models/buckets.go index f52dbb750f..08ff7a49ab 100644 --- a/pkg/compute/models/buckets.go +++ b/pkg/compute/models/buckets.go @@ -1585,15 +1585,16 @@ func (bucket *SBucket) GetDetailsPolicy( } for i := range policyStatements { policy.Data = append(policy.Data, api.BucketPolicyStatement{ - Principal: policyStatements[i].Principal, - Action: policyStatements[i].Action, - Effect: policyStatements[i].Effect, - Resource: policyStatements[i].Resource, - Condition: policyStatements[i].Condition, - PrincipalId: policyStatements[i].PrincipalId, - CannedAction: policyStatements[i].CannedAction, - ResourcePath: policyStatements[i].ResourcePath, - Id: policyStatements[i].Id, + Principal: policyStatements[i].Principal, + Action: policyStatements[i].Action, + Effect: policyStatements[i].Effect, + Resource: policyStatements[i].Resource, + Condition: policyStatements[i].Condition, + PrincipalId: policyStatements[i].PrincipalId, + PrincipalNames: policyStatements[i].PrincipalNames, + CannedAction: policyStatements[i].CannedAction, + ResourcePath: policyStatements[i].ResourcePath, + Id: policyStatements[i].Id, }) } return policy, nil diff --git a/pkg/multicloud/qcloud/bucket.go b/pkg/multicloud/qcloud/bucket.go index a0d4a3b096..771c29b0c7 100644 --- a/pkg/multicloud/qcloud/bucket.go +++ b/pkg/multicloud/qcloud/bucket.go @@ -892,20 +892,28 @@ func (b *SBucket) GetPolicy() ([]cloudprovider.SBucketPolicyStatement, error) { policyOptions := []cloudprovider.SBucketPolicyStatement{} coscli, err := b.region.GetCosClient(b) if err != nil { - log.Errorf("GetCosClient fail %s", err) - return nil, errors.Wrap(err, "b.region.GetCosClient(b)") + return nil, errors.Wrap(err, "GetCosClient") } result, _, err := coscli.Bucket.GetPolicy(context.Background()) if err != nil { if strings.Contains(err.Error(), "404") { return nil, nil } - log.Errorf("coscli.Bucket.GetACL fail %s", err) - return nil, errors.Wrap(err, "coscli.Bucket.GetPolicy(context.Background())") + return nil, errors.Wrap(err, "GetPolicy") + } + + users, err := b.region.client.GetICloudusers() + if err != nil { + return nil, errors.Wrapf(err, "GetICloudusers") + } + + userMaps := map[string]string{} + for i := range users { + userMaps[fmt.Sprintf("%s:%s", b.region.client.ownerName, users[i].GetGlobalId())] = users[i].GetName() } for i := range result.Statement { - policyOptions = append(policyOptions, cloudprovider.SBucketPolicyStatement{ + policyOption := cloudprovider.SBucketPolicyStatement{ Principal: result.Statement[i].Principal, Action: result.Statement[i].Action, Effect: result.Statement[i].Effect, @@ -916,7 +924,15 @@ func (b *SBucket) GetPolicy() ([]cloudprovider.SBucketPolicyStatement, error) { CannedAction: getCannedAction(result.Statement[i].Action), ResourcePath: getQcsResourcePath(result.Statement[i].Resource), Id: strconv.Itoa(i), - }) + } + policyOption.PrincipalNames = func() map[string]string { + ret := map[string]string{} + for _, id := range policyOption.PrincipalId { + ret[id], _ = userMaps[id] + } + return ret + }() + policyOptions = append(policyOptions, policyOption) } return policyOptions, nil } @@ -924,16 +940,14 @@ func (b *SBucket) GetPolicy() ([]cloudprovider.SBucketPolicyStatement, error) { func (b *SBucket) SetPolicy(policy cloudprovider.SBucketPolicyStatementInput) error { coscli, err := b.region.GetCosClient(b) if err != nil { - log.Errorf("GetCosClient fail %s", err) - return nil + return errors.Wrapf(err, "GetCosClient") } opts := cos.BucketPutPolicyOptions{} opts.Version = "2.0" oldOpts, _, err := coscli.Bucket.GetPolicy(context.Background()) if err != nil { if !strings.Contains(err.Error(), "404") { - log.Errorf("coscli.Bucket.GetACL fail %s", err) - return errors.Wrap(err, "coscli.Bucket.GetPolicy(context.Background())") + return errors.Wrap(err, "GetPolicy") } } if len(oldOpts.Statement) > 0 { @@ -1000,7 +1014,7 @@ func (b *SBucket) SetPolicy(policy cloudprovider.SBucketPolicyStatementInput) er if policy.CannedAction == "ReadWrite" { newStatement.Action = cannedReadWriteActions[:] } - opts.Statement = append(opts.Statement, newStatement) + opts.Statement = append([]cos.BucketStatement{newStatement}, opts.Statement...) _, err = coscli.Bucket.PutPolicy(context.Background(), &opts) if err != nil {