auth.go 10 KB

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