range_perm_cache.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2016 The etcd Authors
  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/mvcc/backend"
  18. "github.com/coreos/etcd/pkg/adt"
  19. )
  20. func getMergedPerms(tx backend.BatchTx, userName string) *unifiedRangePermissions {
  21. user := getUser(tx, userName)
  22. if user == nil {
  23. plog.Errorf("invalid user name %s", userName)
  24. return nil
  25. }
  26. readPerms := &adt.IntervalTree{}
  27. writePerms := &adt.IntervalTree{}
  28. for _, roleName := range user.Roles {
  29. role := getRole(tx, roleName)
  30. if role == nil {
  31. continue
  32. }
  33. for _, perm := range role.KeyPermission {
  34. var ivl adt.Interval
  35. var rangeEnd string
  36. if len(perm.RangeEnd) == 1 && perm.RangeEnd[0] == 0 {
  37. rangeEnd = ""
  38. } else {
  39. rangeEnd = string(perm.RangeEnd)
  40. }
  41. if len(perm.RangeEnd) != 0 {
  42. ivl = adt.NewStringAffineInterval(string(perm.Key), string(rangeEnd))
  43. } else {
  44. ivl = adt.NewStringAffinePoint(string(perm.Key))
  45. }
  46. switch perm.PermType {
  47. case authpb.READWRITE:
  48. readPerms.Insert(ivl, struct{}{})
  49. writePerms.Insert(ivl, struct{}{})
  50. case authpb.READ:
  51. readPerms.Insert(ivl, struct{}{})
  52. case authpb.WRITE:
  53. writePerms.Insert(ivl, struct{}{})
  54. }
  55. }
  56. }
  57. return &unifiedRangePermissions{
  58. readPerms: readPerms,
  59. writePerms: writePerms,
  60. }
  61. }
  62. func checkKeyInterval(cachedPerms *unifiedRangePermissions, key, rangeEnd string, permtyp authpb.Permission_Type) bool {
  63. if len(rangeEnd) == 1 && rangeEnd[0] == '\x00' {
  64. rangeEnd = ""
  65. }
  66. ivl := adt.NewStringAffineInterval(key, rangeEnd)
  67. switch permtyp {
  68. case authpb.READ:
  69. return cachedPerms.readPerms.Contains(ivl)
  70. case authpb.WRITE:
  71. return cachedPerms.writePerms.Contains(ivl)
  72. default:
  73. plog.Panicf("unknown auth type: %v", permtyp)
  74. }
  75. return false
  76. }
  77. func checkKeyPoint(cachedPerms *unifiedRangePermissions, key string, permtyp authpb.Permission_Type) bool {
  78. pt := adt.NewStringAffinePoint(key)
  79. switch permtyp {
  80. case authpb.READ:
  81. return cachedPerms.readPerms.Intersects(pt)
  82. case authpb.WRITE:
  83. return cachedPerms.writePerms.Intersects(pt)
  84. default:
  85. plog.Panicf("unknown auth type: %v", permtyp)
  86. }
  87. return false
  88. }
  89. func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd string, permtyp authpb.Permission_Type) bool {
  90. // assumption: tx is Lock()ed
  91. _, ok := as.rangePermCache[userName]
  92. if !ok {
  93. perms := getMergedPerms(tx, userName)
  94. if perms == nil {
  95. plog.Errorf("failed to create a unified permission of user %s", userName)
  96. return false
  97. }
  98. as.rangePermCache[userName] = perms
  99. }
  100. if len(rangeEnd) == 0 {
  101. return checkKeyPoint(as.rangePermCache[userName], key, permtyp)
  102. }
  103. return checkKeyInterval(as.rangePermCache[userName], key, rangeEnd, permtyp)
  104. }
  105. func (as *authStore) clearCachedPerm() {
  106. as.rangePermCache = make(map[string]*unifiedRangePermissions)
  107. }
  108. func (as *authStore) invalidateCachedPerm(userName string) {
  109. delete(as.rangePermCache, userName)
  110. }
  111. type unifiedRangePermissions struct {
  112. readPerms *adt.IntervalTree
  113. writePerms *adt.IntervalTree
  114. }