iam.go 2.8 KB

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