store.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 auth
  15. import (
  16. "errors"
  17. "github.com/coreos/etcd/auth/authpb"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/coreos/etcd/storage/backend"
  20. "github.com/coreos/pkg/capnslog"
  21. "golang.org/x/crypto/bcrypt"
  22. )
  23. var (
  24. enableFlagKey = []byte("authEnabled")
  25. authBucketName = []byte("auth")
  26. authUsersBucketName = []byte("authUsers")
  27. authRolesBucketName = []byte("authRoles")
  28. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "auth")
  29. ErrUserAlreadyExist = errors.New("auth: user already exists")
  30. ErrUserNotFound = errors.New("auth: user not found")
  31. ErrRoleAlreadyExist = errors.New("auth: role already exists")
  32. )
  33. type AuthStore interface {
  34. // AuthEnable() turns on the authentication feature
  35. AuthEnable()
  36. // Recover recovers the state of auth store from the given backend
  37. Recover(b backend.Backend)
  38. // UserAdd adds a new user
  39. UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error)
  40. // UserDelete deletes a user
  41. UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error)
  42. // UserChangePassword changes a password of a user
  43. UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error)
  44. // RoleAdd adds a new role
  45. RoleAdd(r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error)
  46. }
  47. type authStore struct {
  48. be backend.Backend
  49. }
  50. func (as *authStore) AuthEnable() {
  51. value := []byte{1}
  52. b := as.be
  53. tx := b.BatchTx()
  54. tx.Lock()
  55. tx.UnsafePut(authBucketName, enableFlagKey, value)
  56. tx.Unlock()
  57. b.ForceCommit()
  58. plog.Noticef("Authentication enabled")
  59. }
  60. func (as *authStore) Recover(be backend.Backend) {
  61. as.be = be
  62. // TODO(mitake): recovery process
  63. }
  64. func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error) {
  65. plog.Noticef("adding a new user: %s", r.Name)
  66. hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), bcrypt.DefaultCost)
  67. if err != nil {
  68. plog.Errorf("failed to hash password: %s", err)
  69. return nil, err
  70. }
  71. tx := as.be.BatchTx()
  72. tx.Lock()
  73. defer tx.Unlock()
  74. _, vs := tx.UnsafeRange(authUsersBucketName, []byte(r.Name), nil, 0)
  75. if len(vs) != 0 {
  76. return &pb.AuthUserAddResponse{}, ErrUserAlreadyExist
  77. }
  78. newUser := authpb.User{
  79. Name: []byte(r.Name),
  80. Password: hashed,
  81. }
  82. marshaledUser, merr := newUser.Marshal()
  83. if merr != nil {
  84. plog.Errorf("failed to marshal a new user data: %s", merr)
  85. return nil, merr
  86. }
  87. tx.UnsafePut(authUsersBucketName, []byte(r.Name), marshaledUser)
  88. plog.Noticef("added a new user: %s", r.Name)
  89. return &pb.AuthUserAddResponse{}, nil
  90. }
  91. func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error) {
  92. tx := as.be.BatchTx()
  93. tx.Lock()
  94. defer tx.Unlock()
  95. _, vs := tx.UnsafeRange(authUsersBucketName, []byte(r.Name), nil, 0)
  96. if len(vs) != 1 {
  97. return &pb.AuthUserDeleteResponse{}, ErrUserNotFound
  98. }
  99. tx.UnsafeDelete(authUsersBucketName, []byte(r.Name))
  100. plog.Noticef("deleted a user: %s", r.Name)
  101. return &pb.AuthUserDeleteResponse{}, nil
  102. }
  103. func (as *authStore) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error) {
  104. // TODO(mitake): measure the cost of bcrypt.GenerateFromPassword()
  105. // If the cost is too high, we should move the encryption to outside of the raft
  106. hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), bcrypt.DefaultCost)
  107. if err != nil {
  108. plog.Errorf("failed to hash password: %s", err)
  109. return nil, err
  110. }
  111. tx := as.be.BatchTx()
  112. tx.Lock()
  113. defer tx.Unlock()
  114. _, vs := tx.UnsafeRange(authUsersBucketName, []byte(r.Name), nil, 0)
  115. if len(vs) != 1 {
  116. return &pb.AuthUserChangePasswordResponse{}, ErrUserNotFound
  117. }
  118. updatedUser := authpb.User{
  119. Name: []byte(r.Name),
  120. Password: hashed,
  121. }
  122. marshaledUser, merr := updatedUser.Marshal()
  123. if merr != nil {
  124. plog.Errorf("failed to marshal a new user data: %s", merr)
  125. return nil, merr
  126. }
  127. tx.UnsafePut(authUsersBucketName, []byte(r.Name), marshaledUser)
  128. plog.Noticef("changed a password of a user: %s", r.Name)
  129. return &pb.AuthUserChangePasswordResponse{}, nil
  130. }
  131. func (as *authStore) RoleAdd(r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error) {
  132. tx := as.be.BatchTx()
  133. tx.Lock()
  134. defer tx.Unlock()
  135. _, vs := tx.UnsafeRange(authRolesBucketName, []byte(r.Name), nil, 0)
  136. if len(vs) != 0 {
  137. return nil, ErrRoleAlreadyExist
  138. }
  139. newRole := &authpb.Role{
  140. Name: []byte(r.Name),
  141. }
  142. marshaledRole, err := newRole.Marshal()
  143. if err != nil {
  144. return nil, err
  145. }
  146. tx.UnsafePut(authRolesBucketName, []byte(r.Name), marshaledRole)
  147. plog.Noticef("Role %s is created", r.Name)
  148. return &pb.AuthRoleAddResponse{}, nil
  149. }
  150. func NewAuthStore(be backend.Backend) *authStore {
  151. tx := be.BatchTx()
  152. tx.Lock()
  153. tx.UnsafeCreateBucket(authBucketName)
  154. tx.UnsafeCreateBucket(authUsersBucketName)
  155. tx.UnsafeCreateBucket(authRolesBucketName)
  156. tx.Unlock()
  157. be.ForceCommit()
  158. return &authStore{
  159. be: be,
  160. }
  161. }