store.go 1.6 KB

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