acl.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  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 storage
  15. import (
  16. "fmt"
  17. "net/http"
  18. "reflect"
  19. "golang.org/x/net/context"
  20. "google.golang.org/api/googleapi"
  21. raw "google.golang.org/api/storage/v1"
  22. )
  23. // ACLRole is the level of access to grant.
  24. type ACLRole string
  25. const (
  26. RoleOwner ACLRole = "OWNER"
  27. RoleReader ACLRole = "READER"
  28. RoleWriter ACLRole = "WRITER"
  29. )
  30. // ACLEntity refers to a user or group.
  31. // They are sometimes referred to as grantees.
  32. //
  33. // It could be in the form of:
  34. // "user-<userId>", "user-<email>", "group-<groupId>", "group-<email>",
  35. // "domain-<domain>" and "project-team-<projectId>".
  36. //
  37. // Or one of the predefined constants: AllUsers, AllAuthenticatedUsers.
  38. type ACLEntity string
  39. const (
  40. AllUsers ACLEntity = "allUsers"
  41. AllAuthenticatedUsers ACLEntity = "allAuthenticatedUsers"
  42. )
  43. // ACLRule represents a grant for a role to an entity (user, group or team) for a Google Cloud Storage object or bucket.
  44. type ACLRule struct {
  45. Entity ACLEntity
  46. Role ACLRole
  47. }
  48. // ACLHandle provides operations on an access control list for a Google Cloud Storage bucket or object.
  49. type ACLHandle struct {
  50. c *Client
  51. bucket string
  52. object string
  53. isDefault bool
  54. userProject string // for requester-pays buckets
  55. }
  56. // Delete permanently deletes the ACL entry for the given entity.
  57. func (a *ACLHandle) Delete(ctx context.Context, entity ACLEntity) error {
  58. if a.object != "" {
  59. return a.objectDelete(ctx, entity)
  60. }
  61. if a.isDefault {
  62. return a.bucketDefaultDelete(ctx, entity)
  63. }
  64. return a.bucketDelete(ctx, entity)
  65. }
  66. // Set sets the permission level for the given entity.
  67. func (a *ACLHandle) Set(ctx context.Context, entity ACLEntity, role ACLRole) error {
  68. if a.object != "" {
  69. return a.objectSet(ctx, entity, role, false)
  70. }
  71. if a.isDefault {
  72. return a.objectSet(ctx, entity, role, true)
  73. }
  74. return a.bucketSet(ctx, entity, role)
  75. }
  76. // List retrieves ACL entries.
  77. func (a *ACLHandle) List(ctx context.Context) ([]ACLRule, error) {
  78. if a.object != "" {
  79. return a.objectList(ctx)
  80. }
  81. if a.isDefault {
  82. return a.bucketDefaultList(ctx)
  83. }
  84. return a.bucketList(ctx)
  85. }
  86. func (a *ACLHandle) bucketDefaultList(ctx context.Context) ([]ACLRule, error) {
  87. var acls *raw.ObjectAccessControls
  88. var err error
  89. err = runWithRetry(ctx, func() error {
  90. req := a.c.raw.DefaultObjectAccessControls.List(a.bucket)
  91. a.configureCall(req, ctx)
  92. acls, err = req.Do()
  93. return err
  94. })
  95. if err != nil {
  96. return nil, fmt.Errorf("storage: error listing default object ACL for bucket %q: %v", a.bucket, err)
  97. }
  98. return toACLRules(acls.Items), nil
  99. }
  100. func (a *ACLHandle) bucketDefaultDelete(ctx context.Context, entity ACLEntity) error {
  101. err := runWithRetry(ctx, func() error {
  102. req := a.c.raw.DefaultObjectAccessControls.Delete(a.bucket, string(entity))
  103. a.configureCall(req, ctx)
  104. return req.Do()
  105. })
  106. if err != nil {
  107. return fmt.Errorf("storage: error deleting default ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  108. }
  109. return nil
  110. }
  111. func (a *ACLHandle) bucketList(ctx context.Context) ([]ACLRule, error) {
  112. var acls *raw.BucketAccessControls
  113. var err error
  114. err = runWithRetry(ctx, func() error {
  115. req := a.c.raw.BucketAccessControls.List(a.bucket)
  116. a.configureCall(req, ctx)
  117. acls, err = req.Do()
  118. return err
  119. })
  120. if err != nil {
  121. return nil, fmt.Errorf("storage: error listing bucket ACL for bucket %q: %v", a.bucket, err)
  122. }
  123. r := make([]ACLRule, len(acls.Items))
  124. for i, v := range acls.Items {
  125. r[i].Entity = ACLEntity(v.Entity)
  126. r[i].Role = ACLRole(v.Role)
  127. }
  128. return r, nil
  129. }
  130. func (a *ACLHandle) bucketSet(ctx context.Context, entity ACLEntity, role ACLRole) error {
  131. acl := &raw.BucketAccessControl{
  132. Bucket: a.bucket,
  133. Entity: string(entity),
  134. Role: string(role),
  135. }
  136. err := runWithRetry(ctx, func() error {
  137. req := a.c.raw.BucketAccessControls.Update(a.bucket, string(entity), acl)
  138. a.configureCall(req, ctx)
  139. _, err := req.Do()
  140. return err
  141. })
  142. if err != nil {
  143. return fmt.Errorf("storage: error updating bucket ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  144. }
  145. return nil
  146. }
  147. func (a *ACLHandle) bucketDelete(ctx context.Context, entity ACLEntity) error {
  148. err := runWithRetry(ctx, func() error {
  149. req := a.c.raw.BucketAccessControls.Delete(a.bucket, string(entity))
  150. a.configureCall(req, ctx)
  151. return req.Do()
  152. })
  153. if err != nil {
  154. return fmt.Errorf("storage: error deleting bucket ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  155. }
  156. return nil
  157. }
  158. func (a *ACLHandle) objectList(ctx context.Context) ([]ACLRule, error) {
  159. var acls *raw.ObjectAccessControls
  160. var err error
  161. err = runWithRetry(ctx, func() error {
  162. req := a.c.raw.ObjectAccessControls.List(a.bucket, a.object)
  163. a.configureCall(req, ctx)
  164. acls, err = req.Do()
  165. return err
  166. })
  167. if err != nil {
  168. return nil, fmt.Errorf("storage: error listing object ACL for bucket %q, file %q: %v", a.bucket, a.object, err)
  169. }
  170. return toACLRules(acls.Items), nil
  171. }
  172. func (a *ACLHandle) objectSet(ctx context.Context, entity ACLEntity, role ACLRole, isBucketDefault bool) error {
  173. type setRequest interface {
  174. Do(opts ...googleapi.CallOption) (*raw.ObjectAccessControl, error)
  175. Header() http.Header
  176. }
  177. acl := &raw.ObjectAccessControl{
  178. Bucket: a.bucket,
  179. Entity: string(entity),
  180. Role: string(role),
  181. }
  182. var req setRequest
  183. if isBucketDefault {
  184. req = a.c.raw.DefaultObjectAccessControls.Update(a.bucket, string(entity), acl)
  185. } else {
  186. req = a.c.raw.ObjectAccessControls.Update(a.bucket, a.object, string(entity), acl)
  187. }
  188. a.configureCall(req, ctx)
  189. err := runWithRetry(ctx, func() error {
  190. _, err := req.Do()
  191. return err
  192. })
  193. if err != nil {
  194. if isBucketDefault {
  195. return fmt.Errorf("storage: error updating default ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  196. } else {
  197. return fmt.Errorf("storage: error updating object ACL entry for bucket %q, object %q, entity %q: %v", a.bucket, a.object, entity, err)
  198. }
  199. }
  200. return nil
  201. }
  202. func (a *ACLHandle) objectDelete(ctx context.Context, entity ACLEntity) error {
  203. err := runWithRetry(ctx, func() error {
  204. req := a.c.raw.ObjectAccessControls.Delete(a.bucket, a.object, string(entity))
  205. a.configureCall(req, ctx)
  206. return req.Do()
  207. })
  208. if err != nil {
  209. return fmt.Errorf("storage: error deleting object ACL entry for bucket %q, file %q, entity %q: %v", a.bucket, a.object, entity, err)
  210. }
  211. return nil
  212. }
  213. func (a *ACLHandle) configureCall(call interface {
  214. Header() http.Header
  215. }, ctx context.Context) {
  216. vc := reflect.ValueOf(call)
  217. vc.MethodByName("Context").Call([]reflect.Value{reflect.ValueOf(ctx)})
  218. if a.userProject != "" {
  219. vc.MethodByName("UserProject").Call([]reflect.Value{reflect.ValueOf(a.userProject)})
  220. }
  221. setClientHeader(call.Header())
  222. }
  223. func toACLRules(items []*raw.ObjectAccessControl) []ACLRule {
  224. r := make([]ACLRule, 0, len(items))
  225. for _, item := range items {
  226. r = append(r, ACLRule{Entity: ACLEntity(item.Entity), Role: ACLRole(item.Role)})
  227. }
  228. return r
  229. }