apply_auth.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "context"
  17. "sync"
  18. "go.etcd.io/etcd/auth"
  19. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. "go.etcd.io/etcd/lease"
  21. "go.etcd.io/etcd/mvcc"
  22. )
  23. type authApplierV3 struct {
  24. applierV3
  25. as auth.AuthStore
  26. lessor lease.Lessor
  27. // mu serializes Apply so that user isn't corrupted and so that
  28. // serialized requests don't leak data from TOCTOU errors
  29. mu sync.Mutex
  30. authInfo auth.AuthInfo
  31. }
  32. func newAuthApplierV3(as auth.AuthStore, base applierV3, lessor lease.Lessor) *authApplierV3 {
  33. return &authApplierV3{applierV3: base, as: as, lessor: lessor}
  34. }
  35. func (aa *authApplierV3) Apply(r *pb.InternalRaftRequest) *applyResult {
  36. aa.mu.Lock()
  37. defer aa.mu.Unlock()
  38. if r.Header != nil {
  39. // backward-compatible with pre-3.0 releases when internalRaftRequest
  40. // does not have header field
  41. aa.authInfo.Username = r.Header.Username
  42. aa.authInfo.Revision = r.Header.AuthRevision
  43. }
  44. if needAdminPermission(r) {
  45. if err := aa.as.IsAdminPermitted(&aa.authInfo); err != nil {
  46. aa.authInfo.Username = ""
  47. aa.authInfo.Revision = 0
  48. return &applyResult{err: err}
  49. }
  50. }
  51. ret := aa.applierV3.Apply(r)
  52. aa.authInfo.Username = ""
  53. aa.authInfo.Revision = 0
  54. return ret
  55. }
  56. func (aa *authApplierV3) Put(txn mvcc.TxnWrite, r *pb.PutRequest) (*pb.PutResponse, error) {
  57. if err := aa.as.IsPutPermitted(&aa.authInfo, r.Key); err != nil {
  58. return nil, err
  59. }
  60. if err := aa.checkLeasePuts(lease.LeaseID(r.Lease)); err != nil {
  61. // The specified lease is already attached with a key that cannot
  62. // be written by this user. It means the user cannot revoke the
  63. // lease so attaching the lease to the newly written key should
  64. // be forbidden.
  65. return nil, err
  66. }
  67. if r.PrevKv {
  68. err := aa.as.IsRangePermitted(&aa.authInfo, r.Key, nil)
  69. if err != nil {
  70. return nil, err
  71. }
  72. }
  73. return aa.applierV3.Put(txn, r)
  74. }
  75. func (aa *authApplierV3) Range(ctx context.Context, txn mvcc.TxnRead, r *pb.RangeRequest) (*pb.RangeResponse, error) {
  76. if err := aa.as.IsRangePermitted(&aa.authInfo, r.Key, r.RangeEnd); err != nil {
  77. return nil, err
  78. }
  79. return aa.applierV3.Range(ctx, txn, r)
  80. }
  81. func (aa *authApplierV3) DeleteRange(txn mvcc.TxnWrite, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
  82. if err := aa.as.IsDeleteRangePermitted(&aa.authInfo, r.Key, r.RangeEnd); err != nil {
  83. return nil, err
  84. }
  85. if r.PrevKv {
  86. err := aa.as.IsRangePermitted(&aa.authInfo, r.Key, r.RangeEnd)
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. return aa.applierV3.DeleteRange(txn, r)
  92. }
  93. func checkTxnReqsPermission(as auth.AuthStore, ai *auth.AuthInfo, reqs []*pb.RequestOp) error {
  94. for _, requ := range reqs {
  95. switch tv := requ.Request.(type) {
  96. case *pb.RequestOp_RequestRange:
  97. if tv.RequestRange == nil {
  98. continue
  99. }
  100. if err := as.IsRangePermitted(ai, tv.RequestRange.Key, tv.RequestRange.RangeEnd); err != nil {
  101. return err
  102. }
  103. case *pb.RequestOp_RequestPut:
  104. if tv.RequestPut == nil {
  105. continue
  106. }
  107. if err := as.IsPutPermitted(ai, tv.RequestPut.Key); err != nil {
  108. return err
  109. }
  110. case *pb.RequestOp_RequestDeleteRange:
  111. if tv.RequestDeleteRange == nil {
  112. continue
  113. }
  114. if tv.RequestDeleteRange.PrevKv {
  115. err := as.IsRangePermitted(ai, tv.RequestDeleteRange.Key, tv.RequestDeleteRange.RangeEnd)
  116. if err != nil {
  117. return err
  118. }
  119. }
  120. err := as.IsDeleteRangePermitted(ai, tv.RequestDeleteRange.Key, tv.RequestDeleteRange.RangeEnd)
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. }
  126. return nil
  127. }
  128. func checkTxnAuth(as auth.AuthStore, ai *auth.AuthInfo, rt *pb.TxnRequest) error {
  129. for _, c := range rt.Compare {
  130. if err := as.IsRangePermitted(ai, c.Key, c.RangeEnd); err != nil {
  131. return err
  132. }
  133. }
  134. if err := checkTxnReqsPermission(as, ai, rt.Success); err != nil {
  135. return err
  136. }
  137. return checkTxnReqsPermission(as, ai, rt.Failure)
  138. }
  139. func (aa *authApplierV3) Txn(rt *pb.TxnRequest) (*pb.TxnResponse, error) {
  140. if err := checkTxnAuth(aa.as, &aa.authInfo, rt); err != nil {
  141. return nil, err
  142. }
  143. return aa.applierV3.Txn(rt)
  144. }
  145. func (aa *authApplierV3) LeaseRevoke(lc *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error) {
  146. if err := aa.checkLeasePuts(lease.LeaseID(lc.ID)); err != nil {
  147. return nil, err
  148. }
  149. return aa.applierV3.LeaseRevoke(lc)
  150. }
  151. func (aa *authApplierV3) checkLeasePuts(leaseID lease.LeaseID) error {
  152. lease := aa.lessor.Lookup(leaseID)
  153. if lease != nil {
  154. for _, key := range lease.Keys() {
  155. if err := aa.as.IsPutPermitted(&aa.authInfo, []byte(key)); err != nil {
  156. return err
  157. }
  158. }
  159. }
  160. return nil
  161. }
  162. func (aa *authApplierV3) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) {
  163. err := aa.as.IsAdminPermitted(&aa.authInfo)
  164. if err != nil && r.Name != aa.authInfo.Username {
  165. aa.authInfo.Username = ""
  166. aa.authInfo.Revision = 0
  167. return &pb.AuthUserGetResponse{}, err
  168. }
  169. return aa.applierV3.UserGet(r)
  170. }
  171. func (aa *authApplierV3) RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error) {
  172. err := aa.as.IsAdminPermitted(&aa.authInfo)
  173. if err != nil && !aa.as.HasRole(aa.authInfo.Username, r.Role) {
  174. aa.authInfo.Username = ""
  175. aa.authInfo.Revision = 0
  176. return &pb.AuthRoleGetResponse{}, err
  177. }
  178. return aa.applierV3.RoleGet(r)
  179. }
  180. func needAdminPermission(r *pb.InternalRaftRequest) bool {
  181. switch {
  182. case r.AuthEnable != nil:
  183. return true
  184. case r.AuthDisable != nil:
  185. return true
  186. case r.AuthUserAdd != nil:
  187. return true
  188. case r.AuthUserDelete != nil:
  189. return true
  190. case r.AuthUserChangePassword != nil:
  191. return true
  192. case r.AuthUserGrantRole != nil:
  193. return true
  194. case r.AuthUserRevokeRole != nil:
  195. return true
  196. case r.AuthRoleAdd != nil:
  197. return true
  198. case r.AuthRoleGrantPermission != nil:
  199. return true
  200. case r.AuthRoleRevokePermission != nil:
  201. return true
  202. case r.AuthRoleDelete != nil:
  203. return true
  204. case r.AuthUserList != nil:
  205. return true
  206. case r.AuthRoleList != nil:
  207. return true
  208. default:
  209. return false
  210. }
  211. }