policyset.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 Yunion
  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 rbacutils
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/httperrors"
  19. )
  20. type TPolicySet []SPolicy
  21. func (policies TPolicySet) GetMatchRules(service string, resource string, action string, extra ...string) []SPolicyMatch {
  22. matchRules := make([]SPolicyMatch, 0)
  23. for i := range policies {
  24. rule := policies[i].GetMatchRule(service, resource, action, extra...)
  25. if rule != nil {
  26. matchRules = append(matchRules, *rule)
  27. }
  28. }
  29. return matchRules
  30. }
  31. func DecodePolicySet(jsonObj jsonutils.JSONObject) (TPolicySet, error) {
  32. jsonArr, err := jsonObj.GetArray()
  33. if err != nil {
  34. return nil, errors.Wrap(httperrors.ErrInvalidFormat, "invalid json: not an array")
  35. }
  36. set := TPolicySet{}
  37. for i := range jsonArr {
  38. policy, err := DecodePolicy(jsonArr[i])
  39. if err != nil {
  40. return nil, errors.Wrapf(err, "decode %d", i)
  41. }
  42. set = append(set, *policy)
  43. }
  44. return set, nil
  45. }
  46. func (policies TPolicySet) Encode() jsonutils.JSONObject {
  47. obj := make([]jsonutils.JSONObject, len(policies))
  48. for i := range policies {
  49. obj[i] = policies[i].Encode()
  50. }
  51. return jsonutils.NewArray(obj...)
  52. }
  53. // Contains of TPolicySet
  54. //
  55. // TPolicySet ps1 contains ps2 means any member of ps2 is contained by one of the members of ps1
  56. func (policies1 TPolicySet) Contains(policies2 TPolicySet) bool {
  57. for _, ps2 := range policies2 {
  58. contained := false
  59. for _, ps1 := range policies1 {
  60. if ps1.Contains(ps2) {
  61. contained = true
  62. break
  63. }
  64. }
  65. if !contained {
  66. return false
  67. }
  68. }
  69. return true
  70. }
  71. // ViolatedBy: policies中deny的权限,但是assign中却是allow
  72. // if any assign allow, but policies deny
  73. // OR
  74. // assign allow, if any policies deny
  75. /* func (policies TPolicySet) ViolatedBy(assign TPolicySet) bool {
  76. if policies.violatedBySet(assign, Allow) {
  77. return true
  78. }
  79. if assign.violatedBySet(policies, Deny) {
  80. return true
  81. }
  82. return false
  83. }
  84. func (policies TPolicySet) violatedBySet(assign TPolicySet, expect TRbacResult) bool {
  85. for i := range assign {
  86. if policies.violatedByPolicy(assign[i], expect) {
  87. return true
  88. }
  89. }
  90. return false
  91. }
  92. func (policies TPolicySet) violatedByPolicy(policy SPolicy, expect TRbacResult) bool {
  93. for i := range policy.Rules {
  94. rule := policy.Rules[i]
  95. if rule.Result != expect {
  96. continue
  97. }
  98. matchRules := policies.GetMatchRules(rule.Service, rule.Resource, rule.Action, rule.Extra...)
  99. for i := range matchRules {
  100. }
  101. matchRule := GetMatchRule(matchRules, rule.Service, rule.Resource, rule.Action, rule.Extra...)
  102. if expect == Allow && (matchRule == nil || matchRule.Result == Deny) {
  103. return true
  104. } else if expect == Deny && matchRule != nil && matchRule.Result == Allow {
  105. return true
  106. }
  107. }
  108. return false
  109. }
  110. */