store.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/storage/backend"
  17. "github.com/coreos/pkg/capnslog"
  18. )
  19. var (
  20. enableFlagKey = []byte("authEnabled")
  21. authBucketName = []byte("auth")
  22. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "auth")
  23. )
  24. type AuthStore interface {
  25. // AuthEnable() turns on the authentication feature
  26. AuthEnable()
  27. // Recover recovers the state of auth store from the given backend
  28. Recover(b backend.Backend)
  29. }
  30. type authStore struct {
  31. be backend.Backend
  32. }
  33. func (as *authStore) AuthEnable() {
  34. value := []byte{1}
  35. b := as.be
  36. tx := b.BatchTx()
  37. tx.Lock()
  38. tx.UnsafePut(authBucketName, enableFlagKey, value)
  39. tx.Unlock()
  40. b.ForceCommit()
  41. plog.Noticef("Authentication enabled")
  42. }
  43. func (as *authStore) Recover(be backend.Backend) {
  44. as.be = be
  45. // TODO(mitake): recovery process
  46. }
  47. func NewAuthStore(be backend.Backend) *authStore {
  48. tx := be.BatchTx()
  49. tx.Lock()
  50. tx.UnsafeCreateBucket(authBucketName)
  51. tx.Unlock()
  52. be.ForceCommit()
  53. return &authStore{
  54. be: be,
  55. }
  56. }