auth.go 2.2 KB

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