bucketacl.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 Yunion
  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. package google
  15. import (
  16. "fmt"
  17. "net/url"
  18. "cloud.google.com/go/storage"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. )
  23. type GCSAcl struct {
  24. Kind string
  25. Id string
  26. SelfLink string
  27. Bucket string
  28. Entity string
  29. Role string
  30. Etag string
  31. ProjectTeam map[string]string
  32. }
  33. func (region *SRegion) GetBucketAcl(bucket string) ([]GCSAcl, error) {
  34. resource := fmt.Sprintf("b/%s/acl", bucket)
  35. acls := []GCSAcl{}
  36. err := region.StorageListAll(resource, map[string]string{}, &acls)
  37. if err != nil {
  38. return nil, errors.Wrapf(err, "StorageListAll(%s)", resource)
  39. }
  40. return acls, nil
  41. }
  42. func (region *SRegion) SetObjectAcl(bucket, object string, cannedAcl cloudprovider.TBucketACLType) error {
  43. resource := fmt.Sprintf("b/%s/o/%s", bucket, url.PathEscape(object))
  44. acl := map[string]string{}
  45. switch cannedAcl {
  46. case cloudprovider.ACLPrivate:
  47. acls, err := region.GetObjectAcl(bucket, object)
  48. if err != nil {
  49. return errors.Wrap(err, "GetObjectAcl")
  50. }
  51. for _, _acl := range acls {
  52. if _acl.Entity == string(storage.AllUsers) || _acl.Entity == string(storage.AllAuthenticatedUsers) {
  53. resource := fmt.Sprintf("b/%s/o/%s/acl/%s", bucket, url.PathEscape(object), _acl.Entity)
  54. err = region.StorageDelete(resource)
  55. if err != nil {
  56. return errors.Wrapf(err, "StorageDelete(%s)", resource)
  57. }
  58. }
  59. }
  60. return nil
  61. case cloudprovider.ACLAuthRead:
  62. acl["entity"] = "allAuthenticatedUsers"
  63. acl["role"] = "READER"
  64. case cloudprovider.ACLPublicRead:
  65. acl["entity"] = "allUsers"
  66. acl["role"] = "READER"
  67. case cloudprovider.ACLPublicReadWrite:
  68. acl["entity"] = "allUsers"
  69. acl["role"] = "OWNER"
  70. }
  71. body := jsonutils.Marshal(acl)
  72. return region.StorageDo(resource, "acl", nil, body)
  73. }
  74. type BindingCondition struct {
  75. Title string
  76. Description string
  77. Expression string
  78. }
  79. type SBucketBinding struct {
  80. Role string
  81. Members []string
  82. Condition BindingCondition
  83. }
  84. type SBucketIam struct {
  85. Version int
  86. Kind string
  87. ResourceId string
  88. Bindings []SBucketBinding
  89. Etag string
  90. }
  91. func (region *SRegion) GetBucketIam(bucket string) (*SBucketIam, error) {
  92. resource := fmt.Sprintf("b/%s/iam", bucket)
  93. iam := SBucketIam{}
  94. err := region.StorageGet(resource, &iam)
  95. if err != nil {
  96. return nil, errors.Wrapf(err, "StorageListAll(%s)", resource)
  97. }
  98. return &iam, nil
  99. }
  100. func (region *SRegion) SetBucketIam(bucket string, iam *SBucketIam) (*SBucketIam, error) {
  101. resource := fmt.Sprintf("b/%s/iam", bucket)
  102. ret := SBucketIam{}
  103. err := region.StoragePut(resource, jsonutils.Marshal(iam), &ret)
  104. if err != nil {
  105. return nil, errors.Wrapf(err, "StoragePut(%s)", resource)
  106. }
  107. return &ret, nil
  108. }
  109. func (region *SRegion) GetObjectAcl(bucket string, object string) ([]GCSAcl, error) {
  110. resource := fmt.Sprintf("b/%s/o/%s/acl", bucket, url.PathEscape(object))
  111. acls := []GCSAcl{}
  112. err := region.StorageListAll(resource, map[string]string{}, &acls)
  113. if err != nil {
  114. return nil, errors.Wrapf(err, "StorageListAll(%s)", resource)
  115. }
  116. return acls, nil
  117. }