store.go 4.6 KB

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