apply_auth.go 6.4 KB

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