apply_auth.go 6.4 KB

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