auth.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2016 The etcd Authors
  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 clientv3
  15. import (
  16. "fmt"
  17. "strings"
  18. "github.com/coreos/etcd/auth/authpb"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "golang.org/x/net/context"
  22. "google.golang.org/grpc"
  23. )
  24. type (
  25. AuthEnableResponse pb.AuthEnableResponse
  26. AuthDisableResponse pb.AuthDisableResponse
  27. AuthenticateResponse pb.AuthenticateResponse
  28. AuthUserAddResponse pb.AuthUserAddResponse
  29. AuthUserDeleteResponse pb.AuthUserDeleteResponse
  30. AuthUserChangePasswordResponse pb.AuthUserChangePasswordResponse
  31. AuthUserGrantResponse pb.AuthUserGrantResponse
  32. AuthUserGetResponse pb.AuthUserGetResponse
  33. AuthUserRevokeRoleResponse pb.AuthUserRevokeRoleResponse
  34. AuthRoleAddResponse pb.AuthRoleAddResponse
  35. AuthRoleGrantResponse pb.AuthRoleGrantResponse
  36. AuthRoleGetResponse pb.AuthRoleGetResponse
  37. AuthRoleRevokePermissionResponse pb.AuthRoleRevokePermissionResponse
  38. AuthRoleDeleteResponse pb.AuthRoleDeleteResponse
  39. PermissionType authpb.Permission_Type
  40. )
  41. const (
  42. PermRead = authpb.READ
  43. PermWrite = authpb.WRITE
  44. PermReadWrite = authpb.READWRITE
  45. )
  46. type Auth interface {
  47. // AuthEnable enables auth of an etcd cluster.
  48. AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
  49. // AuthDisable disables auth of an etcd cluster.
  50. AuthDisable(ctx context.Context) (*AuthDisableResponse, error)
  51. // UserAdd adds a new user to an etcd cluster.
  52. UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
  53. // UserDelete deletes a user from an etcd cluster.
  54. UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
  55. // UserChangePassword changes a password of a user.
  56. UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
  57. // UserGrant grants a role to a user.
  58. UserGrant(ctx context.Context, user string, role string) (*AuthUserGrantResponse, error)
  59. // UserGet gets a detailed information of a user.
  60. UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error)
  61. // UserRevokeRole revokes a role of a user.
  62. UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error)
  63. // RoleAdd adds a new role to an etcd cluster.
  64. RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error)
  65. // RoleGrant grants a permission to a role.
  66. RoleGrant(ctx context.Context, name string, key string, permType PermissionType) (*AuthRoleGrantResponse, error)
  67. // RoleGet gets a detailed information of a role.
  68. RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error)
  69. // RoleRevokePermission revokes a key from a user.
  70. RoleRevokePermission(ctx context.Context, role string, key string) (*AuthRoleRevokePermissionResponse, error)
  71. // RoleDelete deletes a role.
  72. RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error)
  73. }
  74. type auth struct {
  75. c *Client
  76. conn *grpc.ClientConn // conn in-use
  77. remote pb.AuthClient
  78. }
  79. func NewAuth(c *Client) Auth {
  80. conn := c.ActiveConnection()
  81. return &auth{
  82. conn: c.ActiveConnection(),
  83. remote: pb.NewAuthClient(conn),
  84. c: c,
  85. }
  86. }
  87. func (auth *auth) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
  88. resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{})
  89. return (*AuthEnableResponse)(resp), rpctypes.Error(err)
  90. }
  91. func (auth *auth) AuthDisable(ctx context.Context) (*AuthDisableResponse, error) {
  92. resp, err := auth.remote.AuthDisable(ctx, &pb.AuthDisableRequest{})
  93. return (*AuthDisableResponse)(resp), rpctypes.Error(err)
  94. }
  95. func (auth *auth) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
  96. resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password})
  97. return (*AuthUserAddResponse)(resp), rpctypes.Error(err)
  98. }
  99. func (auth *auth) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
  100. resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name})
  101. return (*AuthUserDeleteResponse)(resp), rpctypes.Error(err)
  102. }
  103. func (auth *auth) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
  104. resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password})
  105. return (*AuthUserChangePasswordResponse)(resp), rpctypes.Error(err)
  106. }
  107. func (auth *auth) UserGrant(ctx context.Context, user string, role string) (*AuthUserGrantResponse, error) {
  108. resp, err := auth.remote.UserGrant(ctx, &pb.AuthUserGrantRequest{User: user, Role: role})
  109. return (*AuthUserGrantResponse)(resp), rpctypes.Error(err)
  110. }
  111. func (auth *auth) UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error) {
  112. resp, err := auth.remote.UserGet(ctx, &pb.AuthUserGetRequest{Name: name})
  113. return (*AuthUserGetResponse)(resp), rpctypes.Error(err)
  114. }
  115. func (auth *auth) UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error) {
  116. resp, err := auth.remote.UserRevokeRole(ctx, &pb.AuthUserRevokeRoleRequest{Name: name, Role: role})
  117. return (*AuthUserRevokeRoleResponse)(resp), rpctypes.Error(err)
  118. }
  119. func (auth *auth) RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error) {
  120. resp, err := auth.remote.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: name})
  121. return (*AuthRoleAddResponse)(resp), rpctypes.Error(err)
  122. }
  123. func (auth *auth) RoleGrant(ctx context.Context, name string, key string, permType PermissionType) (*AuthRoleGrantResponse, error) {
  124. perm := &authpb.Permission{
  125. Key: []byte(key),
  126. PermType: authpb.Permission_Type(permType),
  127. }
  128. resp, err := auth.remote.RoleGrant(ctx, &pb.AuthRoleGrantRequest{Name: name, Perm: perm})
  129. return (*AuthRoleGrantResponse)(resp), rpctypes.Error(err)
  130. }
  131. func (auth *auth) RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error) {
  132. resp, err := auth.remote.RoleGet(ctx, &pb.AuthRoleGetRequest{Role: role})
  133. return (*AuthRoleGetResponse)(resp), rpctypes.Error(err)
  134. }
  135. func (auth *auth) RoleRevokePermission(ctx context.Context, role string, key string) (*AuthRoleRevokePermissionResponse, error) {
  136. resp, err := auth.remote.RoleRevokePermission(ctx, &pb.AuthRoleRevokePermissionRequest{Role: role, Key: key})
  137. return (*AuthRoleRevokePermissionResponse)(resp), rpctypes.Error(err)
  138. }
  139. func (auth *auth) RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error) {
  140. resp, err := auth.remote.RoleDelete(ctx, &pb.AuthRoleDeleteRequest{Role: role})
  141. return (*AuthRoleDeleteResponse)(resp), rpctypes.Error(err)
  142. }
  143. func StrToPermissionType(s string) (PermissionType, error) {
  144. val, ok := authpb.Permission_Type_value[strings.ToUpper(s)]
  145. if ok {
  146. return PermissionType(val), nil
  147. }
  148. return PermissionType(-1), fmt.Errorf("invalid permission type: %s", s)
  149. }
  150. type authenticator struct {
  151. conn *grpc.ClientConn // conn in-use
  152. remote pb.AuthClient
  153. }
  154. func (auth *authenticator) authenticate(ctx context.Context, name string, password string) (*AuthenticateResponse, error) {
  155. resp, err := auth.remote.Authenticate(ctx, &pb.AuthenticateRequest{Name: name, Password: password})
  156. return (*AuthenticateResponse)(resp), rpctypes.Error(err)
  157. }
  158. func (auth *authenticator) close() {
  159. auth.conn.Close()
  160. }
  161. func newAuthenticator(endpoint string, opts []grpc.DialOption) (*authenticator, error) {
  162. conn, err := grpc.Dial(endpoint, opts...)
  163. if err != nil {
  164. return nil, err
  165. }
  166. return &authenticator{
  167. conn: conn,
  168. remote: pb.NewAuthClient(conn),
  169. }, nil
  170. }