apply_auth.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. return checkTxnReqsPermission(as, ai, rt.Failure)
  137. }
  138. func (aa *authApplierV3) Txn(rt *pb.TxnRequest) (*pb.TxnResponse, error) {
  139. if err := checkTxnAuth(aa.as, &aa.authInfo, rt); err != nil {
  140. return nil, err
  141. }
  142. return aa.applierV3.Txn(rt)
  143. }
  144. func (aa *authApplierV3) LeaseRevoke(lc *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error) {
  145. if err := aa.checkLeasePuts(lease.LeaseID(lc.ID)); err != nil {
  146. return nil, err
  147. }
  148. return aa.applierV3.LeaseRevoke(lc)
  149. }
  150. func (aa *authApplierV3) checkLeasePuts(leaseID lease.LeaseID) error {
  151. lease := aa.lessor.Lookup(leaseID)
  152. if lease != nil {
  153. for _, key := range lease.Keys() {
  154. if err := aa.as.IsPutPermitted(&aa.authInfo, []byte(key)); err != nil {
  155. return err
  156. }
  157. }
  158. }
  159. return nil
  160. }
  161. func (aa *authApplierV3) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) {
  162. err := aa.as.IsAdminPermitted(&aa.authInfo)
  163. if err != nil && r.Name != aa.authInfo.Username {
  164. aa.authInfo.Username = ""
  165. aa.authInfo.Revision = 0
  166. return &pb.AuthUserGetResponse{}, err
  167. }
  168. return aa.applierV3.UserGet(r)
  169. }
  170. func (aa *authApplierV3) RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error) {
  171. err := aa.as.IsAdminPermitted(&aa.authInfo)
  172. if err != nil && !aa.as.HasRole(aa.authInfo.Username, r.Role) {
  173. aa.authInfo.Username = ""
  174. aa.authInfo.Revision = 0
  175. return &pb.AuthRoleGetResponse{}, err
  176. }
  177. return aa.applierV3.RoleGet(r)
  178. }
  179. func needAdminPermission(r *pb.InternalRaftRequest) bool {
  180. switch {
  181. case r.AuthEnable != nil:
  182. return true
  183. case r.AuthDisable != nil:
  184. return true
  185. case r.AuthUserAdd != nil:
  186. return true
  187. case r.AuthUserDelete != nil:
  188. return true
  189. case r.AuthUserChangePassword != nil:
  190. return true
  191. case r.AuthUserGrantRole != nil:
  192. return true
  193. case r.AuthUserRevokeRole != nil:
  194. return true
  195. case r.AuthRoleAdd != nil:
  196. return true
  197. case r.AuthRoleGrantPermission != nil:
  198. return true
  199. case r.AuthRoleRevokePermission != nil:
  200. return true
  201. case r.AuthRoleDelete != nil:
  202. return true
  203. case r.AuthUserList != nil:
  204. return true
  205. case r.AuthRoleList != nil:
  206. return true
  207. default:
  208. return false
  209. }
  210. }