acl.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "golang.org/x/net/context"
  18. raw "google.golang.org/api/storage/v1"
  19. )
  20. // ACLRole is the level of access to grant.
  21. type ACLRole string
  22. const (
  23. RoleOwner ACLRole = "OWNER"
  24. RoleReader ACLRole = "READER"
  25. )
  26. // ACLEntity refers to a user or group.
  27. // They are sometimes referred to as grantees.
  28. //
  29. // It could be in the form of:
  30. // "user-<userId>", "user-<email>", "group-<groupId>", "group-<email>",
  31. // "domain-<domain>" and "project-team-<projectId>".
  32. //
  33. // Or one of the predefined constants: AllUsers, AllAuthenticatedUsers.
  34. type ACLEntity string
  35. const (
  36. AllUsers ACLEntity = "allUsers"
  37. AllAuthenticatedUsers ACLEntity = "allAuthenticatedUsers"
  38. )
  39. // ACLRule represents a grant for a role to an entity (user, group or team) for a Google Cloud Storage object or bucket.
  40. type ACLRule struct {
  41. Entity ACLEntity
  42. Role ACLRole
  43. }
  44. // ACLHandle provides operations on an access control list for a Google Cloud Storage bucket or object.
  45. type ACLHandle struct {
  46. c *Client
  47. bucket string
  48. object string
  49. isDefault bool
  50. }
  51. // Delete permanently deletes the ACL entry for the given entity.
  52. func (a *ACLHandle) Delete(ctx context.Context, entity ACLEntity) error {
  53. if a.object != "" {
  54. return a.objectDelete(ctx, entity)
  55. }
  56. if a.isDefault {
  57. return a.bucketDefaultDelete(ctx, entity)
  58. }
  59. return a.bucketDelete(ctx, entity)
  60. }
  61. // Set sets the permission level for the given entity.
  62. func (a *ACLHandle) Set(ctx context.Context, entity ACLEntity, role ACLRole) error {
  63. if a.object != "" {
  64. return a.objectSet(ctx, entity, role)
  65. }
  66. if a.isDefault {
  67. return a.bucketDefaultSet(ctx, entity, role)
  68. }
  69. return a.bucketSet(ctx, entity, role)
  70. }
  71. // List retrieves ACL entries.
  72. func (a *ACLHandle) List(ctx context.Context) ([]ACLRule, error) {
  73. if a.object != "" {
  74. return a.objectList(ctx)
  75. }
  76. if a.isDefault {
  77. return a.bucketDefaultList(ctx)
  78. }
  79. return a.bucketList(ctx)
  80. }
  81. func (a *ACLHandle) bucketDefaultList(ctx context.Context) ([]ACLRule, error) {
  82. acls, err := a.c.raw.DefaultObjectAccessControls.List(a.bucket).Context(ctx).Do()
  83. if err != nil {
  84. return nil, fmt.Errorf("storage: error listing default object ACL for bucket %q: %v", a.bucket, err)
  85. }
  86. r := make([]ACLRule, 0, len(acls.Items))
  87. for _, v := range acls.Items {
  88. if m, ok := v.(map[string]interface{}); ok {
  89. entity, ok1 := m["entity"].(string)
  90. role, ok2 := m["role"].(string)
  91. if ok1 && ok2 {
  92. r = append(r, ACLRule{Entity: ACLEntity(entity), Role: ACLRole(role)})
  93. }
  94. }
  95. }
  96. return r, nil
  97. }
  98. func (a *ACLHandle) bucketDefaultSet(ctx context.Context, entity ACLEntity, role ACLRole) error {
  99. acl := &raw.ObjectAccessControl{
  100. Bucket: a.bucket,
  101. Entity: string(entity),
  102. Role: string(role),
  103. }
  104. _, err := a.c.raw.DefaultObjectAccessControls.Update(a.bucket, string(entity), acl).Context(ctx).Do()
  105. if err != nil {
  106. return fmt.Errorf("storage: error updating default ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  107. }
  108. return nil
  109. }
  110. func (a *ACLHandle) bucketDefaultDelete(ctx context.Context, entity ACLEntity) error {
  111. err := a.c.raw.DefaultObjectAccessControls.Delete(a.bucket, string(entity)).Context(ctx).Do()
  112. if err != nil {
  113. return fmt.Errorf("storage: error deleting default ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  114. }
  115. return nil
  116. }
  117. func (a *ACLHandle) bucketList(ctx context.Context) ([]ACLRule, error) {
  118. acls, err := a.c.raw.BucketAccessControls.List(a.bucket).Context(ctx).Do()
  119. if err != nil {
  120. return nil, fmt.Errorf("storage: error listing bucket ACL for bucket %q: %v", a.bucket, err)
  121. }
  122. r := make([]ACLRule, len(acls.Items))
  123. for i, v := range acls.Items {
  124. r[i].Entity = ACLEntity(v.Entity)
  125. r[i].Role = ACLRole(v.Role)
  126. }
  127. return r, nil
  128. }
  129. func (a *ACLHandle) bucketSet(ctx context.Context, entity ACLEntity, role ACLRole) error {
  130. acl := &raw.BucketAccessControl{
  131. Bucket: a.bucket,
  132. Entity: string(entity),
  133. Role: string(role),
  134. }
  135. _, err := a.c.raw.BucketAccessControls.Update(a.bucket, string(entity), acl).Context(ctx).Do()
  136. if err != nil {
  137. return fmt.Errorf("storage: error updating bucket ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  138. }
  139. return nil
  140. }
  141. func (a *ACLHandle) bucketDelete(ctx context.Context, entity ACLEntity) error {
  142. err := a.c.raw.BucketAccessControls.Delete(a.bucket, string(entity)).Context(ctx).Do()
  143. if err != nil {
  144. return fmt.Errorf("storage: error deleting bucket ACL entry for bucket %q, entity %q: %v", a.bucket, entity, err)
  145. }
  146. return nil
  147. }
  148. func (a *ACLHandle) objectList(ctx context.Context) ([]ACLRule, error) {
  149. acls, err := a.c.raw.ObjectAccessControls.List(a.bucket, a.object).Context(ctx).Do()
  150. if err != nil {
  151. return nil, fmt.Errorf("storage: error listing object ACL for bucket %q, file %q: %v", a.bucket, a.object, err)
  152. }
  153. r := make([]ACLRule, 0, len(acls.Items))
  154. for _, v := range acls.Items {
  155. if m, ok := v.(map[string]interface{}); ok {
  156. entity, ok1 := m["entity"].(string)
  157. role, ok2 := m["role"].(string)
  158. if ok1 && ok2 {
  159. r = append(r, ACLRule{Entity: ACLEntity(entity), Role: ACLRole(role)})
  160. }
  161. }
  162. }
  163. return r, nil
  164. }
  165. func (a *ACLHandle) objectSet(ctx context.Context, entity ACLEntity, role ACLRole) error {
  166. acl := &raw.ObjectAccessControl{
  167. Bucket: a.bucket,
  168. Entity: string(entity),
  169. Role: string(role),
  170. }
  171. _, err := a.c.raw.ObjectAccessControls.Update(a.bucket, a.object, string(entity), acl).Context(ctx).Do()
  172. if err != nil {
  173. return fmt.Errorf("storage: error updating object ACL entry for bucket %q, file %q, entity %q: %v", a.bucket, a.object, entity, err)
  174. }
  175. return nil
  176. }
  177. func (a *ACLHandle) objectDelete(ctx context.Context, entity ACLEntity) error {
  178. err := a.c.raw.ObjectAccessControls.Delete(a.bucket, a.object, string(entity)).Context(ctx).Do()
  179. if err != nil {
  180. return fmt.Errorf("storage: error deleting object ACL entry for bucket %q, file %q, entity %q: %v", a.bucket, a.object, entity, err)
  181. }
  182. return nil
  183. }