auth.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. "context"
  17. "fmt"
  18. "strings"
  19. "go.etcd.io/etcd/auth/authpb"
  20. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  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 UserAddOptions authpb.UserAddOptions
  49. type Auth interface {
  50. // AuthEnable enables auth of an etcd cluster.
  51. AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
  52. // AuthDisable disables auth of an etcd cluster.
  53. AuthDisable(ctx context.Context) (*AuthDisableResponse, error)
  54. // UserAdd adds a new user to an etcd cluster.
  55. UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
  56. // UserAddWithOptions adds a new user to an etcd cluster with some options.
  57. UserAddWithOptions(ctx context.Context, name string, password string, opt *UserAddOptions) (*AuthUserAddResponse, error)
  58. // UserDelete deletes a user from an etcd cluster.
  59. UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
  60. // UserChangePassword changes a password of a user.
  61. UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
  62. // UserGrantRole grants a role to a user.
  63. UserGrantRole(ctx context.Context, user string, role string) (*AuthUserGrantRoleResponse, error)
  64. // UserGet gets a detailed information of a user.
  65. UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error)
  66. // UserList gets a list of all users.
  67. UserList(ctx context.Context) (*AuthUserListResponse, error)
  68. // UserRevokeRole revokes a role of a user.
  69. UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error)
  70. // RoleAdd adds a new role to an etcd cluster.
  71. RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error)
  72. // RoleGrantPermission grants a permission to a role.
  73. RoleGrantPermission(ctx context.Context, name string, key, rangeEnd string, permType PermissionType) (*AuthRoleGrantPermissionResponse, error)
  74. // RoleGet gets a detailed information of a role.
  75. RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error)
  76. // RoleList gets a list of all roles.
  77. RoleList(ctx context.Context) (*AuthRoleListResponse, error)
  78. // RoleRevokePermission revokes a permission from a role.
  79. RoleRevokePermission(ctx context.Context, role string, key, rangeEnd string) (*AuthRoleRevokePermissionResponse, error)
  80. // RoleDelete deletes a role.
  81. RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error)
  82. }
  83. type authClient struct {
  84. remote pb.AuthClient
  85. callOpts []grpc.CallOption
  86. }
  87. func NewAuth(c *Client) Auth {
  88. api := &authClient{remote: RetryAuthClient(c)}
  89. if c != nil {
  90. api.callOpts = c.callOpts
  91. }
  92. return api
  93. }
  94. func (auth *authClient) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
  95. resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{}, auth.callOpts...)
  96. return (*AuthEnableResponse)(resp), toErr(ctx, err)
  97. }
  98. func (auth *authClient) AuthDisable(ctx context.Context) (*AuthDisableResponse, error) {
  99. resp, err := auth.remote.AuthDisable(ctx, &pb.AuthDisableRequest{}, auth.callOpts...)
  100. return (*AuthDisableResponse)(resp), toErr(ctx, err)
  101. }
  102. func (auth *authClient) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
  103. resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password, Options: &authpb.UserAddOptions{NoPassword: false}}, auth.callOpts...)
  104. return (*AuthUserAddResponse)(resp), toErr(ctx, err)
  105. }
  106. func (auth *authClient) UserAddWithOptions(ctx context.Context, name string, password string, options *UserAddOptions) (*AuthUserAddResponse, error) {
  107. resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password, Options: (*authpb.UserAddOptions)(options)}, auth.callOpts...)
  108. return (*AuthUserAddResponse)(resp), toErr(ctx, err)
  109. }
  110. func (auth *authClient) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
  111. resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name}, auth.callOpts...)
  112. return (*AuthUserDeleteResponse)(resp), toErr(ctx, err)
  113. }
  114. func (auth *authClient) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
  115. resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password}, auth.callOpts...)
  116. return (*AuthUserChangePasswordResponse)(resp), toErr(ctx, err)
  117. }
  118. func (auth *authClient) UserGrantRole(ctx context.Context, user string, role string) (*AuthUserGrantRoleResponse, error) {
  119. resp, err := auth.remote.UserGrantRole(ctx, &pb.AuthUserGrantRoleRequest{User: user, Role: role}, auth.callOpts...)
  120. return (*AuthUserGrantRoleResponse)(resp), toErr(ctx, err)
  121. }
  122. func (auth *authClient) UserGet(ctx context.Context, name string) (*AuthUserGetResponse, error) {
  123. resp, err := auth.remote.UserGet(ctx, &pb.AuthUserGetRequest{Name: name}, auth.callOpts...)
  124. return (*AuthUserGetResponse)(resp), toErr(ctx, err)
  125. }
  126. func (auth *authClient) UserList(ctx context.Context) (*AuthUserListResponse, error) {
  127. resp, err := auth.remote.UserList(ctx, &pb.AuthUserListRequest{}, auth.callOpts...)
  128. return (*AuthUserListResponse)(resp), toErr(ctx, err)
  129. }
  130. func (auth *authClient) UserRevokeRole(ctx context.Context, name string, role string) (*AuthUserRevokeRoleResponse, error) {
  131. resp, err := auth.remote.UserRevokeRole(ctx, &pb.AuthUserRevokeRoleRequest{Name: name, Role: role}, auth.callOpts...)
  132. return (*AuthUserRevokeRoleResponse)(resp), toErr(ctx, err)
  133. }
  134. func (auth *authClient) RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error) {
  135. resp, err := auth.remote.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: name}, auth.callOpts...)
  136. return (*AuthRoleAddResponse)(resp), toErr(ctx, err)
  137. }
  138. func (auth *authClient) RoleGrantPermission(ctx context.Context, name string, key, rangeEnd string, permType PermissionType) (*AuthRoleGrantPermissionResponse, error) {
  139. perm := &authpb.Permission{
  140. Key: []byte(key),
  141. RangeEnd: []byte(rangeEnd),
  142. PermType: authpb.Permission_Type(permType),
  143. }
  144. resp, err := auth.remote.RoleGrantPermission(ctx, &pb.AuthRoleGrantPermissionRequest{Name: name, Perm: perm}, auth.callOpts...)
  145. return (*AuthRoleGrantPermissionResponse)(resp), toErr(ctx, err)
  146. }
  147. func (auth *authClient) RoleGet(ctx context.Context, role string) (*AuthRoleGetResponse, error) {
  148. resp, err := auth.remote.RoleGet(ctx, &pb.AuthRoleGetRequest{Role: role}, auth.callOpts...)
  149. return (*AuthRoleGetResponse)(resp), toErr(ctx, err)
  150. }
  151. func (auth *authClient) RoleList(ctx context.Context) (*AuthRoleListResponse, error) {
  152. resp, err := auth.remote.RoleList(ctx, &pb.AuthRoleListRequest{}, auth.callOpts...)
  153. return (*AuthRoleListResponse)(resp), toErr(ctx, err)
  154. }
  155. func (auth *authClient) RoleRevokePermission(ctx context.Context, role string, key, rangeEnd string) (*AuthRoleRevokePermissionResponse, error) {
  156. resp, err := auth.remote.RoleRevokePermission(ctx, &pb.AuthRoleRevokePermissionRequest{Role: role, Key: []byte(key), RangeEnd: []byte(rangeEnd)}, auth.callOpts...)
  157. return (*AuthRoleRevokePermissionResponse)(resp), toErr(ctx, err)
  158. }
  159. func (auth *authClient) RoleDelete(ctx context.Context, role string) (*AuthRoleDeleteResponse, error) {
  160. resp, err := auth.remote.RoleDelete(ctx, &pb.AuthRoleDeleteRequest{Role: role}, auth.callOpts...)
  161. return (*AuthRoleDeleteResponse)(resp), toErr(ctx, err)
  162. }
  163. func StrToPermissionType(s string) (PermissionType, error) {
  164. val, ok := authpb.Permission_Type_value[strings.ToUpper(s)]
  165. if ok {
  166. return PermissionType(val), nil
  167. }
  168. return PermissionType(-1), fmt.Errorf("invalid permission type: %s", s)
  169. }
  170. type authenticator struct {
  171. conn *grpc.ClientConn // conn in-use
  172. remote pb.AuthClient
  173. callOpts []grpc.CallOption
  174. }
  175. func (auth *authenticator) authenticate(ctx context.Context, name string, password string) (*AuthenticateResponse, error) {
  176. resp, err := auth.remote.Authenticate(ctx, &pb.AuthenticateRequest{Name: name, Password: password}, auth.callOpts...)
  177. return (*AuthenticateResponse)(resp), toErr(ctx, err)
  178. }
  179. func (auth *authenticator) close() {
  180. auth.conn.Close()
  181. }
  182. func newAuthenticator(ctx context.Context, target string, opts []grpc.DialOption, c *Client) (*authenticator, error) {
  183. conn, err := grpc.DialContext(ctx, target, opts...)
  184. if err != nil {
  185. return nil, err
  186. }
  187. api := &authenticator{
  188. conn: conn,
  189. remote: pb.NewAuthClient(conn),
  190. }
  191. if c != nil {
  192. api.callOpts = c.callOpts
  193. }
  194. return api, nil
  195. }