apply_auth.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 etcdserver
  15. import (
  16. "sync"
  17. "github.com/coreos/etcd/auth"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/coreos/etcd/mvcc"
  20. )
  21. type authApplierV3 struct {
  22. applierV3
  23. as auth.AuthStore
  24. // mu serializes Apply so that user isn't corrupted and so that
  25. // serialized requests don't leak data from TOCTOU errors
  26. mu sync.Mutex
  27. authInfo auth.AuthInfo
  28. }
  29. func newAuthApplierV3(as auth.AuthStore, base applierV3) *authApplierV3 {
  30. return &authApplierV3{applierV3: base, as: as}
  31. }
  32. func (aa *authApplierV3) Apply(r *pb.InternalRaftRequest) *applyResult {
  33. aa.mu.Lock()
  34. defer aa.mu.Unlock()
  35. if r.Header != nil {
  36. // backward-compatible with pre-3.0 releases when internalRaftRequest
  37. // does not have header field
  38. aa.authInfo.Username = r.Header.Username
  39. aa.authInfo.Revision = r.Header.AuthRevision
  40. }
  41. if needAdminPermission(r) {
  42. if err := aa.as.IsAdminPermitted(&aa.authInfo); err != nil {
  43. aa.authInfo.Username = ""
  44. aa.authInfo.Revision = 0
  45. return &applyResult{err: err}
  46. }
  47. }
  48. ret := aa.applierV3.Apply(r)
  49. aa.authInfo.Username = ""
  50. aa.authInfo.Revision = 0
  51. return ret
  52. }
  53. func (aa *authApplierV3) Put(txn mvcc.TxnWrite, r *pb.PutRequest) (*pb.PutResponse, error) {
  54. if err := aa.as.IsPutPermitted(&aa.authInfo, r.Key); err != nil {
  55. return nil, err
  56. }
  57. if r.PrevKv {
  58. err := aa.as.IsRangePermitted(&aa.authInfo, r.Key, nil)
  59. if err != nil {
  60. return nil, err
  61. }
  62. }
  63. return aa.applierV3.Put(txn, r)
  64. }
  65. func (aa *authApplierV3) Range(txn mvcc.TxnRead, r *pb.RangeRequest) (*pb.RangeResponse, error) {
  66. if err := aa.as.IsRangePermitted(&aa.authInfo, r.Key, r.RangeEnd); err != nil {
  67. return nil, err
  68. }
  69. return aa.applierV3.Range(txn, r)
  70. }
  71. func (aa *authApplierV3) DeleteRange(txn mvcc.TxnWrite, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
  72. if err := aa.as.IsDeleteRangePermitted(&aa.authInfo, r.Key, r.RangeEnd); err != nil {
  73. return nil, err
  74. }
  75. if r.PrevKv {
  76. err := aa.as.IsRangePermitted(&aa.authInfo, r.Key, r.RangeEnd)
  77. if err != nil {
  78. return nil, err
  79. }
  80. }
  81. return aa.applierV3.DeleteRange(txn, r)
  82. }
  83. func checkTxnReqsPermission(as auth.AuthStore, ai *auth.AuthInfo, reqs []*pb.RequestOp) error {
  84. for _, requ := range reqs {
  85. switch tv := requ.Request.(type) {
  86. case *pb.RequestOp_RequestRange:
  87. if tv.RequestRange == nil {
  88. continue
  89. }
  90. if err := as.IsRangePermitted(ai, tv.RequestRange.Key, tv.RequestRange.RangeEnd); err != nil {
  91. return err
  92. }
  93. case *pb.RequestOp_RequestPut:
  94. if tv.RequestPut == nil {
  95. continue
  96. }
  97. if err := as.IsPutPermitted(ai, tv.RequestPut.Key); err != nil {
  98. return err
  99. }
  100. case *pb.RequestOp_RequestDeleteRange:
  101. if tv.RequestDeleteRange == nil {
  102. continue
  103. }
  104. if tv.RequestDeleteRange.PrevKv {
  105. err := as.IsRangePermitted(ai, tv.RequestDeleteRange.Key, tv.RequestDeleteRange.RangeEnd)
  106. if err != nil {
  107. return err
  108. }
  109. }
  110. err := as.IsDeleteRangePermitted(ai, tv.RequestDeleteRange.Key, tv.RequestDeleteRange.RangeEnd)
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. }
  116. return nil
  117. }
  118. func checkTxnAuth(as auth.AuthStore, ai *auth.AuthInfo, rt *pb.TxnRequest) error {
  119. for _, c := range rt.Compare {
  120. if err := as.IsRangePermitted(ai, c.Key, nil); err != nil {
  121. return err
  122. }
  123. }
  124. if err := checkTxnReqsPermission(as, ai, rt.Success); err != nil {
  125. return err
  126. }
  127. if err := checkTxnReqsPermission(as, ai, rt.Failure); err != nil {
  128. return err
  129. }
  130. return nil
  131. }
  132. func (aa *authApplierV3) Txn(rt *pb.TxnRequest) (*pb.TxnResponse, error) {
  133. if err := checkTxnAuth(aa.as, &aa.authInfo, rt); err != nil {
  134. return nil, err
  135. }
  136. return aa.applierV3.Txn(rt)
  137. }
  138. func needAdminPermission(r *pb.InternalRaftRequest) bool {
  139. switch {
  140. case r.AuthEnable != nil:
  141. return true
  142. case r.AuthDisable != nil:
  143. return true
  144. case r.AuthUserAdd != nil:
  145. return true
  146. case r.AuthUserDelete != nil:
  147. return true
  148. case r.AuthUserChangePassword != nil:
  149. return true
  150. case r.AuthUserGrantRole != nil:
  151. return true
  152. case r.AuthUserGet != nil:
  153. return true
  154. case r.AuthUserRevokeRole != nil:
  155. return true
  156. case r.AuthRoleAdd != nil:
  157. return true
  158. case r.AuthRoleGrantPermission != nil:
  159. return true
  160. case r.AuthRoleGet != nil:
  161. return true
  162. case r.AuthRoleRevokePermission != nil:
  163. return true
  164. case r.AuthRoleDelete != nil:
  165. return true
  166. case r.AuthUserList != nil:
  167. return true
  168. case r.AuthRoleList != nil:
  169. return true
  170. default:
  171. return false
  172. }
  173. }