auth.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2016 Nippon Telegraph and Telephone Corporation.
  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. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. "golang.org/x/net/context"
  18. "google.golang.org/grpc"
  19. )
  20. type (
  21. AuthEnableResponse pb.AuthEnableResponse
  22. AuthUserAddResponse pb.AuthUserAddResponse
  23. AuthUserDeleteResponse pb.AuthUserDeleteResponse
  24. AuthUserChangePasswordResponse pb.AuthUserChangePasswordResponse
  25. AuthRoleAddResponse pb.AuthRoleAddResponse
  26. )
  27. type Auth interface {
  28. // AuthEnable enables auth of an etcd cluster.
  29. AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
  30. // UserAdd adds a new user to an etcd cluster.
  31. UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
  32. // UserDelete deletes a user from an etcd cluster.
  33. UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
  34. // UserChangePassword changes a password of a user.
  35. UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
  36. // RoleAdd adds a new user to an etcd cluster.
  37. RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error)
  38. }
  39. type auth struct {
  40. c *Client
  41. conn *grpc.ClientConn // conn in-use
  42. remote pb.AuthClient
  43. }
  44. func NewAuth(c *Client) Auth {
  45. conn := c.ActiveConnection()
  46. return &auth{
  47. conn: c.ActiveConnection(),
  48. remote: pb.NewAuthClient(conn),
  49. c: c,
  50. }
  51. }
  52. func (auth *auth) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
  53. resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{})
  54. return (*AuthEnableResponse)(resp), err
  55. }
  56. func (auth *auth) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
  57. resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password})
  58. return (*AuthUserAddResponse)(resp), err
  59. }
  60. func (auth *auth) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
  61. resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name})
  62. return (*AuthUserDeleteResponse)(resp), err
  63. }
  64. func (auth *auth) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
  65. resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password})
  66. return (*AuthUserChangePasswordResponse)(resp), err
  67. }
  68. func (auth *auth) RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error) {
  69. resp, err := auth.remote.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: name})
  70. return (*AuthRoleAddResponse)(resp), err
  71. }