auth.go 9.0 KB

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