auth.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. )
  26. type Auth interface {
  27. // AuthEnable enables auth of an etcd cluster.
  28. AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
  29. // UserAdd adds a new user to an etcd cluster.
  30. UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
  31. // UserDelete deletes a user from an etcd cluster.
  32. UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
  33. // UserChangePassword changes a password of a user.
  34. UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
  35. }
  36. type auth struct {
  37. c *Client
  38. conn *grpc.ClientConn // conn in-use
  39. remote pb.AuthClient
  40. }
  41. func NewAuth(c *Client) Auth {
  42. conn := c.ActiveConnection()
  43. return &auth{
  44. conn: c.ActiveConnection(),
  45. remote: pb.NewAuthClient(conn),
  46. c: c,
  47. }
  48. }
  49. func (auth *auth) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
  50. resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{})
  51. return (*AuthEnableResponse)(resp), err
  52. }
  53. func (auth *auth) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
  54. resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password})
  55. return (*AuthUserAddResponse)(resp), err
  56. }
  57. func (auth *auth) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
  58. resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name})
  59. return (*AuthUserDeleteResponse)(resp), err
  60. }
  61. func (auth *auth) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
  62. resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password})
  63. return (*AuthUserChangePasswordResponse)(resp), err
  64. }