store.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "github.com/coreos/etcd/auth/authpb"
  17. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  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. )
  29. type AuthStore interface {
  30. // AuthEnable() turns on the authentication feature
  31. AuthEnable()
  32. // Recover recovers the state of auth store from the given backend
  33. Recover(b backend.Backend)
  34. // UserAdd adds a new user
  35. UserAdd(r *pb.UserAddRequest) (*pb.UserAddResponse, error)
  36. }
  37. type authStore struct {
  38. be backend.Backend
  39. }
  40. func (as *authStore) AuthEnable() {
  41. value := []byte{1}
  42. b := as.be
  43. tx := b.BatchTx()
  44. tx.Lock()
  45. tx.UnsafePut(authBucketName, enableFlagKey, value)
  46. tx.Unlock()
  47. b.ForceCommit()
  48. plog.Noticef("Authentication enabled")
  49. }
  50. func (as *authStore) Recover(be backend.Backend) {
  51. as.be = be
  52. // TODO(mitake): recovery process
  53. }
  54. func (as *authStore) UserAdd(r *pb.UserAddRequest) (*pb.UserAddResponse, error) {
  55. plog.Noticef("adding a new user: %s", r.Name)
  56. hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), bcrypt.DefaultCost)
  57. if err != nil {
  58. plog.Errorf("failed to hash password: %s", err)
  59. return nil, err
  60. }
  61. tx := as.be.BatchTx()
  62. tx.Lock()
  63. defer tx.Unlock()
  64. _, vs := tx.UnsafeRange(authUsersBucketName, []byte(r.Name), nil, 0)
  65. if len(vs) != 0 {
  66. return &pb.UserAddResponse{}, rpctypes.ErrUserAlreadyExist
  67. }
  68. newUser := authpb.User{
  69. Name: []byte(r.Name),
  70. Password: hashed,
  71. }
  72. marshaledUser, merr := newUser.Marshal()
  73. if merr != nil {
  74. plog.Errorf("failed to marshal a new user data: %s", merr)
  75. return nil, merr
  76. }
  77. tx.UnsafePut(authUsersBucketName, []byte(r.Name), marshaledUser)
  78. plog.Noticef("added a new user: %s", r.Name)
  79. return &pb.UserAddResponse{}, nil
  80. }
  81. func NewAuthStore(be backend.Backend) *authStore {
  82. tx := be.BatchTx()
  83. tx.Lock()
  84. tx.UnsafeCreateBucket(authBucketName)
  85. tx.UnsafeCreateBucket(authUsersBucketName)
  86. tx.Unlock()
  87. be.ForceCommit()
  88. return &authStore{
  89. be: be,
  90. }
  91. }