iam.go 3.8 KB

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