auth.go 1.8 KB

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