iam.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2016 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 iam supports the resource-specific operations of Google Cloud
  15. // IAM (Identity and Access Management) for the Google Cloud Libraries.
  16. // See https://cloud.google.com/iam for more about IAM.
  17. //
  18. // Users of the Google Cloud Libraries will typically not use this package
  19. // directly. Instead they will begin with some resource that supports IAM, like
  20. // a pubsub topic, and call its IAM method to get a Handle for that resource.
  21. package iam
  22. import (
  23. "golang.org/x/net/context"
  24. pb "google.golang.org/genproto/googleapis/iam/v1"
  25. "google.golang.org/grpc"
  26. )
  27. // client abstracts the IAMPolicy API to allow multiple implementations.
  28. type client interface {
  29. Get(ctx context.Context, resource string) (*pb.Policy, error)
  30. Set(ctx context.Context, resource string, p *pb.Policy) error
  31. Test(ctx context.Context, resource string, perms []string) ([]string, error)
  32. }
  33. // grpcClient implements client for the standard gRPC-based IAMPolicy service.
  34. type grpcClient struct {
  35. c pb.IAMPolicyClient
  36. }
  37. func (g *grpcClient) Get(ctx context.Context, resource string) (*pb.Policy, error) {
  38. proto, err := g.c.GetIamPolicy(ctx, &pb.GetIamPolicyRequest{Resource: resource})
  39. if err != nil {
  40. return nil, err
  41. }
  42. return proto, nil
  43. }
  44. func (g *grpcClient) Set(ctx context.Context, resource string, p *pb.Policy) error {
  45. _, err := g.c.SetIamPolicy(ctx, &pb.SetIamPolicyRequest{
  46. Resource: resource,
  47. Policy: p,
  48. })
  49. return err
  50. }
  51. func (g *grpcClient) Test(ctx context.Context, resource string, perms []string) ([]string, error) {
  52. res, err := g.c.TestIamPermissions(ctx, &pb.TestIamPermissionsRequest{
  53. Resource: resource,
  54. Permissions: perms,
  55. })
  56. if err != nil {
  57. return nil, err
  58. }
  59. return res.Permissions, nil
  60. }
  61. // A Handle provides IAM operations for a resource.
  62. type Handle struct {
  63. c client
  64. resource string
  65. }
  66. // InternalNewHandle is for use by the Google Cloud Libraries only.
  67. //
  68. // InternalNewHandle returns a Handle for resource.
  69. // The conn parameter refers to a server that must support the IAMPolicy service.
  70. func InternalNewHandle(conn *grpc.ClientConn, resource string) *Handle {
  71. return InternalNewHandleClient(&grpcClient{c: pb.NewIAMPolicyClient(conn)}, resource)
  72. }
  73. // InternalNewHandleClient is for use by the Google Cloud Libraries only.
  74. //
  75. // InternalNewHandleClient returns a Handle for resource using the given
  76. // client implementation.
  77. func InternalNewHandleClient(c client, resource string) *Handle {
  78. return &Handle{
  79. c: c,
  80. resource: resource,
  81. }
  82. }
  83. // Policy retrieves the IAM policy for the resource.
  84. func (h *Handle) Policy(ctx context.Context) (*Policy, error) {
  85. proto, err := h.c.Get(ctx, h.resource)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &Policy{InternalProto: proto}, nil
  90. }
  91. // SetPolicy replaces the resource's current policy with the supplied Policy.
  92. //
  93. // If policy was created from a prior call to Get, then the modification will
  94. // only succeed if the policy has not changed since the Get.
  95. func (h *Handle) SetPolicy(ctx context.Context, policy *Policy) error {
  96. return h.c.Set(ctx, h.resource, policy.InternalProto)
  97. }
  98. // TestPermissions returns the subset of permissions that the caller has on the resource.
  99. func (h *Handle) TestPermissions(ctx context.Context, permissions []string) ([]string, error) {
  100. return h.c.Test(ctx, h.resource, permissions)
  101. }
  102. // A RoleName is a name representing a collection of permissions.
  103. type RoleName string
  104. // Common role names.
  105. const (
  106. Owner RoleName = "roles/owner"
  107. Editor RoleName = "roles/editor"
  108. Viewer RoleName = "roles/viewer"
  109. )
  110. const (
  111. // AllUsers is a special member that denotes all users, even unauthenticated ones.
  112. AllUsers = "allUsers"
  113. // AllAuthenticatedUsers is a special member that denotes all authenticated users.
  114. AllAuthenticatedUsers = "allAuthenticatedUsers"
  115. )
  116. // A Policy is a list of Bindings representing roles
  117. // granted to members.
  118. //
  119. // The zero Policy is a valid policy with no bindings.
  120. type Policy struct {
  121. // TODO(jba): when type aliases are available, put Policy into an internal package
  122. // and provide an exported alias here.
  123. // This field is exported for use by the Google Cloud Libraries only.
  124. // It may become unexported in a future release.
  125. InternalProto *pb.Policy
  126. }
  127. // Members returns the list of members with the supplied role.
  128. // The return value should not be modified. Use Add and Remove
  129. // to modify the members of a role.
  130. func (p *Policy) Members(r RoleName) []string {
  131. b := p.binding(r)
  132. if b == nil {
  133. return nil
  134. }
  135. return b.Members
  136. }
  137. // HasRole reports whether member has role r.
  138. func (p *Policy) HasRole(member string, r RoleName) bool {
  139. return memberIndex(member, p.binding(r)) >= 0
  140. }
  141. // Add adds member member to role r if it is not already present.
  142. // A new binding is created if there is no binding for the role.
  143. func (p *Policy) Add(member string, r RoleName) {
  144. b := p.binding(r)
  145. if b == nil {
  146. if p.InternalProto == nil {
  147. p.InternalProto = &pb.Policy{}
  148. }
  149. p.InternalProto.Bindings = append(p.InternalProto.Bindings, &pb.Binding{
  150. Role: string(r),
  151. Members: []string{member},
  152. })
  153. return
  154. }
  155. if memberIndex(member, b) < 0 {
  156. b.Members = append(b.Members, member)
  157. return
  158. }
  159. }
  160. // Remove removes member from role r if it is present.
  161. func (p *Policy) Remove(member string, r RoleName) {
  162. bi := p.bindingIndex(r)
  163. if bi < 0 {
  164. return
  165. }
  166. bindings := p.InternalProto.Bindings
  167. b := bindings[bi]
  168. mi := memberIndex(member, b)
  169. if mi < 0 {
  170. return
  171. }
  172. // Order doesn't matter for bindings or members, so to remove, move the last item
  173. // into the removed spot and shrink the slice.
  174. if len(b.Members) == 1 {
  175. // Remove binding.
  176. last := len(bindings) - 1
  177. bindings[bi] = bindings[last]
  178. bindings[last] = nil
  179. p.InternalProto.Bindings = bindings[:last]
  180. return
  181. }
  182. // Remove member.
  183. // TODO(jba): worry about multiple copies of m?
  184. last := len(b.Members) - 1
  185. b.Members[mi] = b.Members[last]
  186. b.Members[last] = ""
  187. b.Members = b.Members[:last]
  188. }
  189. // Roles returns the names of all the roles that appear in the Policy.
  190. func (p *Policy) Roles() []RoleName {
  191. if p.InternalProto == nil {
  192. return nil
  193. }
  194. var rns []RoleName
  195. for _, b := range p.InternalProto.Bindings {
  196. rns = append(rns, RoleName(b.Role))
  197. }
  198. return rns
  199. }
  200. // binding returns the Binding for the suppied role, or nil if there isn't one.
  201. func (p *Policy) binding(r RoleName) *pb.Binding {
  202. i := p.bindingIndex(r)
  203. if i < 0 {
  204. return nil
  205. }
  206. return p.InternalProto.Bindings[i]
  207. }
  208. func (p *Policy) bindingIndex(r RoleName) int {
  209. if p.InternalProto == nil {
  210. return -1
  211. }
  212. for i, b := range p.InternalProto.Bindings {
  213. if b.Role == string(r) {
  214. return i
  215. }
  216. }
  217. return -1
  218. }
  219. // memberIndex returns the index of m in b's Members, or -1 if not found.
  220. func memberIndex(m string, b *pb.Binding) int {
  221. if b == nil {
  222. return -1
  223. }
  224. for i, mm := range b.Members {
  225. if mm == m {
  226. return i
  227. }
  228. }
  229. return -1
  230. }