auth.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "fmt"
  17. "strings"
  18. "github.com/coreos/etcd/auth/authpb"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "golang.org/x/net/context"
  22. "google.golang.org/grpc"
  23. )
  24. type (
  25. AuthEnableResponse pb.AuthEnableResponse
  26. AuthDisableResponse pb.AuthDisableResponse
  27. AuthenticateResponse pb.AuthenticateResponse
  28. AuthUserAddResponse pb.AuthUserAddResponse
  29. AuthUserDeleteResponse pb.AuthUserDeleteResponse
  30. AuthUserChangePasswordResponse pb.AuthUserChangePasswordResponse
  31. AuthUserGrantResponse pb.AuthUserGrantResponse
  32. AuthRoleAddResponse pb.AuthRoleAddResponse
  33. AuthRoleGrantResponse pb.AuthRoleGrantResponse
  34. PermissionType authpb.Permission_Type
  35. )
  36. const (
  37. PermRead = authpb.READ
  38. PermWrite = authpb.WRITE
  39. PermReadWrite = authpb.READWRITE
  40. )
  41. type Auth interface {
  42. // AuthEnable enables auth of an etcd cluster.
  43. AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
  44. // AuthDisable disables auth of an etcd cluster.
  45. AuthDisable(ctx context.Context) (*AuthDisableResponse, error)
  46. // UserAdd adds a new user to an etcd cluster.
  47. UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
  48. // UserDelete deletes a user from an etcd cluster.
  49. UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
  50. // UserChangePassword changes a password of a user.
  51. UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
  52. // UserGrant grants a role to a user.
  53. UserGrant(ctx context.Context, user string, role string) (*AuthUserGrantResponse, error)
  54. // RoleAdd adds a new role to an etcd cluster.
  55. RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error)
  56. // RoleGrant grants a permission to a role.
  57. RoleGrant(ctx context.Context, name string, key string, permType PermissionType) (*AuthRoleGrantResponse, error)
  58. }
  59. type auth struct {
  60. c *Client
  61. conn *grpc.ClientConn // conn in-use
  62. remote pb.AuthClient
  63. }
  64. func NewAuth(c *Client) Auth {
  65. conn := c.ActiveConnection()
  66. return &auth{
  67. conn: c.ActiveConnection(),
  68. remote: pb.NewAuthClient(conn),
  69. c: c,
  70. }
  71. }
  72. func (auth *auth) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
  73. resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{})
  74. return (*AuthEnableResponse)(resp), rpctypes.Error(err)
  75. }
  76. func (auth *auth) AuthDisable(ctx context.Context) (*AuthDisableResponse, error) {
  77. resp, err := auth.remote.AuthDisable(ctx, &pb.AuthDisableRequest{})
  78. return (*AuthDisableResponse)(resp), rpctypes.Error(err)
  79. }
  80. func (auth *auth) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
  81. resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password})
  82. return (*AuthUserAddResponse)(resp), rpctypes.Error(err)
  83. }
  84. func (auth *auth) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
  85. resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name})
  86. return (*AuthUserDeleteResponse)(resp), rpctypes.Error(err)
  87. }
  88. func (auth *auth) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
  89. resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password})
  90. return (*AuthUserChangePasswordResponse)(resp), rpctypes.Error(err)
  91. }
  92. func (auth *auth) UserGrant(ctx context.Context, user string, role string) (*AuthUserGrantResponse, error) {
  93. resp, err := auth.remote.UserGrant(ctx, &pb.AuthUserGrantRequest{User: user, Role: role})
  94. return (*AuthUserGrantResponse)(resp), rpctypes.Error(err)
  95. }
  96. func (auth *auth) RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error) {
  97. resp, err := auth.remote.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: name})
  98. return (*AuthRoleAddResponse)(resp), rpctypes.Error(err)
  99. }
  100. func (auth *auth) RoleGrant(ctx context.Context, name string, key string, permType PermissionType) (*AuthRoleGrantResponse, error) {
  101. perm := &authpb.Permission{
  102. Key: []byte(key),
  103. PermType: authpb.Permission_Type(permType),
  104. }
  105. resp, err := auth.remote.RoleGrant(ctx, &pb.AuthRoleGrantRequest{Name: name, Perm: perm})
  106. return (*AuthRoleGrantResponse)(resp), rpctypes.Error(err)
  107. }
  108. func StrToPermissionType(s string) (PermissionType, error) {
  109. val, ok := authpb.Permission_Type_value[strings.ToUpper(s)]
  110. if ok {
  111. return PermissionType(val), nil
  112. }
  113. return PermissionType(-1), fmt.Errorf("invalid permission type: %s", s)
  114. }
  115. type authenticator struct {
  116. conn *grpc.ClientConn // conn in-use
  117. remote pb.AuthClient
  118. }
  119. func (auth *authenticator) authenticate(ctx context.Context, name string, password string) (*AuthenticateResponse, error) {
  120. resp, err := auth.remote.Authenticate(ctx, &pb.AuthenticateRequest{Name: name, Password: password})
  121. return (*AuthenticateResponse)(resp), rpctypes.Error(err)
  122. }
  123. func (auth *authenticator) close() {
  124. auth.conn.Close()
  125. }
  126. func newAuthenticator(endpoint string, opts []grpc.DialOption) (*authenticator, error) {
  127. conn, err := grpc.Dial(endpoint, opts...)
  128. if err != nil {
  129. return nil, err
  130. }
  131. return &authenticator{
  132. conn: conn,
  133. remote: pb.NewAuthClient(conn),
  134. }, nil
  135. }