range_perm_cache.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "go.etcd.io/etcd/auth/authpb"
  17. "go.etcd.io/etcd/mvcc/backend"
  18. "go.etcd.io/etcd/pkg/adt"
  19. "go.uber.org/zap"
  20. )
  21. func getMergedPerms(lg *zap.Logger, tx backend.BatchTx, userName string) *unifiedRangePermissions {
  22. user := getUser(lg, tx, userName)
  23. if user == nil {
  24. return nil
  25. }
  26. readPerms := adt.NewIntervalTree()
  27. writePerms := adt.NewIntervalTree()
  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 []byte
  36. if len(perm.RangeEnd) != 1 || perm.RangeEnd[0] != 0 {
  37. rangeEnd = perm.RangeEnd
  38. }
  39. if len(perm.RangeEnd) != 0 {
  40. ivl = adt.NewBytesAffineInterval(perm.Key, rangeEnd)
  41. } else {
  42. ivl = adt.NewBytesAffinePoint(perm.Key)
  43. }
  44. switch perm.PermType {
  45. case authpb.READWRITE:
  46. readPerms.Insert(ivl, struct{}{})
  47. writePerms.Insert(ivl, struct{}{})
  48. case authpb.READ:
  49. readPerms.Insert(ivl, struct{}{})
  50. case authpb.WRITE:
  51. writePerms.Insert(ivl, struct{}{})
  52. }
  53. }
  54. }
  55. return &unifiedRangePermissions{
  56. readPerms: readPerms,
  57. writePerms: writePerms,
  58. }
  59. }
  60. func checkKeyInterval(
  61. lg *zap.Logger,
  62. cachedPerms *unifiedRangePermissions,
  63. key, rangeEnd []byte,
  64. permtyp authpb.Permission_Type) bool {
  65. if len(rangeEnd) == 1 && rangeEnd[0] == 0 {
  66. rangeEnd = nil
  67. }
  68. ivl := adt.NewBytesAffineInterval(key, rangeEnd)
  69. switch permtyp {
  70. case authpb.READ:
  71. return cachedPerms.readPerms.Contains(ivl)
  72. case authpb.WRITE:
  73. return cachedPerms.writePerms.Contains(ivl)
  74. default:
  75. if lg != nil {
  76. lg.Panic("unknown auth type", zap.String("auth-type", permtyp.String()))
  77. } else {
  78. plog.Panicf("unknown auth type: %v", permtyp)
  79. }
  80. }
  81. return false
  82. }
  83. func checkKeyPoint(lg *zap.Logger, cachedPerms *unifiedRangePermissions, key []byte, permtyp authpb.Permission_Type) bool {
  84. pt := adt.NewBytesAffinePoint(key)
  85. switch permtyp {
  86. case authpb.READ:
  87. return cachedPerms.readPerms.Intersects(pt)
  88. case authpb.WRITE:
  89. return cachedPerms.writePerms.Intersects(pt)
  90. default:
  91. if lg != nil {
  92. lg.Panic("unknown auth type", zap.String("auth-type", permtyp.String()))
  93. } else {
  94. plog.Panicf("unknown auth type: %v", permtyp)
  95. }
  96. }
  97. return false
  98. }
  99. func (as *authStore) isRangeOpPermitted(tx backend.BatchTx, userName string, key, rangeEnd []byte, permtyp authpb.Permission_Type) bool {
  100. // assumption: tx is Lock()ed
  101. _, ok := as.rangePermCache[userName]
  102. if !ok {
  103. perms := getMergedPerms(as.lg, tx, userName)
  104. if perms == nil {
  105. if as.lg != nil {
  106. as.lg.Warn(
  107. "failed to create a merged permission",
  108. zap.String("user-name", userName),
  109. )
  110. } else {
  111. plog.Errorf("failed to create a unified permission of user %s", userName)
  112. }
  113. return false
  114. }
  115. as.rangePermCache[userName] = perms
  116. }
  117. if len(rangeEnd) == 0 {
  118. return checkKeyPoint(as.lg, as.rangePermCache[userName], key, permtyp)
  119. }
  120. return checkKeyInterval(as.lg, as.rangePermCache[userName], key, rangeEnd, permtyp)
  121. }
  122. func (as *authStore) clearCachedPerm() {
  123. as.rangePermCache = make(map[string]*unifiedRangePermissions)
  124. }
  125. func (as *authStore) invalidateCachedPerm(userName string) {
  126. delete(as.rangePermCache, userName)
  127. }
  128. type unifiedRangePermissions struct {
  129. readPerms adt.IntervalTree
  130. writePerms adt.IntervalTree
  131. }