auth.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "fmt"
  17. "strings"
  18. "github.com/coreos/etcd/auth/authpb"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "golang.org/x/net/context"
  21. "google.golang.org/grpc"
  22. )
  23. type (
  24. AuthEnableResponse pb.AuthEnableResponse
  25. AuthUserAddResponse pb.AuthUserAddResponse
  26. AuthUserDeleteResponse pb.AuthUserDeleteResponse
  27. AuthUserChangePasswordResponse pb.AuthUserChangePasswordResponse
  28. AuthRoleAddResponse pb.AuthRoleAddResponse
  29. AuthRoleGrantResponse pb.AuthRoleGrantResponse
  30. PermissionType authpb.Permission_Type
  31. )
  32. const (
  33. PermRead = authpb.READ
  34. PermWrite = authpb.WRITE
  35. PermReadWrite = authpb.READWRITE
  36. )
  37. type Auth interface {
  38. // AuthEnable enables auth of an etcd cluster.
  39. AuthEnable(ctx context.Context) (*AuthEnableResponse, error)
  40. // UserAdd adds a new user to an etcd cluster.
  41. UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error)
  42. // UserDelete deletes a user from an etcd cluster.
  43. UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error)
  44. // UserChangePassword changes a password of a user.
  45. UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error)
  46. // RoleAdd adds a new role to an etcd cluster.
  47. RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error)
  48. // RoleGrant grants a permission to a role.
  49. RoleGrant(ctx context.Context, name string, key string, permType PermissionType) (*AuthRoleGrantResponse, error)
  50. }
  51. type auth struct {
  52. c *Client
  53. conn *grpc.ClientConn // conn in-use
  54. remote pb.AuthClient
  55. }
  56. func NewAuth(c *Client) Auth {
  57. conn := c.ActiveConnection()
  58. return &auth{
  59. conn: c.ActiveConnection(),
  60. remote: pb.NewAuthClient(conn),
  61. c: c,
  62. }
  63. }
  64. func (auth *auth) AuthEnable(ctx context.Context) (*AuthEnableResponse, error) {
  65. resp, err := auth.remote.AuthEnable(ctx, &pb.AuthEnableRequest{})
  66. return (*AuthEnableResponse)(resp), err
  67. }
  68. func (auth *auth) UserAdd(ctx context.Context, name string, password string) (*AuthUserAddResponse, error) {
  69. resp, err := auth.remote.UserAdd(ctx, &pb.AuthUserAddRequest{Name: name, Password: password})
  70. return (*AuthUserAddResponse)(resp), err
  71. }
  72. func (auth *auth) UserDelete(ctx context.Context, name string) (*AuthUserDeleteResponse, error) {
  73. resp, err := auth.remote.UserDelete(ctx, &pb.AuthUserDeleteRequest{Name: name})
  74. return (*AuthUserDeleteResponse)(resp), err
  75. }
  76. func (auth *auth) UserChangePassword(ctx context.Context, name string, password string) (*AuthUserChangePasswordResponse, error) {
  77. resp, err := auth.remote.UserChangePassword(ctx, &pb.AuthUserChangePasswordRequest{Name: name, Password: password})
  78. return (*AuthUserChangePasswordResponse)(resp), err
  79. }
  80. func (auth *auth) RoleAdd(ctx context.Context, name string) (*AuthRoleAddResponse, error) {
  81. resp, err := auth.remote.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: name})
  82. return (*AuthRoleAddResponse)(resp), err
  83. }
  84. func (auth *auth) RoleGrant(ctx context.Context, name string, key string, permType PermissionType) (*AuthRoleGrantResponse, error) {
  85. perm := &authpb.Permission{
  86. Key: []byte(key),
  87. PermType: authpb.Permission_Type(permType),
  88. }
  89. resp, err := auth.remote.RoleGrant(ctx, &pb.AuthRoleGrantRequest{Name: name, Perm: perm})
  90. return (*AuthRoleGrantResponse)(resp), err
  91. }
  92. func StrToPermissionType(s string) (PermissionType, error) {
  93. val, ok := authpb.Permission_Type_value[strings.ToUpper(s)]
  94. if ok {
  95. return PermissionType(val), nil
  96. }
  97. return PermissionType(-1), fmt.Errorf("invalid permission type: %s", s)
  98. }