You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

131 lines
3.4 KiB

  1. // Copyright 2017 Google LLC
  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. "context"
  17. "cloud.google.com/go/iam"
  18. "cloud.google.com/go/internal/trace"
  19. raw "google.golang.org/api/storage/v1"
  20. iampb "google.golang.org/genproto/googleapis/iam/v1"
  21. )
  22. // IAM provides access to IAM access control for the bucket.
  23. func (b *BucketHandle) IAM() *iam.Handle {
  24. return iam.InternalNewHandleClient(&iamClient{
  25. raw: b.c.raw,
  26. userProject: b.userProject,
  27. }, b.name)
  28. }
  29. // iamClient implements the iam.client interface.
  30. type iamClient struct {
  31. raw *raw.Service
  32. userProject string
  33. }
  34. func (c *iamClient) Get(ctx context.Context, resource string) (p *iampb.Policy, err error) {
  35. ctx = trace.StartSpan(ctx, "cloud.google.com/go/storage.IAM.Get")
  36. defer func() { trace.EndSpan(ctx, err) }()
  37. call := c.raw.Buckets.GetIamPolicy(resource)
  38. setClientHeader(call.Header())
  39. if c.userProject != "" {
  40. call.UserProject(c.userProject)
  41. }
  42. var rp *raw.Policy
  43. err = runWithRetry(ctx, func() error {
  44. rp, err = call.Context(ctx).Do()
  45. return err
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. return iamFromStoragePolicy(rp), nil
  51. }
  52. func (c *iamClient) Set(ctx context.Context, resource string, p *iampb.Policy) (err error) {
  53. ctx = trace.StartSpan(ctx, "cloud.google.com/go/storage.IAM.Set")
  54. defer func() { trace.EndSpan(ctx, err) }()
  55. rp := iamToStoragePolicy(p)
  56. call := c.raw.Buckets.SetIamPolicy(resource, rp)
  57. setClientHeader(call.Header())
  58. if c.userProject != "" {
  59. call.UserProject(c.userProject)
  60. }
  61. return runWithRetry(ctx, func() error {
  62. _, err := call.Context(ctx).Do()
  63. return err
  64. })
  65. }
  66. func (c *iamClient) Test(ctx context.Context, resource string, perms []string) (permissions []string, err error) {
  67. ctx = trace.StartSpan(ctx, "cloud.google.com/go/storage.IAM.Test")
  68. defer func() { trace.EndSpan(ctx, err) }()
  69. call := c.raw.Buckets.TestIamPermissions(resource, perms)
  70. setClientHeader(call.Header())
  71. if c.userProject != "" {
  72. call.UserProject(c.userProject)
  73. }
  74. var res *raw.TestIamPermissionsResponse
  75. err = runWithRetry(ctx, func() error {
  76. res, err = call.Context(ctx).Do()
  77. return err
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. return res.Permissions, nil
  83. }
  84. func iamToStoragePolicy(ip *iampb.Policy) *raw.Policy {
  85. return &raw.Policy{
  86. Bindings: iamToStorageBindings(ip.Bindings),
  87. Etag: string(ip.Etag),
  88. }
  89. }
  90. func iamToStorageBindings(ibs []*iampb.Binding) []*raw.PolicyBindings {
  91. var rbs []*raw.PolicyBindings
  92. for _, ib := range ibs {
  93. rbs = append(rbs, &raw.PolicyBindings{
  94. Role: ib.Role,
  95. Members: ib.Members,
  96. })
  97. }
  98. return rbs
  99. }
  100. func iamFromStoragePolicy(rp *raw.Policy) *iampb.Policy {
  101. return &iampb.Policy{
  102. Bindings: iamFromStorageBindings(rp.Bindings),
  103. Etag: []byte(rp.Etag),
  104. }
  105. }
  106. func iamFromStorageBindings(rbs []*raw.PolicyBindings) []*iampb.Binding {
  107. var ibs []*iampb.Binding
  108. for _, rb := range rbs {
  109. ibs = append(ibs, &iampb.Binding{
  110. Role: rb.Role,
  111. Members: rb.Members,
  112. })
  113. }
  114. return ibs
  115. }